forked from PX4/PX4-Bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_f4.c
518 lines (433 loc) · 13 KB
/
main_f4.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* STM32F4 board support for the bootloader.
*
*/
#include <stdlib.h>
#include <libopencm3/stm32/f4/rcc.h>
#include <libopencm3/stm32/f4/gpio.h>
#include <libopencm3/stm32/f4/flash.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/systick.h>
#include <libopencm3/stm32/pwr.h>
#include "bl.h"
/* flash parameters that we should not really know */
static struct {
uint32_t erase_code;
uint32_t size;
} flash_sectors[] = {
/* flash sector zero reserved for bootloader */
{ (0x01 << 3), 16 * 1024},
{ (0x02 << 3), 16 * 1024},
{ (0x03 << 3), 16 * 1024},
{ (0x04 << 3), 64 * 1024},
{ (0x05 << 3), 128 * 1024},
{ (0x06 << 3), 128 * 1024},
{ (0x07 << 3), 128 * 1024},
{ (0x08 << 3), 128 * 1024},
{ (0x09 << 3), 128 * 1024},
{ (0x0a << 3), 128 * 1024},
{ (0x0b << 3), 128 * 1024},
/* flash sectors only in 2MiB devices */
{ (0x10 << 3), 16 * 1024},
{ (0x11 << 3), 16 * 1024},
{ (0x12 << 3), 16 * 1024},
{ (0x13 << 3), 16 * 1024},
{ (0x14 << 3), 64 * 1024},
{ (0x15 << 3), 128 * 1024},
{ (0x16 << 3), 128 * 1024},
{ (0x17 << 3), 128 * 1024},
{ (0x18 << 3), 128 * 1024},
{ (0x19 << 3), 128 * 1024},
{ (0x1a << 3), 128 * 1024},
{ (0x1b << 3), 128 * 1024},
};
#define BOOTLOADER_RESERVATION_SIZE (16 * 1024)
#define OTP_BASE 0x1fff7800
#define OTP_SIZE 512
#define UDID_START 0x1FFF7A10
#ifdef BOARD_F4BY
# define BOARD_TYPE 20
# define BOARD_FLASH_SECTORS 11
# define BOARD_FLASH_SIZE (1024 * 1024)
# define OSC_FREQ 8
# define BOARD_PIN_LED_ACTIVITY GPIO3
# define BOARD_PIN_LED_BOOTLOADER GPIO2
# define BOARD_PORT_LEDS GPIOE
# define BOARD_CLOCK_LEDS RCC_AHB1ENR_IOPEEN
# define BOARD_LED_ON gpio_clear
# define BOARD_LED_OFF gpio_set
# define BOARD_FORCE_BL_PIN GPIO10
# define BOARD_FORCE_BL_PORT GPIOA
# define BOARD_FORCE_BL_CLOCK_REGISTER RCC_AHB1ENR
# define BOARD_FORCE_BL_CLOCK_BIT RCC_AHB1ENR_IOPAEN
# define BOARD_FORCE_BL_PULL GPIO_PUPD_PULLUP
# define BOARD_FORCE_BL_STATE 0
#endif
#ifdef BOARD_FMU
# define BOARD_TYPE 5
# define BOARD_FLASH_SECTORS 11
# define BOARD_FLASH_SIZE (1024 * 1024)
# define OSC_FREQ 24
# define BOARD_PIN_LED_ACTIVITY GPIO15
# define BOARD_PIN_LED_BOOTLOADER GPIO14
# define BOARD_PORT_LEDS GPIOB
# define BOARD_CLOCK_LEDS RCC_AHB1ENR_IOPBEN
# define BOARD_LED_ON gpio_clear
# define BOARD_LED_OFF gpio_set
# define BOARD_FORCE_BL_PIN GPIO10
# define BOARD_FORCE_BL_PORT GPIOA
# define BOARD_FORCE_BL_CLOCK_REGISTER RCC_AHB1ENR
# define BOARD_FORCE_BL_CLOCK_BIT RCC_AHB1ENR_IOPAEN
# define BOARD_FORCE_BL_PULL GPIO_PUPD_PULLUP
# define BOARD_FORCE_BL_STATE 0
#endif
#ifdef BOARD_FLOW
# define BOARD_TYPE 6
# define BOARD_FLASH_SECTORS 11
# define BOARD_FLASH_SIZE (1024 * 1024)
# define OSC_FREQ 24
# define BOARD_PIN_LED_ACTIVITY GPIO3
# define BOARD_PIN_LED_BOOTLOADER GPIO2
# define BOARD_PORT_LEDS GPIOE
# define BOARD_CLOCK_LEDS RCC_AHB1ENR_IOPEEN
# define BOARD_LED_ON gpio_clear
# define BOARD_LED_OFF gpio_set
#endif
#ifdef BOARD_DISCOVERY
# define BOARD_TYPE 99
# define BOARD_FLASH_SECTORS 11
# define BOARD_FLASH_SIZE (1024 * 1024)
# define OSC_FREQ 8
# define BOARD_PIN_LED_ACTIVITY GPIO12
# define BOARD_PIN_LED_BOOTLOADER GPIO13
# define BOARD_PORT_LEDS GPIOD
# define BOARD_CLOCK_LEDS RCC_AHB1ENR_IOPDEN
# define BOARD_LED_ON gpio_set
# define BOARD_LED_OFF gpio_clear
#endif
#ifdef BOARD_FMUV2
# define BOARD_TYPE 9
# define _FLASH_KBYTES (*(uint16_t *)0x1fff7a22)
# define BOARD_FLASH_SECTORS ((_FLASH_KBYTES == 0x400) ? 11 : 23)
# define BOARD_FLASH_SIZE (_FLASH_KBYTES * 1024)
# define OSC_FREQ 24
# define BOARD_PIN_LED_ACTIVITY 0 // no activity LED
# define BOARD_PIN_LED_BOOTLOADER GPIO12
# define BOARD_PORT_LEDS GPIOE
# define BOARD_CLOCK_LEDS RCC_AHB1ENR_IOPEEN
# define BOARD_LED_ON gpio_clear
# define BOARD_LED_OFF gpio_set
# define BOARD_FORCE_BL_PIN_OUT GPIO14
# define BOARD_FORCE_BL_PIN_IN GPIO11
# define BOARD_FORCE_BL_PORT GPIOE
# define BOARD_FORCE_BL_CLOCK_REGISTER RCC_AHB1ENR
# define BOARD_FORCE_BL_CLOCK_BIT RCC_AHB1ENR_IOPEEN
# define BOARD_FORCE_BL_PULL GPIO_PUPD_PULLUP
//# define BOARD_BOOT_FAIL_DETECT /* V2 boards should support boot failure detection */
#endif
#define APP_SIZE_MAX (BOARD_FLASH_SIZE - BOOTLOADER_RESERVATION_SIZE)
/* context passed to cinit */
#define BOARD_INTERFACE_CONFIG NULL
/* board definition */
struct boardinfo board_info = {
.board_type = BOARD_TYPE,
.board_rev = 0,
.fw_size = 0,
.systick_mhz = 168,
};
static void board_init(void);
#define BOOT_RTC_SIGNATURE 0xb007b007
#define BOOT_RTC_REG MMIO32(RTC_BASE + 0x50)
/* standard clocking for all F4 boards */
static const clock_scale_t clock_setup =
{
.pllm = OSC_FREQ,
.plln = 336,
.pllp = 2,
.pllq = 7,
.hpre = RCC_CFGR_HPRE_DIV_NONE,
.ppre1 = RCC_CFGR_PPRE_DIV_4,
.ppre2 = RCC_CFGR_PPRE_DIV_2,
.power_save = 0,
.flash_config = FLASH_ICE | FLASH_DCE | FLASH_LATENCY_5WS,
.apb1_frequency = 42000000,
.apb2_frequency = 84000000,
};
static uint32_t
board_get_rtc_signature()
{
/* enable the backup registers */
PWR_CR |= PWR_CR_DBP;
RCC_BDCR |= RCC_BDCR_RTCEN;
uint32_t result = BOOT_RTC_REG;
/* disable the backup registers */
RCC_BDCR &= RCC_BDCR_RTCEN;
PWR_CR &= ~PWR_CR_DBP;
return result;
}
static void
board_set_rtc_signature(uint32_t sig)
{
/* enable the backup registers */
PWR_CR |= PWR_CR_DBP;
RCC_BDCR |= RCC_BDCR_RTCEN;
BOOT_RTC_REG = sig;
/* disable the backup registers */
RCC_BDCR &= RCC_BDCR_RTCEN;
PWR_CR &= ~PWR_CR_DBP;
}
static bool
board_test_force_pin()
{
#if defined(BOARD_FORCE_BL_PIN_IN) && defined(BOARD_FORCE_BL_PIN_OUT)
/* two pins strapped together */
volatile unsigned samples = 0;
volatile unsigned vote = 0;
/* (re)configure the force BL pins */
rcc_peripheral_enable_clock(&BOARD_FORCE_BL_CLOCK_REGISTER, BOARD_FORCE_BL_CLOCK_BIT);
gpio_mode_setup(BOARD_FORCE_BL_PORT, GPIO_MODE_INPUT, BOARD_FORCE_BL_PULL, BOARD_FORCE_BL_PIN_IN);
gpio_mode_setup(BOARD_FORCE_BL_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, BOARD_FORCE_BL_PIN_OUT);
gpio_set_output_options(BOARD_FORCE_BL_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, BOARD_FORCE_BL_PIN_OUT);
for (volatile unsigned cycles = 0; cycles < 10; cycles++) {
gpio_set(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN_OUT);
for (unsigned count = 0; count < 20; count++) {
if (gpio_get(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN_IN) != 0)
vote++;
samples++;
}
gpio_clear(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN_OUT);
for (unsigned count = 0; count < 20; count++) {
if (gpio_get(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN_IN) == 0)
vote++;
samples++;
}
}
/* revert the driver pin */
gpio_mode_setup(BOARD_FORCE_BL_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_FORCE_BL_PIN_OUT);
/* the idea here is to reject wire-to-wire coupling, so require > 90% agreement */
if ((vote * 100) > (samples * 90))
return true;
#endif
#if defined(BOARD_FORCE_BL_PIN)
/* single pin pulled up or down */
volatile unsigned samples = 0;
volatile unsigned vote = 0;
/* (re)configure the force BL pins */
rcc_peripheral_enable_clock(&BOARD_FORCE_BL_CLOCK_REGISTER, BOARD_FORCE_BL_CLOCK_BIT);
gpio_mode_setup(BOARD_FORCE_BL_PORT, GPIO_MODE_INPUT, BOARD_FORCE_BL_PULL, BOARD_FORCE_BL_PIN);
for (samples = 0; samples < 200; samples++) {
if ((gpio_get(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN) ? 1 : 0) == BOARD_FORCE_BL_STATE)
vote++;
}
/* reject a little noise */
if ((vote * 100) > (samples * 90))
return true;
#endif
return false;
}
static void
board_init(void)
{
/* fix up the max firmware size, we have to read memory to get this */
board_info.fw_size = APP_SIZE_MAX,
#ifdef INTERFACE_USB
/* enable GPIO9 with a pulldown to sniff VBUS */
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO9);
#endif
/* initialise LEDs */
rcc_peripheral_enable_clock(&RCC_AHB1ENR, BOARD_CLOCK_LEDS);
gpio_mode_setup(
BOARD_PORT_LEDS,
GPIO_MODE_OUTPUT,
GPIO_PUPD_NONE,
BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
gpio_set_output_options(
BOARD_PORT_LEDS,
GPIO_OTYPE_PP,
GPIO_OSPEED_2MHZ,
BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
BOARD_LED_ON (
BOARD_PORT_LEDS,
BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
/* enable the power controller clock */
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_PWREN);
}
uint32_t
flash_func_sector_size(unsigned sector)
{
if (sector < BOARD_FLASH_SECTORS)
return flash_sectors[sector].size;
return 0;
}
void
flash_func_erase_sector(unsigned sector)
{
if (sector >= BOARD_FLASH_SECTORS)
return;
/* get the base address of the sector */
uint32_t address = 0;
for (unsigned i = 0; i < sector; i++)
address += flash_func_sector_size(i);
/* blank-check the sector */
unsigned size = flash_func_sector_size(sector);
bool blank = true;
for (unsigned i = 0; i < size; i += sizeof(uint32_t)) {
if (flash_func_read_word(address + i) != 0xffffffff) {
blank = false;
break;
}
}
/* erase the sector if it failed the blank check */
if (!blank)
flash_erase_sector(flash_sectors[sector].erase_code, FLASH_PROGRAM_X32);
}
void
flash_func_write_word(uint32_t address, uint32_t word)
{
flash_program_word(address + APP_LOAD_ADDRESS, word, FLASH_PROGRAM_X32);
}
uint32_t
flash_func_read_word(uint32_t address)
{
if (address & 3)
return 0;
return *(uint32_t *)(address + APP_LOAD_ADDRESS);
}
uint32_t
flash_func_read_otp(uint32_t address)
{
if (address & 3)
return 0;
if (address > OTP_SIZE)
return 0;
return *(uint32_t *)(address + OTP_BASE);
}
uint32_t
flash_func_read_sn(uint32_t address)
{
// read a byte out from unique chip ID area
// it's 12 bytes, or 3 words.
return *(uint32_t *)(address + UDID_START);
}
void
led_on(unsigned led)
{
switch (led) {
case LED_ACTIVITY:
BOARD_LED_ON (BOARD_PORT_LEDS, BOARD_PIN_LED_ACTIVITY);
break;
case LED_BOOTLOADER:
BOARD_LED_ON (BOARD_PORT_LEDS, BOARD_PIN_LED_BOOTLOADER);
break;
}
}
void
led_off(unsigned led)
{
switch (led) {
case LED_ACTIVITY:
BOARD_LED_OFF (BOARD_PORT_LEDS, BOARD_PIN_LED_ACTIVITY);
break;
case LED_BOOTLOADER:
BOARD_LED_OFF (BOARD_PORT_LEDS, BOARD_PIN_LED_BOOTLOADER);
break;
}
}
void
led_toggle(unsigned led)
{
switch (led) {
case LED_ACTIVITY:
gpio_toggle(BOARD_PORT_LEDS, BOARD_PIN_LED_ACTIVITY);
break;
case LED_BOOTLOADER:
gpio_toggle(BOARD_PORT_LEDS, BOARD_PIN_LED_BOOTLOADER);
break;
}
}
/* we should know this, but we don't */
#ifndef SCB_CPACR
# define SCB_CPACR (*((volatile uint32_t *) (((0xE000E000UL) + 0x0D00UL) + 0x088)))
#endif
int
main(void)
{
bool try_boot = true; /* try booting before we drop to the bootloader */
unsigned timeout = BOOTLOADER_DELAY; /* if nonzero, drop out of the bootloader after this time */
/* Enable the FPU before we hit any FP instructions */
SCB_CPACR |= ((3UL << 10*2) | (3UL << 11*2)); /* set CP10 Full Access and set CP11 Full Access */
/* do board-specific initialisation */
board_init();
/*
* Check the force-bootloader register; if we find the signature there, don't
* try booting.
*/
if (board_get_rtc_signature() == BOOT_RTC_SIGNATURE) {
/*
* Don't even try to boot before dropping to the bootloader.
*/
try_boot = false;
/*
* Don't drop out of the bootloader until something has been uploaded.
*/
timeout = 0;
/*
* Clear the signature so that if someone resets us while we're
* in the bootloader we'll try to boot next time.
*/
board_set_rtc_signature(0);
}
#ifdef INTERFACE_USB
/*
* Check for USB connection - if present, don't try to boot, but set a timeout after
* which we will fall out of the bootloader.
*
* If the force-bootloader pins are tied, we will stay here until they are removed and
* we then time out.
*/
if (gpio_get(GPIOA, GPIO9) != 0) {
/* don't try booting before we set up the bootloader */
try_boot = false;
}
#endif
/* Try to boot the app if we think we should just go straight there */
if (try_boot) {
/* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */
#ifdef BOARD_BOOT_FAIL_DETECT
board_set_rtc_signature(BOOT_RTC_SIGNATURE);
#endif
/* try to boot immediately */
jump_to_app();
/* booting failed, stay in the bootloader forever */
timeout = 0;
}
/* configure the clock for bootloader activity */
rcc_clock_setup_hse_3v3(&clock_setup);
/* start the interface */
cinit(BOARD_INTERFACE_CONFIG);
#if 0
// MCO1/02
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8);
gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIO8);
gpio_set_af(GPIOA, GPIO_AF0, GPIO8);
gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9);
gpio_set_af(GPIOC, GPIO_AF0, GPIO9);
#endif
while (1)
{
/* run the bootloader, come back after an app is uploaded or we time out */
bootloader(timeout);
/* if the force-bootloader pins are strapped, just loop back */
if (board_test_force_pin())
continue;
/* set the boot-to-bootloader flag so that if boot fails on reset we will stop here */
#ifdef BOARD_BOOT_FAIL_DETECT
board_set_rtc_signature(BOOT_RTC_SIGNATURE);
#endif
/* look to see if we can boot the app */
jump_to_app();
/* launching the app failed - stay in the bootloader forever */
timeout = 0;
}
}