1
\$\begingroup\$

I am designing a board with the ATmega328P and have decided to write my own bootloader rather than use Optiboot or Arduino, etc. I understand the logic of writing the boot code. Also that it resides in a specific area of the flash (max of about 1K). I am using the AVR Studio 6.

How do I target my code to precisely reside in that area? When I generate the hex file they always end up at 0x0000. How to locate them in 0x7000 or wherever the boot section is.

\$\endgroup\$
  • 1
    \$\begingroup\$ your first 1kb of code IS the boot sector. This code is run first on reset of the microcontroller, and you would put the code in here to detect a new program/handshake over UART/USB. The aim is to then facilitate the incoming data bytes as program code and load up the flash memory from 2KB+ up to the maximum of 32KB in the ATMEGA328P. Then the bootloader would tell the PC that all is done, and then it will be reset. On reset, no new program data is detected coming through, and you "Jump" the program counter to the start of the actual program stored in memory. \$\endgroup\$ – KyranF Apr 26 '14 at 13:03
  • 2
    \$\begingroup\$ @KyranF, on AVR microcontrollers, the boot loader resides near the top of the address space, not the bottom. \$\endgroup\$ – microtherion Apr 26 '14 at 18:15
  • \$\begingroup\$ @microtherion thank you. So the top 2KB is taken up, and 0-30kB is available for progmem? Just as the ATMEGA328P for example \$\endgroup\$ – KyranF Apr 27 '14 at 0:55
  • \$\begingroup\$ The exact numbers are configurable, but yes, that's the basic layout. \$\endgroup\$ – microtherion Apr 27 '14 at 7:57
1
\$\begingroup\$

First of all you will need to set the BOOTSZx fuses according to your bootloader size, the boot reset address is not fixed and changes with that. (page 282 of the datasheet , top). You will then need to tell the linker where to put your bootloader code, and this depends on what you are using to write code. In assembly what you need is the .org statement, with AVR studio you should mess around with the "memory settings" and put ".text=0x1234" under flash. Of course 0x1234 should be the right address as per page 282, i.e. 0x3800, 0x3c00, 0x3e00 or 0x3f00.

\$\endgroup\$
  • \$\begingroup\$ thank u sir. i understand ur point. i am using avr studio6 . i am confused where the "memory settings" is. i know where it is in avr 4. cld u kindly point me how to modify in avr 6 pls. \$\endgroup\$ – Board-Man Apr 26 '14 at 13:50
  • \$\begingroup\$ I've never used AVR studio so I do not know precisley where this option is located, but I believe it's somewhere in the project's settings. It's written on the manual of AVR studio... atmel.no/webdoc/atmelstudio/… search for "memory settings" \$\endgroup\$ – Vladimir Cravero Apr 26 '14 at 14:02
  • \$\begingroup\$ 0x3F00 in the datasheet is in unit of word (2 bytes) - so the (byte) address is 0x7E00... \$\endgroup\$ – Peter Mortensen Apr 17 '17 at 4:41

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.