1

Whenever I need to upload the bootloader to my Arduino Mega (ATMEGA2560) I use an AVRISP MKII with the fuses/lock bits described in boards.txt file. Doing this I am able to burn arduino sketches by serial connection with my FTDI.

My question is: is it possible to burn arduino sketches (.hex files) with the AVRISP but keeping the bootloader? Whenever I try to do that (I use the same avrdude command I use to program the bootloader but changing the bootloader .hex file to the sketch .hex file) I am no longer able to program the processor with FTDI (and then I need to program the bootloader again).

I think (of course I can be wrong) this problem occurs due to the fuse and lock bits settings that cannot be the same as those used to program the bootloader.

This is the fuse settings I use (from boards.txt file under Arduino folder)

mega2560.name=Arduino Mega 2560 or Mega ADK

mega2560.upload.protocol=wiring
mega2560.upload.maximum_size=258048
mega2560.upload.speed=115200

mega2560.bootloader.low_fuses=0xFF
mega2560.bootloader.high_fuses=0xD8
mega2560.bootloader.extended_fuses=0xFD
mega2560.bootloader.path=stk500v2
mega2560.bootloader.file=stk500boot_v2_mega2560.hex
mega2560.bootloader.unlock_bits=0x3F
mega2560.bootloader.lock_bits=0x0F

mega2560.build.mcu=atmega2560
mega2560.build.f_cpu=16000000L
mega2560.build.core=arduino
mega2560.build.variant=mega

Can you help me?

Thanks in advance.

user2815706
  • 13
  • 1
  • 3

1 Answers1

1

Some controllers like the AT*X*MEGA series support individual flash sections that can be programmed/erased independently. However, with the ATMEGA2560 this is not possible.

One way would be to concatenate the .hex files as part of the build process. This can be achieved with some modifications to the makefile.

Something similar to this

all: $(TARGET).hex 
    srec_cat bootloader.hex -intel $(TARGET).hex -intel -o combined.hex -intel

Flashing the ´combined.hex´ would then include both, main application and boot loader. There is a related discussion on AVRFreaks.

Regarding the fuses, you just have to make sure that the boot loader (address) stays configured as the entry point.

EDIT: You can use this fuse bit calculator to verify fuse settings. Select your device and make sure "Boot Reset vector Enabled (default address=$0000); [BOOTRST=0]" is enabled with your current high fuse value. You currently use

mega2560.bootloader.high_fuses=0xD8

so that seems fine.

Rev
  • 5,827
  • 4
  • 27
  • 51
  • Thanks a lot for your reply. I already concatenated the bootloader and program into a single file. What I am trying to do right now is to figure out what fuses/lock bits should I use to "point" to bootloader. – user2815706 Nov 12 '13 at 15:10
  • @user2815706: I edit my answer to include some info regarding fuses. I hope that helps. – Rev Nov 12 '13 at 15:40
  • Thanks again for your answer. I was already using that fuse calculator and using 0xD8 as high fuse. I tried to program the processor with the merged .hex but I got no luck :( . "avrdude: stk500v2_ReceiveMessage(): timeout" is this message I keep receiving from avrdude. Maybe it has something to do with the lock bits and not with the fuses? – user2815706 Nov 12 '13 at 15:51
  • I am not familiar with avrdude so I can help you specifically with that. Lock bits are normally cleared when you perform an erase, but you could always check the lock bit settings with AVR studio or probably any other avr programming tool. However, I would start with a full erase and default fuse settings. You should be able to flash code into the boot loader section even with the boot loader fuse disabled. Its just a location in the flash. – Rev Nov 12 '13 at 17:06
  • I wasn't correctly generating the merged .hex file. The proper way to do it is: 1) edit the program .hex file with any text editor and remove the last line (something like :00000001FF), 2) open the bootloader .hex with the text editor, copy all the lines and paste them in the end of the program .hex file. Now I can upload the code without any issue :) . Thank you! – user2815706 Nov 19 '13 at 10:14
  • Glad to hear that you got it working. But merging the files can probably be automated by the makefile, which is safer and less time consuming than manual editing. I never did it, but as I suggested in my answer, it is obviously possible. – Rev Nov 19 '13 at 10:33