-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
472 lines (388 loc) · 9.8 KB
/
main.c
File metadata and controls
472 lines (388 loc) · 9.8 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/***************************************************************************
* Artekit AK-CMSIS-DAP JTAG bootloader
* https://www.artekit.eu/products/debug/ak-cmsis-dap
*
* Written by Ruben Meleca
* Copyright (c) 2018 Artekit Labs
* https://www.artekit.eu
### main.c
# AK-CMSIS-DAP bootloader is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# AK-CMSIS-DAP bootloader is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with AK-CMSIS-DAP bootloader.
# If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#include <string.h>
#include <stdio.h>
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_usart.h"
#include "em_gpio.h"
#include "em_emu.h"
#include "em_msc.h"
#define FW_BASE_ADDR 0x3000
#define FW_FLASH_TOP 0x10000
#define FW_MAX_LEN (FW_FLASH_TOP - FW_BASE_ADDR)
#define FW_PAGE_SIZE 0x400
#define FW_MAX_PAGES (FW_MAX_LEN / FW_PAGE_SIZE)
#define FW_CRC_ADDR 0x0FE00000
#define FW_VECTORS_SIZE 0x94
#define FW_VER_ADDR (FW_BASE_ADDR + FW_VECTORS_SIZE)
#define XMODEM_SOH 0x01
#define XMODEM_EOT 0x04
#define XMODEM_ACK 0x06
#define XMODEM_NAK 0x15
#define XMODEM_ETB 0x17
#define XMODEM_CAN 0x18
#define XMODEM_C 0x43
#define XMODEM_TIMEOUT 10000
#define XMODEM_STATE_START 0
#define XMODEM_STATE_C 1
#define XMODEM_STATE_HEADER 2
#define XMODEM_STATE_STORE 3
#define XMODEM_STATE_END 4
#define BOOTLOADER_VERSION "1.0"
typedef struct
{
uint16_t crc;
uint32_t fw_len;
} FW_CRC;
typedef struct xmodem_packet
{
uint8_t dummy; /* for padding */
uint8_t header;
uint8_t number;
uint8_t number_chksum;
uint8_t data[128];
uint8_t crc_low;
uint8_t crc_high;
} XMODEM_PACKET;
typedef void (*jump_func)(void);
static volatile uint32_t tick_count = 0;
static FW_CRC* fw_crc = (FW_CRC*) FW_CRC_ADDR;
static XMODEM_PACKET rx_packet;
static uint8_t next_seq;
static uint32_t curr_addr = FW_BASE_ADDR;
static char* fw_ver = (char*) FW_VER_ADDR;
static void delay_ms(uint32_t ms)
{
uint32_t ticks = tick_count;
while (tick_count - ticks < ms);
}
void SysTick_Handler(void)
{
tick_count++;
}
static void uart_init(void)
{
USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;
CMU_ClockEnable(cmuClock_USART1, true);
USART_InitAsync(USART1, &init);
USART1->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | USART_ROUTE_LOCATION_LOC0;
}
static void uart_deinit(void)
{
while (!(USART1->STATUS & USART_STATUS_TXC));
USART_Reset(USART1);
CMU_ClockEnable(cmuClock_USART1, false);
}
static void gpio_init(void)
{
CMU_ClockEnable(cmuClock_GPIO, true);
CMU_ClockEnable(cmuClock_HFPER, true);
/* Boot button, PA0 input with pull-up */
GPIO_PinModeSet(gpioPortA, 0, gpioModeInputPull, 1);
/* LED on PB14 */
GPIO_PinModeSet(gpioPortB, 14, gpioModePushPull, 0);
GPIO_PinOutSet(gpioPortB, 14);
/* UART pins */
GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1);
GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0);
}
static void gpio_deinit(void)
{
/* LED on PB14 */
GPIO_PinModeSet(gpioPortB, 14, gpioModeInput, 0);
/* Boot button on PA0 */
GPIO_PinModeSet(gpioPortA, 0, gpioModeInput, 0);
/* UART pins */
GPIO_PinModeSet(gpioPortC, 0, gpioModeDisabled, 0);
GPIO_PinModeSet(gpioPortC, 1, gpioModeDisabled, 0);
}
static void flash_write(uint8_t* data, uint32_t len)
{
/* len must be aligned to 4 bytes */
if (len & 0x03)
return;
/* Check if the limit was reached */
if (curr_addr + len > FW_FLASH_TOP)
return;
MSC_WriteWordFast((uint32_t*) curr_addr, data, len);
curr_addr += len;
data += len;
}
static void flash_erase_page(uint32_t page_addr)
{
MSC->ADDRB = page_addr;
MSC->WRITECMD = MSC_WRITECMD_LADDRIM;
MSC->WRITECMD = MSC_WRITECMD_ERASEPAGE;
while (MSC->STATUS & MSC_STATUS_BUSY);
}
static void flash_erase_all(void)
{
uint32_t page = FW_BASE_ADDR;
MSC->WRITECTRL |= MSC_WRITECTRL_WREN;
while (page < FW_FLASH_TOP)
{
flash_erase_page(page);
page += FW_PAGE_SIZE;
}
flash_erase_page(FW_CRC_ADDR);
MSC->WRITECTRL &= ~MSC_WRITECTRL_WREN;
}
static void flash_store_crc(FW_CRC* crc)
{
MSC_WriteWordFast((uint32_t*) FW_CRC_ADDR, crc, sizeof(FW_CRC));
}
static uint16_t crc16(uint8_t* data, uint32_t len)
{
uint8_t rotate;
uint32_t crc = 0;
while (len)
{
len--;
crc = crc ^ (*data++ << 8);
rotate = 8;
while (rotate)
{
rotate--;
crc = crc << 1;
if (crc & 0x10000)
{
crc = (crc ^ 0x1021) & 0xFFFF;
}
}
}
return (uint16_t) crc;
}
static uint16_t get_fw_crc(uint32_t addr, uint32_t len)
{
uint8_t* ptr = (uint8_t*) addr;
return crc16(ptr, len);
}
static uint8_t xmodem_verify(XMODEM_PACKET* packet)
{
uint16_t crc;
uint16_t calc_crc;
/* Check packet number */
if (packet->number + packet->number_chksum != 255)
return 0;
if (packet->number != next_seq)
return 0;
/* Check CRC */
crc = (packet->crc_low << 8) | packet->crc_high;
calc_crc = crc16(packet->data, sizeof(packet->data));
return (crc == calc_crc);
}
static void print_char(const char c)
{
USART_Tx(USART1, c);
}
static void print_string(const char* string)
{
while (*string)
print_char(*string++);
}
static void print_bootloader_version(void)
{
print_string("\r\nArtekit Labs - Bootloader for AK-CMSIS-DAP - version ");
print_string(BOOTLOADER_VERSION);
print_string("\r\n");
}
static void print_fw_version(void)
{
print_string("AK-CMSIS-DAP firmware version ");
print_string(fw_ver);
print_string("\r\n");
}
static uint8_t xmodem_download()
{
uint32_t timeout = 0;
FW_CRC new_crc;
uint8_t state = 0;
uint8_t* ptr = (uint8_t*) &rx_packet;
uint8_t ret = 0;
uint8_t first_time = 1;
/* Read any spurious UART data */
while (USART1->STATUS & USART_STATUS_RXDATAV)
USART1->RXDATA;
print_string("Press any key to start firmware download...\r\n");
USART_Rx(USART1);
while (1)
{
switch (state)
{
case XMODEM_STATE_START:
/* Read any spurious UART data */
while (USART1->STATUS & USART_STATUS_RXDATAV)
USART1->RXDATA;
next_seq = 1;
curr_addr = FW_BASE_ADDR;
new_crc.fw_len = 0;
timeout = tick_count;
state = XMODEM_STATE_C;
GPIO_PinOutSet(gpioPortB, 14);
/* no break */
/* Send XMODEM_C */
case XMODEM_STATE_C:
if (USART1->STATUS & USART_STATUS_RXDATAV)
{
state = XMODEM_STATE_HEADER;
break;
}
if (tick_count - timeout > 1000)
{
timeout = tick_count;
USART1->TXDATA = XMODEM_C;
GPIO_PinOutToggle(gpioPortB, 14);
}
break;
/* Wait for header */
case XMODEM_STATE_HEADER:
if (USART1->STATUS & USART_STATUS_RXDATAV)
{
rx_packet.header = USART1->RXDATA;
if (rx_packet.header == XMODEM_SOH)
{
ptr = (uint8_t*) &rx_packet.header;
ptr++;
timeout = tick_count;
state = XMODEM_STATE_STORE;
} else if (rx_packet.header == XMODEM_EOT)
{
USART_Tx(USART1, XMODEM_ACK);
ret = 1;
new_crc.crc = get_fw_crc(FW_BASE_ADDR, new_crc.fw_len);
flash_store_crc(&new_crc);
state = XMODEM_STATE_END;
} else {
ret = 0;
state = XMODEM_STATE_END;
}
}
break;
/* Read packet */
case XMODEM_STATE_STORE:
/* Check for completion */
if (ptr == (uint8_t*) &rx_packet + sizeof(rx_packet))
{
/* Verify and write */
GPIO_PinOutToggle(gpioPortB, 14);
if (xmodem_verify(&rx_packet))
{
if (first_time)
{
MSC_Init();
flash_erase_all();
first_time = 0;
}
/* Write */
flash_write(rx_packet.data, sizeof(rx_packet.data));
next_seq++;
new_crc.fw_len += sizeof(rx_packet.data);
/* Send ACK */
USART_Tx(USART1, XMODEM_ACK);
} else {
/* Send NACK */
USART_Tx(USART1, XMODEM_NAK);
}
state = XMODEM_STATE_HEADER;
} else {
/* Store in buffer */
if (USART1->STATUS & USART_STATUS_RXDATAV)
{
*ptr = USART1->RXDATA;
ptr++;
timeout = tick_count;
}
if (tick_count - timeout >= XMODEM_TIMEOUT)
/* Start all over */
state = XMODEM_STATE_START;
}
break;
case XMODEM_STATE_END:
GPIO_PinOutSet(gpioPortB, 14);
MSC_Deinit();
return ret;
}
}
}
static uint8_t check_fw_crc(void)
{
uint16_t crc;
if (fw_crc->fw_len > FW_MAX_LEN)
return 0;
crc = get_fw_crc(FW_BASE_ADDR, fw_crc->fw_len);
return (crc == fw_crc->crc);
}
static uint8_t check_boot_button(void)
{
return (GPIO_PinInGet(gpioPortA, 0) ? 0 : 1);
}
static void jump_to_app()
{
uint32_t* sp = (uint32_t*) FW_BASE_ADDR;
uint32_t* pc = sp + 1;
jump_func pfunc = (jump_func) *pc;
__set_MSP(*sp);
__set_PSP(*sp);
SCB->VTOR = (uint32_t)FW_BASE_ADDR;
(pfunc)();
}
int main(void)
{
uint32_t freq;
uint8_t crc_good;
uint8_t button_pressed;
/* Chip errata */
CHIP_Init();
freq = SystemCoreClockGet();
SysTick_Config((freq / 1000) - 1);
gpio_init();
uart_init();
delay_ms(100);
print_bootloader_version();
while (1)
{
/* Check firmware CRC */
crc_good = check_fw_crc();
if (crc_good)
print_fw_version();
else
print_string("Invalid firmware CRC\r\n");
button_pressed = check_boot_button();
if (button_pressed)
print_string("Firmware download triggered by BOOT button\r\n");
if (button_pressed || !crc_good)
{
if (xmodem_download(0))
print_string("\r\nFirmware download succeeded\r\n");
else
print_string("\r\nFirmware download failed\r\n");
} else break;
}
print_string("Jumping to main program...\r\n");
uart_deinit();
gpio_deinit();
delay_ms(100);
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk;
jump_to_app();
while(1);
}