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> |
Kévin Redon | 9b36787 | 2019-02-07 17:52:08 +0100 | [diff] [blame] | 4 | * (C) 2018-2019 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 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" |
Kévin Redon | ff3d849 | 2018-08-06 17:57:20 +0200 | [diff] [blame] | 25 | #include "USBD_HAL.h" |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 26 | |
Harald Welte | 3bbaba0 | 2017-03-06 22:47:06 +0100 | [diff] [blame] | 27 | #include <osmocom/core/timer.h> |
| 28 | |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 29 | /* USB alternate interface index used to identify which partition to flash */ |
| 30 | /** USB alternate interface index indicating RAM partition */ |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 31 | #define ALTIF_RAM 0 |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 32 | /** USB alternate interface index indicating flash partition */ |
| 33 | #if defined(ENVIRONMENT_flash) |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 34 | #define ALTIF_FLASH 1 |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 35 | #elif defined(ENVIRONMENT_dfu) |
| 36 | #define ALTIF_FLASH 2 |
| 37 | #endif |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 38 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 39 | unsigned int g_unique_id[4]; |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 40 | /* remember if the watchdog has been configured in the main loop so we can kick it in the ISR */ |
| 41 | static bool watchdog_configured = false; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 42 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 43 | /* There is not enough space in the 16 KiB DFU bootloader to include led.h functions */ |
| 44 | #ifdef PINS_LEDS |
| 45 | /** LED pin configurations */ |
| 46 | static const Pin pinsLeds[] = { PINS_LEDS } ; |
| 47 | #endif |
| 48 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 49 | /*---------------------------------------------------------------------------- |
| 50 | * Callbacks |
| 51 | *----------------------------------------------------------------------------*/ |
| 52 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 53 | #define RAM_ADDR(offset) (IRAM_ADDR + BOARD_DFU_RAM_SIZE + offset) |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 54 | #if defined(ENVIRONMENT_flash) |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 55 | #define FLASH_ADDR(offset) (IFLASH_ADDR + BOARD_DFU_BOOT_SIZE + offset) |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 56 | #elif defined(ENVIRONMENT_dfu) |
| 57 | #define FLASH_ADDR(offset) (IFLASH_ADDR + offset) |
| 58 | #endif |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 59 | |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 60 | #define IRAM_END ((uint8_t *)IRAM_ADDR + IRAM_SIZE) |
| 61 | #if defined(ENVIRONMENT_flash) |
| 62 | #define IFLASH_END ((uint8_t *)IFLASH_ADDR + IFLASH_SIZE) |
| 63 | #elif defined(ENVIRONMENT_dfu) |
| 64 | #define IFLASH_END ((uint8_t *)IFLASH_ADDR + BOARD_DFU_BOOT_SIZE) |
| 65 | #endif |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 66 | |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 67 | /* incoming call-back: Host has transferred 'len' bytes (stored at |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 68 | * 'data'), which we shall write to 'offset' into the partition |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 69 | * associated with 'altif'. Guaranted to be less than |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 70 | * BOARD_DFU_PAGE_SIZE */ |
| 71 | int USBDFU_handle_dnload(uint8_t altif, unsigned int offset, |
| 72 | uint8_t *data, unsigned int len) |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 73 | { |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 74 | uint32_t addr; |
Harald Welte | 1cfc261 | 2018-06-29 21:07:41 +0200 | [diff] [blame] | 75 | unsigned int i; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 76 | int rc; |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 77 | /* address of the last allocated variable on the stack */ |
| 78 | uint32_t stack_addr = (uint32_t)&rc; |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 79 | /* kick the dog to have enough time to flash */ |
| 80 | if (watchdog_configured) { |
| 81 | WDT_Restart(WDT); |
| 82 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 83 | |
Kévin Redon | 98fbf23 | 2019-02-07 17:55:24 +0100 | [diff] [blame] | 84 | #if TRACE_LEVEL >= TRACE_LEVEL_INFO |
| 85 | TRACE_INFO("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len); |
| 86 | #else |
| 87 | printf("DL off=%u\n\r", offset); |
| 88 | #endif |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 89 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 90 | #ifdef PINS_LEDS |
| 91 | PIO_Clear(&pinsLeds[LED_NUM_RED]); |
| 92 | #endif |
| 93 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 94 | switch (altif) { |
| 95 | case ALTIF_RAM: |
| 96 | addr = RAM_ADDR(offset); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 97 | 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] | 98 | g_dfu->state = DFU_STATE_dfuERROR; |
| 99 | g_dfu->status = DFU_STATUS_errADDRESS; |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 100 | rc = DFU_RET_STALL; |
| 101 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 102 | } |
| 103 | memcpy((void *)addr, data, len); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 104 | rc = DFU_RET_ZLP; |
| 105 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 106 | case ALTIF_FLASH: |
| 107 | addr = FLASH_ADDR(offset); |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 108 | #if defined(ENVIRONMENT_flash) |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 109 | if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + IFLASH_SIZE) { |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 110 | #elif defined(ENVIRONMENT_dfu) |
| 111 | if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + BOARD_DFU_BOOT_SIZE) { |
| 112 | #endif |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 113 | g_dfu->state = DFU_STATE_dfuERROR; |
| 114 | g_dfu->status = DFU_STATUS_errADDRESS; |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 115 | rc = DFU_RET_STALL; |
| 116 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 117 | } |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 118 | rc = FLASHD_Unlock(addr, addr + len, 0, 0); |
| 119 | if (rc != 0) { |
| 120 | TRACE_ERROR("DFU download flash unlock failed\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 121 | rc = DFU_RET_STALL; |
| 122 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 123 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 124 | rc = FLASHD_Write(addr, data, len); |
| 125 | if (rc != 0) { |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 126 | TRACE_ERROR("DFU download flash erase failed\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 127 | rc = DFU_RET_STALL; |
| 128 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 129 | } |
Harald Welte | 1cfc261 | 2018-06-29 21:07:41 +0200 | [diff] [blame] | 130 | for (i = 0; i < len; i++) { |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 131 | if (((uint8_t*)addr)[i]!=data[i]) { |
| 132 | TRACE_ERROR("DFU download flash data written not correct\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 133 | rc = DFU_RET_STALL; |
| 134 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 135 | } |
| 136 | } |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 137 | rc = DFU_RET_ZLP; |
| 138 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 139 | default: |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 140 | TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 141 | rc = DFU_RET_STALL; |
| 142 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 143 | } |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 144 | |
| 145 | #ifdef PINS_LEDS |
| 146 | PIO_Set(&pinsLeds[LED_NUM_RED]); |
| 147 | #endif |
| 148 | |
| 149 | return rc; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 150 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 151 | |
| 152 | /* incoming call-back: Host has requested to read back 'req_len' bytes |
| 153 | * starting from 'offset' of the firmware * associated with partition |
| 154 | * 'altif' */ |
| 155 | int USBDFU_handle_upload(uint8_t altif, unsigned int offset, |
| 156 | uint8_t *data, unsigned int req_len) |
| 157 | { |
| 158 | uint32_t addr; |
| 159 | |
| 160 | printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len); |
| 161 | |
| 162 | switch (altif) { |
| 163 | case ALTIF_RAM: |
| 164 | addr = RAM_ADDR(offset); |
| 165 | if (addr > IRAM_ADDR + IRAM_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 166 | g_dfu->state = DFU_STATE_dfuERROR; |
| 167 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 168 | return -1; |
| 169 | } |
| 170 | if ((uint8_t *)addr + req_len > IRAM_END) |
| 171 | req_len = IRAM_END - (uint8_t *)addr; |
| 172 | memcpy(data, (void *)addr, req_len); |
| 173 | break; |
| 174 | case ALTIF_FLASH: |
| 175 | addr = FLASH_ADDR(offset); |
| 176 | if (addr > IFLASH_ADDR + IFLASH_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 177 | g_dfu->state = DFU_STATE_dfuERROR; |
| 178 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 179 | return -1; |
| 180 | } |
| 181 | if ((uint8_t *)addr + req_len > IFLASH_END) |
| 182 | req_len = IFLASH_END - (uint8_t *)addr; |
| 183 | memcpy(data, (void *)addr, req_len); |
| 184 | break; |
| 185 | default: |
| 186 | TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif); |
| 187 | /* FIXME: set error codes */ |
| 188 | return -1; |
| 189 | } |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 190 | printf("=%u\n\r", req_len); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 191 | return req_len; |
| 192 | } |
| 193 | |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 194 | /* can be overridden by board specific code, e.g. by pushbutton */ |
| 195 | WEAK int board_override_enter_dfu(void) |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 196 | { |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 197 | return 0; |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* using this function we can determine if we should enter DFU mode |
| 201 | * during boot, or if we should proceed towards the application/runtime */ |
| 202 | int USBDFU_OverrideEnterDFU(void) |
| 203 | { |
| 204 | uint32_t *app_part = (uint32_t *)FLASH_ADDR(0); |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 205 | /* at the first call we are before the text segment has been relocated, |
| 206 | * so g_dfu is not initialized yet */ |
| 207 | g_dfu = &_g_dfu; |
| 208 | if (USB_DFU_MAGIC == g_dfu->magic) { |
| 209 | return 1; |
| 210 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 211 | |
| 212 | /* If the loopback jumper is set, we enter DFU mode */ |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 213 | if (board_override_enter_dfu()) { |
| 214 | return 2; |
| 215 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 216 | |
| 217 | /* if the first word of the application partition doesn't look |
| 218 | * 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] | 219 | if ((app_part[0] < IRAM_ADDR) || ((uint8_t *)app_part[0] > IRAM_END)) { |
| 220 | return 3; |
| 221 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 222 | |
| 223 | /* if the second word of the application partition doesn't look |
| 224 | * like a function from flash (reset vector), enter DFU mode */ |
| 225 | if (((uint32_t *)app_part[1] < app_part) || |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 226 | ((uint8_t *)app_part[1] > IFLASH_END)) { |
| 227 | return 4; |
| 228 | } |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 232 | |
| 233 | /* returns '1' in case we should break any endless loop */ |
| 234 | static void check_exec_dbg_cmd(void) |
| 235 | { |
| 236 | int ch; |
| 237 | |
| 238 | if (!UART_IsRxReady()) |
| 239 | return; |
| 240 | |
| 241 | ch = UART_GetChar(); |
| 242 | |
| 243 | //board_exec_dbg_cmd(ch); |
| 244 | } |
| 245 | |
Harald Welte | 3907085 | 2020-08-16 15:51:14 +0200 | [diff] [blame] | 246 | /* print a horizontal line of '=' characters; Doing this in a loop vs. using a 'const' |
| 247 | * string saves us ~60 bytes of executable size (matters particularly for DFU loader) */ |
| 248 | static void print_line(void) |
| 249 | { |
| 250 | int i; |
| 251 | for (i = 0; i < 78; i++) |
| 252 | fputc('=', stdout); |
| 253 | fputc('\n', stdout); |
| 254 | fputc('\r', stdout); |
| 255 | } |
| 256 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 257 | /*------------------------------------------------------------------------------ |
| 258 | * Main |
| 259 | *------------------------------------------------------------------------------*/ |
| 260 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 261 | extern int main(void) |
| 262 | { |
| 263 | uint8_t isUsbConnected = 0; |
| 264 | unsigned int i = 0; |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 265 | 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] | 266 | |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 267 | /* Enable watchdog for 2000ms, with no window */ |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 268 | 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] | 269 | (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000)); |
| 270 | watchdog_configured = true; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 271 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 272 | #ifdef PINS_LEDS |
| 273 | /* Configure LED */ |
| 274 | PIO_Configure(pinsLeds, sizeof(pinsLeds)); |
| 275 | PIO_Set(&pinsLeds[LED_NUM_RED]); |
| 276 | PIO_Clear(&pinsLeds[LED_NUM_GREEN]); |
| 277 | #endif |
| 278 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 279 | EEFC_ReadUniqueID(g_unique_id); |
| 280 | |
Harald Welte | 3907085 | 2020-08-16 15:51:14 +0200 | [diff] [blame] | 281 | printf("\n\r\n\r"); |
| 282 | print_line(); |
| 283 | printf("DFU bootloader %s for board %s\n\r" |
| 284 | "(C) 2010-2017 by Harald Welte, 2018-2019 by Kevin Redon\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 285 | manifest_revision, manifest_board); |
Harald Welte | 3907085 | 2020-08-16 15:51:14 +0200 | [diff] [blame] | 286 | print_line(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 287 | |
Kévin Redon | 75a5f22 | 2019-12-11 16:44:02 +0100 | [diff] [blame] | 288 | #if (TRACE_LEVEL >= TRACE_LEVEL_INFO) |
| 289 | TRACE_INFO("Chip ID: 0x%08lx (Ext 0x%08lx)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 290 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 291 | g_unique_id[0], g_unique_id[1], |
| 292 | g_unique_id[2], g_unique_id[3]); |
Kévin Redon | 75a5f22 | 2019-12-11 16:44:02 +0100 | [diff] [blame] | 293 | static const char* reset_causes[] = { |
| 294 | "general reset (first power-up reset)", |
| 295 | "backup reset (return from backup mode)", |
| 296 | "watchdog reset (watchdog fault occurred)", |
| 297 | "software reset (processor reset required by the software)", |
| 298 | "user reset (NRST pin detected low)", |
| 299 | }; |
| 300 | if (reset_cause < ARRAY_SIZE(reset_causes)) { |
| 301 | TRACE_INFO("Reset Cause: %s\n\r", reset_causes[reset_cause]); |
| 302 | } else { |
| 303 | TRACE_INFO("Reset Cause: 0x%lx\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos); |
| 304 | } |
| 305 | #endif |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 306 | |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 307 | #if (TRACE_LEVEL >= TRACE_LEVEL_INFO) |
| 308 | /* Find out why we are in the DFU bootloader, and not the main application */ |
| 309 | TRACE_INFO("DFU bootloader start reason: "); |
| 310 | switch (USBDFU_OverrideEnterDFU()) { |
| 311 | case 0: |
Kévin Redon | b6e2f0f | 2019-12-11 17:04:21 +0100 | [diff] [blame] | 312 | if (SCB->VTOR < IFLASH_ADDR + BOARD_DFU_BOOT_SIZE) { |
| 313 | TRACE_INFO_WP("unknown\n\r"); |
| 314 | } else { |
| 315 | TRACE_INFO_WP("DFU is the main application\n\r"); |
| 316 | } |
| 317 | break; |
Kévin Redon | 9918c28 | 2018-07-07 15:22:16 +0200 | [diff] [blame] | 318 | case 1: |
| 319 | TRACE_INFO_WP("DFU switch requested by main application\n\r"); |
| 320 | break; |
| 321 | case 2: |
| 322 | TRACE_INFO_WP("bootloader forced (button pressed or jumper set)\n\r"); |
| 323 | break; |
| 324 | case 3: |
| 325 | TRACE_INFO_WP("stack pointer (first application word) does no point in RAM\n\r"); |
| 326 | break; |
| 327 | case 4: // the is no reason |
| 328 | TRACE_INFO_WP("reset vector (second application word) does no point in flash\n\r"); |
| 329 | break; |
| 330 | default: |
| 331 | TRACE_INFO_WP("unknown\n\r"); |
| 332 | break; |
| 333 | } |
| 334 | #endif |
| 335 | |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 336 | /* clear g_dfu on power-up reset */ |
| 337 | if (reset_cause == 0) |
| 338 | memset(g_dfu, 0, sizeof(*g_dfu)); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 339 | |
Harald Welte | 8adf0ac | 2017-03-03 01:52:34 +0100 | [diff] [blame] | 340 | board_main_top(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 341 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 342 | TRACE_INFO("USB init...\n\r"); |
Kévin Redon | f5869d4 | 2018-06-17 22:31:21 +0200 | [diff] [blame] | 343 | /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */ |
Kévin Redon | ed3ceec | 2019-12-03 15:29:33 +0100 | [diff] [blame] | 344 | USBD_Disconnect(); |
Kévin Redon | ff3d849 | 2018-08-06 17:57:20 +0200 | [diff] [blame] | 345 | #ifdef PIN_USB_PULLUP |
Kévin Redon | f5869d4 | 2018-06-17 22:31:21 +0200 | [diff] [blame] | 346 | const Pin usb_dp_pullup = PIN_USB_PULLUP; |
| 347 | PIO_Configure(&usb_dp_pullup, 1); |
| 348 | PIO_Set(&usb_dp_pullup); |
Kévin Redon | ff3d849 | 2018-08-06 17:57:20 +0200 | [diff] [blame] | 349 | #endif |
Kévin Redon | ed3ceec | 2019-12-03 15:29:33 +0100 | [diff] [blame] | 350 | mdelay(50); |
Kévin Redon | ff3d849 | 2018-08-06 17:57:20 +0200 | [diff] [blame] | 351 | #ifdef PIN_USB_PULLUP |
Kévin Redon | f5869d4 | 2018-06-17 22:31:21 +0200 | [diff] [blame] | 352 | PIO_Clear(&usb_dp_pullup); |
Kévin Redon | ff3d849 | 2018-08-06 17:57:20 +0200 | [diff] [blame] | 353 | #endif |
Kévin Redon | ff3d849 | 2018-08-06 17:57:20 +0200 | [diff] [blame] | 354 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 355 | USBDFU_Initialize(&dfu_descriptors); |
| 356 | |
| 357 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 358 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 359 | check_exec_dbg_cmd(); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 360 | #if 1 |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 361 | if (i >= MAX_USB_ITER * 3) { |
Kévin Redon | d70836f | 2019-12-03 15:36:58 +0100 | [diff] [blame] | 362 | TRACE_ERROR("Resetting board (USB could not be configured)\n\r"); |
| 363 | g_dfu->magic = USB_DFU_MAGIC; // start the bootloader after reboot |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 364 | USBD_Disconnect(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 365 | NVIC_SystemReset(); |
| 366 | } |
| 367 | #endif |
| 368 | i++; |
| 369 | } |
| 370 | |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 371 | /* 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] | 372 | FLASHD_Initialize(BOARD_MCK, 1); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 373 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 374 | TRACE_INFO("entering main loop...\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 375 | while (1) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 376 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 377 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
| 378 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 379 | putchar('\b'); |
| 380 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
| 381 | #endif |
| 382 | check_exec_dbg_cmd(); |
Harald Welte | 3bbaba0 | 2017-03-06 22:47:06 +0100 | [diff] [blame] | 383 | #if 0 |
| 384 | osmo_timers_prepare(); |
| 385 | osmo_timers_update(); |
| 386 | #endif |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 387 | |
| 388 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 389 | |
| 390 | if (isUsbConnected) { |
| 391 | isUsbConnected = 0; |
| 392 | } |
| 393 | } else if (isUsbConnected == 0) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 394 | TRACE_INFO("USB is now configured\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 395 | |
| 396 | isUsbConnected = 1; |
| 397 | } |
| 398 | } |
| 399 | } |