-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootloader.c
executable file
·203 lines (156 loc) · 4.95 KB
/
bootloader.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "config.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/boot.h>
#include <util/delay.h>
#include "uart.h"
const uint8_t header_len = 2 + 4 + 2 + 2;
struct hex_record {
uint8_t data_length[2];
uint8_t address[4];
uint8_t record_type[2];
uint8_t data[(32 + 2) * 2];
};
static void boot_app(void) __attribute__((noreturn));
static uint8_t record_checksum_verify(struct hex_record* record);
static uint8_t hex_to_uint8(const unsigned char c[2]);
static uint16_t hex_to_uint16(const unsigned char c[4]);
static void program_page(uint16_t page, uint8_t* data);
ISR(TIMER1_COMPA_vect) {
boot_app();
}
int main(void)
{
// Use interrupts of bootloader
MCUCR = _BV(IVCE);
MCUCR = _BV(IVSEL);
uart_init();
uart_sendstr("Press p to program\n");
// 16-Bit timer in CTC mode, Prescaler 1024
TCCR1A = _BV(WGM12);
OCR1A = 65535;
TIMSK1 = _BV(OCIE1A);
TCCR1B = _BV(CS12) | _BV(CS10);
sei();
// Loop until p is read, or we get killed by the timer.
unsigned char c;
while (1) {
if (!uart_read(&c, 1)) {
continue;
}
if (c == 'p') {
break;
}
}
// Disable timer.
TCCR1B = 0;
uart_sendstr("Entering programming mode\n");
uint8_t page_buffer_start_set = 0;
uint16_t page_buffer_start = 0;
uint8_t page_buffer[SPM_PAGESIZE];
for (uint8_t i = 0; i < SPM_PAGESIZE; i++) {
page_buffer[i] = 0xFF;
}
while (1) {
unsigned char c;
while (!uart_read(&c, 1) || c != ':');
struct hex_record record;
uint8_t data_read = 0;
while (data_read < sizeof(struct hex_record) && (data_read < header_len || 2 * hex_to_uint8(record.data_length) + header_len != data_read)) {
data_read += uart_read(((unsigned char*) &record) + data_read, 1);
}
if (!record_checksum_verify(&record)) {
uart_sendstr("Checksum verification failed\n");
continue;
}
uint16_t recordAddr;
switch (record.record_type[1]) {
/* End of Transmission */
case '1':
if (page_buffer_start_set) {
program_page(page_buffer_start, page_buffer);
}
boot_app();
break;
/* Data */
case '0':
recordAddr = hex_to_uint16(record.address);
if (!page_buffer_start_set) {
page_buffer_start_set = 1;
page_buffer_start = recordAddr - recordAddr % SPM_PAGESIZE;
}
uint8_t dataLength = hex_to_uint8(record.data_length);
for (uint8_t i = 0; i < dataLength; i++) {
uint16_t byteAddr = recordAddr + i;
if (byteAddr >= page_buffer_start + sizeof(page_buffer) || byteAddr < page_buffer_start) {
program_page(page_buffer_start, page_buffer);
for (uint8_t i = 0; i < SPM_PAGESIZE; i++) {
page_buffer[i] = 0xFF;
}
page_buffer_start = byteAddr - byteAddr % sizeof(page_buffer);
}
uint8_t offset = byteAddr - page_buffer_start;
page_buffer[offset] = hex_to_uint8(record.data + i*2);
}
continue;
default:
continue;
}
}
while (1);
}
static void boot_app(void) {
// Disable timer.
TCCR1B = 0;
uart_sendstr("Booting app\n");
cli();
// Move interrupts back to address 0
MCUCR = _BV(IVCE);
MCUCR = 0;
goto *0;
}
static uint8_t record_checksum_verify(struct hex_record* record) {
uint8_t sum = 0;
unsigned char* p = (unsigned char*) record;
uint8_t record_length = header_len/2 + hex_to_uint8(record->data_length);
for (uint8_t i = 0; i < record_length; i++) {
sum += hex_to_uint8(p + i*2);
}
return sum == 0;
}
static uint8_t hex_to_uint4(unsigned char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'A' && c <= 'Z') {
return c - 'A' + 10;
} else if (c >= 'a' && c <= 'z') {
return c - 'a' + 10;
}
return 0;
}
static uint8_t hex_to_uint8(const unsigned char c[2]) {
return hex_to_uint4(c[0]) << 4 |
hex_to_uint4(c[1]);
}
static uint16_t hex_to_uint16(const unsigned char c[4]) {
return hex_to_uint8(c) << 8 |
hex_to_uint8(c + 2);
}
static void program_page(uint16_t page, uint8_t* data) {
uart_sendstr("Programming page ");
uart_sendhex16(page);
uart_putc('\n');
uart_xoff();
_delay_ms(1);
cli();
boot_page_erase(page);
boot_spm_busy_wait();
for (uint8_t i = 0; i < SPM_PAGESIZE; i += 2) {
boot_page_fill(page + i, data[i] | data[i + 1] << 8);
}
boot_page_write(page);
boot_spm_busy_wait();
boot_rww_enable();
sei();
uart_xon();
}