gil22:
Is there a simple way to create an hex file to be sent to the chip for programming via serial, from a simple few lines of code ?
for example, how you create an hex from this basic thing :
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Is there a simple algorithm to do this ?
Well, I assume that you know that "Verify" or "Upload" in your Arduino IDE will compile and/or compile and upload (send to your Arduino the finished, compiled program) as binary machine code in the form of an Intel IHEX file that the bootloader turns back into pure bytes and writes them to the flash memory of your board...
For example, one line of an IHEX file that the Arduino IDE generates looks something like this:
[b]:1000000064C0A9C072C071C070C06FC06EC06DC046
[/b]
It contains information:
** **:[color=red]10[/color] [color=green]0000[/color] [color=blue]00[/color] [color=purple]64C0A9C072C071C070C06FC06EC06DC0[/color] 46** **
The colon ":" means this is the start of a line of hex data
Red is how many bytes the line contains (0x10 == 16 decimal)
Green is the memory address that the code will go into (here 0x0000 to 0x000F)
Blue is the "record type" and 00 means "Data Record" (see ** below)
Purple is the 16 bytes of actual data
Black is the checksum of the line derived from "0xFF - 8 bit rolling sum of the data"
When this code is uploaded into your Arduino board, the memory will be written like this:
[b]
[tt]Address Data
0x0000 0x64
0x0001 0xC0
0x0002 0xA9
..... ....
0x000D 0xC0
0x000E 0x6D
0x000F 0xC0
[/b][/tt]
The next line will start at address 0x0010 and contain 16 more bytes up to 0x001F.
This goes on until all of your code is uploaded and written to flash memory. Then when you reset (uploading automatically does this, or you can press the button manually), the Arduino goes to address 0x0000 and start executing code from there (i.e. runs your sketch).
** Data type codes:
DATA_RECORD 00
END_OF_RECORD 01
EXT_SEGMENT_ADDR 02
SEGMENT_START_ADDR 03
EXT_LINEAR_ADDR 04
LINEAR_START_ADDR 05
If this isn't what you're asking about, then you must want to do something else. Please elaborate what you want to do so maybe I can help you.