blob: 3b843c876c783b7f0bcdaf967127aa8aabba4ad1 [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 Welte8ee15db2016-03-20 16:00:39 +010012uint32_t g_unique_id[4];
13
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 Welte7dd3dfd2016-03-03 12:32:04 +0100128 /* Disable watchdog */
129 WDT_Disable(WDT);
Christina Quast32906bb2015-02-24 11:35:19 +0100130
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100131 req_ctx_init();
Harald Welteebbb6452016-02-29 17:57:51 +0100132
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100133 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100134
Harald Welte8ee15db2016-03-20 16:00:39 +0100135 EEFC_ReadUniqueID(g_unique_id);
136
Harald Welte2315e6b2016-03-19 21:37:55 +0100137 printf("\r\n\r\n"
138 "=============================================================================\r\n"
Harald Welted8a003d2017-02-27 20:31:09 +0100139 "SIMtrace2 firmware " GIT_REVISION " (C) 2010-2017 by Harald Welte\r\n"
Harald Welte2315e6b2016-03-19 21:37:55 +0100140 "=============================================================================\r\n");
141
Harald Welte8ee15db2016-03-20 16:00:39 +0100142 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n",
143 g_unique_id[0], g_unique_id[1],
144 g_unique_id[2], g_unique_id[3]);
145
Harald Welteaa3b8672017-02-10 19:21:10 +0100146 board_main_top();
Harald Welte67322482017-02-04 14:43:41 +0100147
Harald Weltecf1c19a2016-03-20 15:18:18 +0100148 TRACE_INFO("USB init...\r\n");
Harald Weltee07aed62016-12-22 22:32:15 +0100149 SIMtrace_USB_Initialize();
150
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100151 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte006b16d2016-12-22 21:29:10 +0100152 check_exec_dbg_cmd();
Harald Welte67415e32016-09-01 20:57:56 +0200153#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100154 if (i >= MAX_USB_ITER * 3) {
155 TRACE_ERROR("Resetting board (USB could "
Harald Weltecf1c19a2016-03-20 15:18:18 +0100156 "not be configured)\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100157 NVIC_SystemReset();
158 }
Harald Welte67415e32016-09-01 20:57:56 +0200159#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100160 i++;
161 }
Christina Quastde688672015-04-12 15:20:57 +0200162
Harald Weltecf1c19a2016-03-20 15:18:18 +0100163 TRACE_INFO("calling configure of all configurations...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100164 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
165 ++i) {
166 if (config_func_ptrs[i].configure)
167 config_func_ptrs[i].configure();
168 }
Christina Quast95d66162015-04-09 22:38:47 +0200169
Harald Weltecf1c19a2016-03-20 15:18:18 +0100170 TRACE_INFO("calling init of config %u...\r\n", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100171 config_func_ptrs[simtrace_config].init();
172 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200173
Harald Weltecf1c19a2016-03-20 15:18:18 +0100174 TRACE_INFO("entering main loop...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100175 while (1) {
Harald Welte04e37a82016-03-20 15:15:34 +0100176#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100177 const char rotor[] = { '-', '\\', '|', '/' };
178 putchar('\b');
179 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100180#endif
Harald Welte006b16d2016-12-22 21:29:10 +0100181 check_exec_dbg_cmd();
Harald Welte987f59a2017-02-04 12:34:35 +0100182 osmo_timers_prepare();
183 osmo_timers_update();
Christina Quast1161b272015-02-25 14:15:57 +0100184
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100185 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100186
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100187 if (isUsbConnected) {
188 isUsbConnected = 0;
189 }
190 } else if (isUsbConnected == 0) {
Harald Weltecf1c19a2016-03-20 15:18:18 +0100191 TRACE_INFO("USB is now configured\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100192 LED_Set(LED_NUM_GREEN);
193 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100194
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100195 isUsbConnected = 1;
196 }
197 if (last_simtrace_config != simtrace_config) {
198 TRACE_INFO("USB config chg %u -> %u\r\n",
199 last_simtrace_config, simtrace_config);
200 config_func_ptrs[last_simtrace_config].exit();
201 config_func_ptrs[simtrace_config].init();
202 last_simtrace_config = simtrace_config;
203 } else {
204 config_func_ptrs[simtrace_config].run();
205 }
206 }
Christina Quast32906bb2015-02-24 11:35:19 +0100207}