cancel
Showing results for 
Search instead for 
Did you mean: 

How to write data into board flash memory?

NPash.1
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions

use this example as reference:

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

It works for​ me flawlessly.

View solution in original post

8 REPLIES 8

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 Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

Include your chip part number.

If you feel a post has answered your question, please click "Accept as Solution".
prain
Senior III

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
Associate II

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.

LLehm.1
Associate

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

NPash.1
Associate II

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
Associate

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)

use this example as reference:

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

It works for​ me flawlessly.