I want to use block of flash(16KB) as eeprom. I am using STM32F205RBT6 & keil 4.72.
What I have to do: 1. Store 5Kb of user data & retrieve it on startup. 2. Flash endurance of 10000 cycles is good for my project & I have fixed 16Kb block for it.
What I have done is :
1. OPtion for target-> target-> (check)IROM1 ; Start 0x8000000 ; size 0x4000 OPtion for target-> target-> (check)IROM2 ; Start 0x8008000 ; size 0x18000
i.e fixing flash block 0x80004000 fix for eeprom whose size is 16Kb
2. Flah write code for 5Kb data:
void flash_write(void) { uint32_t address = 0x08004000; uint32_t data; uint32_t cnt = 0; /* read entire sector */ while(address < 0x08008000) { data = *(__IO uint32_t*)address; flah_union.data_u32[cnt++] = data; address = address + 4; } /* fill the array */ for( cnt = 0 ; cnt < 5120 ; cnt++ ) { flah_union.data_u8[cnt] = g_ft232_arr_data[cnt]; } /* unlock flash */ FLASH_Unlock(); /* Clear pending flags (if any) */ FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); /* erase the entire sector */ if (FLASH_EraseSector(FLASH_Sector_1, VoltageRange_3) != FLASH_COMPLETE) { /* Error occurred while sector erase. User can add here some code to deal with this error */ while (1) { } } /* write back array */ address = 0x08004000; cnt = 0; while(address < 0x08008000) { if (FLASH_ProgramWord(address, flah_union.data_u32[cnt++]) == FLASH_COMPLETE) { address = address + 4; } else { while (1) { } } } FLASH_Lock(); }
3. Flash read code for 5Kb data:
void flash_read(void) { uint32_t address = 0x08004000; uint32_t data; uint32_t cnt = 0; /* read entire sector */ while(address < 0x08008000) { data = *(__IO uint32_t*)address; flah_union.data_u32[cnt++] = data; address = address + 4; } /* fill the array */ for( cnt = 0 ; cnt < 5120 ; cnt++ ) { g_ft232_arr_data[cnt] = flah_union.data_u8[cnt]; } }
My queries: 1. Do I need to disable interrupts before reading or writing to flash even if I had defines separate block for eeprom, as I said earlier.
2. Since I am writing to flash & also while write to flash, code is getting executing from flash i.e code for reading/writing flash. Do I need to move the code to Ram? If yes how
Edit: All the flash function inside write/read functions are called from standard peripheral library provided by ST.
This questions relate to the specific hardware implementation of the STM32, and ST's own library - so you should be asking ST, not Keil!
flash_read() seems unnecessarily complex, use memcpy(), or just map a pointer to the in-memory structure.