Electronic – arduino – Append source code to flash image when programming an AVR

arduinoatmegaattinyavrlinux

Is it possible for Arduino / ATmega / ATtiny when programming the device to attach the C source code at the end the machine code flash image? That way if I have the device, I can always retrieve the source code by reading flash memory.

Easiest way I can think of myself is some include-like statement at the end of the C-program that reads the source code from file and attaches it as data to the program. But I have no clue how to do that.

Best Answer

You should always be able to add any kind of data to your code. Just create a text array which you fill with the C code.

char source_code[9189] = {'m', 'a', 'i', 'n', '(', ')', 13, 10, '{' ,'}', 0};

You'll definitely want to write a program to create the array from a text file!


Personally I think this is a bad way to do it. Get organized! Include a code reference including version number, which refers to the code on your PC. You may even add the possibility to read out the reference in-system. I often include code that outputs the controller's identification over UART when a certain test pin is pulled low, like it would on a test block. The test block could then display it as for example

Program ID: 10121
Revision: 02

This should be enough to find the source code back on your PC.

edit
If the idea is to let the code travel with the controller so that you can get it to other PCs, then I would also use Fake's idea: instead of just sending the program's idea you could purge the complete source that way.
Note that I'm still not completely convinced. Except for the most simple programs a program design is more than just the source code. What about flow charts or other design documents.