bootloader.c

00001 #include "bootloader.h"
00002 #include "dev/watchdog.h"
00003 #include <util/delay.h>
00004 #include <avr/wdt.h>
00005 #include <avr/interrupt.h>
00006 #include <avr/pgmspace.h>
00007 #include "dev/usb/usb_drv.h"
00008 
00009 volatile uint32_t Boot_Key ATTR_NO_INIT;
00010 
00011 bool
00012 bootloader_is_present(void) {
00013         return pgm_read_word_far(BOOTLOADER_START_ADDRESS)!=0xFFFF;
00014 }
00015 
00016 void
00017 Jump_To_Bootloader(void)
00018 {
00019         uint8_t i;
00020         
00021 #ifdef UDCON
00022         // If USB is used, detach from the bus
00023         Usb_detach();
00024 #endif
00025 
00026         // Disable all interrupts
00027         cli();
00028 
00029         // Set the bootloader key to the magic value and force a reset
00030         Boot_Key = MAGIC_BOOT_KEY;
00031 
00032         // Wait two seconds for the USB detachment to register on the host
00033         for (i = 0; i < 128; i++)
00034                 _delay_ms(16);
00035 
00036         // Set the bootloader key to the magic value and force a reset
00037         Boot_Key = MAGIC_BOOT_KEY;
00038         
00039         watchdog_reboot();
00040 }
00041 
00042 extern void Bootloader_Jump_Check(void) ATTR_INIT_SECTION(3);
00043 
00044 void
00045 Bootloader_Jump_Check(void)
00046 {
00047         // If the reset source was the bootloader and the key is correct, clear it and jump to the bootloader
00048         if(MCUSR & (1<<WDRF)) {
00049                 MCUSR = 0;
00050                 if(Boot_Key == MAGIC_BOOT_KEY) {
00051                         Boot_Key = 0;
00052                         wdt_disable();
00053                         
00054                         ((void (*)(void))BOOTLOADER_START_ADDRESS)();
00055                 } else {
00056                         Boot_Key++;
00057                 }
00058         } else {
00059                 Boot_Key = MAGIC_BOOT_KEY-4;
00060         }
00061 }

Generated on Mon Apr 11 14:23:35 2011 for Contiki 2.5 by  doxygen 1.6.1