How to Embed Software Version, Build Date, and Time into Flash with Keil MDK
This guide explains a practical method for storing software version, compilation date, and time directly in a designated Flash address using Keil MDK, covering the necessary __attribute__ syntax, code examples, and tips to reduce firmware size.
In many embedded projects, adding version information to the firmware is essential for tracking releases and debugging. This article shares a common technique for writing software version, compile date, and compile time into a specific Flash region using the Keil MDK environment.
Implementation Method
The approach stores strings at fixed Flash addresses by leveraging the GCC-style __attribute__((at(address))) syntax. Example code:
#define VERINFO_ADDR_BASE (0x0800FF00) // Flash storage base address
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 places three null‑terminated strings at the defined addresses, allowing the firmware to read version information at runtime.
Understanding __attribute__
The __attribute__ keyword lets you assign special properties to functions, variables, or types. The general form is __attribute__((attribute-list)). For placing data in Flash, the at(address) attribute is used.
Standard C Predefined Macros
Two useful predefined macros are demonstrated: __DATE__ – compilation date string (e.g., "Apr 13 2021"). __TIME__ – compilation time string (e.g., "20:00:00").
Other macros such as __FILE__, __LINE__, and __STDC__ are mentioned for reference.
Ensuring Correct Build Information
Keil MDK may not rebuild source files automatically, so the "Always Build" option must be enabled to guarantee that the version, date, and time strings are updated on each compilation.
Reducing Firmware Size
Generated HEX files pad unused Flash with 0x00, inflating size. Two strategies are suggested:
Place the version info at an appropriate Flash address to avoid excessive offset padding.
Adjust the ROM size setting in the project options to eliminate unnecessary padding.
Both methods can shrink the final binary, but they carry risk if the code size grows beyond the allocated region, so careful Flash layout planning is advised.
Key Takeaways
Use __attribute__((at(address))) to embed constant strings in Flash.
Leverage __DATE__ and __TIME__ for automatic build timestamps.
Enable "Always Build" in Keil MDK to keep version data current.
Optimize Flash placement and ROM size to keep firmware binaries compact.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
