Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 1 | // FIXME: Copyright license here |
| 2 | /*------------------------------------------------------------------------------ |
| 3 | * Headers |
| 4 | *------------------------------------------------------------------------------*/ |
| 5 | |
| 6 | #include "board.h" |
Harald Welte | 1605564 | 2016-03-03 11:02:45 +0100 | [diff] [blame] | 7 | #include "simtrace.h" |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 8 | #include "utils.h" |
| 9 | #include "req_ctx.h" |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 10 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 11 | uint32_t g_unique_id[4]; |
| 12 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 13 | /*------------------------------------------------------------------------------ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 14 | * Internal variables |
| 15 | *------------------------------------------------------------------------------*/ |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 16 | typedef struct { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 17 | /* static initialization, called whether or not the usb config is active */ |
| 18 | void (*configure) (void); |
| 19 | /* initialization function after the config was selected */ |
| 20 | void (*init) (void); |
| 21 | /* de-initialization before selecting new config */ |
| 22 | void (*exit) (void); |
| 23 | /* main loop content for given configuration */ |
| 24 | void (*run) (void); |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 25 | /* Interrupt handler for USART1 */ |
| 26 | void (*usart0_irq) (void); |
| 27 | /* Interrupt handler for USART1 */ |
| 28 | void (*usart1_irq) (void); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 29 | } conf_func; |
| 30 | |
Harald Welte | d4c1421 | 2015-11-07 18:25:46 +0100 | [diff] [blame] | 31 | static const conf_func config_func_ptrs[] = { |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 32 | /* array slot 0 is empty, usb configs start at 1 */ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 33 | #ifdef HAVE_SNIFFER |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 34 | [CFG_NUM_SNIFF] = { |
| 35 | .configure = Sniffer_configure, |
| 36 | .init = Sniffer_init, |
| 37 | .exit = Sniffer_exit, |
| 38 | .run = Sniffer_run, |
| 39 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 40 | #endif |
| 41 | #ifdef HAVE_CCID |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 42 | [CFG_NUM_CCID] = { |
| 43 | .configure = CCID_configure, |
| 44 | .init = CCID_init, |
| 45 | .exit = CCID_exit, |
| 46 | .run = CCID_run, |
| 47 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 48 | #endif |
| 49 | #ifdef HAVE_CARDEM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 50 | [CFG_NUM_PHONE] = { |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 51 | .configure = mode_cardemu_configure, |
| 52 | .init = mode_cardemu_init, |
| 53 | .exit = mode_cardemu_exit, |
| 54 | .run = mode_cardemu_run, |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 55 | .usart0_irq = mode_cardemu_usart0_irq, |
| 56 | .usart1_irq = mode_cardemu_usart1_irq, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 57 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 58 | #endif |
| 59 | #ifdef HAVE_MITM |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 60 | [CFG_NUM_MITM] = { |
| 61 | .configure = MITM_configure, |
| 62 | .init = MITM_init, |
| 63 | .exit = MITM_exit, |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 64 | .run = MITM_run, |
Harald Welte | fefd571 | 2015-11-07 18:19:11 +0100 | [diff] [blame] | 65 | }, |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 66 | #endif |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 69 | /*------------------------------------------------------------------------------ |
| 70 | * Internal variables |
| 71 | *------------------------------------------------------------------------------*/ |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 72 | #if defined(HAVE_SNIFFER) |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 73 | static volatile enum confNum simtrace_config = CFG_NUM_SNIFF; |
Harald Welte | 2fb5996 | 2016-02-28 12:34:26 +0100 | [diff] [blame] | 74 | #elif defined(HAVE_CARDEM) |
| 75 | static volatile enum confNum simtrace_config = CFG_NUM_PHONE; |
| 76 | #elif defined(HAVE_CCID) |
| 77 | static volatile enum confNum simtrace_config = CFG_NUM_CCID; |
| 78 | #endif |
| 79 | |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 80 | /*---------------------------------------------------------------------------- |
| 81 | * Callbacks |
| 82 | *----------------------------------------------------------------------------*/ |
| 83 | |
| 84 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 85 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 86 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 87 | simtrace_config = cfgnum; |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 88 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 89 | |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 90 | void USART1_IrqHandler(void) |
| 91 | { |
| 92 | config_func_ptrs[simtrace_config].usart1_irq(); |
| 93 | } |
| 94 | |
| 95 | void USART0_IrqHandler(void) |
| 96 | { |
| 97 | config_func_ptrs[simtrace_config].usart0_irq(); |
| 98 | } |
| 99 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 100 | #include "i2c.h" |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 101 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 102 | * Main |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 103 | *------------------------------------------------------------------------------*/ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 104 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 105 | extern int main(void) |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 106 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 107 | uint8_t isUsbConnected = 0; |
| 108 | enum confNum last_simtrace_config = simtrace_config; |
| 109 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 110 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 111 | LED_Configure(LED_NUM_RED); |
| 112 | LED_Configure(LED_NUM_GREEN); |
| 113 | LED_Set(LED_NUM_RED); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 114 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 115 | /* Disable watchdog */ |
| 116 | WDT_Disable(WDT); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 117 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 118 | req_ctx_init(); |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 119 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 120 | PIO_InitializeInterrupts(0); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 121 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 122 | SIMtrace_USB_Initialize(); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 123 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 124 | EEFC_ReadUniqueID(g_unique_id); |
| 125 | |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 126 | printf("\r\n\r\n" |
| 127 | "=============================================================================\r\n" |
| 128 | "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\r\n" |
| 129 | "=============================================================================\r\n"); |
| 130 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 131 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n", |
| 132 | g_unique_id[0], g_unique_id[1], |
| 133 | g_unique_id[2], g_unique_id[3]); |
| 134 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 135 | #if 1 |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame^] | 136 | |
| 137 | static const unsigned char __eeprom_bin[] = { |
| 138 | 0x23, 0x42, 0x17, 0x25, 0x00, 0x00, 0x9b, 0x20, 0x01, 0xa0, 0x00, 0x5e, |
| 139 | 0x01, 0x32, 0x01, 0x32, 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, |
| 140 | 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x6f, 0x00, |
| 141 | 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, |
| 142 | 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, |
| 143 | 0x20, 0x00, 0x47, 0x00, 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, |
| 144 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 145 | 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, |
| 146 | 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, |
| 147 | 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 148 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 149 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 150 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 151 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 152 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 153 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 154 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 155 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 156 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 157 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 158 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 159 | 0x00, 0x00, 0x00, 0x00 |
| 160 | }; |
| 161 | static const unsigned int __eeprom_bin_len = 256; |
| 162 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 163 | static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}; |
| 164 | PIO_Configure(&pin_hub_rst, 1); |
| 165 | i2c_pin_init(); |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame^] | 166 | |
| 167 | /* wait */ |
| 168 | volatile int v; |
| 169 | /* 440ns per cycle here */ |
| 170 | for (i = 0; i < 1000000; i++) { |
| 171 | v = 0; |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 172 | } |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame^] | 173 | |
| 174 | /* write the EEPROM once */ |
| 175 | for (i = 0; i < 256; i++) { |
| 176 | int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]); |
| 177 | /* if the result was negative, repeat that write */ |
| 178 | if (rc < 0) |
| 179 | i--; |
| 180 | } |
| 181 | |
| 182 | /* then pursue re-reading it again and again */ |
| 183 | for (i = 0; i < 256; i++) { |
| 184 | int byte = eeprom_read_byte(0x50, i); |
| 185 | TRACE_INFO("0x%02x: %02x\r\n", i, byte); |
| 186 | if (byte != __eeprom_bin[i]) |
| 187 | TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\r\n", |
| 188 | i, __eeprom_bin[i], byte); |
| 189 | } |
| 190 | PIO_Clear(&pin_hub_rst); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 191 | #endif |
| 192 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 193 | TRACE_INFO("USB init...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 194 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame^] | 195 | #if 0 |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 196 | if (i >= MAX_USB_ITER * 3) { |
| 197 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 198 | "not be configured)\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 199 | NVIC_SystemReset(); |
| 200 | } |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame^] | 201 | #endif |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 202 | i++; |
| 203 | } |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 204 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 205 | TRACE_INFO("calling configure of all configurations...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 206 | for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]); |
| 207 | ++i) { |
| 208 | if (config_func_ptrs[i].configure) |
| 209 | config_func_ptrs[i].configure(); |
| 210 | } |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 211 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 212 | TRACE_INFO("calling init of config %u...\r\n", simtrace_config); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 213 | config_func_ptrs[simtrace_config].init(); |
| 214 | last_simtrace_config = simtrace_config; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 215 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 216 | TRACE_INFO("entering main loop...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 217 | while (1) { |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 218 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 219 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 220 | putchar('\b'); |
| 221 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 222 | #endif |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 223 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 224 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 225 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 226 | if (isUsbConnected) { |
| 227 | isUsbConnected = 0; |
| 228 | } |
| 229 | } else if (isUsbConnected == 0) { |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 230 | TRACE_INFO("USB is now configured\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 231 | LED_Set(LED_NUM_GREEN); |
| 232 | LED_Clear(LED_NUM_RED); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 233 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 234 | isUsbConnected = 1; |
| 235 | } |
| 236 | if (last_simtrace_config != simtrace_config) { |
| 237 | TRACE_INFO("USB config chg %u -> %u\r\n", |
| 238 | last_simtrace_config, simtrace_config); |
| 239 | config_func_ptrs[last_simtrace_config].exit(); |
| 240 | config_func_ptrs[simtrace_config].init(); |
| 241 | last_simtrace_config = simtrace_config; |
| 242 | } else { |
| 243 | config_func_ptrs[simtrace_config].run(); |
| 244 | } |
| 245 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 246 | } |