Skip to content

mega bootloader watchdog sporadic hang #263

Open
@hydrocontrol

Description

@hydrocontrol

If the restart was caused by the watchdog, the bootloader currently exits with

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

In any other case, the bootloader exits with

// exit the bootloader in an orderly fashion

asm volatile ("nop");           // No operation. Do nothing for one clock cycle. Probably not necessary, but doesn't harm either. Copy and pasted from code snippet.
UART_STATUS_REG &=  0xfd;   //for the mega2560, this is the register UCSR0A. We clear bit 1 to zero here. 0xfd = 0b1111 1101 . Bit 1 - U2Xn: Double the USART Transmission Speed This bit only has effect for the asynchronous operation. Write this bit to zero when using synchronous operation.
boot_rww_enable();              // from <avr/boot.h>: Bootloader Support Utilities. Enable the Read-While-Write memory section. enable application section

// the next instruction is more complicated than necessary for the mega2560. asm jmp 0 would do the job as well, but the jmp command is not available on all processors

/* Indirect jump to the address pointed to by the Z (16 bits) pointer register in the register file.
The Z pointer register is 16 bits wide and allows jump within the lowest 64K words (128K bytes) section of program memory. This instruction is not available in all devices. Refer to the device specific instruction set summary.
*/

// Z pointer is at registers 30 and 31

asm volatile //the volatile keyword tells the compiler to disable certain optimizations. These optimizations would attempt to make the code smaller, but break it in the process.
        (
            "clr    r30     \n\t" // clear register 30. The new line and tab characters are only there so the assembler file will look nice and human readable.
            "clr    r31     \n\t"
            "ijmp           \n\t" // indirect jump
        );


/*
* Never return (stay in an endless loop) to stop GCC to generate exit return code
* Actually we will never reach this point, because we jumped away earlier, but the compiler doesn't
* understand this.
*/
for(;;); // endless loop

If the code for the orderly exit is used also for the watchdog, then the sporadic hang after watchdog reset disappears.
See also http://forum.arduino.cc/index.php?topic=378288.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions