blob: 2b44f01daef56a57555af7220218754fd0422bc2 [file] [log] [blame]
Harald Welted8a003d2017-02-27 20:31:09 +01001#include "board.h"
2#include "utils.h"
3#include "usb/device/dfu/dfu.h"
4#include "usb/common/dfu/usb_dfu.h"
5#include "manifest.h"
6
Harald Welte3bbaba02017-03-06 22:47:06 +01007#include <osmocom/core/timer.h>
8
Harald Welte32852bc2017-02-28 00:21:45 +01009#define ALTIF_RAM 0
10#define ALTIF_FLASH 1
11
Harald Welted8a003d2017-02-27 20:31:09 +010012unsigned int g_unique_id[4];
13
14/*----------------------------------------------------------------------------
15 * Callbacks
16 *----------------------------------------------------------------------------*/
17
Harald Welte32852bc2017-02-28 00:21:45 +010018#define RAM_ADDR(offset) (IRAM_ADDR + BOARD_DFU_RAM_SIZE + offset)
19#define FLASH_ADDR(offset) (IFLASH_ADDR + BOARD_DFU_BOOT_SIZE + offset)
20
21#define IFLASH_END ((uint8_t *)IFLASH_ADDR + IFLASH_SIZE)
22#define IRAM_END ((uint8_t *)IRAM_ADDR + IRAM_SIZE)
23
24/* incoming call-back: Host has transfered 'len' bytes (stored at
25 * 'data'), which we shall write to 'offset' into the partition
26 * associated with 'altif'. Guaranted to be les than
27 * BOARD_DFU_PAGE_SIZE */
28int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
29 uint8_t *data, unsigned int len)
Harald Welted8a003d2017-02-27 20:31:09 +010030{
Harald Welte32852bc2017-02-28 00:21:45 +010031 uint32_t addr;
32 int rc;
33
Harald Weltee8eea292017-03-03 00:35:51 +010034 printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
Harald Welte32852bc2017-02-28 00:21:45 +010035
36 switch (altif) {
37 case ALTIF_RAM:
38 addr = RAM_ADDR(offset);
39 if (addr > IRAM_ADDR + IRAM_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010040 g_dfu->state = DFU_STATE_dfuERROR;
41 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010042 return DFU_RET_STALL;
43 }
44 memcpy((void *)addr, data, len);
45 return DFU_RET_ZLP;
46 case ALTIF_FLASH:
47 addr = FLASH_ADDR(offset);
48 if (addr > IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010049 g_dfu->state = DFU_STATE_dfuERROR;
50 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010051 return DFU_RET_STALL;
52 }
53 rc = FLASHD_Write(addr, data, len);
54 if (rc != 0) {
55 /* FIXME: set error codes */
56 return DFU_RET_STALL;
57 }
58 return DFU_RET_ZLP;
59 default:
60 /* FIXME: set error codes */
61 TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif);
62 return DFU_RET_STALL;
63 }
Harald Welted8a003d2017-02-27 20:31:09 +010064}
Harald Welte32852bc2017-02-28 00:21:45 +010065
66/* incoming call-back: Host has requested to read back 'req_len' bytes
67 * starting from 'offset' of the firmware * associated with partition
68 * 'altif' */
69int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
70 uint8_t *data, unsigned int req_len)
71{
72 uint32_t addr;
73
74 printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len);
75
76 switch (altif) {
77 case ALTIF_RAM:
78 addr = RAM_ADDR(offset);
79 if (addr > IRAM_ADDR + IRAM_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010080 g_dfu->state = DFU_STATE_dfuERROR;
81 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010082 return -1;
83 }
84 if ((uint8_t *)addr + req_len > IRAM_END)
85 req_len = IRAM_END - (uint8_t *)addr;
86 memcpy(data, (void *)addr, req_len);
87 break;
88 case ALTIF_FLASH:
89 addr = FLASH_ADDR(offset);
90 if (addr > IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010091 g_dfu->state = DFU_STATE_dfuERROR;
92 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010093 return -1;
94 }
95 if ((uint8_t *)addr + req_len > IFLASH_END)
96 req_len = IFLASH_END - (uint8_t *)addr;
97 memcpy(data, (void *)addr, req_len);
98 break;
99 default:
100 TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif);
101 /* FIXME: set error codes */
102 return -1;
103 }
Harald Welteec0837c2017-03-03 01:33:24 +0100104 printf("=%u\n\r", req_len);
Harald Welte32852bc2017-02-28 00:21:45 +0100105 return req_len;
106}
107
Harald Welte14051002017-03-04 19:17:27 +0100108static int uart_has_loopback_jumper(void)
109{
110 unsigned int i;
111 const Pin uart_loopback_pins[] = {
112 {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT},
113 {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
114 };
115
116 /* Configure UART pins as I/O */
117 PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins));
118
119 for (i = 0; i < 10; i++) {
120 /* Set TxD high; abort if RxD doesn't go high either */
121 PIO_Set(&uart_loopback_pins[1]);
122 if (!PIO_Get(&uart_loopback_pins[0]))
123 return 0;
124 /* Set TxD low, abort if RxD doesn't go low either */
125 PIO_Clear(&uart_loopback_pins[1]);
126 if (PIO_Get(&uart_loopback_pins[0]))
127 return 0;
128 }
129 /* if we reached here, RxD always follows TxD and thus a
130 * loopback jumper has been placed on RxD/TxD, and we will boot
131 * into DFU unconditionally */
132 return 1;
133}
134
135/* using this function we can determine if we should enter DFU mode
136 * during boot, or if we should proceed towards the application/runtime */
137int USBDFU_OverrideEnterDFU(void)
138{
139 uint32_t *app_part = (uint32_t *)FLASH_ADDR(0);
140
141 /* If the loopback jumper is set, we enter DFU mode */
142 if (uart_has_loopback_jumper())
143 return 1;
144
145 /* if the first word of the application partition doesn't look
146 * like a stack pointer (i.e. point to RAM), enter DFU mode */
147 if ((app_part[0] < IRAM_ADDR) ||
148 ((uint8_t *)app_part[0] > IRAM_END))
149 return 1;
150
151 /* if the second word of the application partition doesn't look
152 * like a function from flash (reset vector), enter DFU mode */
153 if (((uint32_t *)app_part[1] < app_part) ||
154 ((uint8_t *)app_part[1] > IFLASH_END))
155 return 1;
156
157 return 0;
158}
Harald Welted8a003d2017-02-27 20:31:09 +0100159
160/* returns '1' in case we should break any endless loop */
161static void check_exec_dbg_cmd(void)
162{
163 int ch;
164
165 if (!UART_IsRxReady())
166 return;
167
168 ch = UART_GetChar();
169
170 //board_exec_dbg_cmd(ch);
171}
172
173/*------------------------------------------------------------------------------
174 * Main
175 *------------------------------------------------------------------------------*/
176#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
177extern int main(void)
178{
179 uint8_t isUsbConnected = 0;
180 unsigned int i = 0;
Harald Welte5b108d82017-03-06 21:51:55 +0100181 uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
Harald Welted8a003d2017-02-27 20:31:09 +0100182
Harald Welte3bbaba02017-03-06 22:47:06 +0100183#if 0
Harald Welteabba8a82017-03-06 16:58:00 +0100184 led_init();
185 led_blink(LED_GREEN, BLINK_3O_30F);
186 led_blink(LED_RED, BLINK_3O_30F);
Harald Welte3bbaba02017-03-06 22:47:06 +0100187#endif
Harald Welte45ebe452017-03-03 19:01:53 +0100188
189 /* Enable watchdog for 500ms, with no window */
190 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
191 (WDT_GetPeriod(500) << 16) | WDT_GetPeriod(500));
Harald Welted8a003d2017-02-27 20:31:09 +0100192
193 //req_ctx_init();
194
195 PIO_InitializeInterrupts(0);
196
197 EEFC_ReadUniqueID(g_unique_id);
198
Harald Welteec0837c2017-03-03 01:33:24 +0100199 printf("\n\r\n\r"
200 "=============================================================================\n\r"
201 "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r"
202 "=============================================================================\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100203 manifest_revision, manifest_board);
204
Harald Welteec0837c2017-03-03 01:33:24 +0100205 TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
206 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100207 g_unique_id[0], g_unique_id[1],
208 g_unique_id[2], g_unique_id[3]);
Harald Welte5b108d82017-03-06 21:51:55 +0100209 TRACE_INFO("Reset Cause: 0x%x\n\r", reset_cause);
210
211 /* clear g_dfu on power-up reset */
212 if (reset_cause == 0)
213 memset(g_dfu, 0, sizeof(*g_dfu));
Harald Welted8a003d2017-02-27 20:31:09 +0100214
Harald Welte8adf0ac2017-03-03 01:52:34 +0100215 board_main_top();
Harald Welted8a003d2017-02-27 20:31:09 +0100216
Harald Welteec0837c2017-03-03 01:33:24 +0100217 TRACE_INFO("USB init...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100218 USBDFU_Initialize(&dfu_descriptors);
219
220 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100221 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100222 check_exec_dbg_cmd();
Harald Welte32852bc2017-02-28 00:21:45 +0100223#if 1
Harald Welted8a003d2017-02-27 20:31:09 +0100224 if (i >= MAX_USB_ITER * 3) {
225 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100226 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100227 USBD_Disconnect();
Harald Welted8a003d2017-02-27 20:31:09 +0100228 NVIC_SystemReset();
229 }
230#endif
231 i++;
232 }
233
Harald Welteec9b5ff2017-03-02 23:18:40 +0100234 FLASHD_Initialize(BOARD_MCK, 1);
Harald Welteec0837c2017-03-03 01:33:24 +0100235 TRACE_INFO("entering main loop...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100236 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100237 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100238#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
239 const char rotor[] = { '-', '\\', '|', '/' };
240 putchar('\b');
241 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
242#endif
243 check_exec_dbg_cmd();
Harald Welte3bbaba02017-03-06 22:47:06 +0100244#if 0
245 osmo_timers_prepare();
246 osmo_timers_update();
247#endif
Harald Welted8a003d2017-02-27 20:31:09 +0100248
249 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
250
251 if (isUsbConnected) {
252 isUsbConnected = 0;
253 }
254 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100255 TRACE_INFO("USB is now configured\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100256
257 isUsbConnected = 1;
258 }
259 }
260}