how to read write stm32 flash memory
snippet in c

how to read write stm32 flash memory

user9833

//setup memory
// Modify Flash Address according to target MCU
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};

uint32_t flashAddress = 0x080E0000;
for(uint32_t i=0; i<5; i++)
{
    HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, flashAddress, ((uint8_t *)wrBuf)[i]);
    flashAddress++;
}
HAL_FLASH_Lock();

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