cancel
Showing results for 
Search instead for 
Did you mean: 

How to share variable between bootloader and application without overwriting

RCesc.1
Associate II

Good morning,

I have a problem sharing a variable between bootloader and application into my STM32F303ret6 uC. I need to write a shared variable to tell the bootloader that it has to wait for new firmware.

I read many posts which suggest to declare a variable into RAM memory. It works, but I noticed that if I disconnect the board (i'm working with Nucleo64 board) and then reconnect again, that memory address is overwritten with other values. I want to reserve it only for my variable. How can I do that? is it better to store that variable into a flash page?

Into the linker I reduced RAM length by 1Kb.

RAM_D1 is the address where I write my variable.

/* Memories definition */

MEMORY

{

 CCMRAM (xrw)  : ORIGIN = 0x10000000,  LENGTH = 16K

 RAM   (xrw)  : ORIGIN = 0x20000000,  LENGTH = 63K /* change to free 1Kb */

 RAM_D1  (rw)  : ORIGIN = 0x2000FE00,  LENGTH = 500

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

}

... (at the end of the linker file..)

 .ram1block (NOLOAD) :

 {

  KEEP(*(.ram1section))

 } > RAM_D1

variable declaration in main.c:

uint8_t __attribute__(( section(".ram1section") )) b_RemainBootMode;

1 REPLY 1
TDK
Guru

> noticed that if I disconnect the board (i'm working with Nucleo64 board) and then reconnect again, that memory address is overwritten with other values. I want to reserve it only for my variable

If you remove power, the contents of the RAM are lost. No way around that.

Typically this is done with a pointer, as you can't guarantee the bootloader and application will place the variable at the same address. If you use a pointer, you don't need anything special in the linker file other than not declaring the memory the variable is at as a part of a section.

uint8_t * b_RemainBootMode_ptr = (uint8_t *) 0x2000FE000;
 
...
 
if (*b_RemainBootMode_ptr) {
    ...
}

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