2010-09-17 23:59:09 +02:00
|
|
|
#include "bootloader.h"
|
|
|
|
#include "dev/watchdog.h"
|
|
|
|
#include <util/delay.h>
|
|
|
|
#include <avr/wdt.h>
|
2010-11-03 17:55:40 +01:00
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/pgmspace.h>
|
2010-09-17 23:59:09 +02:00
|
|
|
#include "dev/usb/usb_drv.h"
|
2012-05-17 17:48:41 +02:00
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/eeprom.h>
|
2010-09-17 23:59:09 +02:00
|
|
|
|
2014-08-30 01:58:18 +02:00
|
|
|
/* MCUSR is a deprecated name but older avr-libc versions may define it */
|
|
|
|
#if !defined (MCUCSR)
|
|
|
|
# if defined (MCUSR)
|
|
|
|
# warning *** MCUCSR not defined, using MCUSR instead ***
|
|
|
|
# define MCUCSR MCUSR
|
|
|
|
# endif
|
2011-08-03 17:18:55 +02:00
|
|
|
#endif
|
|
|
|
|
2012-05-17 17:48:41 +02:00
|
|
|
#ifndef EEPROM_MAGIC_BYTE_ADDR
|
|
|
|
#define EEPROM_MAGIC_BYTE_ADDR (uint8_t*)(E2END-3)
|
|
|
|
#endif
|
|
|
|
|
2010-09-17 23:59:09 +02:00
|
|
|
volatile uint32_t Boot_Key ATTR_NO_INIT;
|
|
|
|
|
2012-05-17 17:48:41 +02:00
|
|
|
extern void Bootloader_Jump_Check(void) ATTR_INIT_SECTION(3);
|
|
|
|
|
2010-09-17 23:59:09 +02:00
|
|
|
bool
|
2012-05-17 17:48:41 +02:00
|
|
|
bootloader_is_present(void)
|
|
|
|
{
|
|
|
|
#if defined(BOOTLOADER_START_ADDRESS)
|
|
|
|
return pgm_read_word_far(BOOTLOADER_START_ADDRESS) != 0xFFFF;
|
2011-08-03 17:18:55 +02:00
|
|
|
#else
|
2012-05-17 17:48:41 +02:00
|
|
|
return false;
|
2011-08-03 17:18:55 +02:00
|
|
|
#endif
|
2010-09-17 23:59:09 +02:00
|
|
|
}
|
2012-05-17 17:48:41 +02:00
|
|
|
|
2010-09-17 23:59:09 +02:00
|
|
|
void
|
|
|
|
Jump_To_Bootloader(void)
|
|
|
|
{
|
2012-05-17 17:48:41 +02:00
|
|
|
/* Disable all interrupts */
|
|
|
|
cli();
|
|
|
|
|
2010-09-17 23:59:09 +02:00
|
|
|
#ifdef UDCON
|
2012-05-17 17:48:41 +02:00
|
|
|
/* If USB is used, detach from the bus */
|
|
|
|
Usb_detach();
|
|
|
|
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
/* Wait two seconds for the USB detachment to register on the host */
|
|
|
|
for(i = 0; i < 200; i++) {
|
|
|
|
_delay_ms(10);
|
|
|
|
watchdog_periodic();
|
|
|
|
}
|
2010-09-17 23:59:09 +02:00
|
|
|
#endif
|
|
|
|
|
2012-05-17 17:48:41 +02:00
|
|
|
/* Set the bootloader key to the magic value and force a reset */
|
|
|
|
Boot_Key = MAGIC_BOOT_KEY;
|
2010-09-17 23:59:09 +02:00
|
|
|
|
2012-05-17 17:48:41 +02:00
|
|
|
eeprom_write_byte(EEPROM_MAGIC_BYTE_ADDR, 0xFF);
|
2010-09-17 23:59:09 +02:00
|
|
|
|
2012-05-17 17:48:41 +02:00
|
|
|
/* Enable interrupts */
|
|
|
|
sei();
|
2010-09-17 23:59:09 +02:00
|
|
|
|
2012-05-17 17:48:41 +02:00
|
|
|
watchdog_reboot();
|
2010-09-17 23:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Bootloader_Jump_Check(void)
|
|
|
|
{
|
2012-05-17 17:48:41 +02:00
|
|
|
/* If the reset source was the bootloader and the key is correct,
|
|
|
|
* clear it and jump to the bootloader
|
|
|
|
*/
|
2014-08-30 01:58:18 +02:00
|
|
|
if(MCUCSR & (1 << WDRF)) {
|
|
|
|
MCUCSR = 0;
|
2012-05-17 17:48:41 +02:00
|
|
|
if(Boot_Key == MAGIC_BOOT_KEY) {
|
|
|
|
Boot_Key = 0;
|
|
|
|
wdt_disable();
|
|
|
|
|
|
|
|
/* Disable all interrupts */
|
|
|
|
cli();
|
|
|
|
|
|
|
|
eeprom_write_byte(EEPROM_MAGIC_BYTE_ADDR, 0xFF);
|
|
|
|
|
|
|
|
/* Enable interrupts */
|
|
|
|
sei();
|
|
|
|
|
|
|
|
((void (*)(void))(BOOTLOADER_START_ADDRESS)) ();
|
|
|
|
} else {
|
|
|
|
/* The watchdog fired. Probably means we
|
|
|
|
* crashed. Wait two seconds before continuing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Boot_Key++;
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
for(i = 0; i < 200; i++) {
|
|
|
|
_delay_ms(10);
|
|
|
|
watchdog_periodic();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Boot_Key = MAGIC_BOOT_KEY - 4;
|
|
|
|
}
|
2010-09-17 23:59:09 +02:00
|
|
|
}
|