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 | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 80 | static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE; |
Harald Welte | 6dfcf70 | 2017-01-11 22:23:13 +0100 | [diff] [blame] | 81 | static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 82 | static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP}; |
| 83 | static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 84 | static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 85 | |
| 86 | |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 87 | /*---------------------------------------------------------------------------- |
| 88 | * Callbacks |
| 89 | *----------------------------------------------------------------------------*/ |
| 90 | |
| 91 | void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) |
| 92 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 93 | TRACE_INFO_WP("cfgChanged%d ", cfgnum); |
| 94 | simtrace_config = cfgnum; |
Harald Welte | 8d6a5d8 | 2015-11-07 18:27:05 +0100 | [diff] [blame] | 95 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 96 | |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 97 | void USART1_IrqHandler(void) |
| 98 | { |
| 99 | config_func_ptrs[simtrace_config].usart1_irq(); |
| 100 | } |
| 101 | |
| 102 | void USART0_IrqHandler(void) |
| 103 | { |
| 104 | config_func_ptrs[simtrace_config].usart0_irq(); |
| 105 | } |
| 106 | |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 107 | static int qmod_sam3_is_12(void) |
| 108 | { |
| 109 | if (PIO_Get(&pin_1234_detect) == 0) |
| 110 | return 1; |
| 111 | else |
| 112 | return 0; |
| 113 | } |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 114 | |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 115 | #include "i2c.h" |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 116 | static int write_hub_eeprom(void) |
| 117 | { |
| 118 | const unsigned char __eeprom_bin[] = { |
| 119 | 0x23, 0x42, 0x17, 0x25, 0x00, 0x00, 0x9b, 0x20, 0x01, 0xa0, 0x00, 0x5e, |
Harald Welte | ae8a465 | 2017-01-11 22:59:09 +0100 | [diff] [blame^] | 120 | 0x32, 0x32, 0x32, 0x32, 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 121 | 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x6f, 0x00, |
| 122 | 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, |
| 123 | 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, |
| 124 | 0x20, 0x00, 0x47, 0x00, 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, |
| 125 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 126 | 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, |
| 127 | 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, |
Harald Welte | ae8a465 | 2017-01-11 22:59:09 +0100 | [diff] [blame^] | 128 | 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 129 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 130 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 131 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 132 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 133 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 134 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 135 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 136 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 137 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 138 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Harald Welte | ae8a465 | 2017-01-11 22:59:09 +0100 | [diff] [blame^] | 139 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, |
| 140 | 0x23, 0x71, 0x04, 0x00 |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 141 | }; |
| 142 | const unsigned int __eeprom_bin_len = 256; |
| 143 | |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 144 | int i; |
| 145 | |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 146 | /* wait */ |
| 147 | volatile int v; |
| 148 | /* 440ns per cycle here */ |
| 149 | for (i = 0; i < 1000000; i++) { |
| 150 | v = 0; |
| 151 | } |
| 152 | |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 153 | TRACE_INFO("Writing EEPROM...\r\n"); |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 154 | /* write the EEPROM once */ |
| 155 | for (i = 0; i < 256; i++) { |
| 156 | int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]); |
| 157 | /* if the result was negative, repeat that write */ |
| 158 | if (rc < 0) |
| 159 | i--; |
| 160 | } |
| 161 | |
| 162 | /* then pursue re-reading it again and again */ |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 163 | TRACE_INFO("Verifying EEPROM...\r\n"); |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 164 | for (i = 0; i < 256; i++) { |
| 165 | int byte = eeprom_read_byte(0x50, i); |
| 166 | TRACE_INFO("0x%02x: %02x\r\n", i, byte); |
| 167 | if (byte != __eeprom_bin[i]) |
| 168 | TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\r\n", |
| 169 | i, __eeprom_bin[i], byte); |
| 170 | } |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 171 | |
| 172 | /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is |
| 173 | * again powering us up */ |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 178 | /* returns '1' in case we should break any endless loop */ |
| 179 | static int check_exec_dbg_cmd(void) |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 180 | { |
| 181 | uint32_t addr, val; |
| 182 | |
| 183 | if (!UART_IsRxReady()) |
| 184 | return; |
| 185 | |
| 186 | int ch = UART_GetChar(); |
| 187 | switch (ch) { |
| 188 | case '?': |
| 189 | printf("\t?\thelp\r\n"); |
| 190 | printf("\tE\tprogram EEPROM\r\n"); |
| 191 | printf("\tR\treset SAM3\r\n"); |
| 192 | printf("\tO\tEnable PRTPWR_OVERRIDE\r\n"); |
| 193 | printf("\to\tDisable PRTPWR_OVERRIDE\r\n"); |
| 194 | printf("\tH\tRelease HUB RESET (high)\r\n"); |
| 195 | printf("\th\tAssert HUB RESET (low)\r\n"); |
| 196 | printf("\tw\tWrite single byte in EEPROM\r\n"); |
| 197 | printf("\tr\tRead single byte from EEPROM\r\n"); |
| 198 | printf("\tX\tRelease peer SAM3 from reset\r\n"); |
| 199 | printf("\tx\tAssert peer SAM3 reset\r\n"); |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 200 | printf("\tY\tRelease peer SAM3 ERASE signal\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 201 | printf("\ty\tAssert peer SAM3 ERASE signal\r\n"); |
Harald Welte | e07aed6 | 2016-12-22 22:32:15 +0100 | [diff] [blame] | 202 | printf("\tU\tProceed to USB Initialization\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 203 | break; |
| 204 | case 'E': |
| 205 | write_hub_eeprom(); |
| 206 | break; |
| 207 | case 'R': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 208 | printf("Asking NVIC to reset us\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 209 | NVIC_SystemReset(); |
| 210 | break; |
| 211 | case 'O': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 212 | printf("Setting PRTPWR_OVERRIDE\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 213 | PIO_Set(&pin_hubpwr_override); |
| 214 | break; |
| 215 | case 'o': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 216 | printf("Clearing PRTPWR_OVERRIDE\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 217 | PIO_Clear(&pin_hubpwr_override); |
| 218 | break; |
| 219 | case 'H': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 220 | printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 221 | PIO_Clear(&pin_hub_rst); |
| 222 | break; |
| 223 | case 'h': |
| 224 | /* high level drives transistor -> HUB_RESET low */ |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 225 | printf("Asserting _HUB_RESET -> HUB_RESET low (active)\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 226 | PIO_Set(&pin_hub_rst); |
| 227 | break; |
| 228 | case 'w': |
| 229 | if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0) |
| 230 | printf("WARNING: attempting EEPROM access while HUB not in reset\r\n"); |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 231 | printf("Please enter EEPROM offset:\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 232 | UART_GetIntegerMinMax(&addr, 0, 255); |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 233 | printf("Please enter EEPROM value:\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 234 | UART_GetIntegerMinMax(&val, 0, 255); |
| 235 | printf("Writing value 0x%02x to EEPROM offset 0x%02x\r\n", val, addr); |
| 236 | eeprom_write_byte(0x50, addr, val); |
| 237 | break; |
| 238 | case 'r': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 239 | printf("Please enter EEPROM offset:\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 240 | UART_GetIntegerMinMax(&addr, 0, 255); |
| 241 | printf("EEPROM[0x%02x] = 0x%02x\r\n", addr, eeprom_read_byte(0x50, addr)); |
| 242 | break; |
| 243 | case 'X': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 244 | printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 245 | PIO_Clear(&pin_peer_rst); |
| 246 | break; |
| 247 | case 'x': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 248 | printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 249 | PIO_Set(&pin_peer_rst); |
| 250 | break; |
| 251 | case 'Y': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 252 | printf("Clearing SIMTRACExx_ERASE (inactive)\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 253 | PIO_Clear(&pin_peer_erase); |
| 254 | break; |
| 255 | case 'y': |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 256 | printf("Seetting SIMTRACExx_ERASE (active)\r\n"); |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 257 | PIO_Set(&pin_peer_erase); |
| 258 | break; |
Harald Welte | e07aed6 | 2016-12-22 22:32:15 +0100 | [diff] [blame] | 259 | case 'U': |
| 260 | printf("Proceeding to USB init\r\n"); |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 261 | return 1; |
Harald Welte | 396354c | 2016-12-22 22:28:26 +0100 | [diff] [blame] | 262 | default: |
| 263 | printf("Unknown command '%c'\r\n", ch); |
| 264 | break; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 265 | } |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 266 | return 0; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 267 | } |
Harald Welte | e974fbb | 2016-10-19 19:36:07 +0200 | [diff] [blame] | 268 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 269 | /*------------------------------------------------------------------------------ |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 270 | * Main |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 271 | *------------------------------------------------------------------------------*/ |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 272 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 273 | extern int main(void) |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 274 | { |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 275 | uint8_t isUsbConnected = 0; |
| 276 | enum confNum last_simtrace_config = simtrace_config; |
| 277 | unsigned int i = 0; |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 278 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 279 | LED_Configure(LED_NUM_RED); |
| 280 | LED_Configure(LED_NUM_GREEN); |
| 281 | LED_Set(LED_NUM_RED); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 282 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 283 | /* Disable watchdog */ |
| 284 | WDT_Disable(WDT); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 285 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 286 | req_ctx_init(); |
Harald Welte | ebbb645 | 2016-02-29 17:57:51 +0100 | [diff] [blame] | 287 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 288 | PIO_InitializeInterrupts(0); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 289 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 290 | EEFC_ReadUniqueID(g_unique_id); |
| 291 | |
Harald Welte | 2315e6b | 2016-03-19 21:37:55 +0100 | [diff] [blame] | 292 | printf("\r\n\r\n" |
| 293 | "=============================================================================\r\n" |
| 294 | "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\r\n" |
| 295 | "=============================================================================\r\n"); |
| 296 | |
Harald Welte | 8ee15db | 2016-03-20 16:00:39 +0100 | [diff] [blame] | 297 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n", |
| 298 | g_unique_id[0], g_unique_id[1], |
| 299 | g_unique_id[2], g_unique_id[3]); |
| 300 | |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 301 | /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal |
| 302 | * pull-up on the input to keep SIMTRACE12 alive */ |
| 303 | PIO_Configure(&pin_hubpwr_override, 1); |
| 304 | PIO_Configure(&pin_hub_rst, 1); |
| 305 | PIO_Configure(&pin_1234_detect, 1); |
| 306 | PIO_Configure(&pin_peer_rst, 1); |
| 307 | PIO_Configure(&pin_peer_erase, 1); |
| 308 | i2c_pin_init(); |
| 309 | |
| 310 | if (qmod_sam3_is_12()) { |
| 311 | TRACE_INFO("Detected Quad-Modem ST12\r\n"); |
| 312 | } else { |
| 313 | TRACE_INFO("Detected Quad-Modem ST34\r\n"); |
| 314 | } |
| 315 | |
| 316 | while (1) { |
Harald Welte | f9a182d | 2017-01-11 22:22:16 +0100 | [diff] [blame] | 317 | if (check_exec_dbg_cmd() == 1) |
| 318 | break; |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 319 | } |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 320 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 321 | TRACE_INFO("USB init...\r\n"); |
Harald Welte | e07aed6 | 2016-12-22 22:32:15 +0100 | [diff] [blame] | 322 | SIMtrace_USB_Initialize(); |
| 323 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 324 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 325 | check_exec_dbg_cmd(); |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 326 | #if 0 |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 327 | if (i >= MAX_USB_ITER * 3) { |
| 328 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 329 | "not be configured)\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 330 | NVIC_SystemReset(); |
| 331 | } |
Harald Welte | 67415e3 | 2016-09-01 20:57:56 +0200 | [diff] [blame] | 332 | #endif |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 333 | i++; |
| 334 | } |
Christina Quast | de68867 | 2015-04-12 15:20:57 +0200 | [diff] [blame] | 335 | |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 336 | TRACE_INFO("Releasing ST12_PRTPWR_OVERRIDE\r\n"); |
| 337 | PIO_Clear(&pin_hubpwr_override); |
| 338 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 339 | TRACE_INFO("calling configure of all configurations...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 340 | for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]); |
| 341 | ++i) { |
| 342 | if (config_func_ptrs[i].configure) |
| 343 | config_func_ptrs[i].configure(); |
| 344 | } |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 345 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 346 | TRACE_INFO("calling init of config %u...\r\n", simtrace_config); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 347 | config_func_ptrs[simtrace_config].init(); |
| 348 | last_simtrace_config = simtrace_config; |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 349 | |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 350 | TRACE_INFO("entering main loop...\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 351 | while (1) { |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 352 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 353 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 354 | putchar('\b'); |
| 355 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
Harald Welte | 04e37a8 | 2016-03-20 15:15:34 +0100 | [diff] [blame] | 356 | #endif |
Harald Welte | 006b16d | 2016-12-22 21:29:10 +0100 | [diff] [blame] | 357 | check_exec_dbg_cmd(); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 358 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 359 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 360 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 361 | if (isUsbConnected) { |
| 362 | isUsbConnected = 0; |
| 363 | } |
| 364 | } else if (isUsbConnected == 0) { |
Harald Welte | cf1c19a | 2016-03-20 15:18:18 +0100 | [diff] [blame] | 365 | TRACE_INFO("USB is now configured\r\n"); |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 366 | LED_Set(LED_NUM_GREEN); |
| 367 | LED_Clear(LED_NUM_RED); |
Christina Quast | 1161b27 | 2015-02-25 14:15:57 +0100 | [diff] [blame] | 368 | |
Harald Welte | 7dd3dfd | 2016-03-03 12:32:04 +0100 | [diff] [blame] | 369 | isUsbConnected = 1; |
| 370 | } |
| 371 | if (last_simtrace_config != simtrace_config) { |
| 372 | TRACE_INFO("USB config chg %u -> %u\r\n", |
| 373 | last_simtrace_config, simtrace_config); |
| 374 | config_func_ptrs[last_simtrace_config].exit(); |
| 375 | config_func_ptrs[simtrace_config].init(); |
| 376 | last_simtrace_config = simtrace_config; |
| 377 | } else { |
| 378 | config_func_ptrs[simtrace_config].run(); |
| 379 | } |
| 380 | } |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 381 | } |