Search code examples
flasharmembeddedstm32keil

How to Write & Erase On-Chip flash Continuously in STM32F030 family?


I tried writing a value to the Flash memory, initially i wrote 0x0000 to the position i mentioned in code it was successfully written but after that i'm not able to overwrite or erase the data in that position.So for the First time i could write to Flash successfully but after that i couldn't write /erase data in that location .What may be issue? I've pinned the memory stack image also.


  uint32_t pageAddress = 0x08008000;
  uint16_t buffer = 0xdddd;          // data buffer
  HAL_HAL_StatusTypeDef  status;

  while(1)
  { 
     HAL_FLASH_Unlock();              // unlock the flash memory in ST
     //FLASH_PageErase(pageAddress); 
     status=HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, pageAddress, 
     buffer);
     HAL_FLASH_Lock();                // Flash memory locked
  }

Memory stack -Debug


Solution

  • I found answer to my question ,the issue is that Flash can handle 2 values either "0000" or "FFFF" so user will be able to write to the location only if the value at that location is "FF" if its "00" user will not be able to write to it unless that location is Erased.In my case i wrote "00" to that location so i could not write again to that location with different values.

    Earlier in comments it was told that Flash can handle 1000 write/erase cycles.its true so its better you utilize the entire sector(1 kB) for writing once and erase if 1 kB is over.By doing this STM32 can handle much more Flash operations