blob: c4df7bcb8f264136532c8f684199cfb607037743 [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"
Christina Quast32906bb2015-02-24 11:35:19 +010010
Harald Welte8ee15db2016-03-20 16:00:39 +010011uint32_t g_unique_id[4];
12
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{
92 config_func_ptrs[simtrace_config].usart1_irq();
93}
94
95void USART0_IrqHandler(void)
96{
97 config_func_ptrs[simtrace_config].usart0_irq();
98}
99
Harald Welte226b40a2016-08-21 19:33:24 +0200100#include "i2c.h"
Christina Quast32906bb2015-02-24 11:35:19 +0100101/*------------------------------------------------------------------------------
Christina Quast95d66162015-04-09 22:38:47 +0200102 * Main
Christina Quast32906bb2015-02-24 11:35:19 +0100103 *------------------------------------------------------------------------------*/
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100104#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
105extern int main(void)
Christina Quast32906bb2015-02-24 11:35:19 +0100106{
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100107 uint8_t isUsbConnected = 0;
108 enum confNum last_simtrace_config = simtrace_config;
109 unsigned int i = 0;
Christina Quast1161b272015-02-25 14:15:57 +0100110
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100111 LED_Configure(LED_NUM_RED);
112 LED_Configure(LED_NUM_GREEN);
113 LED_Set(LED_NUM_RED);
Christina Quast32906bb2015-02-24 11:35:19 +0100114
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100115 /* Disable watchdog */
116 WDT_Disable(WDT);
Christina Quast32906bb2015-02-24 11:35:19 +0100117
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100118 req_ctx_init();
Harald Welteebbb6452016-02-29 17:57:51 +0100119
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100120 PIO_InitializeInterrupts(0);
Christina Quast32906bb2015-02-24 11:35:19 +0100121
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100122 SIMtrace_USB_Initialize();
Christina Quast1161b272015-02-25 14:15:57 +0100123
Harald Welte8ee15db2016-03-20 16:00:39 +0100124 EEFC_ReadUniqueID(g_unique_id);
125
Harald Welte2315e6b2016-03-19 21:37:55 +0100126 printf("\r\n\r\n"
127 "=============================================================================\r\n"
128 "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\r\n"
129 "=============================================================================\r\n");
130
Harald Welte8ee15db2016-03-20 16:00:39 +0100131 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n",
132 g_unique_id[0], g_unique_id[1],
133 g_unique_id[2], g_unique_id[3]);
134
Harald Welte226b40a2016-08-21 19:33:24 +0200135#if 1
Harald Welte67415e32016-09-01 20:57:56 +0200136
137static const unsigned char __eeprom_bin[] = {
138 0x23, 0x42, 0x17, 0x25, 0x00, 0x00, 0x9b, 0x20, 0x01, 0xa0, 0x00, 0x5e,
139 0x01, 0x32, 0x01, 0x32, 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00,
140 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x6f, 0x00,
141 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00,
142 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00,
143 0x20, 0x00, 0x47, 0x00, 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00,
146 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00,
147 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
158 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00
160};
161static const unsigned int __eeprom_bin_len = 256;
162
Harald Welte226b40a2016-08-21 19:33:24 +0200163 static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT};
164 PIO_Configure(&pin_hub_rst, 1);
165 i2c_pin_init();
Harald Welte67415e32016-09-01 20:57:56 +0200166
167 /* wait */
168 volatile int v;
169 /* 440ns per cycle here */
170 for (i = 0; i < 1000000; i++) {
171 v = 0;
Harald Welte226b40a2016-08-21 19:33:24 +0200172 }
Harald Welte67415e32016-09-01 20:57:56 +0200173
174 /* write the EEPROM once */
175 for (i = 0; i < 256; i++) {
176 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
177 /* if the result was negative, repeat that write */
178 if (rc < 0)
179 i--;
180 }
181
182 /* then pursue re-reading it again and again */
183 for (i = 0; i < 256; i++) {
184 int byte = eeprom_read_byte(0x50, i);
185 TRACE_INFO("0x%02x: %02x\r\n", i, byte);
186 if (byte != __eeprom_bin[i])
187 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\r\n",
188 i, __eeprom_bin[i], byte);
189 }
190 PIO_Clear(&pin_hub_rst);
Harald Welte226b40a2016-08-21 19:33:24 +0200191#endif
192
Harald Weltecf1c19a2016-03-20 15:18:18 +0100193 TRACE_INFO("USB init...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100194 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte67415e32016-09-01 20:57:56 +0200195#if 0
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100196 if (i >= MAX_USB_ITER * 3) {
197 TRACE_ERROR("Resetting board (USB could "
Harald Weltecf1c19a2016-03-20 15:18:18 +0100198 "not be configured)\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100199 NVIC_SystemReset();
200 }
Harald Welte67415e32016-09-01 20:57:56 +0200201#endif
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100202 i++;
203 }
Christina Quastde688672015-04-12 15:20:57 +0200204
Harald Weltecf1c19a2016-03-20 15:18:18 +0100205 TRACE_INFO("calling configure of all configurations...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100206 for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
207 ++i) {
208 if (config_func_ptrs[i].configure)
209 config_func_ptrs[i].configure();
210 }
Christina Quast95d66162015-04-09 22:38:47 +0200211
Harald Weltecf1c19a2016-03-20 15:18:18 +0100212 TRACE_INFO("calling init of config %u...\r\n", simtrace_config);
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100213 config_func_ptrs[simtrace_config].init();
214 last_simtrace_config = simtrace_config;
Christina Quast95d66162015-04-09 22:38:47 +0200215
Harald Weltecf1c19a2016-03-20 15:18:18 +0100216 TRACE_INFO("entering main loop...\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100217 while (1) {
Harald Welte04e37a82016-03-20 15:15:34 +0100218#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100219 const char rotor[] = { '-', '\\', '|', '/' };
220 putchar('\b');
221 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
Harald Welte04e37a82016-03-20 15:15:34 +0100222#endif
Christina Quast1161b272015-02-25 14:15:57 +0100223
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100224 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
Christina Quast1161b272015-02-25 14:15:57 +0100225
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100226 if (isUsbConnected) {
227 isUsbConnected = 0;
228 }
229 } else if (isUsbConnected == 0) {
Harald Weltecf1c19a2016-03-20 15:18:18 +0100230 TRACE_INFO("USB is now configured\r\n");
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100231 LED_Set(LED_NUM_GREEN);
232 LED_Clear(LED_NUM_RED);
Christina Quast1161b272015-02-25 14:15:57 +0100233
Harald Welte7dd3dfd2016-03-03 12:32:04 +0100234 isUsbConnected = 1;
235 }
236 if (last_simtrace_config != simtrace_config) {
237 TRACE_INFO("USB config chg %u -> %u\r\n",
238 last_simtrace_config, simtrace_config);
239 config_func_ptrs[last_simtrace_config].exit();
240 config_func_ptrs[simtrace_config].init();
241 last_simtrace_config = simtrace_config;
242 } else {
243 config_func_ptrs[simtrace_config].run();
244 }
245 }
Christina Quast32906bb2015-02-24 11:35:19 +0100246}