-
Notifications
You must be signed in to change notification settings - Fork 3
/
boot.c
38 lines (35 loc) · 945 Bytes
/
boot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <LUFA/Version.h>
#include <LUFA/Drivers/Misc/RingBuffer.h>
#include <LUFA/Drivers/USB/USB.h>
#include "bitm.h"
#include "usb.h"
#include "timer.h"
#include "boot.h"
uint32_t Boot_Key ATTR_NO_INIT;
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 ((MCUSR & (1 << WDRF)) && (Boot_Key == MAGIC_BOOT_KEY))
{
Boot_Key = 0;
((void (*)(void))BOOTLOADER_START_ADDRESS)();
}
}
void Jump_To_Bootloader(void)
{
// If USB is used, detach from the bus and reset it
USB_Disable();
// Disable all interrupts
cli();
// Wait two seconds for the USB detachment to register on the host
Delay_MS(2000);
// Set the bootloader key to the magic value and force a reset
Boot_Key = MAGIC_BOOT_KEY;
wdt_enable(WDTO_250MS);
for (;;);
}