blob: 32c855c6b0da6b241820bea751ffd4c922452ae5 [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 Welte32852bc2017-02-28 00:21:45 +01007#define ALTIF_RAM 0
8#define ALTIF_FLASH 1
9
Harald Welted8a003d2017-02-27 20:31:09 +010010unsigned int g_unique_id[4];
11
12/*----------------------------------------------------------------------------
13 * Callbacks
14 *----------------------------------------------------------------------------*/
15
Harald Welte32852bc2017-02-28 00:21:45 +010016#define RAM_ADDR(offset) (IRAM_ADDR + BOARD_DFU_RAM_SIZE + offset)
17#define FLASH_ADDR(offset) (IFLASH_ADDR + BOARD_DFU_BOOT_SIZE + offset)
18
19#define IFLASH_END ((uint8_t *)IFLASH_ADDR + IFLASH_SIZE)
20#define IRAM_END ((uint8_t *)IRAM_ADDR + IRAM_SIZE)
21
22/* incoming call-back: Host has transfered 'len' bytes (stored at
23 * 'data'), which we shall write to 'offset' into the partition
24 * associated with 'altif'. Guaranted to be les than
25 * BOARD_DFU_PAGE_SIZE */
26int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
27 uint8_t *data, unsigned int len)
Harald Welted8a003d2017-02-27 20:31:09 +010028{
Harald Welte32852bc2017-02-28 00:21:45 +010029 uint32_t addr;
30 int rc;
31
Harald Weltee8eea292017-03-03 00:35:51 +010032 printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
Harald Welte32852bc2017-02-28 00:21:45 +010033
34 switch (altif) {
35 case ALTIF_RAM:
36 addr = RAM_ADDR(offset);
37 if (addr > IRAM_ADDR + IRAM_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010038 g_dfu->state = DFU_STATE_dfuERROR;
39 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010040 return DFU_RET_STALL;
41 }
42 memcpy((void *)addr, data, len);
43 return DFU_RET_ZLP;
44 case ALTIF_FLASH:
45 addr = FLASH_ADDR(offset);
46 if (addr > IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010047 g_dfu->state = DFU_STATE_dfuERROR;
48 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010049 return DFU_RET_STALL;
50 }
51 rc = FLASHD_Write(addr, data, len);
52 if (rc != 0) {
53 /* FIXME: set error codes */
54 return DFU_RET_STALL;
55 }
56 return DFU_RET_ZLP;
57 default:
58 /* FIXME: set error codes */
59 TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif);
60 return DFU_RET_STALL;
61 }
Harald Welted8a003d2017-02-27 20:31:09 +010062}
Harald Welte32852bc2017-02-28 00:21:45 +010063
64/* incoming call-back: Host has requested to read back 'req_len' bytes
65 * starting from 'offset' of the firmware * associated with partition
66 * 'altif' */
67int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
68 uint8_t *data, unsigned int req_len)
69{
70 uint32_t addr;
71
72 printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len);
73
74 switch (altif) {
75 case ALTIF_RAM:
76 addr = RAM_ADDR(offset);
77 if (addr > IRAM_ADDR + IRAM_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010078 g_dfu->state = DFU_STATE_dfuERROR;
79 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010080 return -1;
81 }
82 if ((uint8_t *)addr + req_len > IRAM_END)
83 req_len = IRAM_END - (uint8_t *)addr;
84 memcpy(data, (void *)addr, req_len);
85 break;
86 case ALTIF_FLASH:
87 addr = FLASH_ADDR(offset);
88 if (addr > IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010089 g_dfu->state = DFU_STATE_dfuERROR;
90 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010091 return -1;
92 }
93 if ((uint8_t *)addr + req_len > IFLASH_END)
94 req_len = IFLASH_END - (uint8_t *)addr;
95 memcpy(data, (void *)addr, req_len);
96 break;
97 default:
98 TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif);
99 /* FIXME: set error codes */
100 return -1;
101 }
Harald Welteec0837c2017-03-03 01:33:24 +0100102 printf("=%u\n\r", req_len);
Harald Welte32852bc2017-02-28 00:21:45 +0100103 return req_len;
104}
105
Harald Welte14051002017-03-04 19:17:27 +0100106static int uart_has_loopback_jumper(void)
107{
108 unsigned int i;
109 const Pin uart_loopback_pins[] = {
110 {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT},
111 {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
112 };
113
114 /* Configure UART pins as I/O */
115 PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins));
116
117 for (i = 0; i < 10; i++) {
118 /* Set TxD high; abort if RxD doesn't go high either */
119 PIO_Set(&uart_loopback_pins[1]);
120 if (!PIO_Get(&uart_loopback_pins[0]))
121 return 0;
122 /* Set TxD low, abort if RxD doesn't go low either */
123 PIO_Clear(&uart_loopback_pins[1]);
124 if (PIO_Get(&uart_loopback_pins[0]))
125 return 0;
126 }
127 /* if we reached here, RxD always follows TxD and thus a
128 * loopback jumper has been placed on RxD/TxD, and we will boot
129 * into DFU unconditionally */
130 return 1;
131}
132
133/* using this function we can determine if we should enter DFU mode
134 * during boot, or if we should proceed towards the application/runtime */
135int USBDFU_OverrideEnterDFU(void)
136{
137 uint32_t *app_part = (uint32_t *)FLASH_ADDR(0);
138
139 /* If the loopback jumper is set, we enter DFU mode */
140 if (uart_has_loopback_jumper())
141 return 1;
142
143 /* if the first word of the application partition doesn't look
144 * like a stack pointer (i.e. point to RAM), enter DFU mode */
145 if ((app_part[0] < IRAM_ADDR) ||
146 ((uint8_t *)app_part[0] > IRAM_END))
147 return 1;
148
149 /* if the second word of the application partition doesn't look
150 * like a function from flash (reset vector), enter DFU mode */
151 if (((uint32_t *)app_part[1] < app_part) ||
152 ((uint8_t *)app_part[1] > IFLASH_END))
153 return 1;
154
155 return 0;
156}
Harald Welted8a003d2017-02-27 20:31:09 +0100157
158/* returns '1' in case we should break any endless loop */
159static void check_exec_dbg_cmd(void)
160{
161 int ch;
162
163 if (!UART_IsRxReady())
164 return;
165
166 ch = UART_GetChar();
167
168 //board_exec_dbg_cmd(ch);
169}
170
171/*------------------------------------------------------------------------------
172 * Main
173 *------------------------------------------------------------------------------*/
174#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
175extern int main(void)
176{
177 uint8_t isUsbConnected = 0;
178 unsigned int i = 0;
179
180 LED_Configure(LED_NUM_RED);
181 LED_Configure(LED_NUM_GREEN);
182 LED_Set(LED_NUM_RED);
183
Harald Welte45ebe452017-03-03 19:01:53 +0100184
185 /* Enable watchdog for 500ms, with no window */
186 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
187 (WDT_GetPeriod(500) << 16) | WDT_GetPeriod(500));
Harald Welted8a003d2017-02-27 20:31:09 +0100188
189 //req_ctx_init();
190
191 PIO_InitializeInterrupts(0);
192
193 EEFC_ReadUniqueID(g_unique_id);
194
Harald Welteec0837c2017-03-03 01:33:24 +0100195 printf("\n\r\n\r"
196 "=============================================================================\n\r"
197 "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r"
198 "=============================================================================\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100199 manifest_revision, manifest_board);
200
Harald Welteec0837c2017-03-03 01:33:24 +0100201 TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
202 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100203 g_unique_id[0], g_unique_id[1],
204 g_unique_id[2], g_unique_id[3]);
Harald Welte7214b472017-03-03 19:02:09 +0100205 TRACE_INFO("Reset Cause: 0x%x\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos);
Harald Welted8a003d2017-02-27 20:31:09 +0100206
Harald Welte8adf0ac2017-03-03 01:52:34 +0100207 board_main_top();
Harald Welted8a003d2017-02-27 20:31:09 +0100208
Harald Welteec0837c2017-03-03 01:33:24 +0100209 TRACE_INFO("USB init...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100210 USBDFU_Initialize(&dfu_descriptors);
211
212 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100213 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100214 check_exec_dbg_cmd();
Harald Welte32852bc2017-02-28 00:21:45 +0100215#if 1
Harald Welted8a003d2017-02-27 20:31:09 +0100216 if (i >= MAX_USB_ITER * 3) {
217 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100218 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100219 USBD_Disconnect();
Harald Welted8a003d2017-02-27 20:31:09 +0100220 NVIC_SystemReset();
221 }
222#endif
223 i++;
224 }
225
Harald Welteec9b5ff2017-03-02 23:18:40 +0100226 FLASHD_Initialize(BOARD_MCK, 1);
Harald Welteec0837c2017-03-03 01:33:24 +0100227 TRACE_INFO("entering main loop...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100228 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100229 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100230#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
231 const char rotor[] = { '-', '\\', '|', '/' };
232 putchar('\b');
233 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
234#endif
235 check_exec_dbg_cmd();
236 //osmo_timers_prepare();
237 //osmo_timers_update();
238
239 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
240
241 if (isUsbConnected) {
242 isUsbConnected = 0;
243 }
244 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100245 TRACE_INFO("USB is now configured\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100246 LED_Set(LED_NUM_GREEN);
247 LED_Clear(LED_NUM_RED);
248
249 isUsbConnected = 1;
250 }
251 }
252}