Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* SIMtrace 2 firmware USB DFU bootloader |
| 2 | * |
| 3 | * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de> |
| 4 | * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 20 | #include "board.h" |
| 21 | #include "utils.h" |
| 22 | #include "usb/device/dfu/dfu.h" |
| 23 | #include "usb/common/dfu/usb_dfu.h" |
| 24 | #include "manifest.h" |
| 25 | |
Harald Welte | 3bbaba0 | 2017-03-06 22:47:06 +0100 | [diff] [blame] | 26 | #include <osmocom/core/timer.h> |
| 27 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 28 | #define ALTIF_RAM 0 |
| 29 | #define ALTIF_FLASH 1 |
| 30 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 31 | unsigned int g_unique_id[4]; |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 32 | /* remember if the watchdog has been configured in the main loop so we can kick it in the ISR */ |
| 33 | static bool watchdog_configured = false; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 34 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 35 | /* There is not enough space in the 16 KiB DFU bootloader to include led.h functions */ |
| 36 | #ifdef PINS_LEDS |
| 37 | /** LED pin configurations */ |
| 38 | static const Pin pinsLeds[] = { PINS_LEDS } ; |
| 39 | #endif |
| 40 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 41 | /*---------------------------------------------------------------------------- |
| 42 | * Callbacks |
| 43 | *----------------------------------------------------------------------------*/ |
| 44 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 45 | #define RAM_ADDR(offset) (IRAM_ADDR + BOARD_DFU_RAM_SIZE + offset) |
| 46 | #define FLASH_ADDR(offset) (IFLASH_ADDR + BOARD_DFU_BOOT_SIZE + offset) |
| 47 | |
| 48 | #define IFLASH_END ((uint8_t *)IFLASH_ADDR + IFLASH_SIZE) |
| 49 | #define IRAM_END ((uint8_t *)IRAM_ADDR + IRAM_SIZE) |
| 50 | |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 51 | /* incoming call-back: Host has transferred 'len' bytes (stored at |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 52 | * 'data'), which we shall write to 'offset' into the partition |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 53 | * associated with 'altif'. Guaranted to be less than |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 54 | * BOARD_DFU_PAGE_SIZE */ |
| 55 | int USBDFU_handle_dnload(uint8_t altif, unsigned int offset, |
| 56 | uint8_t *data, unsigned int len) |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 57 | { |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 58 | uint32_t addr; |
Harald Welte | 1cfc261 | 2018-06-29 21:07:41 +0200 | [diff] [blame] | 59 | unsigned int i; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 60 | int rc; |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 61 | /* address of the last allocated variable on the stack */ |
| 62 | uint32_t stack_addr = (uint32_t)&rc; |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 63 | /* kick the dog to have enough time to flash */ |
| 64 | if (watchdog_configured) { |
| 65 | WDT_Restart(WDT); |
| 66 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 67 | |
Harald Welte | e8eea29 | 2017-03-03 00:35:51 +0100 | [diff] [blame] | 68 | 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] | 69 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 70 | #ifdef PINS_LEDS |
| 71 | PIO_Clear(&pinsLeds[LED_NUM_RED]); |
| 72 | #endif |
| 73 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 74 | switch (altif) { |
| 75 | case ALTIF_RAM: |
| 76 | addr = RAM_ADDR(offset); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 77 | if (addr < IRAM_ADDR || addr + len >= IRAM_ADDR + IRAM_SIZE || addr + len >= stack_addr) { |
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; |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 80 | rc = DFU_RET_STALL; |
| 81 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 82 | } |
| 83 | memcpy((void *)addr, data, len); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 84 | rc = DFU_RET_ZLP; |
| 85 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 86 | case ALTIF_FLASH: |
| 87 | addr = FLASH_ADDR(offset); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 88 | if (addr < IFLASH_ADDR || addr + len >= 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; |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 91 | rc = DFU_RET_STALL; |
| 92 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 93 | } |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 94 | rc = FLASHD_Unlock(addr, addr + len, 0, 0); |
| 95 | if (rc != 0) { |
| 96 | TRACE_ERROR("DFU download flash unlock failed\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 97 | rc = DFU_RET_STALL; |
| 98 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 99 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 100 | rc = FLASHD_Write(addr, data, len); |
| 101 | if (rc != 0) { |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 102 | TRACE_ERROR("DFU download flash erase failed\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 103 | rc = DFU_RET_STALL; |
| 104 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 105 | } |
Harald Welte | 1cfc261 | 2018-06-29 21:07:41 +0200 | [diff] [blame] | 106 | for (i = 0; i < len; i++) { |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 107 | if (((uint8_t*)addr)[i]!=data[i]) { |
| 108 | TRACE_ERROR("DFU download flash data written not correct\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 109 | rc = DFU_RET_STALL; |
| 110 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 111 | } |
| 112 | } |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 113 | rc = DFU_RET_ZLP; |
| 114 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 115 | default: |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 116 | TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 117 | rc = DFU_RET_STALL; |
| 118 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 119 | } |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 120 | |
| 121 | #ifdef PINS_LEDS |
| 122 | PIO_Set(&pinsLeds[LED_NUM_RED]); |
| 123 | #endif |
| 124 | |
| 125 | return rc; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 126 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 127 | |
| 128 | /* incoming call-back: Host has requested to read back 'req_len' bytes |
| 129 | * starting from 'offset' of the firmware * associated with partition |
| 130 | * 'altif' */ |
| 131 | int USBDFU_handle_upload(uint8_t altif, unsigned int offset, |
| 132 | uint8_t *data, unsigned int req_len) |
| 133 | { |
| 134 | uint32_t addr; |
| 135 | |
| 136 | printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len); |
| 137 | |
| 138 | switch (altif) { |
| 139 | case ALTIF_RAM: |
| 140 | addr = RAM_ADDR(offset); |
| 141 | if (addr > IRAM_ADDR + IRAM_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 142 | g_dfu->state = DFU_STATE_dfuERROR; |
| 143 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | if ((uint8_t *)addr + req_len > IRAM_END) |
| 147 | req_len = IRAM_END - (uint8_t *)addr; |
| 148 | memcpy(data, (void *)addr, req_len); |
| 149 | break; |
| 150 | case ALTIF_FLASH: |
| 151 | addr = FLASH_ADDR(offset); |
| 152 | if (addr > IFLASH_ADDR + IFLASH_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 153 | g_dfu->state = DFU_STATE_dfuERROR; |
| 154 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | if ((uint8_t *)addr + req_len > IFLASH_END) |
| 158 | req_len = IFLASH_END - (uint8_t *)addr; |
| 159 | memcpy(data, (void *)addr, req_len); |
| 160 | break; |
| 161 | default: |
| 162 | TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif); |
| 163 | /* FIXME: set error codes */ |
| 164 | return -1; |
| 165 | } |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 166 | printf("=%u\n\r", req_len); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 167 | return req_len; |
| 168 | } |
| 169 | |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 170 | /* can be overridden by board specific code, e.g. by pushbutton */ |
| 171 | WEAK int board_override_enter_dfu(void) |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 172 | { |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 173 | return 0; |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* using this function we can determine if we should enter DFU mode |
| 177 | * during boot, or if we should proceed towards the application/runtime */ |
| 178 | int USBDFU_OverrideEnterDFU(void) |
| 179 | { |
| 180 | uint32_t *app_part = (uint32_t *)FLASH_ADDR(0); |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 181 | /* at the first call we are before the text segment has been relocated, |
| 182 | * so g_dfu is not initialized yet */ |
| 183 | g_dfu = &_g_dfu; |
| 184 | if (USB_DFU_MAGIC == g_dfu->magic) { |
| 185 | return 1; |
| 186 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 187 | |
| 188 | /* If the loopback jumper is set, we enter DFU mode */ |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 189 | if (board_override_enter_dfu()) { |
| 190 | return 2; |
| 191 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 192 | |
| 193 | /* if the first word of the application partition doesn't look |
| 194 | * like a stack pointer (i.e. point to RAM), enter DFU mode */ |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 195 | if ((app_part[0] < IRAM_ADDR) || ((uint8_t *)app_part[0] > IRAM_END)) { |
| 196 | return 3; |
| 197 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 198 | |
| 199 | /* if the second word of the application partition doesn't look |
| 200 | * like a function from flash (reset vector), enter DFU mode */ |
| 201 | if (((uint32_t *)app_part[1] < app_part) || |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 202 | ((uint8_t *)app_part[1] > IFLASH_END)) { |
| 203 | return 4; |
| 204 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 208 | |
| 209 | /* returns '1' in case we should break any endless loop */ |
| 210 | static void check_exec_dbg_cmd(void) |
| 211 | { |
| 212 | int ch; |
| 213 | |
| 214 | if (!UART_IsRxReady()) |
| 215 | return; |
| 216 | |
| 217 | ch = UART_GetChar(); |
| 218 | |
| 219 | //board_exec_dbg_cmd(ch); |
| 220 | } |
| 221 | |
| 222 | /*------------------------------------------------------------------------------ |
| 223 | * Main |
| 224 | *------------------------------------------------------------------------------*/ |
| 225 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 226 | extern int main(void) |
| 227 | { |
| 228 | uint8_t isUsbConnected = 0; |
| 229 | unsigned int i = 0; |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 230 | uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 231 | |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 232 | /* Enable watchdog for 2000ms, with no window */ |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 233 | WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT | |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 234 | (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000)); |
| 235 | watchdog_configured = true; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 236 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 237 | #ifdef PINS_LEDS |
| 238 | /* Configure LED */ |
| 239 | PIO_Configure(pinsLeds, sizeof(pinsLeds)); |
| 240 | PIO_Set(&pinsLeds[LED_NUM_RED]); |
| 241 | PIO_Clear(&pinsLeds[LED_NUM_GREEN]); |
| 242 | #endif |
| 243 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 244 | PIO_InitializeInterrupts(0); |
| 245 | |
| 246 | EEFC_ReadUniqueID(g_unique_id); |
| 247 | |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 248 | printf("\n\r\n\r" |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 249 | "=============================================================================\n\r" |
| 250 | "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r" |
| 251 | "=============================================================================\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 252 | manifest_revision, manifest_board); |
| 253 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 254 | TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID); |
| 255 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 256 | g_unique_id[0], g_unique_id[1], |
| 257 | g_unique_id[2], g_unique_id[3]); |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 258 | TRACE_INFO("Reset Cause: 0x%x\n\r", reset_cause); |
| 259 | |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 260 | #if (TRACE_LEVEL >= TRACE_LEVEL_INFO) |
| 261 | /* Find out why we are in the DFU bootloader, and not the main application */ |
| 262 | TRACE_INFO("DFU bootloader start reason: "); |
| 263 | switch (USBDFU_OverrideEnterDFU()) { |
| 264 | case 0: |
| 265 | /* 0 normally means that there is no override, but we are in the bootloader, |
| 266 | * thus the first check in board_cstartup_gnu did return something else than 0. |
| 267 | * this can only be g_dfu->magic which is erased when the segment are |
| 268 | * relocated, which happens in board_cstartup_gnu just after USBDFU_OverrideEnterDFU. |
| 269 | * no static variable can be used to store this case since this will also be overwritten |
| 270 | */ |
| 271 | case 1: |
| 272 | TRACE_INFO_WP("DFU switch requested by main application\n\r"); |
| 273 | break; |
| 274 | case 2: |
| 275 | TRACE_INFO_WP("bootloader forced (button pressed or jumper set)\n\r"); |
| 276 | break; |
| 277 | case 3: |
| 278 | TRACE_INFO_WP("stack pointer (first application word) does no point in RAM\n\r"); |
| 279 | break; |
| 280 | case 4: // the is no reason |
| 281 | TRACE_INFO_WP("reset vector (second application word) does no point in flash\n\r"); |
| 282 | break; |
| 283 | default: |
| 284 | TRACE_INFO_WP("unknown\n\r"); |
| 285 | break; |
| 286 | } |
| 287 | #endif |
| 288 | |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 289 | /* clear g_dfu on power-up reset */ |
| 290 | if (reset_cause == 0) |
| 291 | memset(g_dfu, 0, sizeof(*g_dfu)); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 292 | |
Harald Welte | 8adf0ac | 2017-03-03 01:52:34 +0100 | [diff] [blame] | 293 | board_main_top(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 294 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 295 | TRACE_INFO("USB init...\n\r"); |
Kévin Redon | f5869d4 | 2018-06-17 22:31:21 +0200 | [diff] [blame] | 296 | /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */ |
| 297 | const Pin usb_dp_pullup = PIN_USB_PULLUP; |
| 298 | PIO_Configure(&usb_dp_pullup, 1); |
| 299 | PIO_Set(&usb_dp_pullup); |
| 300 | mdelay(15); |
| 301 | PIO_Clear(&usb_dp_pullup); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 302 | USBDFU_Initialize(&dfu_descriptors); |
| 303 | |
| 304 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 305 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 306 | check_exec_dbg_cmd(); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 307 | #if 1 |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 308 | if (i >= MAX_USB_ITER * 3) { |
| 309 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 310 | "not be configured)\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 311 | USBD_Disconnect(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 312 | NVIC_SystemReset(); |
| 313 | } |
| 314 | #endif |
| 315 | i++; |
| 316 | } |
| 317 | |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 318 | /* Initialize the flash to be able to write it, using the IAP ROM code */ |
Harald Welte | ec9b5ff | 2017-03-02 23:18:40 +0100 | [diff] [blame] | 319 | FLASHD_Initialize(BOARD_MCK, 1); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 320 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 321 | TRACE_INFO("entering main loop...\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 322 | while (1) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 323 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 324 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
| 325 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 326 | putchar('\b'); |
| 327 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
| 328 | #endif |
| 329 | check_exec_dbg_cmd(); |
Harald Welte | 3bbaba0 | 2017-03-06 22:47:06 +0100 | [diff] [blame] | 330 | #if 0 |
| 331 | osmo_timers_prepare(); |
| 332 | osmo_timers_update(); |
| 333 | #endif |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 334 | |
| 335 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 336 | |
| 337 | if (isUsbConnected) { |
| 338 | isUsbConnected = 0; |
| 339 | } |
| 340 | } else if (isUsbConnected == 0) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 341 | TRACE_INFO("USB is now configured\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 342 | |
| 343 | isUsbConnected = 1; |
| 344 | } |
| 345 | } |
| 346 | } |