-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.c
54 lines (46 loc) · 1.23 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <avr/boot.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
/* Bootloader start address in bytes */
#define BOOTLOADER_START (FLASHEND - bootloader_size() + 1)
/* Reset all modified ports and drawdown resistors */
void reset_ports(void) {
DDRC = 0;
DDRD = 0;
PORTC = 0;
PORTD = 0;
}
/* Release USB devices */
void reset_usb(void) {
UDCON = 1;
USBCON = (1 << FRZCLK);
UCSR1B = 0;
_delay_ms(5);
}
/* Reset state to prepare for bootloader jump */
void reset_bootloader(void) {
cli();
reset_usb();
reset_ports();
}
/* Boot section size in bytes */
static inline uint16_t bootloader_size(void) {
uint8_t fuse_bits = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
switch ((fuse_bits >> 1) & 3) {
case 0: return 8192;
case 1: return 4096;
case 2: return 2048;
case 3: return 1024;
}
return 8192;
}
/* Jump to the start of the bootloader section */
void bootloader_jump(void) {
((void (*)(void))( (uint16_t)(BOOTLOADER_START / 2) ))();
}
int main(void) {
reset_bootloader();
bootloader_jump();
}