Merge pull request #153 from darconeous/pull-requests/cpu-avr-bootloader
avr/bootloader.c: Now compatible with bootloaders which always run first
This commit is contained in:
commit
a445c4020d
@ -5,69 +5,99 @@
|
|||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/pgmspace.h>
|
#include <avr/pgmspace.h>
|
||||||
#include "dev/usb/usb_drv.h"
|
#include "dev/usb/usb_drv.h"
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/eeprom.h>
|
||||||
|
|
||||||
//Not all AVR toolchains alias MCUSR to the older MSUSCR name
|
/* Not all AVR toolchains alias MCUSR to the older MSUSCR name */
|
||||||
//#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) || defined (__AVR_ATmega16__)
|
|
||||||
#if !defined (MCUSR) && defined (MCUCSR)
|
#if !defined (MCUSR) && defined (MCUCSR)
|
||||||
#warning *** MCUSR not defined, using MCUCSR instead ***
|
#warning *** MCUSR not defined, using MCUCSR instead ***
|
||||||
#define MCUSR MCUCSR
|
#define MCUSR MCUCSR
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef EEPROM_MAGIC_BYTE_ADDR
|
||||||
|
#define EEPROM_MAGIC_BYTE_ADDR (uint8_t*)(E2END-3)
|
||||||
|
#endif
|
||||||
|
|
||||||
volatile uint32_t Boot_Key ATTR_NO_INIT;
|
volatile uint32_t Boot_Key ATTR_NO_INIT;
|
||||||
|
|
||||||
|
extern void Bootloader_Jump_Check(void) ATTR_INIT_SECTION(3);
|
||||||
|
|
||||||
bool
|
bool
|
||||||
bootloader_is_present(void) {
|
bootloader_is_present(void)
|
||||||
#if defined(RAMPZ)
|
{
|
||||||
return pgm_read_word_far(BOOTLOADER_START_ADDRESS)!=0xFFFF;
|
#if defined(BOOTLOADER_START_ADDRESS)
|
||||||
|
return pgm_read_word_far(BOOTLOADER_START_ADDRESS) != 0xFFFF;
|
||||||
#else
|
#else
|
||||||
/* Probably can just return false when < 64K flash */
|
return false;
|
||||||
// return pgm_read_word_near(BOOTLOADER_START_ADDRESS)!=0xFFFF;
|
|
||||||
return false;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Jump_To_Bootloader(void)
|
Jump_To_Bootloader(void)
|
||||||
{
|
{
|
||||||
uint8_t i;
|
/* Disable all interrupts */
|
||||||
|
cli();
|
||||||
|
|
||||||
#ifdef UDCON
|
#ifdef UDCON
|
||||||
// If USB is used, detach from the bus
|
/* If USB is used, detach from the bus */
|
||||||
Usb_detach();
|
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();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Disable all interrupts
|
/* Set the bootloader key to the magic value and force a reset */
|
||||||
cli();
|
Boot_Key = MAGIC_BOOT_KEY;
|
||||||
|
|
||||||
// Set the bootloader key to the magic value and force a reset
|
eeprom_write_byte(EEPROM_MAGIC_BYTE_ADDR, 0xFF);
|
||||||
Boot_Key = MAGIC_BOOT_KEY;
|
|
||||||
|
|
||||||
// Wait two seconds for the USB detachment to register on the host
|
/* Enable interrupts */
|
||||||
for (i = 0; i < 128; i++)
|
sei();
|
||||||
_delay_ms(16);
|
|
||||||
|
|
||||||
// Set the bootloader key to the magic value and force a reset
|
watchdog_reboot();
|
||||||
Boot_Key = MAGIC_BOOT_KEY;
|
|
||||||
|
|
||||||
watchdog_reboot();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void Bootloader_Jump_Check(void) ATTR_INIT_SECTION(3);
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Bootloader_Jump_Check(void)
|
Bootloader_Jump_Check(void)
|
||||||
{
|
{
|
||||||
// If the reset source was the bootloader and the key is correct, clear it and jump to the bootloader
|
/* If the reset source was the bootloader and the key is correct,
|
||||||
if(MCUSR & (1<<WDRF)) {
|
* clear it and jump to the bootloader
|
||||||
MCUSR = 0;
|
*/
|
||||||
if(Boot_Key == MAGIC_BOOT_KEY) {
|
if(MCUSR & (1 << WDRF)) {
|
||||||
Boot_Key = 0;
|
MCUSR = 0;
|
||||||
wdt_disable();
|
if(Boot_Key == MAGIC_BOOT_KEY) {
|
||||||
|
Boot_Key = 0;
|
||||||
((void (*)(void))BOOTLOADER_START_ADDRESS)();
|
wdt_disable();
|
||||||
} else {
|
|
||||||
Boot_Key++;
|
/* Disable all interrupts */
|
||||||
}
|
cli();
|
||||||
} else {
|
|
||||||
Boot_Key = MAGIC_BOOT_KEY-4;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ CONTIKI_TARGET_DIRS = . apps net loader dev/usb dev/serial
|
|||||||
CONTIKI_CORE=contiki-raven-main
|
CONTIKI_CORE=contiki-raven-main
|
||||||
CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o
|
CONTIKI_TARGET_MAIN = ${CONTIKI_CORE}.o
|
||||||
|
|
||||||
|
BOOTLOADER_START = 0x1F000
|
||||||
|
|
||||||
#USB Ethernet Interface + USB Serial Port TX Only
|
#USB Ethernet Interface + USB Serial Port TX Only
|
||||||
USB = uart_usb_lib.c \
|
USB = uart_usb_lib.c \
|
||||||
cdc_task.c \
|
cdc_task.c \
|
||||||
|
Loading…
Reference in New Issue
Block a user