cancel
Showing results for 
Search instead for 
Did you mean: 

Save data in Flash of STM32 through Code 1, Accessing same data with Code 2

Lucifer37
Associate II

Hi,

I am working on a project that store the Configuration data of one device, It needs to be stored in specific section

static unsigned char myBuffer[4] __attribute__((section (".m_data_20000000"))) { 11, 22, 33, 44};

with this command I'm able to store it in Program-1 

linker script of program-1

.m_data_20000000 :
{
. = ALIGN(4);
KEEP (*(.m_data_20000000*))
. = ALIGN(4);
} >FLASH

I have tried NOLOAD NOINIT it is not working...........what changes need to do in program-2

But now I want the stored myBuffer[4] data from Flash of stm32 which is stored previously by program-1, without erasing I want access of myBuffer[4] with program-2?

What changes I need to do in IAR and STM32Cube IDE for telling them not erase that section, I want access, what type of variable I need to declare in program-2 it is pointer or extern..........

Please Help me.... 

1 ACCEPTED SOLUTION

Accepted Solutions

0x20000000 strikes me as a RAM address.

Perhaps use structures and a pointer to the structure. Hide an area of FLASH from the Linker so it doesn't touch or erase it. And then manage the space directly in the applications. 

The EEPROM Emulation could work similarly just carve out common FLASH pages both use for this data. TBH that's likely overkill.for things like keys, serial numbers and calibration data.

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

View solution in original post

15 REPLIES 15
Andrew Neil
Evangelist III

So you want to:

  1. Some code in the STM32 writes data to flash.
  2. You re-program the STM32 with different code.
  3. You want that new code to be able to read the data stored at (1).

Yes?

 


@Lucifer37 wrote:

I have tried NOLOAD NOINIT it is not working... 


What, exactly, is "not working" ?

  • The data isn't stored at all?
  • The data is stored in the wrong place?
  • The data gets erased during the re-programming?
  • The data survives re-programming, but the new code doesn't read it (properly)?
  • other??

STM32Cube IDE shouldn't erase more of the flash than it needs to - so, for the data to persist, you just have to make sure that it's outside the area that gets erased during programming.

You'd need to ask IAR what their tool does - but there should certainly be a way to configure this.

Lucifer37
Associate II

These problems I'm facing, how to access data through re-programming and it's getting erased how to stop it

should I use it as extern or coping through section address?

  • The data gets erased during the re-programming?
  • The data survives re-programming, but the new code doesn't read it (properly)?

How to do it in STM IDE and IAR please send me recourses

I answered the question about erasing.

One way to easily access it is to just have it at a fixed address, and access that address directly.

You haven't said what STM32 you're using, but that's exactly how the STM32F0 EEPROM emulation does it:

 

/* EEPROM start address in Flash */
#define EEPROM_START_ADDRESS  ((uint32_t)0x0800F800) /* EEPROM emulation start address:
                                                      * Penultimate 1K page of a 64K flash
                                                      */

 

https://community.st.com/t5/stm32-mcus-embedded-software/x-cube-eeprom-not-for-stm32f0/m-p/674820

 

PS:

Rather than use a Magic Number, the stm32f030x8.h file defines FLASH_BANK1_END...

Lucifer37
Associate II

I'm using STM32H745

Lucifer37
Associate II

Hi Andrew Neil,

I'm using STM32H745ZIT3.....I don't know you understood my requirement or not, This EEOPROM Emulation seems

different concept, it uses different approach like when power is off we able to access same data from same code

when re-program or dump new program-2 it will erase that config data

I'm using Linker script to store config data in specific region.......

Program-1
--------------------
| config data   |
| store @        |
| 0x0800F000 |
|                      |
|linker can do |
|define sec     |
|                      |
---------------

STM32 Flash
-------------------
| source code | 0x08000000
|                      |
|                      |
| code End      |
|                      |
|-------------------|0x0800F000
| Config data   |
-------------------|0x080FFFFF

Program-2
--------------------
|Access the    |
| config data   |
| store @        |
| 0x0800F000 |
|                      |
|                      |
|                      |
|                      |
-------------------

0x20000000 strikes me as a RAM address.

Perhaps use structures and a pointer to the structure. Hide an area of FLASH from the Linker so it doesn't touch or erase it. And then manage the space directly in the applications. 

The EEPROM Emulation could work similarly just carve out common FLASH pages both use for this data. TBH that's likely overkill.for things like keys, serial numbers and calibration data.

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

@Lucifer37 wrote:

This EEPROM Emulation seems different concept


I'm just using it as a known-working example of how to write data to Flash, and read that data back from Flash.

Those basic principles will be the same whether you code it yourself from scratch, or use the software pack.

 

The issue of erasing the flash during programming is entirely separate - that's just down to the programming tools that you use.

As already noted, STM32CubeIDE will only erase as much Flash as it needs for the code being programmed - so you need to make sure that wherever you put your data is outside that region (or regions).