forked from virtualrobotix/VRX_Bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_f1.c
262 lines (217 loc) · 5.81 KB
/
main_f1.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
/*
* STM32F1 board support for the bootloader.
*
*/
#include <stdlib.h>
#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/stm32/f1/bkp.h>
#include <libopencm3/stm32/f1/gpio.h>
#include <libopencm3/stm32/f1/flash.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/pwr.h>
#include <libopencm3/stm32/systick.h>
#include "bl.h"
/*
* Yes, the nonsense required to configure GPIOs with this
* library is truly insane...
*/
#if defined(BOARD_IO)
# define OSC_FREQ 24
# define BOARD_PIN_LED_ACTIVITY GPIO14
# define BOARD_PIN_LED_BOOTLOADER GPIO15
# define BOARD_PORT_LEDS GPIOB
# define BOARD_CLOCK_LEDS_REGISTER RCC_APB2ENR
# define BOARD_CLOCK_LEDS RCC_APB2ENR_IOPBEN
# define BOARD_LED_ON gpio_clear
# define BOARD_LED_OFF gpio_set
# define BOARD_USART USART2
# define BOARD_USART_CLOCK_REGISTER RCC_APB1ENR
# define BOARD_USART_CLOCK_BIT RCC_APB1ENR_USART2EN
# define BOARD_PORT_USART GPIOA
# define BOARD_PIN_TX GPIO_USART2_TX
# define BOARD_PIN_RX GPIO_USART2_RX
# define BOARD_USART_PIN_CLOCK_REGISTER RCC_APB2ENR
# define BOARD_USART_PIN_CLOCK_BIT RCC_APB2ENR_IOPAEN
# define BOARD_FORCE_BL_PIN GPIO5
# define BOARD_FORCE_BL_PORT GPIOB
# define BOARD_FORCE_BL_CLOCK_REGISTER RCC_APB2ENR
# define BOARD_FORCE_BL_CLOCK_BIT RCC_APB2ENR_IOPBEN
# define BOARD_FORCE_BL_VALUE BOARD_FORCE_BL_PIN
# define BOARD_FLASH_SECTORS 60
# define FLASH_SECTOR_SIZE 0x400
#else
# error Unrecognised BOARD definition
#endif
#ifdef INTERFACE_USART
# define BOARD_INTERFACE_CONFIG (void *)BOARD_USART
#else
# define BOARD_INTERFACE_CONFIG NULL
#endif
/* board definition */
struct boardinfo board_info = {
.board_type = 10,
.board_rev = 0,
.fw_size = APP_SIZE_MAX,
.systick_mhz = OSC_FREQ,
};
static void board_init(void);
static void
board_init(void)
{
/* initialise LEDs */
rcc_peripheral_enable_clock(&BOARD_CLOCK_LEDS_REGISTER, BOARD_CLOCK_LEDS);
gpio_set_mode(BOARD_PORT_LEDS,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
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_set_mode(BOARD_FORCE_BL_PORT,
GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, /* depend on external pull */
BOARD_FORCE_BL_PIN);
#endif
/* enable the backup registers */
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN);
#ifdef INTERFACE_USART
/* configure usart pins */
rcc_peripheral_enable_clock(&BOARD_USART_PIN_CLOCK_REGISTER, BOARD_USART_PIN_CLOCK_BIT);
gpio_set_mode(BOARD_PORT_USART,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
BOARD_PIN_TX);
/* 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
}
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
flash_func_read_sn(uint32_t address)
{
return 0;
}
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 (BKP_DR1 == BL_WAIT_MAGIC) {
result = true;
BKP_DR1 = 0;
}
PWR_CR &= ~PWR_CR_DBP;
return result;
}
int
main(void)
{
unsigned timeout = 0;
/* do board-specific initialisation */
board_init();
#ifdef INTERFACE_USART
/* 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 */
rcc_clock_setup_in_hsi_out_24mhz();
/* start the interface */
cinit(BOARD_INTERFACE_CONFIG);
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;
}
}