I'm making a printed circuit board and within the board there is a 10-pin ISP connector attached to the AVR microcontroller. So I can flash new code to the AVR.
I have several of these same boards, but with different code in it. I don't want to flash it every time I need to know what's inside that particular AVR. I just want to extract the version number of that code somehow through avrdude.
What's the easiest way to do this?
If you can read the content of the flash via a connector, you can have your build process compile and link that flash content in a well defined format which contains the version number, or some other version information.
Then you can read using avrdude and write your own program to parse (part of) the content of the flash, and then maybe script that. This helps avoid cases where your EEPROM contains one version, but the Flash already contains a new one, or vice versa.
You get bonus points for
"1.2.3|main|dirty"
You need to extend your existing build system to do all the necessary steps. Those might involve linker scripts, linker sections, C or assembly language sources, and other things.
Going on my AVR experience using an avr-libc and avr-gcc based toolchain, I would start by adding something like
jmp skip_version_number
.global version_number
.type version_number, @object
.balign 8
version_number:
.quad 0xdeadcafef00d0001
.asciz VERSION_STRING
.asciz GIT_BRANCH_NAME
.asciz GIT_DIRTY_FLAG
.asciz ""
.size version_number, . - version_number
skip_version_number:
to one of the .initN
sections:
The .initN
sections are linked relatively close to the front of the image, so it should not be necessary to read a lot of the flash
It contains a 64bit magical number aligned to an 8 byte boundary, so the version structure should be easy to find.
The magical number can be easily changed to allow for a different number for a different format when I eventually want to change the format by adding e.g. a checksum which this does not contain yet.
Parsing a list of NUL terminated C strings until an empty string appears should be relatively easy to write and still flexible.
Then verify the result from the disassembly dump and the map file.
Adding the checksum might involve generating a binary file for the payload at build time, then calculate that file's checksum as a separate binary file, and replacing the above content definitions with
.balign 8
version_number:
.incbin "version_number.bin"
.balign 8
.incbin "version_number_checksum.bin"
.size version_number, . - version_number
This can be made arbitrarily complex, but I hope this gives a few ideas.