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" |
Harald Welte | b41598b | 2017-02-04 12:15:58 +0100 | [diff] [blame] | 9 | #include "osmocom/core/timer.h" |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 10 | |
Harald Welte | d525231 | 2017-02-27 20:34:20 +0100 | [diff] [blame] | 11 | unsigned int g_unique_id[4]; |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 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 | { |
Harald Welte | 2afd57f | 2017-11-28 22:52:56 +0100 | [diff] [blame] | 92 | if (config_func_ptrs[simtrace_config].usart1_irq) |
| 93 | config_func_ptrs[simtrace_config].usart1_irq(); |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void USART0_IrqHandler(void) |
| 97 | { |
Harald Welte | 2afd57f | 2017-11-28 22:52:56 +0100 | [diff] [blame] | 98 | if (config_func_ptrs[simtrace_config].usart0_irq) |
| 99 | config_func_ptrs[simtrace_config].usart0_irq(); |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 102 | /* returns '1' in case we should break any endless loop */ |
Harald Welte | af61479 | 2017-01-12 18:59:41 +0100 | [diff] [blame] | 103 | static void check_exec_dbg_cmd(void) |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 104 | { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 105 | int ch; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 106 | |
| 107 | if (!UART_IsRxReady()) |
| 108 | return; |
| 109 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 110 | ch = UART_GetChar(); |
| 111 | |
| 112 | board_exec_dbg_cmd(ch); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 113 | } |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 114 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 115 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 116 | * Main |
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 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 119 | extern int main(void) |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 120 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 121 | uint8_t isUsbConnected = 0; |
| 122 | enum confNum last_simtrace_config = simtrace_config; |
| 123 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 124 | |
Harald Welte | abba8a8 | 2017-03-06 16:58:00 +0100 | [diff] [blame] | 125 | led_init(); |
| 126 | led_blink(LED_RED, BLINK_3O_5F); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 127 | |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 128 | /* Enable watchdog for 500ms, with no window */ |
| 129 | WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT | |
| 130 | (WDT_GetPeriod(500) << 16) | WDT_GetPeriod(500)); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 131 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 132 | PIO_InitializeInterrupts(0); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 133 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 134 | EEFC_ReadUniqueID(g_unique_id); |
| 135 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 136 | printf("\n\r\n\r" |
| 137 | "=============================================================================\n\r" |
| 138 | "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r" |
| 139 | "=============================================================================\n\r"); |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 140 | |
Harald Welte | 7214b47 | 2017-03-03 19:02:09 +0100 | [diff] [blame] | 141 | TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 142 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r", |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 143 | g_unique_id[0], g_unique_id[1], |
| 144 | g_unique_id[2], g_unique_id[3]); |
Harald Welte | 7214b47 | 2017-03-03 19:02:09 +0100 | [diff] [blame] | 145 | TRACE_INFO("Reset Cause: 0x%x\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos); |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 146 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 147 | board_main_top(); |
Harald Welte | 6732248 | 2017-02-04 14:43:41 +0100 | [diff] [blame] | 148 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 149 | TRACE_INFO("USB init...\n\r"); |
Harald Welte | e07aed6 | 2016-12-22 22:32:15 +0100 | [diff] [blame] | 150 | SIMtrace_USB_Initialize(); |
| 151 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 152 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 153 | WDT_Restart(WDT); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 154 | check_exec_dbg_cmd(); |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 155 | #if 0 |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 156 | if (i >= MAX_USB_ITER * 3) { |
| 157 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 158 | "not be configured)\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 159 | USBD_Disconnect(); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 160 | NVIC_SystemReset(); |
| 161 | } |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 162 | #endif |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 163 | i++; |
| 164 | } |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 165 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 166 | TRACE_INFO("calling configure of all configurations...\n\r"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 167 | for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]); |
| 168 | ++i) { |
| 169 | if (config_func_ptrs[i].configure) |
| 170 | config_func_ptrs[i].configure(); |
| 171 | } |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 172 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 173 | TRACE_INFO("calling init of config %u...\n\r", simtrace_config); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 174 | config_func_ptrs[simtrace_config].init(); |
| 175 | last_simtrace_config = simtrace_config; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 176 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 177 | TRACE_INFO("entering main loop...\n\r"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 178 | while (1) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 179 | WDT_Restart(WDT); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 180 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 181 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 182 | putchar('\b'); |
| 183 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 184 | #endif |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 185 | check_exec_dbg_cmd(); |
Harald Welte | 987f59a | 2017-02-04 12:34:35 +0100 | [diff] [blame] | 186 | osmo_timers_prepare(); |
| 187 | osmo_timers_update(); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 188 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 189 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 190 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 191 | if (isUsbConnected) { |
| 192 | isUsbConnected = 0; |
| 193 | } |
| 194 | } else if (isUsbConnected == 0) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 195 | TRACE_INFO("USB is now configured\n\r"); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 196 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 197 | isUsbConnected = 1; |
| 198 | } |
| 199 | if (last_simtrace_config != simtrace_config) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 200 | TRACE_INFO("USB config chg %u -> %u\n\r", |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 201 | last_simtrace_config, simtrace_config); |
| 202 | config_func_ptrs[last_simtrace_config].exit(); |
| 203 | config_func_ptrs[simtrace_config].init(); |
| 204 | last_simtrace_config = simtrace_config; |
| 205 | } else { |
| 206 | config_func_ptrs[simtrace_config].run(); |
| 207 | } |
| 208 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 209 | } |