Watchdog friendly bootloader for 2560

Attached (coming shortly, I'm testing a change to this to fix power-on condition) is a slight modification to the stock bootloader for Mega 2560s (source is at http://www.avr-developers.com/bootloaderdocs/index.html ) to make it compatible with use of the Watchdog feature in code.

The only addition to the stock source was to disable the watchdog exactly as per the datasheet - this step is key for atmega chips - then check the source of the reset, and if it was the watchdog interrupt, jump straight to the start of the loaded program (the autopilot).

 

You can flash this bootloader to the ArduPilot Mega board using the 6 pin ISP header which is on the red board, the top right area.

 

This bootloader has the !!! serial monitor section disabled (not #defined) and is therefore 2k in size, instead of 8k.

 

The steps to reflash it (assuming stk500v2 compatible flasher)


# erase the chip (necessary before unlocking can happen), then unlock the boot loader area
avrdude -e -c stk500v2 -p m2560 -P SERIAL_PORT -U lock:w:0x3f:m
# flash the hex file (whatever.hex)
avrdude -v -c stk500v2 -p m2560 -P SERIAL_PORT -U flash:w:whatever.hex
# re-lock the bootloader area of the chip
avrdude -c stk500v2 -p m2560 -P SERIAL_PORT -U lock:w:0x0f:m

Avrdude version 5.8 did not understand how to write high memory, so I used avrdude version 5.11. Avrdude version in the stock IDE is version 5.4

The lfuse/hfuse/efuse values should not change during any of this.
With this modified bootloader, within APM code you are free to set the watchdog prior to the main loop, call wdt_reset() regularly. If the code fails to call wdt_reset() in time, the board will reboot instead of (perhaps) being locked up and useless.
The code change to the bootloader at the start of main() is

void (*app_start)(void) = 0x0000;

uint8_t ch;
ch = MCUSR;

__asm__ __volatile__ ("cli");
__asm__ __volatile__ ("wdr");
MCUSR = 0;
WDTCSR |= _BV(WDCE) | _BV(WDE);
WDTCSR = 0;
__asm__ __volatile__ ("sei");
// check if WDT generated the reset, if so, go straight to app
if (ch & _BV(WDRF))
   app_start();

 

I tested and it behaves correctly. A watchdog timer can be set and if not reset, the board reboots as though the reset button was pressed.

stk500boot_v2_mega2560.hex

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Activity

DIY Robocars via Twitter
RT @a1k0n: Did I get rid of hand-tuned parameters? Yes. Am I still hand-tuning more parameters? Also yes. I have a few knobs to address the…
Monday
DIY Robocars via Twitter
RT @a1k0n: I'm not going to spoil it, but (after charging the battery) this works way better than it has any right to. The car is now faste…
Monday
DIY Robocars via Twitter
RT @a1k0n: Decided to just see what happens if I run the sim-trained neural net on the car, with some safety rails around max throttle slew…
Monday
DIY Robocars via Twitter
Saturday
DIY Robocars via Twitter
RT @SmallpixelCar: @a1k0n @diyrobocars I learned from this. This is my speed profile. Looks like I am too conservative on the right side of…
Saturday
DIY Robocars via Twitter
RT @a1k0n: @SmallpixelCar @diyrobocars Dot color is speed; brighter is faster. Yeah, it has less room to explore in the tighter part, and t…
Saturday
DIY Robocars via Twitter
RT @a1k0n: I'm gonna try to do proper offline reinforcement learning for @diyrobocars and throw away all my manual parameter tuning for the…
Sep 23
DIY Robocars via Twitter
RT @circuitlaunch: DIY Robocars & Brazilian BBQ - Sat 10/1. Our track combines hairpin curves with an intersection for max danger. Take tha…
Sep 22
DIY Robocars via Twitter
RT @SmallpixelCar: Had an great test today on @RAMS_RC_Club track. However the car starts to drift at 40mph. Some experts recommended to ch…
Sep 11
DIY Robocars via Twitter
RT @gclue_akira: 世界最速 チームtamiyaのaiカー https://t.co/1Qq2zOeftG
Sep 10
DIY Robocars via Twitter
RT @DanielChiaJH: Always a good time working on my @diyrobocars car at @circuitlaunch. Still got some work to do if I’m to beat @a1k0n howe…
Sep 10
DIY Robocars via Twitter
RT @SmallpixelCar: My new speed profile for @RAMS_RC_Club track https://t.co/RtLb7TcgIJ
Sep 10
DIY Robocars via Twitter
RT @SmallpixelCar: Practiced at @RAMS_RC_Club today with my new @ARRMARC car https://t.co/AEu2hCx89T
Aug 28
DIY Robocars via Twitter
Aug 24
DIY Robocars via Twitter
RT @gclue_akira: 柏の葉で走行させてるjetracerの中身 #instantNeRF #jetracer https://t.co/giVvuE4hP7
Jul 4
DIY Robocars via Twitter
Cool web-based self-driving simulator. Click save when the AI does the right thing https://github.com/pncsoares/self-driving-car
Jul 4
More…