Skip to main content
Associate
October 18, 2023
Question

STM32H730VBT6 write user settings in FLASH

  • October 18, 2023
  • 17 replies
  • 5011 views

I couldn’t figure out how to write a simple code to write some bytes at the end of the Flash on my STM32H7. I don’t need much bytes, only one uint32_t.

I tried using HAL_FLASH_Program by unlocking the flash first but couldn’t make it work..

Any help?

 

This topic has been closed for replies.

17 replies

Tesla DeLorean
Guru
October 18, 2023

Problems here are there's only one flash sector (128KB), you can only write once per erase cycle, the flash line is 256-bits wide (32-bytes), so you're probably going to want a section toward the end, and journal your writes such that you can write sequential words, and recover the settings from the last set.

The HAL H7 libraries should have examples of writing to FLASH, use those as the basis for your code.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
BaxtouxeAuthor
Associate
October 18, 2023

Thanks for your quick reply.

Any idea where I can find a HAL H7 example to write in the Flash? 

TDK
October 18, 2023

> Any idea where I can find a HAL H7 example to write in the Flash? 

Use the Example Selector in CubeMX to find and run relevant examples.

TDK_0-1697650036948.png

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
October 18, 2023

Might I suggest looking through the repositories locally with a file browser or manager, or on GitHub directly?

https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/NUCLEO-H723ZG/Examples/FLASH/FLASH_EraseProgram

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
MM..1
Super User
October 18, 2023

You choice MCU not optimized for this. You dont specify where is your code flashing placed ? External QSPI?

Optimal simple way is use external I2C EEPROM, but if you strict require write, your flash is with CRC and can write only once into 32 bounded bytes area. And when is flash full of additional flashing , you need reflash full sw.

 

BaxtouxeAuthor
Associate
October 19, 2023

I'm trying to add this small data at the very end of the program FLASH, as far as possible from the code.

BaxtouxeAuthor
Associate
October 19, 2023

I'm not sure which STM32 would be closest to my STM32H730VBT6 to take an example from. STM32H74x? Or STM32H735x?

MM..1
Super User
October 19, 2023

If your write is only once and ontime programming , why not use stmCubeprogrammer cmdline?

BaxtouxeAuthor
Associate
October 19, 2023

I'd like to write when a specific command comes through the UART

Pavel A.
October 19, 2023

Would Option Bytes by a solution to my problem?

Option Bytes - not at all. A similar topic was here recently, about some other STM32. A short summary: unless the firmware install procedure has filled the whole flash page, you can find properly aligned empty space past the firmware image and write your data there. Properly aligned here means on 32 bytes boundary.  Write also in multiples of 32 bytes (256 bits). 

Also, as Tesla DeLorean noted, "value line" STM32H7 can actually have more than one flash page, but its up to you to find them and verify they are working. YMMV.

BaxtouxeAuthor
Associate
October 19, 2023

Writing at the end of the firmware sounds like a great idea.

Do I need to erase the bytes first? Or is it a simple as :


uint32_t data[8] = { 0x123456778,
0x123456778,
0x123456778,
0x123456778,
0x123456778,
0x123456778,
0x123456778,
0x123456778,
};
void writeUserSettings(uint32_t* data, uint32_t address)
{
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, address, data);
HAL_FLASH_Lock();
}

Pavel A.
October 19, 2023

As simple as. Locate an empty place and write.

BaxtouxeAuthor
Associate
October 19, 2023

Can you be more specific? I know the address where I want to write but don’t know how to

Pavel A.
October 19, 2023

Yes. Unlock, then write. Then lock (optionally). 

Tesla DeLorean
Guru
October 19, 2023

The memory must be in an erased state, and then you get one-shot writing it. Perhaps check content first?

When you write/download your test app in the debugger it's going to erase the memory within the whole 128KB sector. You don't need to erase it again, as this will also erase your application, but you'll get one chance to write each block of 32-bytes. You can walk forward, or backward, within memory and write those slots.

https://github.com/STMicroelectronics/STM32CubeH7/blob/master/Projects/NUCLEO-H723ZG/Examples/FLASH/FLASH_EraseProgram/Src/main.c#L131

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