Create a hex from a few lines of code

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 ?

What are you trying to do?

Select File:Preferences and check Verbose outputs,
Compile the code. The .hex file is created. You can see the path where the file is stored.

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 ?

Personally I use the IDE for this. I click on the Upload icon and it does it for me.

What is it that you really want to do ?

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 :

...

Is there a simple algorithm to do this ?

No. It's called a compiler, and compilers are not simple.

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.

OP probably wanted the answer in reply #2 so they're happy and gone away

aarg:
OP probably wanted the answer in reply #2 so they're happy and gone away

Hmmm... well if a rank beginner loaded the "blink" example, they may then wonder what to do with it, but then how would they know that the intermediate result is a "hex file"?

If I myself had no clue, I might ask "OK, now how do I send this program into the Arduino and see the LED blink?

I might not know that the source code is compiled, and I certainly wouldn't know into what form it was compiled or how the compiled code "gets into" the AVR.

That's why I suspect he's asking something else. Well, if he doesn't come back, then obviously you're right and I remain confused. :slight_smile:

(and I wasted a fair bit of time explaining Intel HEX files as well!)

Krupski:
Hmmm... well if a rank beginner loaded the "blink" example, they may then wonder what to do with it, but then how would they know that the intermediate result is a "hex file"?

"sorcerer's apprentice" are quite common here.

Google 'hex editor". There are many of them, and at least one will do what you want.

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 ?

Cross-posted here I think.

@gil22 - cross-posting is likely to get you into trouble.

At least for once it's not ME getting into trouble! :slight_smile: