Problem modifying/compiling Optiboot

Hi.
I am struggling with the problem for about 4 days so far, hence why I came here for help.

I need to modify Optiboot to reset into bootloader on watchdog timer event, in other words, to remove the Adaboot no wait mod (even including power loss etc.)

My setup is:

  1. Target- Atmega328p on Arduino Uno board (which I will later use without UNO board)

  2. Programmer- Arduino Leonardo.

  3. Windows 7 x64 machine

  4. Arduino IDE 1.6.5 (tried on 1.6.11 too)

In very, very short-
(you can skip this part)
I have never been able to program any bootloader using Arduino as ISP.
Leonardo is programmed with ArduinoISP sketch, Uno connected through ICSP, but reset on Leonardo connected to pin 10. In IDE, board selected "Arduino UNO", programmer "Arduino as ISP". Uno resets fine during programming but then I get some sync errors.
When trying to upload the latest Optiboot bootloader, downloaded from GitHub - Optiboot/optiboot: Small and Fast Bootloader for Arduino and other Atmel AVR chips, I select "Board- Optiboot (...)" and get other several java errors.

(ok now the real roblem)
Anyway, I found other method for programming Optiboot bootloader with no issues which is Nick Gammon's method GitHub - nickgammon/arduino_sketches: Publicly-released sketches for the Arduino microprocessor (you're awesome Nick BTW)

So now in short, how I'm trying to modify this bootloader.

  1. Downloaded Optiboot from Bill Westfield (you're awesome too mate) GitHub - Optiboot/optiboot: Small and Fast Bootloader for Arduino and other Atmel AVR chips

  2. edited optiboot.c, basically just commented these lines:

//  if (ch & (_BV(WDRF) | _BV(BORF) | _BV(PORF)))
//      appStart(ch);
  1. Downloaded WinAVR-20100110-install.exe in order to compile in Windows

  2. Copied the whole content of WesfW's optiboot folder to "arduino-1.6.5-r5\hardware\tools\avr\bin" to resolve issues with paths

  3. executed omake atmega328

  4. During compilation I got some baudrate check errors (2.1%) but the file compiled ok (I have no idea whether this was the issue). I finally got files "optiboot_atmega328.lst" and "optiboot_atmega328.hex"

  5. Followed Nick's instruction to convert the hex file using MUSHclient with no issues.

  6. Created new file, edited to convert windows CR, LR to Unix LR only in order to get exactly the same size. Here's the output:

// /arduino-1.6.9/hardware/arduino/avr/bootloaders/optiboot/optiboot_atmega328.hex
// MD5sum = E4 4C E4 E7 5B A8 53 EE DA DF C6 0D C7 67 4D 7B
// Original file MD5 sum = 19b917aeec553ea1e47333cd9b1bbc5a
// This file from Aduino IDE 1.6.9

const byte atmega328_optiboot [] PROGMEM = {
0x1F, 0x92, 0xCD, 0xB7, 0xDE, 0xB7, 0x11, 0x24, 0x84, 0xB7, 0x14, 0xBE, 0x85, 0xE0, 0x80, 0x93,
0x81, 0x00, 0x82, 0xE0, 0x80, 0x93, 0xC0, 0x00, 0x88, 0xE1, 0x80, 0x93, 0xC1, 0x00, 0x86, 0xE0,
0x80, 0x93, 0xC2, 0x00, 0x80, 0xE1, 0x80, 0x93, 0xC4, 0x00, 0x8E, 0xE0, 0xB1, 0xD0, 0x25, 0x9A,
0x26, 0xE0, 0x80, 0xE3, 0x9C, 0xEF, 0x31, 0xE0, 0x90, 0x93, 0x85, 0x00, 0x80, 0x93, 0x84, 0x00,
0x36, 0xBB, 0xB0, 0x9B, 0xFE, 0xCF, 0x1D, 0x9A, 0xA8, 0x95, 0x21, 0x50, 0xA9, 0xF7, 0x81, 0x2C,
0x91, 0x2C, 0x93, 0xE0, 0xF9, 0x2E, 0xEE, 0x24, 0xE3, 0x94, 0x25, 0xE0, 0xD2, 0x2E, 0x31, 0xE1,
0xC3, 0x2E, 0x8A, 0xD0, 0x81, 0x34, 0x79, 0xF4, 0x87, 0xD0, 0x89, 0x83, 0x97, 0xD0, 0x89, 0x81,
0x82, 0x38, 0x11, 0xF4, 0x82, 0xE0, 0x05, 0xC0, 0x81, 0x38, 0x11, 0xF4, 0x86, 0xE0, 0x01, 0xC0,
0x83, 0xE0, 0x73, 0xD0, 0x6F, 0xC0, 0x82, 0x34, 0x11, 0xF4, 0x84, 0xE1, 0x03, 0xC0, 0x85, 0x34,
0x19, 0xF4, 0x85, 0xE0, 0x8B, 0xD0, 0x66, 0xC0, 0x85, 0x35, 0x41, 0xF4, 0x6D, 0xD0, 0x88, 0x2E,
0x6B, 0xD0, 0x91, 0x2C, 0x98, 0x2A,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F4, 0x88, 0xE0,
0x17, 0xD0, 0x1C, 0xD0, 0x80, 0xE1, 0x01, 0xD0, 0x7C, 0xCF, 0x90, 0x91, 0xC0, 0x00, 0x95, 0xFF,
0xFC, 0xCF, 0x80, 0x93, 0xC6, 0x00, 0x08, 0x95, 0x80, 0x91, 0xC0, 0x00, 0x87, 0xFF, 0xFC, 0xCF,
0x80, 0x91, 0xC0, 0x00, 0x84, 0xFD, 0x01, 0xC0, 0xA8, 0x95, 0x80, 0x91, 0xC6, 0x00, 0x08, 0x95,
0xE0, 0xE6, 0xF0, 0xE0, 0x98, 0xE1, 0x90, 0x83, 0x80, 0x83, 0x08, 0x95, 0xED, 0xDF, 0x80, 0x32,
0x19, 0xF0, 0x88, 0xE0, 0xF5, 0xDF, 0xFF, 0xCF, 0x84, 0xE1, 0xDF, 0xCF, 0xCF, 0x93, 0xC8, 0x2F,
0xE3, 0xDF, 0xC1, 0x50, 0xE9, 0xF7, 0xCF, 0x91, 0xF1, 0xCF, 0x28, 0x2E, 0x80, 0xE0, 0xE8, 0xDF,
0xE0, 0xE0, 0xFF, 0x27, 0x09, 0x94, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x06,
}; //  end of optiboot_atmega328

didn't bother about MD5's and all commented stuff.

  1. saved as bootloader_atmega328.h in Nick's Atmega_Board_Programmer replacing the original file.

Uploaded the Atmega_Board_Programmer sketch to Leonardo, and programmed bootloader to UNO. No problems what so ever. Even verified the bootloader to make sure it uploaded ok.

Now my UNO with modified bootloader keeps blinking internal led 3 times, 2-3 seconds break and 3 blinks again. I read about similar issue on forum but it was due to no sketch in UNO. So I uploaded the blink sketch with no problems but it still acts the same. 3 blinks, break, 3 blinks... Seems that blink sketch doesn't work or the UNO keeps resetting, I'm not sure.

I'm willing to even pay Nick for modifying bootloader_atmega328.h for me as it's a bit urgent, but I know I will need that modification for other uC's like Attiny85 later on.

Thanks in advance.

rosak:
Leonardo is programmed with ArduinoISP sketch, Uno connected through ICSP, but reset on Leonardo connected to pin 10. In IDE, board selected "Arduino UNO", programmer "Arduino as ISP". Uno resets fine during programming but then I get some sync errors.

That's a known issue when trying to use an ATmega32U4 board for Arduino as ISP. Forum member PeterVH has explained the issue and a workaround at ArduinoISP on the Leonardo | PeterVH. Changes in recent versions of the Arduino IDE have required some changes to that workaround. I have put these in a package installable via Boards Manager, the instructions are here: GitHub - per1234/ATmega32U4asISP: Allows ATmega32U4 based boards to be used as Arduino as ISP on Windows.

I realize this doesn't answer your question about modifying the bootloader but I though I'd mention it in case you decide you want to use your Leonardo for Arduino as ISP in the future.

pert:
I realize this doesn't answer your question about modifying the bootloader but I though I'd mention it in case you decide you want to use your Leonardo for Arduino as ISP in the future.

You're right pert, it doesn't answer my question but it may also be very helpful for me. I'll check it out when I got back from work. I obviously did some research on that matter and didn't find any solution. If it's a matter of 32u4 then I can use some other board, I have a lot of them. I also ordered Tiny ISP but I need that bootloader before the ISP arrives. As for changes in recent versions of the Arduino IDE- I had a lot of different problems in 1.69, 1.6.10, 1.6.11. After downloading 1.6.5 all problems are gone.

Thanks a lot pert.

Still waiting for help with compiling..

If you got a hex file, then the compile worked... right? What are your fuse settings, this should read them.

avrdude -v -p atmega328p -c stk500v1 -P /dev/ttyACM0 -b 19200 -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h

change /dev/ttyACM0 to your comm port

Verify them with the IDE files and/or a fuse calc.

Can't check it now as I'm still at work, but they are set in Nick's Atmega_Board_Programmer which I use to program the bootloader, that's the part of code:

// ATmega328P
  { { 0x1E, 0x95, 0x0F }, 
        0x7E00,               // start address
        atmega328_optiboot,   // loader image
        sizeof atmega328_optiboot,
        0xFF,         // fuse low byte: external clock, max start-up time
        0xDE,         // fuse high byte: SPI enable, boot into bootloader, 512 byte bootloader
        0x05,         // fuse extended byte: brown-out detection at 2.7V
        0x2F },       // lock bits: SPM is not allowed to write to the Boot Loader section.

So looks like they are exactly as you wrote.

I need to modify Optiboot to reset into bootloader on watchdog timer event, in other words, to remove the Adaboot no wait mod (even including power loss etc.)

  1. edited optiboot.c, basically just commented these lines:
//  if (ch & (_BV(WDRF) | _BV(BORF) | _BV(PORF)))

//      appStart(ch);

Ah; that's not going to work. The way that Optiboot works, using the WDT to start the application is fundamental. if you want a WDT reset to return to the bootloader, you're going to need to make more dramatic changes to the code, and I'm not sure it's even possible.

Now my UNO with modified bootloader keeps blinking internal led 3 times, 2-3 seconds break and 3 blinks again.

Yep; that's exactly what I'd expect. It allows the WDT to expire in order to start the app, but instead it runs the bootloader again. Over and over.

(so it looks like your programming process is fine. But you've written a bootloader that doesn't work. :slight_smile: )