Search code examples
memoryhexavratmegaatmel

AVR memory and intel hex


I work on a simple AVR programmer for my university project, and I am stuck with understanding how I can map memory from hex file to actual flash memory.

For instance, intel hex provides us the information about start address of data block, number of bytes in it and data itself. The trouble comes from that AVR MCUs, in particular ATmega16, often have one address for two bytes: high and low.

At first, I wrote a straightforward function, that just reads all the data from hex file and write it sequentially, increasing address by one each two bytes passed. To my surprise it works on simple blinky code. However, I am not sure, if this approach would work, if someone needs complex memory structure.

So the questions are:

  1. Will this solution work on complex memory structures?
  2. If not, how can I map intel hex address into actual flash address? The problem is there is no high and low bytes in intel hex format, only address = byte.

Solution

  • Intel hex uses byte addresses. The PC program counter refers to 16-bit word addresses. If you mean the word address to be the "actual address", then just double the number that represents the start address of the line in the hex file.

    What do you mean by "complex memory structures"? Memory locations need unique addresses, no matter how that address space is broken up. I am not familiar with program memory spaces that don't start with 0 and continue linearly, but if there were such a scheme, a line in an intel hex file can specify the contents of any contiguous memory section starting at any address.

    Edit:

    Each line of an intel hex file can only contain up to 255 bytes. Typically, the data is split into 16 or 32 bytes chunks. Each line contains the start address of the chunk (which is added to the base address if used). A chunk doesn't have to start at the end of a previous chunk, and they can be out of order, too.

    As for the complex memory structures you describe, most programs have them already. There is usually a vector table at the start, followed by a gap, followed by the crt and main program. Data to initialize global variables follows that. If there is a bootloader, it is placed in a special section at the end of memory.