2
\$\begingroup\$

I've been trying to program a word of flash memory on my STM32F030F4, but for some reason whenever I power it off and on again, the memory location has defaulted to '0xFFFFFFFF' again. Am I missing something here? is there something else I need to be doing to stop it clearing the memory on reboot?

Here's the code I'm using.

#define BRIGHTNESS_MEM 0x08003000

void FlashWrite(int data)
{

if(HAL_FLASH_Unlock()!=HAL_OK)
    ErrorHandler(FLASH_ERROR);

FLASH_PageErase(BRIGHTNESS_MEM);
FLASH->CR = 0;

if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, BRIGHTNESS_MEM, data) != HAL_OK)
    ErrorHandler(FLASH_ERROR);

HAL_FLASH_Lock();

}

I know it's writing the memory because after a write I can use the ST-link utility to inspect that memory location.

The steps taken when powering on to read the memory location:

int *mem = (int *)BRIGHTNESS_MEM;

// Load last brightness level
setBrightness(*mem);

I know it can read from that fine because I've tested writing to the memory and then checking that location for the data.

\$\endgroup\$
  • \$\begingroup\$ Is the HSI active when you program your flash? What are the steps you perform when you power it off and on and read the memory location (detailed)? Is there any other part of the program writing to the flash? \$\endgroup\$ – Arsenal Aug 31 '15 at 12:40
  • \$\begingroup\$ Yes, the HSI is active before any Flash writes are performed. In terms of powering it off, I disconnected the power to simulate battery removal. The only time the Flash is written is after a button press (in the setBrightness() function), and at no other time is it modified. Edited the question a little to show that. \$\endgroup\$ – Sensors Aug 31 '15 at 13:57
  • 1
    \$\begingroup\$ Are you sure that when you check the value, you're actually reading from the flash memory instead of getting some sort of cached value? \$\endgroup\$ – alex.forencich Aug 31 '15 at 19:00
  • \$\begingroup\$ The process I went through was: Press the button, see the change on the brightness output, connect via the st-link utility, verify the value is as expected. Disconnect, press button again, re-connect, verify new value. Works absolutely fine until I remove power from the system. \$\endgroup\$ – Sensors Aug 31 '15 at 23:56
  • 1
    \$\begingroup\$ What supply voltage do you use and how much current can it provide (basically: are you sure your voltage never drops below the allowed limit during writing)? \$\endgroup\$ – Arsenal Sep 1 '15 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.