Skip to main content
NPash.1
Associate II
August 11, 2020
Solved

How to write data into board flash memory?

  • August 11, 2020
  • 8 replies
  • 6572 views

As I am trying to write the data into board flash memory, Is ther any example code to write the 64bytes of data into board flash memory.

I have tried the below shared code but its not showing the result I want.

flashAddress = 0x08040000

void Write_Flash(uint32_t data)

{

HAL_FLASH_Unlock();

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );

FLASH_Erase_Sector(FLASH_SECTOR_6, VOLTAGE_RANGE_3);

HAL_FLASH_Program(TYPEPROGRAM_WORD, FlashAddress, data);

HAL_FLASH_Lock();

}

Can you please help me on this.

This topic has been closed for replies.
Best answer by prain

use this example as reference:

​https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32F429I-Discovery/Applications/EEPROM/EEPROM_Emulation

​

It works for​ me flawlessly.

8 replies

Tesla DeLorean
Guru
August 11, 2020

Perhaps look at the HAL examples within the Cube repository for your unspecified STM32 part.

Usually a couple of examples for NUCLEO, DISCO or EVAL boards that will port.

Alternative is to extend what you have, writing subsequent words looping with a pointer, advancing the address.

Might want to handle errors better.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
TDK
August 11, 2020

Include your chip part number.

"If you feel a post has answered your question, please click ""Accept as Solution""."
prain
Visitor II
August 11, 2020

Do you want to write data in MCU flash memory or another flash memory chip on your board?

the code you mentioned, intended to write data in flash memory embedded in stm32 not external chips.​

NPash.1
NPash.1Author
Associate II
August 12, 2020

Part no: STM32F492IDISCOVERY

I want to write data into MCU flash memory.

FLASH (rx)     : ORIGIN = 0x8000000, LENGTH = 2048K

What are the steps to be followed in code for writing into flash? finding example for it.

prain
prainBest answer
Visitor II
August 12, 2020

use this example as reference:

​https://github.com/STMicroelectronics/STM32CubeF4/tree/master/Projects/STM32F429I-Discovery/Applications/EEPROM/EEPROM_Emulation

​

It works for​ me flawlessly.

LLehm.1
Visitor II
August 12, 2020

To be sure: The code only writes one word = 32 bits. Is this only due to the example?

NPash.1
NPash.1Author
Associate II
August 12, 2020

Yes, It will write only one word = 32bits, but the code is not writing ino specified address. If it works for one word we can make it for other addresses as well by adding loop. Is that shared code is correct in order to write one word.

LLehm.1
Visitor II
August 12, 2020

What is about this?

 /* Fill EraseInit structure*/

 EraseInitStruct.TypeErase  = FLASH_TYPEERASE_PAGES;

 EraseInitStruct.PageAddress = FLASH_USER_START_ADDR;

 EraseInitStruct.NbPages   = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE;

 if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)