blob: aabaa41b043f26693161d3e28198d79a9b357dd3 [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"
Harald Welteb41598b2017-02-04 12:15:58 +01009#include "osmocom/core/timer.h"
Christina Quast32906bb2015-02-24 11:35:19 +010010
Harald Welted5252312017-02-27 20:34:20 +010011unsigned int g_unique_id[4];
Harald Welte8ee15db2016-03-20 16:00:39 +010012
Christina Quast32906bb2015-02-24 11:35:19 +010013/*------------------------------------------------------------------------------
Christina Quast32906bb2015-02-24 11:35:19 +010014 * Internal variables
15 *------------------------------------------------------------------------------*/
Christina Quastfb524b92015-02-27 13:39:45 +010016typedef struct {
Harald Welte7dd3dfd2016-03-03 12:32:04 +010017 /* 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 Welte3bafe432016-03-20 16:43:12 +010025 /* Interrupt handler for USART1 */
26 void (*usart0_irq) (void);
27 /* Interrupt handler for USART1 */
28 void (*usart1_irq) (void);
Christina Quastfb524b92015-02-27 13:39:45 +010029} conf_func;
30
Harald Welted4c14212015-11-07 18:25:46 +010031static const conf_func config_func_ptrs[] = {
Harald Weltefefd5712015-11-07 18:19:11 +010032 /* array slot 0 is empty, usb configs start at 1 */
Harald Welte2fb59962016-02-28 12:34:26 +010033#ifdef HAVE_SNIFFER
Harald Weltefefd5712015-11-07 18:19:11 +010034 [CFG_NUM_SNIFF] = {
35 .configure = Sniffer_configure,
36 .init = Sniffer_init,
37 .exit = Sniffer_exit,
38 .run = Sniffer_run,
39 },
Harald Welte2fb59962016-02-28 12:34:26 +010040#endif
41#ifdef HAVE_CCID
Harald Weltefefd5712015-11-07 18:19:11 +010042 [CFG_NUM_CCID] = {
43 .configure = CCID_configure,
44 .init = CCID_init,
45 .exit = CCID_exit,
46 .run = CCID_run,
47 },
Harald Welte2fb59962016-02-28 12:34:26 +010048#endif
49#ifdef HAVE_CARDEM
Harald Weltefefd5712015-11-07 18:19:11 +010050 [CFG_NUM_PHONE] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010051 .configure = mode_cardemu_configure,
52 .init = mode_cardemu_init,
53 .exit = mode_cardemu_exit,
54 .run = mode_cardemu_run,
Harald Welte3bafe432016-03-20 16:43:12 +010055 .usart0_irq = mode_cardemu_usart0_irq,
56 .usart1_irq = mode_cardemu_usart1_irq,
Harald Weltefefd5712015-11-07 18:19:11 +010057 },
Harald Welte2fb59962016-02-28 12:34:26 +010058#endif
59#ifdef HAVE_MITM
Harald Weltefefd5712015-11-07 18:19:11 +010060 [CFG_NUM_MITM] = {
61 .configure = MITM_configure,
62 .init = MITM_init,
63 .exit = MITM_exit,
Harald Welte7dd3dfd2016-03-03 12:32:04 +010064 .run = MITM_run,
Harald Weltefefd5712015-11-07 18:19:11 +010065 },
Harald Welte2fb59962016-02-28 12:34:26 +010066#endif
Christina Quastfb524b92015-02-27 13:39:45 +010067};
68
Christina Quastfb524b92015-02-27 13:39:45 +010069/*------------------------------------------------------------------------------
70 * Internal variables
71 *------------------------------------------------------------------------------*/
Harald Welte2fb59962016-02-28 12:34:26 +010072#if defined(HAVE_SNIFFER)
Harald Welte8d6a5d82015-11-07 18:27:05 +010073static volatile enum confNum simtrace_config = CFG_NUM_SNIFF;
Harald Welte2fb59962016-02-28 12:34:26 +010074#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
Harald Welte8d6a5d82015-11-07 18:27:05 +010080/*----------------------------------------------------------------------------
81 * Callbacks
82 *----------------------------------------------------------------------------*/
83
84void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
85{
Harald Welte7dd3dfd2016-03-03 12:32:04 +010086 TRACE_INFO_WP("cfgChanged%d ", cfgnum);
87 simtrace_config = cfgnum;
Harald Welte8d6a5d82015-11-07 18:27:05 +010088}
Christina Quast32906bb2015-02-24 11:35:19 +010089
Harald Welte3bafe432016-03-20 16:43:12 +010090void USART1_IrqHandler(void)
91{
Harald Welte2afd57f2017-11-28 22:52:56 +010092 if (config_func_ptrs[simtrace_config].usart1_irq)
93 config_func_ptrs[simtrace_config].usart1_irq();
Harald Welte3bafe432016-03-20 16:43:12 +010094}
95
96void USART0_IrqHandler(void)
97{
Harald Welte2afd57f2017-11-28 22:52:56 +010098 if (config_func_ptrs[simtrace_config].usart0_irq)
99 config_func_ptrs[simtrace_config].usart0_irq();
Harald Welte3bafe432016-03-20 16:43:12 +0100100}
101
Harald Weltef9a182d2017-01-11 22:22:16 +0100102/* returns '1' in case we should break any endless loop */
Harald Welteaf614792017-01-12 18:59:41 +0100103static void check_exec_dbg_cmd(void)
Harald Welte006b16d2016-12-22 21:29:10 +0100104{
Harald Welteaa3b8672017-02-10 19:21:10 +0100105 int ch;
Harald Welte006b16d2016-12-22 21:29:10 +0100106
107 if (!UART_IsRxReady())
108 return;
109
Harald Welteaa3b8672017-02-10 19:21:10 +0100110 ch = UART_GetChar();
111
112 board_exec_dbg_cmd(ch);
Harald Welte006b16d2016-12-22 21:29:10 +0100113}
Harald Weltee974fbb2016-10-19 19:36:07 +0200114
Christina Quast32906bb2015-02-24 11:35:19 +0100115/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200116 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100117 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100118#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
119extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100120{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100121 uint8_t isUsbConnected = 0;
122 enum confNum last_simtrace_config = simtrace_config;
123 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100124
Harald Welteabba8a82017-03-06 16:58:00 +0100125 led_init();
126 led_blink(LED_RED, BLINK_3O_5F);
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 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100133
Harald Welte8ee15db2016-03-20 16:00:39 +0100134 EEFC_ReadUniqueID(g_unique_id);
135
Harald Welteec0837c2017-03-03 01:33:24 +0100136 printf("\n\r\n\r"
137 "=============================================================================\n\r"
138 "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\n\r"
139 "=============================================================================\n\r");
Harald Welte2315e6b2016-03-19 21:37:55 +0100140
Harald Welte7214b472017-03-03 19:02:09 +0100141 TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
Harald Welteec0837c2017-03-03 01:33:24 +0100142 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welte8ee15db2016-03-20 16:00:39 +0100143 g_unique_id[0], g_unique_id[1],
144 g_unique_id[2], g_unique_id[3]);
Harald Welte7214b472017-03-03 19:02:09 +0100145 TRACE_INFO("Reset Cause: 0x%x\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos);
Harald Welte8ee15db2016-03-20 16:00:39 +0100146
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");
Christina Quast1161b272015-02-25 14:15:57 +0100196
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100197 isUsbConnected = 1;
198 }
199 if (last_simtrace_config != simtrace_config) {
Harald Welteec0837c2017-03-03 01:33:24 +0100200 TRACE_INFO("USB config chg %u -> %u\n\r",
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100201 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 }
Christina Quast32906bb2015-02-24 11:35:19 +0100209}