making sense of ATMega328P bootloader addresses

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

In Table 30-7 of the ATMega328P datasheet, it says that with BOOTSZ1 and BOOTSZ0 both programmed to 0, the start of the bootloader secion is 0x3800.

I tried using this address for the start of the .text section as discussed here:

 

  http://www.avrfreaks.net/sites/d...

 

But it didn't work (after concatenating bootloader and application programs as described elsewhere in that FAQ, and programming fuses as required).

 

I took a look in the optiboot .hex file provided with Arduino and see that the first address for that bootloader seems to be 0x7800.  I tried that instead, concatenate and sure enough bootloader and main app then both work as expected.

 

I though maybe word vs byte address or something (but mega is 8 bit so what's the difference?), but 0x7800 != 2 * 0x3800, so I have no more ideas.  What's going on here?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Think about it 32KB is 0x8000 so an address up near the end of that is going to be 0x7??? so when you see the crazy mob at Atmel talking about 0x3800 then it has to be a word (16bit) not a byte (8bit) address. They do this because the finest granularity in AVR flash is 16 bits but it confuses the living hell out of almost every one who encounters this the first time. To convert word to byte double it so 0x3800 is really 0x7000 which makes a whole lot more sense in a chip with 0x8000 bytes. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok I get it now thanks.  What confused me was optiboot uses fuse settings 1024 word = 2048 bytes of bootloader hence 0x7800 addresses in optiboot, not 0x7000.

 

Because it's a memory hole between [0x7000, 0x7800) it works with wrong .text address relative to fuse settings, you just start with a lot of NOPs.  Until you try to use too large a bootloader of course :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

optiboot uses fuse settings 1024 word = 2048 bytes of bootloader

Does not!   Optiboot is 256 words (512 bytes), and starts at 0x7e00 (byte address.)   Perhaps you're looking at ATmegaBoot?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Yes.  A file called ATmegaBOOT_168_atmega328.hex that I had lying around at any rate.  I hate the impression Arduino bootloaders came from optiboot these days.  In a makefile in optiboot I found some stuff showing 1024 words in a stanza on 328P.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Meet "westfw" - he is maintainer of Optiboot in the Arduino project ;-)

 

(so probably knows about this stuff!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah I was, had the old one lying around from duemilanove days still.  It is indeed only 512 sorry to falsely state that your bootloader was fat :)

 

Not sure what I though I was seeing in the optiboto Makefile either.  By the way what is the -Wl,--section-start=.version=0x7ffe doing?  I guess

storage for the bootloader version itself?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
They do this because the finest granularity in AVR flash is 16 bits but it confuses the living hell out of almost every one who encounters this the first time.

No; it only confuses those that think infinite-value toolchains are the best thing since sliced bread, and an AVR8 app is best served with a single stack.

 

Single stack example:

Image result for burger single-stack

Comparison; which would you rather have?

Image result for burger single-stack

 

Double stack example:

Image result for burger double-stack

 

[the bacon on top of the double-stack represents CodeVisionAVR's nearly transparent EEPROM and flash handling]

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bkerin wrote:

I tried using this address for the start of the .text section as discussed here:

 

  http://www.avrfreaks.net/sites/d...

...

I though maybe word vs byte address or something ...

In fairness to you the OP, and Cliff, the datasheet doesn't beat you over the head that the addresses are words -- but it does specify words for the partition size in the table you mentioned, and mentions using words when flash first explained.

 

In the link you gave for the AVR Bootloader FAQ, did you see:

LDFLAGS += -Wl,--section-start=.text=0x3000

That flag specifies the starting byte address of the bootloader. You can find this value in the

datasheet for your AVR. Make sure you use the byte address and not the word address (most

Atmel datasheets list word addresses for flash regions, but some list byte addresses). Rather than

hard-coding the bootloader's starting address, I'd suggest adding a constant to your makefile.

BOOTSTART = 0x3000

LDFLAGS += -Wl,--section-start=.text=$(BOOTSTART)

And again, as I hinted above, the mechanism for specifying addresses and such is toolchain-dependent so [IMO] it is a good thing to specify toolchain and version when making queries such as this.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Thu. Apr 20, 2017 - 08:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the bacon on top of the double-stack represents

heart failure?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah I know it was there, I even read it, just still managed to get confused that size was also in words, happened to be close to the example in the FAQ, plus interactions with the BOOTSZ fuses and multiplying by two and subtracting in hex.  All me.
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what is the -Wl,--section-start=.version=0x7ffe doing?  I guess storage for the bootloader version itself?

Yep; exactly correct.  (always the last word of memory.)

 

The Arduino Uno has been running Optiboot since it was first released, but a bunch of other boards (especially ones that pre-date Uno, like Nano and "pro mini")  are still running the older (2K) bootloader.