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 | |
| 32 | printf("dnload(altif=%u, offset=%u, len=%u\n\r", altif, offset, len); |
| 33 | |
| 34 | switch (altif) { |
| 35 | case ALTIF_RAM: |
| 36 | addr = RAM_ADDR(offset); |
| 37 | if (addr > IRAM_ADDR + IRAM_SIZE) { |
| 38 | g_dfu.state = DFU_STATE_dfuERROR; |
| 39 | g_dfu.status = DFU_STATUS_errADDRESS; |
| 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) { |
| 47 | g_dfu.state = DFU_STATE_dfuERROR; |
| 48 | g_dfu.status = DFU_STATUS_errADDRESS; |
| 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) { |
| 78 | g_dfu.state = DFU_STATE_dfuERROR; |
| 79 | g_dfu.status = DFU_STATUS_errADDRESS; |
| 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) { |
| 89 | g_dfu.state = DFU_STATE_dfuERROR; |
| 90 | g_dfu.status = DFU_STATUS_errADDRESS; |
| 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 | } |
| 102 | printf("=%u\r\n", req_len); |
| 103 | return req_len; |
| 104 | } |
| 105 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 106 | |
| 107 | /* returns '1' in case we should break any endless loop */ |
| 108 | static void check_exec_dbg_cmd(void) |
| 109 | { |
| 110 | int ch; |
| 111 | |
| 112 | if (!UART_IsRxReady()) |
| 113 | return; |
| 114 | |
| 115 | ch = UART_GetChar(); |
| 116 | |
| 117 | //board_exec_dbg_cmd(ch); |
| 118 | } |
| 119 | |
| 120 | /*------------------------------------------------------------------------------ |
| 121 | * Main |
| 122 | *------------------------------------------------------------------------------*/ |
| 123 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 124 | extern int main(void) |
| 125 | { |
| 126 | uint8_t isUsbConnected = 0; |
| 127 | unsigned int i = 0; |
| 128 | |
| 129 | LED_Configure(LED_NUM_RED); |
| 130 | LED_Configure(LED_NUM_GREEN); |
| 131 | LED_Set(LED_NUM_RED); |
| 132 | |
| 133 | /* Disable watchdog */ |
| 134 | WDT_Disable(WDT); |
| 135 | |
| 136 | //req_ctx_init(); |
| 137 | |
| 138 | PIO_InitializeInterrupts(0); |
| 139 | |
| 140 | EEFC_ReadUniqueID(g_unique_id); |
| 141 | |
| 142 | printf("\r\n\r\n" |
| 143 | "=============================================================================\r\n" |
| 144 | "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\r\n" |
| 145 | "=============================================================================\r\n", |
| 146 | manifest_revision, manifest_board); |
| 147 | |
| 148 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n", |
| 149 | g_unique_id[0], g_unique_id[1], |
| 150 | g_unique_id[2], g_unique_id[3]); |
| 151 | |
| 152 | //board_main_top(); |
| 153 | |
| 154 | TRACE_INFO("USB init...\r\n"); |
| 155 | USBDFU_Initialize(&dfu_descriptors); |
| 156 | |
| 157 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 158 | check_exec_dbg_cmd(); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 159 | #if 1 |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 160 | if (i >= MAX_USB_ITER * 3) { |
| 161 | TRACE_ERROR("Resetting board (USB could " |
| 162 | "not be configured)\r\n"); |
| 163 | NVIC_SystemReset(); |
| 164 | } |
| 165 | #endif |
| 166 | i++; |
| 167 | } |
| 168 | |
| 169 | TRACE_INFO("entering main loop...\r\n"); |
| 170 | while (1) { |
| 171 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
| 172 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 173 | putchar('\b'); |
| 174 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
| 175 | #endif |
| 176 | check_exec_dbg_cmd(); |
| 177 | //osmo_timers_prepare(); |
| 178 | //osmo_timers_update(); |
| 179 | |
| 180 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 181 | |
| 182 | if (isUsbConnected) { |
| 183 | isUsbConnected = 0; |
| 184 | } |
| 185 | } else if (isUsbConnected == 0) { |
| 186 | TRACE_INFO("USB is now configured\r\n"); |
| 187 | LED_Set(LED_NUM_GREEN); |
| 188 | LED_Clear(LED_NUM_RED); |
| 189 | |
| 190 | isUsbConnected = 1; |
| 191 | } |
| 192 | } |
| 193 | } |