forked from dword1511/stm32-vserprog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vserprog.c
461 lines (371 loc) · 11.4 KB
/
vserprog.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
#include <stdbool.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/usb/usbd.h>
#ifdef STM32F0
#include <libopencm3/stm32/crs.h>
#include <libopencm3/stm32/syscfg.h>
#endif /* STM32F0 */
#include "flashrom/serprog.h"
#include "flashrom/flash.h" /* For bus type */
#include "board.h"
#include "usbcdc.h"
#include "spi.h"
#define S_IFACE_VERSION 0x01 /* Currently version 1 */
#define S_PGM_NAME "stm32-vserprog" /* The program's name, must < 16 bytes */
#define S_SUPPORTED_BUS BUS_SPI
#define S_CMD_MAP ( \
(1 << S_CMD_NOP) | \
(1 << S_CMD_Q_IFACE) | \
(1 << S_CMD_Q_CMDMAP) | \
(1 << S_CMD_Q_PGMNAME) | \
(1 << S_CMD_Q_SERBUF) | \
(1 << S_CMD_Q_BUSTYPE) | \
(1 << S_CMD_SYNCNOP) | \
(1 << S_CMD_O_SPIOP) | \
(1 << S_CMD_S_BUSTYPE) | \
(1 << S_CMD_S_SPI_FREQ)| \
(1 << S_CMD_S_PIN_STATE) \
)
#ifdef STM32F0
#define LED_ENABLE() { \
gpio_mode_setup(BOARD_PORT_LED, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, BOARD_PIN_LED); \
gpio_set_output_options(BOARD_PORT_LED, GPIO_OTYPE_PP, GPIO_OSPEED_LOW, BOARD_PIN_LED); \
}
#define LED_DISABLE() gpio_mode_setup(BOARD_PORT_LED, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BOARD_PIN_LED);
#else
#define LED_ENABLE() gpio_set_mode(BOARD_PORT_LED, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, BOARD_PIN_LED)
#define LED_DISABLE() gpio_set_mode(BOARD_PORT_LED, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BOARD_PIN_LED)
#endif /* STM32F0 */
#if BOARD_LED_HIGH_IS_BUSY
#define LED_BUSY() gpio_set(BOARD_PORT_LED, BOARD_PIN_LED)
#define LED_IDLE() gpio_clear(BOARD_PORT_LED, BOARD_PIN_LED)
#else
#define LED_BUSY() gpio_clear(BOARD_PORT_LED, BOARD_PIN_LED)
#define LED_IDLE() gpio_set(BOARD_PORT_LED, BOARD_PIN_LED)
#endif /* BOARD_LED_HIGH_IS_BUSY */
void handle_command(unsigned char command) {
static uint8_t i; /* Loop */
static uint8_t l; /* Length */
static uint32_t slen; /* SPIOP write length */
static uint32_t rlen; /* SPIOP read length */
static uint32_t freq_req; /* Requested SPI clock */
LED_BUSY();
switch(command) {
case S_CMD_NOP: {
usbcdc_putc(S_ACK);
break;
}
case S_CMD_Q_IFACE: {
// TODO: use usbcdc_write for better efficiency
usbcdc_putc(S_ACK);
/* little endian multibyte value to complete to 16bit */
usbcdc_putc(S_IFACE_VERSION);
usbcdc_putc(0);
break;
}
case S_CMD_Q_CMDMAP: {
// TODO: use usbcdc_write for better efficiency
usbcdc_putc(S_ACK);
/* little endian */
usbcdc_putu32(S_CMD_MAP);
for(i = 0; i < 32 - sizeof(uint32_t); i++) {
usbcdc_putc(0);
}
break;
}
case S_CMD_Q_PGMNAME: {
// TODO: use usbcdc_write for better efficiency
usbcdc_putc(S_ACK);
l = 0;
while(S_PGM_NAME[l]) {
usbcdc_putc(S_PGM_NAME[l]);
l ++;
}
for(i = l; i < 16; i++) {
usbcdc_putc(0);
}
break;
}
case S_CMD_Q_SERBUF: {
// TODO: use usbcdc_write for better efficiency
usbcdc_putc(S_ACK);
/* Pretend to be 64K (0xffff) */
usbcdc_putc(0xff);
usbcdc_putc(0xff);
break;
}
case S_CMD_Q_BUSTYPE: {
// TODO: use usbcdc_write for better efficiency
// TODO: LPC / FWH IO support via PP-Mode
usbcdc_putc(S_ACK);
usbcdc_putc(S_SUPPORTED_BUS);
break;
}
case S_CMD_Q_CHIPSIZE: {
break;
}
case S_CMD_Q_OPBUF: {
// TODO: opbuf function 0
break;
}
case S_CMD_Q_WRNMAXLEN: {
break;
}
case S_CMD_R_BYTE: {
break;
}
case S_CMD_R_NBYTES: {
break;
}
case S_CMD_O_INIT: {
break;
}
case S_CMD_O_WRITEB: {
// TODO: opbuf function 1
break;
}
case S_CMD_O_WRITEN: {
// TODO: opbuf function 2
break;
}
case S_CMD_O_DELAY: {
// TODO: opbuf function 3
break;
}
case S_CMD_O_EXEC: {
// TODO: opbuf function 4
break;
}
case S_CMD_SYNCNOP: {
// TODO: use usbcdc_write for better efficiency
usbcdc_putc(S_NAK);
usbcdc_putc(S_ACK);
break;
}
case S_CMD_Q_RDNMAXLEN: {
// TODO
break;
}
case S_CMD_S_BUSTYPE: {
/* We do not have multiplexed bus interfaces,
* so simply ack on supported types, no setup needed. */
if((usbcdc_getc() | S_SUPPORTED_BUS) == S_SUPPORTED_BUS) {
usbcdc_putc(S_ACK);
} else {
usbcdc_putc(S_NAK);
}
break;
}
case S_CMD_O_SPIOP: {
slen = usbcdc_getu24();
rlen = usbcdc_getu24();
SPI_SELECT();
/* TODO: handle errors with S_NAK */
if(slen) {
spi_bulk_write(slen);
}
usbcdc_putc(S_ACK); // TODO: S_ACK early for better performance (so while DMA is working, programmer can receive next command)?
if(rlen) {
spi_bulk_read(rlen); // TODO: buffer?
}
SPI_UNSELECT();
break;
}
case S_CMD_S_SPI_FREQ: {
freq_req = usbcdc_getu32();
if(freq_req == 0) {
usbcdc_putc(S_NAK);
} else {
usbcdc_putc(S_ACK);
usbcdc_putu32(spi_setup(freq_req));
}
break;
}
case S_CMD_S_PIN_STATE: {
if( usbcdc_getc() )
spi_enable_pins();
else
spi_disable_pins();
usbcdc_putc(S_ACK);
break;
}
default: {
break; // TODO: notify protocol failure malformed command
}
}
LED_IDLE();
}
#ifdef GD32F103
#define RCC_GCFGR_ADCPS_DIV12 ((uint32_t)0x10004000)
#define RCC_GCFGR_ADCPS_DIV16 ((uint32_t)0x1000C000)
#define RCC_GCFGR_USBPS_Div2_5 ((uint32_t)0x00800000)
#define RCC_GCFGR_USBPS_Div2 ((uint32_t)0x00C00000)
static void rcc_clock_setup_in_hse_12mhz_out_96mhz(void) {
/* Enable internal high-speed oscillator. */
rcc_osc_on(RCC_HSI);
rcc_wait_for_osc_ready(RCC_HSI);
/* Select HSI as SYSCLK source. */
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSICLK);
/* Enable external high-speed oscillator 12MHz. */
rcc_osc_on(RCC_HSE);
rcc_wait_for_osc_ready(RCC_HSE);
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSECLK);
/*
* Set prescalers for AHB, ADC, ABP1, ABP2.
* Do this before touching the PLL
*/
rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_NODIV); /* Set. 96MHz Max. 108MHz */
rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV8); /* Set. 12MHz Max. 14MHz */
rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_DIV2); /* Set. 48MHz Max. 54MHz */
rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV); /* Set. 96MHz Max. 108MHz */
RCC_CFGR |= RCC_GCFGR_USBPS_Div2; /* USB Set. 48MHz Max. 48MHz */
/* GD32 has 0-wait-state flash, do not touch anything! */
/*
* Set the PLL multiplication factor to 8.
* 12MHz (external) * 8 (multiplier) = 96MHz
*/
rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL8);
/* Select HSE as PLL source. */
rcc_set_pll_source(RCC_CFGR_PLLSRC_HSE_CLK);
/*
* External frequency undivided before entering PLL
* (only valid/needed for HSE).
*/
rcc_set_pllxtpre(RCC_CFGR_PLLXTPRE_HSE_CLK);
/* Enable PLL oscillator and wait for it to stabilize. */
rcc_osc_on(RCC_PLL);
rcc_wait_for_osc_ready(RCC_PLL);
/* Select PLL as SYSCLK source. */
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK);
/* Set the peripheral clock frequencies used */
rcc_ahb_frequency = 96000000;
rcc_apb1_frequency = 48000000;
rcc_apb2_frequency = 96000000;
}
static void rcc_clock_setup_in_hse_12mhz_out_120mhz(void) {
/* Enable internal high-speed oscillator. */
rcc_osc_on(RCC_HSI);
rcc_wait_for_osc_ready(RCC_HSI);
/* Select HSI as SYSCLK source. */
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSICLK);
/* Enable external high-speed oscillator 12MHz. */
rcc_osc_on(RCC_HSE);
rcc_wait_for_osc_ready(RCC_HSE);
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_HSECLK);
/*
* Set prescalers for AHB, ADC, ABP1, ABP2.
* Do this before touching the PLL
*/
rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_NODIV); /* Set. 120MHz Max. 108MHz */
RCC_CFGR = (RCC_CFGR & ~RCC_CFGR_ADCPRE) | RCC_GCFGR_ADCPS_DIV12; /* ADC Set. 10MHz Max. 14MHz */
rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_DIV2); /* Set. 60MHz Max. 54MHz */
rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV); /* Set. 120MHz Max. 108MHz */
RCC_CFGR |= RCC_GCFGR_USBPS_Div2_5; /* USB Set. 48MHz Max. 48MHz */
/* GD32 has 0-wait-state flash, do not touch anything! */
/*
* Set the PLL multiplication factor to 10.
* 12MHz (external) * 10 (multiplier) = 120MHz
*/
rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL10);
/* Select HSE as PLL source. */
rcc_set_pll_source(RCC_CFGR_PLLSRC_HSE_CLK);
/*
* External frequency undivided before entering PLL
* (only valid/needed for HSE).
*/
rcc_set_pllxtpre(RCC_CFGR_PLLXTPRE_HSE_CLK);
/* Enable PLL oscillator and wait for it to stabilize. */
rcc_osc_on(RCC_PLL);
rcc_wait_for_osc_ready(RCC_PLL);
/* Select PLL as SYSCLK source. */
rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK);
/* Set the peripheral clock frequencies used */
rcc_ahb_frequency = 120000000;
rcc_apb1_frequency = 60000000;
rcc_apb2_frequency = 120000000;
}
#endif /* GD32F103 */
int main(void) {
uint32_t i;
rcc_periph_clock_enable(BOARD_RCC_LED);
LED_ENABLE();
LED_BUSY();
/* Setup clock accordingly */
#ifdef GD32F103
rcc_clock_setup_in_hse_12mhz_out_120mhz();
#else
#ifdef STM32F0
rcc_clock_setup_in_hsi48_out_48mhz();
rcc_periph_clock_enable(RCC_SYSCFG_COMP);
SYSCFG_CFGR1 |= SYSCFG_CFGR1_PA11_PA12_RMP;
rcc_periph_clock_enable(RCC_CRS);
crs_autotrim_usb_enable();
rcc_set_usbclk_source(RCC_HSI48);
#else
rcc_clock_setup_in_hse_8mhz_out_72mhz();
#endif /* STM32F0 */
#endif /* GD32F103 */
rcc_periph_clock_enable(RCC_GPIOA); /* For USB */
/* STM32F0x2 has internal pullup and does not need AFIO */
#ifndef STM32F0
rcc_periph_clock_enable(BOARD_RCC_USB_PULLUP);
rcc_periph_clock_enable(RCC_AFIO); /* For SPI */
#endif /* STM32F0 */
#if BOARD_USE_DEBUG_PINS_AS_GPIO
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF, AFIO_MAPR_TIM2_REMAP_FULL_REMAP);
#endif
/* Setup GPIO to pull up the D+ high. (STM32F0x2 has internal pullup.) */
#ifndef STM32F0
gpio_set_mode(BOARD_PORT_USB_PULLUP, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, BOARD_PIN_USB_PULLUP);
#if BOARD_USB_HIGH_IS_PULLUP
gpio_set(BOARD_PORT_USB_PULLUP, BOARD_PIN_USB_PULLUP);
#else
gpio_clear(BOARD_PORT_USB_PULLUP, BOARD_PIN_USB_PULLUP);
#endif /* BOARD_USB_HIGH_IS_PULLUP */
#endif /* STM32F0 */
usbcdc_init();
#ifdef HAS_BOARD_INIT
board_init();
#endif
spi_setup(SPI_DEFAULT_CLOCK);
/* The loop. */
while (true) {
/* Wait and blink if USB is not ready. */
LED_IDLE();
while (!usb_ready) {
LED_DISABLE();
for (i = 0; i < rcc_ahb_frequency / 150; i ++) {
asm("nop");
}
LED_ENABLE();
for (i = 0; i < rcc_ahb_frequency / 150; i ++) {
asm("nop");
}
}
/* Actual thing */
/* TODO: we are blocked here, hence no knowledge about USB bet reset. */
handle_command(usbcdc_getc());
}
return 0;
}
/* Interrupts (currently none here) */
static void signal_fault(void) {
uint32_t i;
while (true) {
LED_ENABLE();
LED_BUSY();
for (i = 0; i < 5000000; i ++) {
asm("nop");
}
LED_DISABLE();
for (i = 0; i < 5000000; i ++) {
asm("nop");
}
}
}
void nmi_handler(void)
__attribute__ ((alias ("signal_fault")));
void hard_fault_handler(void)
__attribute__ ((alias ("signal_fault")));