Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 1 | #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 Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 7 | #define ALTIF_RAM 0 |
| 8 | #define ALTIF_FLASH 1 |
| 9 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 10 | unsigned int g_unique_id[4]; |
| 11 | |
| 12 | /*---------------------------------------------------------------------------- |
| 13 | * Callbacks |
| 14 | *----------------------------------------------------------------------------*/ |
| 15 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 16 | #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 */ |
| 26 | int USBDFU_handle_dnload(uint8_t altif, unsigned int offset, |
| 27 | uint8_t *data, unsigned int len) |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 28 | { |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 29 | uint32_t addr; |
| 30 | int rc; |
| 31 | |
Harald Welte | e8eea29 | 2017-03-03 00:35:51 +0100 | [diff] [blame] | 32 | printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 33 | |
| 34 | switch (altif) { |
| 35 | case ALTIF_RAM: |
| 36 | addr = RAM_ADDR(offset); |
| 37 | if (addr > IRAM_ADDR + IRAM_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 38 | g_dfu->state = DFU_STATE_dfuERROR; |
| 39 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 40 | 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 Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 47 | g_dfu->state = DFU_STATE_dfuERROR; |
| 48 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 49 | 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 Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 62 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 63 | |
| 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' */ |
| 67 | int 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 Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 78 | g_dfu->state = DFU_STATE_dfuERROR; |
| 79 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 80 | 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 Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 89 | g_dfu->state = DFU_STATE_dfuERROR; |
| 90 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 91 | 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 Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 102 | printf("=%u\n\r", req_len); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 103 | return req_len; |
| 104 | } |
| 105 | |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame^] | 106 | static 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 */ |
| 135 | int 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 Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 157 | |
| 158 | /* returns '1' in case we should break any endless loop */ |
| 159 | static 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 |
| 175 | extern 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 Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 184 | |
| 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 Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 188 | |
| 189 | //req_ctx_init(); |
| 190 | |
| 191 | PIO_InitializeInterrupts(0); |
| 192 | |
| 193 | EEFC_ReadUniqueID(g_unique_id); |
| 194 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 195 | 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 Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 199 | manifest_revision, manifest_board); |
| 200 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 201 | 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 Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 203 | g_unique_id[0], g_unique_id[1], |
| 204 | g_unique_id[2], g_unique_id[3]); |
Harald Welte | 7214b47 | 2017-03-03 19:02:09 +0100 | [diff] [blame] | 205 | TRACE_INFO("Reset Cause: 0x%x\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 206 | |
Harald Welte | 8adf0ac | 2017-03-03 01:52:34 +0100 | [diff] [blame] | 207 | board_main_top(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 208 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 209 | TRACE_INFO("USB init...\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 210 | USBDFU_Initialize(&dfu_descriptors); |
| 211 | |
| 212 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 213 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 214 | check_exec_dbg_cmd(); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 215 | #if 1 |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 216 | if (i >= MAX_USB_ITER * 3) { |
| 217 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 218 | "not be configured)\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 219 | USBD_Disconnect(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 220 | NVIC_SystemReset(); |
| 221 | } |
| 222 | #endif |
| 223 | i++; |
| 224 | } |
| 225 | |
Harald Welte | ec9b5ff | 2017-03-02 23:18:40 +0100 | [diff] [blame] | 226 | FLASHD_Initialize(BOARD_MCK, 1); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 227 | TRACE_INFO("entering main loop...\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 228 | while (1) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 229 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 230 | #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 Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 245 | TRACE_INFO("USB is now configured\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 246 | LED_Set(LED_NUM_GREEN); |
| 247 | LED_Clear(LED_NUM_RED); |
| 248 | |
| 249 | isUsbConnected = 1; |
| 250 | } |
| 251 | } |
| 252 | } |