STM32 FLASH Write Protection Error
Hi there!
I was trying to emulate eeprom using flash memory. I am using Nucleo-F030R8 board. I was able to program flash 2 times (0xF055 was the test value):

however I cannot reproduce it anymore and whilst trying to program I get PGERR.
Uploading my code below
main.c
//while() loop - event triggered on the button press
uint8_t data[2] = {0x02 , 0x55};
if(ee_write(0, 1, (uint8_t*)data) == true)
//success
else
//fail
I'm using flash page 60, starting from address 0x0800F000 (_EE_ADDR_INUSE )
bool ee_write(uint32_t startVirtualAddress, uint32_t len, uint8_t *data) {
if ((startVirtualAddress + len) > _EE_SIZE)
return false;
if (data == NULL)
return false;
HAL_FLASH_Unlock();
if(*(__IO uint16_t*)_EE_ADDR_INUSE != 0xFFFF){
FLASH_PageErase(startVirtualAddress + _EE_ADDR_INUSE);
FLASH->CR &= ~FLASH_CR_PER;
}
#ifdef FLASH_TYPEPROGRAM_HALFWORD
for (uint32_t i = 0; i < len ; i+=2) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, ((i + startVirtualAddress)) + _EE_ADDR_INUSE, (uint64_t)(data[i] | (data[i+1] << 8))) != HAL_OK)
{
HAL_FLASH_Lock();
return false;
}
}
#endif
HAL_FLASH_Lock();
return true;
}And the error occurs while trying to execute last line of code in that function
static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
{
/* Clean the error context */
pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
/* Proceed to program the new data */
SET_BIT(FLASH->CR, FLASH_CR_PG);
/* Write data in the address */
*(__IO uint16_t*)Address = Data;
}
What am I missing?
