cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE Projects: How to add textural version no and date to project binary

Garnett.Robert
Senior III

Hi,

I have small headless firmware and wish to include a version constant in the Binary image so I can determine the version when using the debugger.

It's easy if your project has a display, but I can't figure out an easy way of doing it for a simple system.  If I put a string constant in main.c the linker doesn't include it as the string isn't used anywhere. 

Does anyone know of an easy way to do this with an example?

Regards

Rob

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Garnett.Robert
Senior III

Hi,

I figured it out:

main.c

static const char version[] __attribute__((used)) __attribute__((section (".version"))) = "Ver: V2.0.0 01/10/2023 13:27";

Linker Script

...

   /* Version Data Into Flash */
  .version :
  {
    . = ALIGN(4);
    KEEP(*(.version))       
    KEEP(*(.version*))       
    . = ALIGN(4);
  } >FLASH_CODE

Snag_38680747.png

 All good

Regards

Rob

 

View solution in original post

2 REPLIES 2
Pavel A.
Evangelist III

The quickest way is to make a reference to this string somewhere in code or data, so that compiler or linker cannot tell that it is unused.

If you have more time see this thread (about the linker, custom sections, KEEP...)

Garnett.Robert
Senior III

Hi,

I figured it out:

main.c

static const char version[] __attribute__((used)) __attribute__((section (".version"))) = "Ver: V2.0.0 01/10/2023 13:27";

Linker Script

...

   /* Version Data Into Flash */
  .version :
  {
    . = ALIGN(4);
    KEEP(*(.version))       
    KEEP(*(.version*))       
    . = ALIGN(4);
  } >FLASH_CODE

Snag_38680747.png

 All good

Regards

Rob