2
votes

I'm trying to understand the Arduino bootloader. I've found some intro here: Arduino Bootloader, but I need more details and because of it I'm asking for help.

During my research i found a start point: OK, Arduino (atmega family) has a specific block of flash dedicated to bootloader. Once the mcu has a bootloader, it can download the new program via serial and store it on flash, at address 0x00.

Let's asign the atmega328p for this question.


#1 - If you take a look at datasheet page 343, you will see a table, showing some info about the bootloader size:

enter image description here

By this table I understood: if i set BOOTSZ1/0 to 0/0, I can have a 2K bootloader and it will be stored in flash stack: 0x3800 ~ 0x3FFF.


#2 - If you open the hex file of ATMEGA328_BOOTLOADER generated by Arduino, you will see the bootloader stored at:

:10**7800**000C94343C0C94513C0C94513C0C94513CE1 

to

:10**7FF0**00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF91

If you consider 7FF0 - 7800 you will get 7F0 (2K bytes of program)


#3 - If you open the makefile (C:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\atmega) you will see this argument for atmega328:

atmega328: LDSECTION  = --section-start=.text=0x7800

Where 0x7800 matches the hexa file from bootloader.


Questions:

1- Why the datasheet tells me that I have an special place for bootloader and the makefile of Arduino force it to be stored in a different place?

2- What means the line of an hexa file?

:10E000000D9489F10D94B2F10D94B2F10D94B2F129

:10    : (?) 
E000   : Address
00     : (?)
0D9489F10D94B2F10D94B2F10D94B2F1   : data (?)
29     : CRC (?)
1

1 Answers

1
votes

First of all, I'm not sure we have the same atmel datasheet; your table was 27-7 on my one, but anyway the data is the same.

Disclaimer: I'm not 100% sure about this, but I'm pretty confident.

I think that the "problem" here is that "words". Reading in the overview of the core they wrote

Most AVR instructions have a single 16-bit word format. Every program memory address contains a 16- or 32-bit instruction

This means that instructions are 2 byte wide. So your address 0x3800 corresponds to 0x7000 byte from the start of the memory, while the end of the bootloader 0x3FFF corresponds to the bytes 0x7FFE and 0x7FFF.

This is confirmed by the memory size: 0x7FFF corresponds to 32k, which is the available flash. Since the bootloader is at the end of the memory (as the pictures from the DS imply), this means that 0x3FFF should be the last memory cell.

EDIT:

When I answered, I did not read the second part of the question. The format is the so-called Intel Binary format. See Wikipedia for the detailed information. Here is a brief summary.

You correctly identified the field division:

:      : Start code
10     : Number of data bytes (in this case: 16)
E000   : Starting address of the data bytes
00     : Record type (in this case: data)
0D9489F10D94B2F10D94B2F10D94B2F1 : data payload (in this case: 16 bytes)
29     : CRC

The record type can be

  • 00: data
  • 01: end of file
  • 02: extended segment address
  • 03: start segment address
  • 04: extended linear address
  • 05: start linear address

The CRC is obtained summing all the bytes (starting from the length one, so the very beginning), then getting the LSB and finally making the 2 complement. In this case the sum is 2519, which is 0x9D7 in hex. The lower byte, D7, corresponds to 215. Its 2 complement can be calculated as 256 - x, so 41, which means 0x29 in hex.