Panel For Example Panel For Example Panel For Example

Adding Version Info to MCU Firmware

Author : Adrian September 03, 2025

Source Code Versioning

Effective management of source code versions is essential for any project. While creating compressed archives of source code is one method, it can make tracking changes cumbersome.

For more robust project management, using version control systems like SVN or Git is highly recommended. Both are widely used tools with extensive documentation available.

Firmware Versioning

Firmware typically refers to the executable files for a microcontroller, such as .hex or .bin files. There are two primary ways to manage firmware versions.

1. Versioning in the Firmware Filename

The firmware filename can be configured in the IDE to automatically include version information upon compilation. Build tools like Keil MDK provide options to customize the output filename.

2. Embedding Version Information in Firmware

For mass-produced devices deployed in various locations, embedding version information directly into the firmware is crucial. This allows for easy identification of the software version on a specific device, which is essential for debugging and support.

A common technique is to write version data to a specific address in the flash memory. The following example demonstrates how to do this in the Keil MDK environment by storing the software version, compilation date, and time.

#define VERINFO_ADDR_BASE (0x0800FF00) // Base address in Flash for version info const char Software_Ver[] __attribute__((at(VERINFO_ADDR_BASE + 0x00))) = "Software: 1.0.0"; const char Compiler_Date[] __attribute__((at(VERINFO_ADDR_BASE + 0x40))) = "Date: " __DATE__; const char Compiler_Time[] __attribute__((at(VERINFO_ADDR_BASE + 0x60))) = "Time: " __TIME__;

This code stores these strings at a predefined flash address. The information can then be read by the application at runtime. This method involves a few key C language features.

The __attribute__ Syntax

__attribute__ is a compiler-specific extension used to specify special attributes of variables or functions. The syntax is:

__attribute__ ((attribute-list))

It can be used to set various attributes for functions, variables, and types. 

Standard C Preprocessor Macros

The code uses __DATE__ and __TIME__, which are standard predefined macros in C:

  • __DATE__: The compilation date as a string literal (e.g., "Apr 13 2021").
  • __TIME__: The compilation time as a string literal (e.g., "20:00:00").

Other useful standard macros include:

  • __FILE__: The name of the current source file.
  • __LINE__: The current line number in the source file.
  • __STDC__: Defined as 1 if the compiler conforms to the standard C specification.

Ensuring Timestamp Updates in Keil

By default, Keil MDK only recompiles source files that have been modified. To ensure the __DATE__ and __TIME__ macros are updated with every build, the source file containing the version information must be configured to "Always Build" in the file's properties within the project options.