blob: aabaa41b043f26693161d3e28198d79a9b357dd3 [file] [log] [blame]
Kévin Redonf9997e92018-06-04 16:21:23 +02001// FIXME: Copyright license here
2/*------------------------------------------------------------------------------
3 * Headers
4 *------------------------------------------------------------------------------*/
5
6#include "board.h"
7#include "simtrace.h"
8#include "utils.h"
9#include "osmocom/core/timer.h"
10
11unsigned int g_unique_id[4];
12
13/*------------------------------------------------------------------------------
14 * Internal variables
15 *------------------------------------------------------------------------------*/
16typedef struct {
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);
25 /* Interrupt handler for USART1 */
26 void (*usart0_irq) (void);
27 /* Interrupt handler for USART1 */
28 void (*usart1_irq) (void);
29} conf_func;
30
31static const conf_func config_func_ptrs[] = {
32 /* array slot 0 is empty, usb configs start at 1 */
33#ifdef HAVE_SNIFFER
34 [CFG_NUM_SNIFF] = {
35 .configure = Sniffer_configure,
36 .init = Sniffer_init,
37 .exit = Sniffer_exit,
38 .run = Sniffer_run,
39 },
40#endif
41#ifdef HAVE_CCID
42 [CFG_NUM_CCID] = {
43 .configure = CCID_configure,
44 .init = CCID_init,
45 .exit = CCID_exit,
46 .run = CCID_run,
47 },
48#endif
49#ifdef HAVE_CARDEM
50 [CFG_NUM_PHONE] = {
51 .configure = mode_cardemu_configure,
52 .init = mode_cardemu_init,
53 .exit = mode_cardemu_exit,
54 .run = mode_cardemu_run,
55 .usart0_irq = mode_cardemu_usart0_irq,
56 .usart1_irq = mode_cardemu_usart1_irq,
57 },
58#endif
59#ifdef HAVE_MITM
60 [CFG_NUM_MITM] = {
61 .configure = MITM_configure,
62 .init = MITM_init,
63 .exit = MITM_exit,
64 .run = MITM_run,
65 },
66#endif
67};
68
69/*------------------------------------------------------------------------------
70 * Internal variables
71 *------------------------------------------------------------------------------*/
72#if defined(HAVE_SNIFFER)
73static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
74#elif defined(HAVE_CARDEM)
75static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
76#elif defined(HAVE_CCID)
77static volatile enum confNum simtrace_config = CFG_NUM_CCID;
78#endif
79
80/*----------------------------------------------------------------------------
81 * Callbacks
82 *----------------------------------------------------------------------------*/
83
84void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
85{
86 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
87 simtrace_config = cfgnum;
88}
89
90void USART1_IrqHandler(void)
91{
92 if (config_func_ptrs[simtrace_config].usart1_irq)
93 config_func_ptrs[simtrace_config].usart1_irq();
94}
95
96void USART0_IrqHandler(void)
97{
98 if (config_func_ptrs[simtrace_config].usart0_irq)
99 config_func_ptrs[simtrace_config].usart0_irq();
100}
101
102/* returns '1' in case we should break any endless loop */
103static void check_exec_dbg_cmd(void)
104{
105 int ch;
106
107 if (!UART_IsRxReady())
108 return;
109
110 ch = UART_GetChar();
111
112 board_exec_dbg_cmd(ch);
113}
114
115/*------------------------------------------------------------------------------
116 * Main
117 *------------------------------------------------------------------------------*/
118#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
119extern int main(void)
120{
121 uint8_t isUsbConnected = 0;
122 enum confNum last_simtrace_config = simtrace_config;
123 unsigned int i = 0;
124
125 led_init();
126 led_blink(LED_RED, BLINK_3O_5F);
127
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));
131
132 PIO_InitializeInterrupts(0);
133
134 EEFC_ReadUniqueID(g_unique_id);
135
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");
140
141 TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
142 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
143 g_unique_id[0], g_unique_id[1],
144 g_unique_id[2], g_unique_id[3]);
145 TRACE_INFO("Reset Cause: 0x%x\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos);
146
147 board_main_top();
148
149 TRACE_INFO("USB init...\n\r");
150 SIMtrace_USB_Initialize();
151
152 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
153 WDT_Restart(WDT);
154 check_exec_dbg_cmd();
155#if 0
156 if (i >= MAX_USB_ITER * 3) {
157 TRACE_ERROR("Resetting board (USB could "
158 "not be configured)\n\r");
159 USBD_Disconnect();
160 NVIC_SystemReset();
161 }
162#endif
163 i++;
164 }
165
166 TRACE_INFO("calling configure of all configurations...\n\r");
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 }
172
173 TRACE_INFO("calling init of config %u...\n\r", simtrace_config);
174 config_func_ptrs[simtrace_config].init();
175 last_simtrace_config = simtrace_config;
176
177 TRACE_INFO("entering main loop...\n\r");
178 while (1) {
179 WDT_Restart(WDT);
180#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
181 const char rotor[] = { '-', '\\', '|', '/' };
182 putchar('\b');
183 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
184#endif
185 check_exec_dbg_cmd();
186 osmo_timers_prepare();
187 osmo_timers_update();
188
189 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
190
191 if (isUsbConnected) {
192 isUsbConnected = 0;
193 }
194 } else if (isUsbConnected == 0) {
195 TRACE_INFO("USB is now configured\n\r");
196
197 isUsbConnected = 1;
198 }
199 if (last_simtrace_config != simtrace_config) {
200 TRACE_INFO("USB config chg %u -> %u\n\r",
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 }
209}