forked from PX4/PX4-Bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_f3.c
479 lines (383 loc) · 11 KB
/
main_f3.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
/*
* STM32F3 board support for the bootloader.
*
*/
#include "hw_config.h"
#include <stdlib.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/desig.h>
#include <libopencm3/stm32/flash.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/pwr.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/systick.h>
#include "bl.h"
#define UDID_START 0x1FFFF7E8
// address of MCU IDCODE
#define DBGMCU_IDCODE 0xE0042000
#define FLASH_BASE (0x08000000U)
#define BOOT_RTC_REG MMIO32(RTC_BASE + 0x50)
#ifdef INTERFACE_USART
# define BOARD_INTERFACE_CONFIG (void *)BOARD_USART
#else
# define BOARD_INTERFACE_CONFIG NULL
#endif
const struct rcc_clock_scale _rcc_hsi_8mhz = {
.pllsrc = RCC_CFGR_PLLSRC_HSI_DIV2,
.pllmul = RCC_CFGR_PLLMUL_MUL16,
.plldiv = RCC_CFGR2_PREDIV_NODIV,
.hpre = RCC_CFGR_HPRE_DIV_NONE,
.ppre1 = RCC_CFGR_PPRE1_DIV_2,
.ppre2 = RCC_CFGR_PPRE2_DIV_NONE,
.flash_waitstates = 2,
.ahb_frequency = 64000000,
.apb1_frequency = 32000000,
.apb2_frequency = 64000000,
};
/* board definition */
struct boardinfo board_info = {
.board_type = BOARD_TYPE,
.board_rev = 0,
.fw_size = APP_SIZE_MAX,
.systick_mhz = OSC_FREQ,
};
static void board_init(void);
uint32_t
board_get_devices(void)
{
return BOOT_DEVICES_SELECTION;
}
static void
board_init(void)
{
/* initialise LEDs */
rcc_peripheral_enable_clock(&BOARD_CLOCK_LEDS_REGISTER, 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);
/* if we have one, enable the force-bootloader pin */
#ifdef BOARD_FORCE_BL_PIN
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);
#endif
/* enable the backup registers */
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_PWREN);
#ifdef INTERFACE_USART
/* configure usart pins */
rcc_peripheral_enable_clock(&BOARD_USART_PIN_CLOCK_REGISTER, BOARD_USART_PIN_CLOCK_BIT_TX);
rcc_peripheral_enable_clock(&BOARD_USART_PIN_CLOCK_REGISTER, BOARD_USART_PIN_CLOCK_BIT_RX);
/* Setup GPIO pins for USART transmit. */
gpio_mode_setup(BOARD_PORT_USART_TX, GPIO_MODE_AF, GPIO_PUPD_PULLUP, BOARD_PIN_TX);
gpio_mode_setup(BOARD_PORT_USART_RX, GPIO_MODE_AF, GPIO_PUPD_PULLUP, BOARD_PIN_RX);
/* Setup USART TX & RX pins as alternate function. */
gpio_set_af(BOARD_PORT_USART_TX, BOARD_PORT_USART_AF_TX, BOARD_PIN_TX);
gpio_set_af(BOARD_PORT_USART_RX, BOARD_PORT_USART_AF_RX, BOARD_PIN_RX);
/* configure USART clock */
rcc_peripheral_enable_clock(&BOARD_USART_CLOCK_REGISTER, BOARD_USART_CLOCK_BIT);
#endif
#ifdef INTERFACE_I2C
# error I2C GPIO config not handled yet
#endif
}
void
board_deinit(void)
{
/* deinitialise LEDs */
gpio_mode_setup(BOARD_PORT_LEDS,
GPIO_MODE_INPUT,
GPIO_PUPD_NONE,
BOARD_PIN_LED_BOOTLOADER | BOARD_PIN_LED_ACTIVITY);
/* if we have one, disable the force-bootloader pin */
#ifdef BOARD_FORCE_BL_PIN
gpio_mode_setup(BOARD_FORCE_BL_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_FORCE_BL_PIN);
#endif
/* disable the backup registers */
rcc_peripheral_disable_clock(&RCC_APB1ENR, RCC_APB1ENR_PWREN);
#ifdef INTERFACE_USART
/* deinitialise GPIO pins for USART transmit. */
gpio_mode_setup(BOARD_PORT_USART_TX, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_PIN_TX);
gpio_mode_setup(BOARD_PORT_USART_RX, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_PIN_RX);
/* disable USART peripheral clock */
rcc_peripheral_disable_clock(&BOARD_USART_CLOCK_REGISTER, BOARD_USART_CLOCK_BIT);
#endif
#ifdef INTERFACE_I2C
# error I2C GPIO config not handled yet
#endif
/* reset the APB2 peripheral clocks */
RCC_AHBENR = 0x00000014; // XXX Magic reset number from STM32F3x reference manual
}
/**
* @brief Initializes the RCC clock configuration.
*
* @param clock_setup : The clock configuration to set
*/
static inline void
clock_init(void)
{
rcc_clock_setup_hsi(&_rcc_hsi_8mhz);
}
inline void arch_systic_init(void)
{
/* (re)start the timer system */
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
systick_set_reload(board_info.systick_mhz * 1000); /* 1ms tick, magic number */
systick_interrupt_enable();
systick_counter_enable();
}
inline void arch_systic_deinit(void)
{
/* kill the systick interrupt */
systick_interrupt_disable();
systick_counter_disable();
}
/**
* @brief Resets the RCC clock configuration to the default reset state.
* @note The default reset state of the clock configuration is given below:
* - HSI ON and used as system clock source
* - HSE, PLL and PLLI2S OFF
* - AHB, APB1 and APB2 prescaler set to 1.
* - CSS, MCO1 and MCO2 OFF
* - All interrupts disabled
* @note This function doesn't modify the configuration of the
* - Peripheral clocks
* - LSI, LSE and RTC clocks
*/
void
clock_deinit(void)
{
/* Enable internal high-speed oscillator. */
rcc_osc_on(RCC_HSI);
rcc_wait_for_osc_ready(RCC_HSI);
/* Reset the RCC_CFGR register */
RCC_CFGR = 0x000000;
/* Stop the HSE, CSS, PLL, PLLI2S, PLLSAI */
rcc_osc_off(RCC_HSE);
rcc_osc_off(RCC_PLL);
rcc_css_disable();
/* Reset the HSEBYP bit */
rcc_osc_bypass_disable(RCC_HSE);
/* Reset the CIR register */
RCC_CIR = 0x000000;
}
inline void arch_flash_lock(void)
{
flash_lock();
}
inline void arch_flash_unlock(void)
{
flash_unlock();
}
inline void arch_setvtor(uint32_t address)
{
SCB_VTOR = address;
}
/*---------------------------------------------------------------------------*/
/** @brief Program a Half Word to FLASH
This performs all operations necessary to program a 16 bit word to FLASH memory.
The program error flag should be checked separately for the event that memory
was not properly erased.
Status bit polling is used to detect end of operation.
@param[in] address Full address of flash half word to be programmed.
@param[in] data half word to write
*/
void flash_program_half_word(uint32_t address, uint16_t data)
{
flash_wait_for_last_operation();
FLASH_CR |= FLASH_CR_PG;
MMIO16(address) = data;
flash_wait_for_last_operation();
FLASH_CR &= ~FLASH_CR_PG;
}
/*---------------------------------------------------------------------------*/
/** @brief Program a 32 bit Word to FLASH
This performs all operations necessary to program a 32 bit word to FLASH memory.
The program error flag should be checked separately for the event that memory
was not properly erased.
Status bit polling is used to detect end of operation.
@param[in] address Full address of flash word to be programmed.
@param[in] data word to write
*/
void flash_program_word(uint32_t address, uint32_t data)
{
flash_program_half_word(address, (uint16_t)data);
flash_program_half_word(address + 2, (uint16_t)(data >> 16));
}
/*---------------------------------------------------------------------------*/
/** @brief Erase a Page of FLASH
This performs all operations necessary to erase a page in FLASH memory.
The page should be checked to ensure that it was properly erased. A page must
first be fully erased before attempting to program it.
Note that the page sizes differ between devices. See the reference manual or
the FLASH programming manual for details.
@param[in] page_address Full address of flash page to be erased.
*/
void flash_erase_page(uint32_t page_address)
{
flash_wait_for_last_operation();
FLASH_CR |= FLASH_CR_PER;
FLASH_AR = page_address;
FLASH_CR |= FLASH_CR_STRT;
flash_wait_for_last_operation();
FLASH_CR &= ~FLASH_CR_PER;
}
uint32_t
flash_func_sector_size(unsigned sector)
{
if (sector < BOARD_FLASH_SECTORS) {
return FLASH_SECTOR_SIZE;
}
return 0;
}
void
flash_func_erase_sector(unsigned sector)
{
if (sector < BOARD_FLASH_SECTORS) {
flash_erase_page(APP_LOAD_ADDRESS + (sector * FLASH_SECTOR_SIZE));
}
}
void
flash_func_write_word(uint32_t address, uint32_t word)
{
flash_program_word(address + APP_LOAD_ADDRESS, word);
}
uint32_t
flash_func_read_word(uint32_t address)
{
return *(uint32_t *)(address + APP_LOAD_ADDRESS);
}
uint32_t
flash_func_read_otp(uint32_t address)
{
return 0;
}
uint32_t get_mcu_id(void)
{
return *(uint32_t *)DBGMCU_IDCODE;
}
// See F4 version for future enhancement for full decoding
int get_mcu_desc(int max, uint8_t *revstr)
{
const char none[] = "STM32F3xxx,?";
int i;
for (i = 0; none[i] && i < max - 1; i++) {
revstr[i] = none[i];
}
return i;
}
int check_silicon(void)
{
return 0;
}
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;
}
}
static bool
should_wait(void)
{
bool result = false;
PWR_CR |= PWR_CR_DBP;
if (BOOT_RTC_REG == BL_WAIT_MAGIC) {
result = true;
BOOT_RTC_REG = 0;
}
PWR_CR &= ~PWR_CR_DBP;
return result;
}
int
main(void)
{
unsigned timeout = 0;
/* do board-specific initialisation */
board_init();
#if defined(INTERFACE_USART) | defined (INTERFACE_USB)
/* XXX sniff for a USART connection to decide whether to wait in the bootloader? */
timeout = BOOTLOADER_DELAY;
#endif
#ifdef INTERFACE_I2C
# error I2C bootloader detection logic not implemented
#endif
/* if the app left a cookie saying we should wait, then wait */
if (should_wait()) {
timeout = BOOTLOADER_DELAY;
}
#ifdef BOARD_FORCE_BL_PIN
/* if the force-BL pin state matches the state of the pin, wait in the bootloader forever */
if (BOARD_FORCE_BL_VALUE == gpio_get(BOARD_FORCE_BL_PORT, BOARD_FORCE_BL_PIN)) {
timeout = 0xffffffff;
}
#endif
/* look for the magic wait-in-bootloader value in backup register zero */
/* if we aren't expected to wait in the bootloader, try to boot immediately */
if (timeout == 0) {
/* try to boot immediately */
jump_to_app();
/* if we returned, there is no app; go to the bootloader and stay there */
timeout = 0;
}
/* configure the clock for bootloader activity */
clock_init();
/* start the interface */
cinit(BOARD_INTERFACE_CONFIG, USART);
while (1) {
/* run the bootloader, possibly coming back after the timeout */
bootloader(timeout);
/* look to see if we can boot the app */
jump_to_app();
/* boot failed; stay in the bootloader forever next time */
timeout = 0;
}
}