# STM32 flash memory write issue

I am attempting to write some data to my stm32 discovery board (STM32F4VET11). Following the data sheet: (Sector 11, address 0x080E0000).

The code compiles, however, the write never takes place. When I enter debug mode and check rdBuf, the addresses hold 0x00.

Any tips?

//setup memory
HAL_FLASH_Unlock();
FLASH_Erase_Sector(11, FLASH_VOLTAGE_RANGE_3);
HAL_FLASH_Lock();

//write to memory
HAL_FLASH_Unlock();
uint8_t rdBuf[5];
uint8_t wrBuf[5] = {0x11, 0x22, 0x33, 0x44, 0x55};

for(uint32_t i=0; i<5; i++)
{
}
HAL_FLASH_Lock();

for(uint32_t i=0; i<5; i++)
{
*((uint8_t *)rdBuf + i) = *(uint8_t *)flashAddress;
}

• You don't need to unlock the flash to perform a read on it. Use the return values of the functions to get an idea where something is going wrong. Check that the required clocks are enabled. Stop casting arrays when it's not needed. Sep 23 '19 at 13:49
• Change the post title. It is not about EEPROM memory Sep 24 '19 at 6:54
• Can you specify which generation of F4 you are using? Sep 24 '19 at 10:58

Flash sector 11 might not available on the E type, only on the G type. As it is the last sector.
Sectors are truncated from the higher addresses when selecting lower size parts.
See chapter "Part numbering" in the datasheet.

Any tips?

Remove the flash lock/unlock between the erase and write. Some of the older ST parts require this to be one session.

Flash unlock is not required to read it.

• You hit the nail on the head. I changed it to sector 6 and it worked instantly.
– Roo
Sep 24 '19 at 12:41

To program the flash you need to erase the page you are going to program. Some STM devices allow to program memory cells which were erased before, without the need of the page erase. But you can only change the bit values to zero. Some devices require page erase every time you are going the program it (CRC checked FLASH)

Show the source code of your write function

• Is that one line 3 is taking care of? FLASH_Erase_Sector(11, FLASH_VOLTAGE_RANGE_3);
– Roo
Sep 24 '19 at 10:45