-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.c
363 lines (309 loc) · 9.04 KB
/
main.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
/*
Copyright 2015 Benjamin Vedder benjamin@vedder.se
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include "ch.h"
#include "hal.h"
#include "stm32f4xx_conf.h"
#include "crc.h"
#include "buffer.h"
#include "heatshrink_decoder.h"
// 0 means generic, no LEDs
#ifndef HW_VER
#define HW_VER 60
#endif
/*
* Defines
*/
#define FLASH_SECTORS 12
#define BOOTLOADER_BASE 11
#define APP_BASE 0
#define APP_SECTORS 8
#define NEW_APP_BASE 8
#define NEW_APP_SECTORS 3
#define NEW_APP_MAX_SIZE (3 * (1 << 17))
// Base address of the Flash sectors
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) // Base @ of Sector 0, 16 Kbytes
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) // Base @ of Sector 1, 16 Kbytes
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) // Base @ of Sector 2, 16 Kbytes
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) // Base @ of Sector 3, 16 Kbytes
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) // Base @ of Sector 4, 64 Kbytes
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) // Base @ of Sector 5, 128 Kbytes
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) // Base @ of Sector 6, 128 Kbytes
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) // Base @ of Sector 7, 128 Kbytes
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) // Base @ of Sector 8, 128 Kbytes
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) // Base @ of Sector 9, 128 Kbytes
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) // Base @ of Sector 10, 128 Kbytes
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) // Base @ of Sector 11, 128 Kbytes
#define ERASE_VOLTAGE_RANGE (uint8_t)((PWR->CSR & PWR_CSR_PVDO) ? VoltageRange_2 : VoltageRange_3)
// LEDs
#if HW_VER == 60
#define LED_GREEN_GPIO GPIOB
#define LED_GREEN_PIN 0
#define LED_RED_GPIO GPIOB
#define LED_RED_PIN 1
#else
#define LED_GREEN_GPIO GPIOC
#define LED_GREEN_PIN 4
#define LED_RED_GPIO GPIOA
#define LED_RED_PIN 7
#endif
#define LED_GREEN 0
#define LED_RED 1
#if HW_VER == 0
#define LED_GREEN_ON()
#define LED_GREEN_OFF()
#define LED_RED_ON()
#define LED_RED_OFF()
#else
#define LED_GREEN_ON() palSetPad(LED_GREEN_GPIO, LED_GREEN_PIN)
#define LED_GREEN_OFF() palClearPad(LED_GREEN_GPIO, LED_GREEN_PIN)
#define LED_RED_ON() palSetPad(LED_RED_GPIO, LED_RED_PIN)
#define LED_RED_OFF() palClearPad(LED_RED_GPIO, LED_RED_PIN)
#endif
// Private constants
static const uint32_t flash_addr[FLASH_SECTORS] = {
ADDR_FLASH_SECTOR_0,
ADDR_FLASH_SECTOR_1,
ADDR_FLASH_SECTOR_2,
ADDR_FLASH_SECTOR_3,
ADDR_FLASH_SECTOR_4,
ADDR_FLASH_SECTOR_5,
ADDR_FLASH_SECTOR_6,
ADDR_FLASH_SECTOR_7,
ADDR_FLASH_SECTOR_8,
ADDR_FLASH_SECTOR_9,
ADDR_FLASH_SECTOR_10,
ADDR_FLASH_SECTOR_11
};
static const uint16_t flash_sector[12] = {
FLASH_Sector_0,
FLASH_Sector_1,
FLASH_Sector_2,
FLASH_Sector_3,
FLASH_Sector_4,
FLASH_Sector_5,
FLASH_Sector_6,
FLASH_Sector_7,
FLASH_Sector_8,
FLASH_Sector_9,
FLASH_Sector_10,
FLASH_Sector_11
};
static heatshrink_decoder hsd;
// Private functions
static void exit_bootloader(void);
static int16_t erase_app(uint32_t new_app_size);
static int16_t erase_app_all(void);
static int16_t write_app_data(uint32_t offset, uint8_t *data, uint32_t len);
void blink_led(int led, int blinks);
void sleep(int time);
// Private variables
static uint8_t *new_app_addr;
static void exit_bootloader(void) {
// Use the WWDG to reset the MCU
RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
WWDG_SetPrescaler(WWDG_Prescaler_1);
WWDG_SetWindowValue(255);
WWDG_Enable(100);
__disable_irq();
for(;;){};
}
static int16_t erase_app(uint32_t new_app_size) {
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
new_app_size += flash_addr[APP_BASE];
for (int i = 0;i < APP_SECTORS;i++) {
if (new_app_size > flash_addr[APP_BASE + i]) {
int16_t res = FLASH_EraseSector(flash_sector[APP_BASE + i], ERASE_VOLTAGE_RANGE);
if (res != FLASH_COMPLETE) {
return res;
}
} else {
break;
}
}
return FLASH_COMPLETE;
}
static int16_t erase_app_all(void) {
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
for (int i = 0;i < APP_SECTORS;i++) {
int16_t res = FLASH_EraseSector(flash_sector[APP_BASE + i], ERASE_VOLTAGE_RANGE);
if (res != FLASH_COMPLETE) {
return res;
}
}
return FLASH_COMPLETE;
}
static int16_t write_app_data(uint32_t offset, uint8_t *data, uint32_t len) {
FLASH_ClearFlag(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |
FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
for (uint32_t i = 0;i < len;i++) {
int16_t res = FLASH_ProgramByte(flash_addr[APP_BASE] + offset + i, data[i]);
if (res != FLASH_COMPLETE) {
return res;
}
}
return FLASH_COMPLETE;
}
static int16_t write_new_app(void) {
int32_t ind = 0;
uint32_t size = buffer_get_uint32(new_app_addr, &ind);
uint16_t crc_app = buffer_get_uint16(new_app_addr, &ind);
bool is_compressed = false;
if ((size >> 24) == 0xCC) {
is_compressed = true;
size &= 0x00FFFFFF;
}
if (size > NEW_APP_MAX_SIZE) {
return -1;
}
if (size == 0) {
return -2;
}
uint16_t crc_calc = crc16(new_app_addr + ind, size);
if (crc_calc != crc_app) {
return -3;
}
int16_t res;
if (is_compressed) {
res = erase_app_all();
} else {
res = erase_app(size);
}
if (res != FLASH_COMPLETE) {
return res;
}
if (is_compressed) {
heatshrink_decoder_reset(&hsd);
uint32_t compressed_size = size;
size_t count = 0;
uint32_t sunk = 0;
uint32_t polled = 0;
uint8_t *input = new_app_addr + ind;
while (sunk < compressed_size) {
HSD_sink_res sres;
sres = heatshrink_decoder_sink(&hsd, &input[sunk], compressed_size - sunk, &count);
if (sres < 0) {
return sres;
}
sunk += count;
if (sunk == compressed_size) {
heatshrink_decoder_finish(&hsd);
}
HSD_poll_res pres;
do {
uint8_t chunk[16];
pres = heatshrink_decoder_poll(&hsd, chunk, 16, &count);
if (pres < 0) {
return pres;
}
res = write_app_data(polled, chunk, count);
if (res != FLASH_COMPLETE) {
return res;
}
polled += count;
} while (pres == HSDR_POLL_MORE);
if (sunk == compressed_size) {
heatshrink_decoder_finish(&hsd);
}
}
} else {
res = write_app_data(0, new_app_addr + ind, size);
}
return res;
}
void blink_led(int led, int blinks) {
#if HW_VER == 0
(void)led;
(void)blinks;
#else
if (led == LED_GREEN) {
for (int i = 0;i < blinks;i++) {
LED_GREEN_ON();
sleep(200);
LED_GREEN_OFF();
sleep(200);
}
} else if (led == LED_RED) {
for (int i = 0;i < blinks;i++) {
LED_RED_ON();
sleep(200);
LED_RED_OFF();
sleep(200);
}
}
#endif
}
void sleep(int time) {
for (volatile int i = 0;i < time * 10000;i++) {
__NOP();
}
// chThdSleepMilliseconds(time);
}
int main(void) {
// halInit();
// chSysInit();
// Change the vector table offset
SCB_VTOR = flash_addr[BOOTLOADER_BASE];
// Set app base address
new_app_addr = (uint8_t *)flash_addr[NEW_APP_BASE];
// LEDs
#if HW_VER != 0
palSetPadMode(LED_GREEN_GPIO, LED_GREEN_PIN,
PAL_MODE_OUTPUT_PUSHPULL |
PAL_STM32_OSPEED_HIGHEST);
palSetPadMode(LED_RED_GPIO, LED_RED_PIN,
PAL_MODE_OUTPUT_PUSHPULL |
PAL_STM32_OSPEED_HIGHEST);
#endif
// Hold shutdown pin on to prevent powering off.
#if HW_VER == 60
palSetPadMode(GPIOC, 5,
PAL_MODE_OUTPUT_PUSHPULL |
PAL_STM32_OSPEED_HIGHEST);
palSetPad(GPIOC, 5);
#endif
LED_GREEN_OFF();
LED_RED_OFF();
// Up to 5 retries
for (int i = 0;i < 5;i++) {
int16_t res = write_new_app();
if (res == -1) {
blink_led(LED_RED, 3);
exit_bootloader();
} else if (res == -2) {
blink_led(LED_RED, 4);
exit_bootloader();
} else if (res == -3) {
blink_led(LED_RED, 5);
exit_bootloader();
} else if (res == FLASH_COMPLETE) {
blink_led(LED_GREEN, 3);
exit_bootloader();
}
blink_led(LED_RED, 1);
}
// Erasing or writing to flash failed. A programmer is needed
// to upload the firmware now.
sleep(2000);
blink_led(LED_RED, 8);
exit_bootloader();
// Should not happen
for(;;) {
sleep(1000);
}
}