Skip to main content
Garnett.Robert
Senior III
September 30, 2023
Solved

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

  • September 30, 2023
  • 2 replies
  • 1487 views

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

 

 

This topic has been closed for replies.
Best answer by Garnett.Robert

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

 

2 replies

Pavel A.
Super User
September 30, 2023

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
Garnett.RobertAuthorBest answer
Senior III
October 1, 2023

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