如何操作,有没有范例? 我根据现在的几个option byte操作函数写了代码,操作起来总有问题,似乎写不进去,但里面的内容变化过,显示写进去过。
FLASH_Status FLASH_SaveConfig()
{
FLASH_Status status = FLASH_COMPLETE;
const uint32_t Addr = 0x1FFFF800;
__IO uint8_t i;
uint16_t pbuf[64];
uint8_t *optbyte = 0x1FFFF800;
status = FLASH_WaitForLastOperation(ProgramTimeout);
if(status == FLASH_COMPLETE)
{
FLASH->OBKEYR = FLASH_KEY1;
FLASH->OBKEYR = FLASH_KEY2;
/* Read optionbytes */
for(i = 0; i < 8; i++){
pbuf[i] = *(uint16_t *)(Addr + 2 * i);
}
/* Erase optionbytes */
FLASH->CTLR |= CR_OPTER_Set;
FLASH->CTLR |= CR_STRT_Set;
while(FLASH->STATR & SR_BSY);
FLASH->CTLR &= ~CR_OPTER_Set;
/* Write optionbytes */
for(i = 8; i < (8+sizeof(DEVICE_CONFIG_t)); i++){
pbuf[i] = ((((uint16_t)~(g_DevConfig.bytes[i-8])) << 8) | ((uint16_t)g_DevConfig.bytes[i-8]));
}
FLASH->CTLR |= CR_OPTPG_Set;
for(i = 0; i < (8+sizeof(DEVICE_CONFIG_t)); i++){
*(uint16_t *)(Addr + 2 * i) = pbuf[i];
PRINT("write option byte %d to 0x%08X:0x%04X\n", i, Addr + 2 * i, pbuf[i]);
while(FLASH->STATR & SR_BSY) ;
}
FLASH->CTLR &= ~CR_OPTPG_Set;
status = FLASH_WaitForLastOperation(ProgramTimeout);
}
for(i = 0; i < (8+sizeof(DEVICE_CONFIG_t)); i++){
PRINT("option byte %d on 0x%08X:0x%04X\n", i, Addr + 2 * i, *(uint16_t *)(Addr + 2 * i));
}
return status;
}