blob: 7660294898bb7fb610d31233056c064bb5a85165 [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 Welteb41598b2017-02-04 12:15:58 +010010#include "osmocom/core/timer.h"
Christina Quast32906bb2015-02-24 11:35:19 +010011
Harald Welted5252312017-02-27 20:34:20 +010012unsigned int g_unique_id[4];
Harald Welte8ee15db2016-03-20 16:00:39 +010013
Christina Quast32906bb2015-02-24 11:35:19 +010014/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010015 * Internal variables
16 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010017typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010018 /* static initialization, called whether or not the usb config is active */
19 void (*configure) (void);
20 /* initialization function after the config was selected */
21 void (*init) (void);
22 /* de-initialization before selecting new config */
23 void (*exit) (void);
24 /* main loop content for given configuration */
25 void (*run) (void);
Harald Welte3bafe432016-03-20 16:43:12 +010026 /* Interrupt handler for USART1 */
27 void (*usart0_irq) (void);
28 /* Interrupt handler for USART1 */
29 void (*usart1_irq) (void);
Christina Quastfb524b92015-02-27 13:39:45 +010030} conf_func;
31
Harald Welted4c14212015-11-07 18:25:46 +010032static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010033 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010034#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010035 [CFG_NUM_SNIFF] = {
36 .configure = Sniffer_configure,
37 .init = Sniffer_init,
38 .exit = Sniffer_exit,
39 .run = Sniffer_run,
40 },
Harald Welte2fb59962016-02-28 12:34:26 +010041#endif
42#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010043 [CFG_NUM_CCID] = {
44 .configure = CCID_configure,
45 .init = CCID_init,
46 .exit = CCID_exit,
47 .run = CCID_run,
48 },
Harald Welte2fb59962016-02-28 12:34:26 +010049#endif
50#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010051 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010052 .configure = mode_cardemu_configure,
53 .init = mode_cardemu_init,
54 .exit = mode_cardemu_exit,
55 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010056 .usart0_irq = mode_cardemu_usart0_irq,
57 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010058 },
Harald Welte2fb59962016-02-28 12:34:26 +010059#endif
60#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010061 [CFG_NUM_MITM] = {
62 .configure = MITM_configure,
63 .init = MITM_init,
64 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010065 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010066 },
Harald Welte2fb59962016-02-28 12:34:26 +010067#endif
Christina Quastfb524b92015-02-27 13:39:45 +010068};
69
Christina Quastfb524b92015-02-27 13:39:45 +010070/*------------------------------------------------------------------------------
71 * Internal variables
72 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010073#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010074static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010075#elif defined(HAVE_CARDEM)
76static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
77#elif defined(HAVE_CCID)
78static volatile enum confNum simtrace_config = CFG_NUM_CCID;
79#endif
80
Harald Welte8d6a5d82015-11-07 18:27:05 +010081/*----------------------------------------------------------------------------
82 * Callbacks
83 *----------------------------------------------------------------------------*/
84
85void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
86{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010087 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
88 simtrace_config = cfgnum;
Harald Welte8d6a5d82015-11-07 18:27:05 +010089}
Christina Quast32906bb2015-02-24 11:35:19 +010090
Harald Welte3bafe432016-03-20 16:43:12 +010091void USART1_IrqHandler(void)
92{
93 config_func_ptrs[simtrace_config].usart1_irq();
94}
95
96void USART0_IrqHandler(void)
97{
98 config_func_ptrs[simtrace_config].usart0_irq();
99}
100
Harald Weltef9a182d2017-01-11 22:22:16 +0100101/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100102static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100103{
Harald Welteaa3b8672017-02-10 19:21:10 +0100104 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100105
106 if (!UART_IsRxReady())
107 return;
108
Harald Welteaa3b8672017-02-10 19:21:10 +0100109 ch = UART_GetChar();
110
111 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100112}
Harald Weltee974fbb2016-10-19 19:36:07 +0200113
Christina Quast32906bb2015-02-24 11:35:19 +0100114/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200115 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100116 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100117#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
118extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100119{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100120 uint8_t isUsbConnected = 0;
121 enum confNum last_simtrace_config = simtrace_config;
122 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100123
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100124 LED_Configure(LED_NUM_RED);
125 LED_Configure(LED_NUM_GREEN);
126 LED_Set(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +0100127
Harald Welte45ebe452017-03-03 19:01:53 +0100128 /* 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 Quast32906bb2015-02-24 11:35:19 +0100131
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100132 req_ctx_init();
Harald Welteebbb6452016-02-29 17:57:51 +0100133
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100134 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100135
Harald Welte8ee15db2016-03-20 16:00:39 +0100136 EEFC_ReadUniqueID(g_unique_id);
137
Harald Welteec0837c2017-03-03 01:33:24 +0100138 printf("\n\r\n\r"
139 "=============================================================================\n\r"
140 "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r"
141 "=============================================================================\n\r");
Harald Welte2315e6b2016-03-19 21:37:55 +0100142
Harald Welteec0837c2017-03-03 01:33:24 +0100143 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welte8ee15db2016-03-20 16:00:39 +0100144 g_unique_id[0], g_unique_id[1],
145 g_unique_id[2], g_unique_id[3]);
146
Harald Welteaa3b8672017-02-10 19:21:10 +0100147 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100148
Harald Welteec0837c2017-03-03 01:33:24 +0100149 TRACE_INFO("USB init...\n\r");
Harald Weltee07aed62016-12-22 22:32:15 +0100150 SIMtrace_USB_Initialize();
151
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100152 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100153 WDT_Restart(WDT);
Harald Welte006b16d2016-12-22 21:29:10 +0100154 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200155#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100156 if (i >= MAX_USB_ITER * 3) {
157 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100158 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100159 USBD_Disconnect();
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100160 NVIC_SystemReset();
161 }
Harald Welte67415e32016-09-01 20:57:56 +0200162#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100163 i++;
164 }
Christina Quastde688672015-04-12 15:20:57 +0200165
Harald Welteec0837c2017-03-03 01:33:24 +0100166 TRACE_INFO("calling configure of all configurations...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100167 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 Quast95d66162015-04-09 22:38:47 +0200172
Harald Welteec0837c2017-03-03 01:33:24 +0100173 TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100174 config_func_ptrs[simtrace_config].init();
175 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200176
Harald Welteec0837c2017-03-03 01:33:24 +0100177 TRACE_INFO("entering main loop...\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100178 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100179 WDT_Restart(WDT);
Harald Welte04e37a82016-03-20 15:15:34 +0100180#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100181 const char rotor[] = { '-', '\\', '|', '/' };
182 putchar('\b');
183 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100184#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100185 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100186 osmo_timers_prepare();
187 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100188
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100189 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100190
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100191 if (isUsbConnected) {
192 isUsbConnected = 0;
193 }
194 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100195 TRACE_INFO("USB is now configured\n\r");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100196 LED_Set(LED_NUM_GREEN);
197 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100198
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100199 isUsbConnected = 1;
200 }
201 if (last_simtrace_config != simtrace_config) {
Harald Welteec0837c2017-03-03 01:33:24 +0100202 TRACE_INFO("USB config chg %u -> %u\n\r",
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100203 last_simtrace_config, simtrace_config);
204 config_func_ptrs[last_simtrace_config].exit();
205 config_func_ptrs[simtrace_config].init();
206 last_simtrace_config = simtrace_config;
207 } else {
208 config_func_ptrs[simtrace_config].run();
209 }
210 }
Christina Quast32906bb2015-02-24 11:35:19 +0100211}