blob: 97f9b9129b365db1267c312f58acb0999fcfbf27 [file] [log] [blame]
Christina Quast32906bb2015-02-24 11:35:19 +01001// FIXME: Copyright license here
2/*------------------------------------------------------------------------------
3 * Headers
4 *------------------------------------------------------------------------------*/
5
6#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +01007#include "simtrace.h"
Harald Welteebbb6452016-02-29 17:57:51 +01008#include "utils.h"
9#include "req_ctx.h"
Harald Welte2819e9c2017-02-03 07:46:01 +010010#include "wwan_led.h"
Harald Welte7e5c7d52017-02-03 22:59:04 +010011#include "wwan_perst.h"
Harald Welte67322482017-02-04 14:43:41 +010012#include "boardver_adc.h"
Harald Welteb41598b2017-02-04 12:15:58 +010013#include "osmocom/core/timer.h"
Christina Quast32906bb2015-02-24 11:35:19 +010014
Harald Welte8ee15db2016-03-20 16:00:39 +010015uint32_t g_unique_id[4];
16
Christina Quast32906bb2015-02-24 11:35:19 +010017/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010018 * Internal variables
19 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010020typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010021 /* static initialization, called whether or not the usb config is active */
22 void (*configure) (void);
23 /* initialization function after the config was selected */
24 void (*init) (void);
25 /* de-initialization before selecting new config */
26 void (*exit) (void);
27 /* main loop content for given configuration */
28 void (*run) (void);
Harald Welte3bafe432016-03-20 16:43:12 +010029 /* Interrupt handler for USART1 */
30 void (*usart0_irq) (void);
31 /* Interrupt handler for USART1 */
32 void (*usart1_irq) (void);
Christina Quastfb524b92015-02-27 13:39:45 +010033} conf_func;
34
Harald Welted4c14212015-11-07 18:25:46 +010035static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010036 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010037#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010038 [CFG_NUM_SNIFF] = {
39 .configure = Sniffer_configure,
40 .init = Sniffer_init,
41 .exit = Sniffer_exit,
42 .run = Sniffer_run,
43 },
Harald Welte2fb59962016-02-28 12:34:26 +010044#endif
45#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010046 [CFG_NUM_CCID] = {
47 .configure = CCID_configure,
48 .init = CCID_init,
49 .exit = CCID_exit,
50 .run = CCID_run,
51 },
Harald Welte2fb59962016-02-28 12:34:26 +010052#endif
53#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010054 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010055 .configure = mode_cardemu_configure,
56 .init = mode_cardemu_init,
57 .exit = mode_cardemu_exit,
58 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010059 .usart0_irq = mode_cardemu_usart0_irq,
60 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010061 },
Harald Welte2fb59962016-02-28 12:34:26 +010062#endif
63#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010064 [CFG_NUM_MITM] = {
65 .configure = MITM_configure,
66 .init = MITM_init,
67 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010068 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010069 },
Harald Welte2fb59962016-02-28 12:34:26 +010070#endif
Christina Quastfb524b92015-02-27 13:39:45 +010071};
72
Christina Quastfb524b92015-02-27 13:39:45 +010073/*------------------------------------------------------------------------------
74 * Internal variables
75 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010076#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010077static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010078#elif defined(HAVE_CARDEM)
79static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
80#elif defined(HAVE_CCID)
81static volatile enum confNum simtrace_config = CFG_NUM_CCID;
82#endif
83
Harald Welte006b16d2016-12-22 21:29:10 +010084static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE;
Harald Welte6dfcf702017-01-11 22:23:13 +010085static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
Harald Welte006b16d2016-12-22 21:29:10 +010086static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP};
87static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
88static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
89
90
Harald Welte8d6a5d82015-11-07 18:27:05 +010091/*----------------------------------------------------------------------------
92 * Callbacks
93 *----------------------------------------------------------------------------*/
94
95void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
96{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010097 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
98 simtrace_config = cfgnum;
Harald Welte8d6a5d82015-11-07 18:27:05 +010099}
Christina Quast32906bb2015-02-24 11:35:19 +0100100
Harald Welte3bafe432016-03-20 16:43:12 +0100101void USART1_IrqHandler(void)
102{
103 config_func_ptrs[simtrace_config].usart1_irq();
104}
105
106void USART0_IrqHandler(void)
107{
108 config_func_ptrs[simtrace_config].usart0_irq();
109}
110
Harald Welte006b16d2016-12-22 21:29:10 +0100111static int qmod_sam3_is_12(void)
112{
113 if (PIO_Get(&pin_1234_detect) == 0)
114 return 1;
115 else
116 return 0;
117}
Harald Weltee974fbb2016-10-19 19:36:07 +0200118
Harald Welteb8713632017-01-11 23:08:35 +0100119const unsigned char __eeprom_bin[256] = {
120 0x23, 0x42, 0x17, 0x25, 0x00, 0x00, 0x9b, 0x20, 0x01, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */
121 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */
122 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */
123 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */
124 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */
125 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */
126 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */
132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */
133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */
134 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */
136};
137
Harald Welte006b16d2016-12-22 21:29:10 +0100138#include "i2c.h"
Harald Weltee974fbb2016-10-19 19:36:07 +0200139static int write_hub_eeprom(void)
140{
Harald Weltee974fbb2016-10-19 19:36:07 +0200141 const unsigned int __eeprom_bin_len = 256;
142
Harald Weltee974fbb2016-10-19 19:36:07 +0200143 int i;
144
Harald Weltee974fbb2016-10-19 19:36:07 +0200145 /* wait */
146 volatile int v;
147 /* 440ns per cycle here */
148 for (i = 0; i < 1000000; i++) {
149 v = 0;
150 }
151
Harald Welte006b16d2016-12-22 21:29:10 +0100152 TRACE_INFO("Writing EEPROM...\r\n");
Harald Weltee974fbb2016-10-19 19:36:07 +0200153 /* write the EEPROM once */
154 for (i = 0; i < 256; i++) {
155 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
156 /* if the result was negative, repeat that write */
157 if (rc < 0)
158 i--;
159 }
160
161 /* then pursue re-reading it again and again */
Harald Welte006b16d2016-12-22 21:29:10 +0100162 TRACE_INFO("Verifying EEPROM...\r\n");
Harald Weltee974fbb2016-10-19 19:36:07 +0200163 for (i = 0; i < 256; i++) {
164 int byte = eeprom_read_byte(0x50, i);
165 TRACE_INFO("0x%02x: %02x\r\n", i, byte);
166 if (byte != __eeprom_bin[i])
167 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\r\n",
168 i, __eeprom_bin[i], byte);
169 }
Harald Weltee974fbb2016-10-19 19:36:07 +0200170
171 /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is
172 * again powering us up */
173
174 return 0;
175}
176
Harald Weltef9a182d2017-01-11 22:22:16 +0100177/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100178static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100179{
180 uint32_t addr, val;
181
182 if (!UART_IsRxReady())
183 return;
184
185 int ch = UART_GetChar();
186 switch (ch) {
187 case '?':
188 printf("\t?\thelp\r\n");
189 printf("\tE\tprogram EEPROM\r\n");
190 printf("\tR\treset SAM3\r\n");
191 printf("\tO\tEnable PRTPWR_OVERRIDE\r\n");
192 printf("\to\tDisable PRTPWR_OVERRIDE\r\n");
193 printf("\tH\tRelease HUB RESET (high)\r\n");
194 printf("\th\tAssert HUB RESET (low)\r\n");
195 printf("\tw\tWrite single byte in EEPROM\r\n");
196 printf("\tr\tRead single byte from EEPROM\r\n");
197 printf("\tX\tRelease peer SAM3 from reset\r\n");
198 printf("\tx\tAssert peer SAM3 reset\r\n");
Harald Welte396354c2016-12-22 22:28:26 +0100199 printf("\tY\tRelease peer SAM3 ERASE signal\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100200 printf("\ty\tAssert peer SAM3 ERASE signal\r\n");
Harald Weltee07aed62016-12-22 22:32:15 +0100201 printf("\tU\tProceed to USB Initialization\r\n");
Harald Welte7e5c7d52017-02-03 22:59:04 +0100202 printf("\t1\tGenerate 1ms reset pulse on WWAN1\r\n");
203 printf("\t2\tGenerate 1ms reset pulse on WWAN2\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100204 break;
205 case 'E':
206 write_hub_eeprom();
207 break;
208 case 'R':
Harald Welte396354c2016-12-22 22:28:26 +0100209 printf("Asking NVIC to reset us\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100210 NVIC_SystemReset();
211 break;
212 case 'O':
Harald Welte396354c2016-12-22 22:28:26 +0100213 printf("Setting PRTPWR_OVERRIDE\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100214 PIO_Set(&pin_hubpwr_override);
215 break;
216 case 'o':
Harald Welte396354c2016-12-22 22:28:26 +0100217 printf("Clearing PRTPWR_OVERRIDE\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100218 PIO_Clear(&pin_hubpwr_override);
219 break;
220 case 'H':
Harald Welte396354c2016-12-22 22:28:26 +0100221 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100222 PIO_Clear(&pin_hub_rst);
223 break;
224 case 'h':
225 /* high level drives transistor -> HUB_RESET low */
Harald Welte396354c2016-12-22 22:28:26 +0100226 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100227 PIO_Set(&pin_hub_rst);
228 break;
229 case 'w':
230 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
231 printf("WARNING: attempting EEPROM access while HUB not in reset\r\n");
Harald Welte396354c2016-12-22 22:28:26 +0100232 printf("Please enter EEPROM offset:\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100233 UART_GetIntegerMinMax(&addr, 0, 255);
Harald Welte396354c2016-12-22 22:28:26 +0100234 printf("Please enter EEPROM value:\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100235 UART_GetIntegerMinMax(&val, 0, 255);
236 printf("Writing value 0x%02x to EEPROM offset 0x%02x\r\n", val, addr);
237 eeprom_write_byte(0x50, addr, val);
238 break;
239 case 'r':
Harald Welte396354c2016-12-22 22:28:26 +0100240 printf("Please enter EEPROM offset:\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100241 UART_GetIntegerMinMax(&addr, 0, 255);
242 printf("EEPROM[0x%02x] = 0x%02x\r\n", addr, eeprom_read_byte(0x50, addr));
243 break;
244 case 'X':
Harald Welte396354c2016-12-22 22:28:26 +0100245 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100246 PIO_Clear(&pin_peer_rst);
247 break;
248 case 'x':
Harald Welte396354c2016-12-22 22:28:26 +0100249 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100250 PIO_Set(&pin_peer_rst);
251 break;
252 case 'Y':
Harald Welte396354c2016-12-22 22:28:26 +0100253 printf("Clearing SIMTRACExx_ERASE (inactive)\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100254 PIO_Clear(&pin_peer_erase);
255 break;
256 case 'y':
Harald Welte396354c2016-12-22 22:28:26 +0100257 printf("Seetting SIMTRACExx_ERASE (active)\r\n");
Harald Welte006b16d2016-12-22 21:29:10 +0100258 PIO_Set(&pin_peer_erase);
259 break;
Harald Welte7e5c7d52017-02-03 22:59:04 +0100260 case '1':
261 printf("Resetting Modem 1 (of this SAM3)\r\n");
262 wwan_perst_do_reset(1);
263 break;
264 case '2':
265 printf("Resetting Modem 2 (of this SAM3)\r\n");
266 wwan_perst_do_reset(2);
267 break;
Harald Welte396354c2016-12-22 22:28:26 +0100268 default:
269 printf("Unknown command '%c'\r\n", ch);
270 break;
Harald Welte006b16d2016-12-22 21:29:10 +0100271 }
272}
Harald Weltee974fbb2016-10-19 19:36:07 +0200273
Christina Quast32906bb2015-02-24 11:35:19 +0100274/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200275 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100276 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100277#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
278extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100279{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100280 uint8_t isUsbConnected = 0;
281 enum confNum last_simtrace_config = simtrace_config;
282 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100283
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100284 LED_Configure(LED_NUM_RED);
285 LED_Configure(LED_NUM_GREEN);
286 LED_Set(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +0100287
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100288 /* Disable watchdog */
289 WDT_Disable(WDT);
Christina Quast32906bb2015-02-24 11:35:19 +0100290
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100291 req_ctx_init();
Harald Welteebbb6452016-02-29 17:57:51 +0100292
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100293 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100294
Harald Welte2819e9c2017-02-03 07:46:01 +0100295 wwan_led_init();
Harald Welte7e5c7d52017-02-03 22:59:04 +0100296 wwan_perst_init();
Harald Welte2819e9c2017-02-03 07:46:01 +0100297
Harald Welte8ee15db2016-03-20 16:00:39 +0100298 EEFC_ReadUniqueID(g_unique_id);
299
Harald Welte2315e6b2016-03-19 21:37:55 +0100300 printf("\r\n\r\n"
301 "=============================================================================\r\n"
302 "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\r\n"
303 "=============================================================================\r\n");
304
Harald Welte8ee15db2016-03-20 16:00:39 +0100305 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n",
306 g_unique_id[0], g_unique_id[1],
307 g_unique_id[2], g_unique_id[3]);
308
Harald Welte006b16d2016-12-22 21:29:10 +0100309 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
310 * pull-up on the input to keep SIMTRACE12 alive */
311 PIO_Configure(&pin_hubpwr_override, 1);
312 PIO_Configure(&pin_hub_rst, 1);
313 PIO_Configure(&pin_1234_detect, 1);
314 PIO_Configure(&pin_peer_rst, 1);
315 PIO_Configure(&pin_peer_erase, 1);
316 i2c_pin_init();
317
318 if (qmod_sam3_is_12()) {
319 TRACE_INFO("Detected Quad-Modem ST12\r\n");
320 } else {
321 TRACE_INFO("Detected Quad-Modem ST34\r\n");
322 }
323
Harald Welte67322482017-02-04 14:43:41 +0100324 /* Obtain the circuit board version (currently just prints voltage */
325 get_board_version_adc();
326
Harald Weltecf1c19a2016-03-20 15:18:18 +0100327 TRACE_INFO("USB init...\r\n");
Harald Weltee07aed62016-12-22 22:32:15 +0100328 SIMtrace_USB_Initialize();
329
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100330 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte006b16d2016-12-22 21:29:10 +0100331 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200332#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100333 if (i >= MAX_USB_ITER * 3) {
334 TRACE_ERROR("Resetting board (USB could "
Harald Weltecf1c19a2016-03-20 15:18:18 +0100335 "not be configured)\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100336 NVIC_SystemReset();
337 }
Harald Welte67415e32016-09-01 20:57:56 +0200338#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100339 i++;
340 }
Christina Quastde688672015-04-12 15:20:57 +0200341
Harald Welte006b16d2016-12-22 21:29:10 +0100342 TRACE_INFO("Releasing ST12_PRTPWR_OVERRIDE\r\n");
343 PIO_Clear(&pin_hubpwr_override);
344
Harald Weltecf1c19a2016-03-20 15:18:18 +0100345 TRACE_INFO("calling configure of all configurations...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100346 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
347 ++i) {
348 if (config_func_ptrs[i].configure)
349 config_func_ptrs[i].configure();
350 }
Christina Quast95d66162015-04-09 22:38:47 +0200351
Harald Weltecf1c19a2016-03-20 15:18:18 +0100352 TRACE_INFO("calling init of config %u...\r\n", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100353 config_func_ptrs[simtrace_config].init();
354 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200355
Harald Weltecf1c19a2016-03-20 15:18:18 +0100356 TRACE_INFO("entering main loop...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100357 while (1) {
Harald Welte04e37a82016-03-20 15:15:34 +0100358#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100359 const char rotor[] = { '-', '\\', '|', '/' };
360 putchar('\b');
361 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100362#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100363 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100364 osmo_timers_prepare();
365 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100366
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100367 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100368
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100369 if (isUsbConnected) {
370 isUsbConnected = 0;
371 }
372 } else if (isUsbConnected == 0) {
Harald Weltecf1c19a2016-03-20 15:18:18 +0100373 TRACE_INFO("USB is now configured\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100374 LED_Set(LED_NUM_GREEN);
375 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100376
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100377 isUsbConnected = 1;
378 }
379 if (last_simtrace_config != simtrace_config) {
380 TRACE_INFO("USB config chg %u -> %u\r\n",
381 last_simtrace_config, simtrace_config);
382 config_func_ptrs[last_simtrace_config].exit();
383 config_func_ptrs[simtrace_config].init();
384 last_simtrace_config = simtrace_config;
385 } else {
386 config_func_ptrs[simtrace_config].run();
387 }
388 }
Christina Quast32906bb2015-02-24 11:35:19 +0100389}