5
\$\begingroup\$

I have a hex file with a size of 42 kB. Does the size matter while loading it in a microcontroller? Can I load 42 kB hex file in a ATMEGA32 chip which has a memory of 32 kB?

\$\endgroup\$
  • \$\begingroup\$ Normally a hex file is compiled for a specific processor and should be used only in that processor. At least for the uc I have used. \$\endgroup\$ – russ_hensel Mar 26 '11 at 20:47
5
\$\begingroup\$

You can use avr-size to check the real size of your program:

[jpc@jpc sepack] avr-size sepack.elf 
   text    data     bss     dec     hex filename
   4396       6     277    4679    1247 sepack.elf
[jpc@jpc sepack] avr-size sepack.hex 
   text    data     bss     dec     hex filename
      0    4402       0    4402    1132 sepack.hex

As you can see it works better with .elf files since it can also show you both how much ram you need (data+bss) and how much flash will be used (text+data). With the .hex file only shows you the second figure (labeling it data)

\$\endgroup\$
  • \$\begingroup\$ So whats the size of my hex file ? I get the following details while i check my file.. text - 13858, data - 926, bss - 1043 dec - 15927, hex - 3dd3 \$\endgroup\$ – Sakti Apr 1 '11 at 4:56
  • \$\begingroup\$ You will need 13858+926=14784 bytes of Flash and 1969 bytes of RAM (+ some space for stack space). I guess that means a part with at least 16kB and 4kB RAM (3 would be enough but that may be hard to find). \$\endgroup\$ – jpc Apr 1 '11 at 15:04
  • \$\begingroup\$ You may have trouble fitting into the RAM of the ATmega32. You should either lower the RAM usage, carefully check stack space usage or switch to a part with 4k RAM. \$\endgroup\$ – jpc Apr 1 '11 at 15:07
9
\$\begingroup\$

The hex file generated is often somewhat human-readable and includes checksum data, both of which inflate the size a few times. I believe that AVRs use the Intel HEX format, while other micro toolchains use other formats; e.g. Codewarrior for Freescale HCS08 provides Motorola S-records, which is extremely similar.

If you open the .hex file with a text editor (Notepad), it should look similar to this, though much longer:

:10010000214601360121470136007EFE09D2190140
:100110002146017EB7C20001FF5F16002148011988

where the various fields are described in the example on Wikipedia. There are only 16 bytes in each of those lines that denote data that will be stored in the micro, the rest is checksum and control data that is just used by the flasher program.

\$\endgroup\$
  • 1
    \$\begingroup\$ Hex files also allow for gaps in the data (the first field is the load address). So, potentially, a small non-contiguous hex file could have a large footprint in RAM/flash \$\endgroup\$ – Toby Jaffey Mar 26 '11 at 21:06
3
\$\begingroup\$

Each byte in a hex file takes two hex digits, plus a few characters per record. Your hex file should fit in the device's flash memory.

\$\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.