user3379745
user3379745

Reputation: 77

Arduino: Detect code info

Is there any way to detect the code version or any info like last updated or size of code or size of binary code?

Here is example:

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  // turn the LED on (HIGH is the voltage level)
  delay(1000);
  // wait for a second
  digitalWrite(LED_BUILTIN, LOW);
  // turn the LED off by making the voltage LOW
  delay(1000);
  // wait for a second
}

If I upload code to Arduino board, can I detect any info inside Arduino regarding the size of the code or the last modified time or upload time etc?

Thanks.

Upvotes: 0

Views: 450

Answers (2)

per1234
per1234

Reputation: 974

Compilation date is stored in the __DATE__ macro, the current system time at compilation is stored in the __TIME__ macro. Also useful may be the __FILE__ macro, which stores the filename. Reference (and other relevant macros): http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_preprocessor.Pre-defined_macros.html

There is some interesting discussion about something along these lines here:

https://github.com/arduino/Arduino/issues/5618

In that thread they're discussing ways to store the Git hash of the code at the time of the compilation/upload. If you're using Git version control that information would probably be more useful than last modified time or code size for determining which version is running on your microcontroller and being able to easily retrieve that version. I'm not sure how crazy I am about automatically doing a commit on every upload by you could always squash them later once testing is finalized. If you don't use Git, the same techniques could be adapted to adding any other version information you like and even doing automatic backups of the code at that version.

As explained by facchinm in that thread, recent versions of the Arduino IDE provide hooks throughout the build process that you can use to add additional actions. See:

https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification#pre-and-post-build-hooks-since-ide-165

The original proposal is to store the version information in EEPROM. I believe the most common way to do this is via an .eep file. A problem with this idea is that the popular Optiboot bootloader used by the Arduino Uno as well as popular 3rd party hardware packages does not support writing to EEPROM during uploading (in order to fit the bootloader in a 0.5 kB boot section). You would also need to avoid conflicts caused by use of the same EEPROM addresses by the sketch.

The other thought was to instead store it in the code itself. I guess this would be done by using a hook to automatically update a header file "library" that is included by the sketch. e.g.:

#include <VersionTracker.h>

Now how do you access that information? You could have code in the sketch that sends out that information on request or startup. A simple example:

void setup() {
  Serial.begin(9600);
  Serial.print(F("Filename: "));
  Serial.println(F(__FILE__));
  Serial.print(F("Compilation timestamp: "));
  Serial.println(F(__DATE__ " " __TIME__));
}

void loop() {}

Will print this information to the Serial Monitor on startup. Of course you could use any other method of communication. This sort of thing will add some significant overhead so it would perhaps be preferable (if less beginner friendly) to download the firmware from the microcontroller and then figure out a way to find the version information from the disassembly.

Upvotes: 1

Delta_G
Delta_G

Reputation: 3243

The time you cannot get. The Arduino doesn't have any clock or any way to know the time.

The code size you could get if you use AvrDude to read the hex code back off the chip and then just look at the size of what you read back.

Upvotes: 0

Related Questions