3
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ The source code for a microcontroller is usually useless without the schematic for the circuit it is in. \$\endgroup\$ – Turbo J Apr 8 '12 at 10:09
  • \$\begingroup\$ @TurboJ - I agree, I mention other documents in my answer. But if a program is well documented it includes in comment a complete listing of all I/O. And as I understand it OP wants to distribute the controller on its PCB. \$\endgroup\$ – stevenvh Apr 8 '12 at 10:28
  • \$\begingroup\$ Or you can have all of this on something like a subversion server hosted online... then your computer and the server have full logs and access. \$\endgroup\$ – Kortuk Jun 15 '12 at 20:52
5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I like the pin pulling method! The thought behind my question is to solve the issue in case my PC is not available. \$\endgroup\$ – jippie Apr 8 '12 at 9:08
  • 1
    \$\begingroup\$ What are you going to with the C code if your PC is not available? And how are you going to read it?? \$\endgroup\$ – stevenvh Apr 8 '12 at 9:09
  • \$\begingroup\$ @jippie You could store your code on an online cource code repository (github or even Dropbox), if you want to have access to your code on PCs other than yours. \$\endgroup\$ – m.Alin Apr 8 '12 at 9:23
  • 1
    \$\begingroup\$ A more relevant example is what if you give the IC to someone without an internet connection? Making the device capable of describing it's own code is a really cool idea. It's like the hardware equivalent of a Quine \$\endgroup\$ – Connor Wolf Apr 8 '12 at 9:34
  • 1
    \$\begingroup\$ @jippie - You could combine stevenvh's idea of a test pin, and your idea of embedding the code in flash, and have it enumerate the code when you pull a test-pin low or high. That would have the advantage of making it easier to get the code-contents. \$\endgroup\$ – Connor Wolf Apr 8 '12 at 9:38
2
\$\begingroup\$

Found a similar question here. It seems not to work for me because the file I'm trying to include has double and single quotes in it. It works if used with a makefile, but the Arduino IDE doesn't. It does work for programming from the command line though.

Combine the:

$ echo hello world | xxd -i > xxd.txt

xxd.txt contains:

  0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x0a

or

$ cat blinker.ccp | xxd -i > xxd.txt

Then include it as follows:

const char text[] = {
#include "xxd.txt"
};

Just need to call it from the program somewhere in a print or so, otherwise the data will be optimized out.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.