blob: 8b3f5fa3e27a9bdeb90f98e4b2896089dd2883ad [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/*
Kévin Redon78d2f442019-01-24 18:45:59 +01002 * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17*/
Kévin Redon69b92d92019-01-24 16:39:20 +010018
Harald Welte1b9a5b82019-02-24 23:04:45 +010019#include <stdlib.h>
Harald Welte5a8af4d2019-05-12 15:57:20 +020020#include <inttypes.h>
Harald Welte1b9a5b82019-02-24 23:04:45 +010021#include <stdio.h>
Kévin Redon072951b2019-05-02 15:17:46 +020022#include <math.h>
Harald Weltef53f2262019-02-24 11:01:08 +010023#include <parts.h>
Harald Welte65101be2019-04-18 18:30:49 +020024#include <errno.h>
Harald Weltec7a58ba2019-04-18 17:59:19 +020025
26#include <osmocom/core/utils.h>
27
Harald Weltef53f2262019-02-24 11:01:08 +010028#include <hal_cache.h>
Harald Welte93f628a2019-02-24 14:32:30 +010029#include <hri_port_e54.h>
Harald Weltef53f2262019-02-24 11:01:08 +010030
Kévin Redon69b92d92019-01-24 16:39:20 +010031#include "atmel_start.h"
32#include "atmel_start_pins.h"
Kévin Redon072951b2019-05-02 15:17:46 +020033#include "config/hpl_gclk_config.h"
Kévin Redon69b92d92019-01-24 16:39:20 +010034
Harald Weltec3f170d2019-02-24 09:06:59 +010035#include "i2c_bitbang.h"
36#include "octsim_i2c.h"
37#include "ncn8025.h"
Kévin Redon0f050722019-05-02 15:56:25 +020038#include "iso7816_3.h"
Harald Weltec3f170d2019-02-24 09:06:59 +010039
Harald Welteff9f4ce2019-02-24 22:51:09 +010040#include "command.h"
41
Kévin Redonc89bb8c2019-04-17 01:20:23 +020042// TODO put declaration in more global file
43// TODO for now SIM7 is not present because used for debug
44static struct usart_async_descriptor* SIM_peripheral_descriptors[] = {&SIM0, &SIM1, &SIM2, &SIM3, &SIM4, &SIM5, &SIM6, NULL};
45
Kévin Redon096c5052019-05-09 15:01:17 +020046/** number of bytes transmitted on the SIM peripheral */
47static volatile bool SIM_tx_count[8];
48
Kévin Redonc89bb8c2019-04-17 01:20:23 +020049static void SIM_rx_cb(const struct usart_async_descriptor *const io_descr)
50{
51}
Kévin Redon78d2f442019-01-24 18:45:59 +010052
Kévin Redon096c5052019-05-09 15:01:17 +020053/** called when the transmission is complete
54 * e.g. this is when the byte has been sent and there is no data to transmit anymore
55 */
56static void SIM_tx_cb(const struct usart_async_descriptor *const io_descr)
57{
58 // find slotnr for corresponding USART
59 uint8_t slotnr;
60 for (slotnr = 0; slotnr < ARRAY_SIZE(SIM_peripheral_descriptors) && SIM_peripheral_descriptors[slotnr] != io_descr; slotnr++);
61
62 // set flag
63 if (slotnr < ARRAY_SIZE(SIM_peripheral_descriptors)) {
64 SIM_tx_count[slotnr] = true;
65 }
66}
67
Kévin Redon072951b2019-05-02 15:17:46 +020068/** possible clock sources for the SERCOM peripheral
69 * warning: the definition must match the GCLK configuration
70 */
71static const uint8_t sercom_glck_sources[] = {GCLK_PCHCTRL_GEN_GCLK2_Val, GCLK_PCHCTRL_GEN_GCLK4_Val, GCLK_PCHCTRL_GEN_GCLK6_Val};
72
73/** possible clock frequencies in MHz for the SERCOM peripheral
74 * warning: the definition must match the GCLK configuration
75 */
76static const double sercom_glck_freqs[] = {100E6 / CONF_GCLK_GEN_2_DIV, 100E6 / CONF_GCLK_GEN_4_DIV, 120E6 / CONF_GCLK_GEN_6_DIV};
77
78/** the GCLK ID for the SERCOM SIM peripherals
79 * @note: used as index for PCHCTRL
80 */
81static const uint8_t SIM_peripheral_GCLK_ID[] = {SERCOM0_GCLK_ID_CORE, SERCOM1_GCLK_ID_CORE, SERCOM2_GCLK_ID_CORE, SERCOM3_GCLK_ID_CORE, SERCOM4_GCLK_ID_CORE, SERCOM5_GCLK_ID_CORE, SERCOM6_GCLK_ID_CORE, SERCOM7_GCLK_ID_CORE};
82
Harald Weltec7a58ba2019-04-18 17:59:19 +020083static void ccid_app_init(void);
84
Harald Weltec3f170d2019-02-24 09:06:59 +010085static void board_init()
86{
87 int i;
88
89 for (i = 0; i < 4; i++)
90 i2c_init(&i2c[i]);
91
Harald Welte255da5e2019-04-16 18:19:53 +020092 for (i = 0; i < 8; i++)
Harald Weltec3f170d2019-02-24 09:06:59 +010093 ncn8025_init(i);
Harald Weltef53f2262019-02-24 11:01:08 +010094
95 cache_init();
96 cache_enable(CMCC);
Harald Welted1bd5c42019-05-17 16:38:30 +020097 calendar_enable(&CALENDAR_0);
Harald Welte93f628a2019-02-24 14:32:30 +010098
99 /* increase drive strength of 20Mhz SIM clock output to 8mA
100 * (there are 8 inputs + traces to drive!) */
101 hri_port_set_PINCFG_DRVSTR_bit(PORT, 0, 11);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200102
103 // enable SIM interfaces
104 for (uint8_t i = 0; i < ARRAY_SIZE(SIM_peripheral_descriptors); i++) {
105 if (NULL == SIM_peripheral_descriptors[i]) {
106 continue;
107 }
108 usart_async_register_callback(SIM_peripheral_descriptors[i], USART_ASYNC_RXC_CB, SIM_rx_cb); // required for RX to work, even if the callback does nothing
Kévin Redon096c5052019-05-09 15:01:17 +0200109 usart_async_register_callback(SIM_peripheral_descriptors[i], USART_ASYNC_TXC_CB, SIM_tx_cb); // to count the number of bytes transmitted since we are using it asynchronously
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200110 usart_async_enable(SIM_peripheral_descriptors[i]);
111 }
Harald Weltec7a58ba2019-04-18 17:59:19 +0200112
113 ccid_app_init();
Harald Weltec3f170d2019-02-24 09:06:59 +0100114}
115
Harald Weltec7a58ba2019-04-18 17:59:19 +0200116/***********************************************************************
117 * CCID Driver integration
118 ***********************************************************************/
119
120#include <osmocom/core/linuxlist.h>
121#include <osmocom/core/msgb.h>
122#include "linuxlist_atomic.h"
123#include "ccid_df.h"
124
125struct usb_ep_q {
126 const char *name;
127 /* msgb queue of pending to-be-transmitted (IN/IRQ) or completed received (OUT)
128 * USB transfers */
129 struct llist_head list;
130 /* currently ongoing/processed msgb (USB transmit or receive */
131 struct msgb *in_progress;
132};
133
134struct ccid_state {
135 /* msgb queue of free msgs */
136 struct llist_head free_q;
137
138 /* msgb queue of pending to-be-transmitted (IN EP) */
139 struct usb_ep_q in_ep;
140 /* msgb queue of pending to-be-transmitted (IRQ EP) */
141 struct usb_ep_q irq_ep;
142 /* msgb queue of completed received (OUT EP) */
143 struct usb_ep_q out_ep;
Harald Welte5a8af4d2019-05-12 15:57:20 +0200144
145 /* bit-mask of card-insert status, as determined from NCN8025 IRQ output */
146 uint8_t card_insert_mask;
Harald Weltec7a58ba2019-04-18 17:59:19 +0200147};
148static struct ccid_state g_ccid_s;
149
150static void ccid_out_read_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred);
151static void ccid_in_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred);
152static void ccid_irq_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred);
153
154static void usb_ep_q_init(struct usb_ep_q *ep_q, const char *name)
155{
156 ep_q->name = name;
157 INIT_LLIST_HEAD(&ep_q->list);
158 ep_q->in_progress = NULL;
159}
160
161static void ccid_app_init(void)
162{
163 /* initialize data structures */
164 INIT_LLIST_HEAD(&g_ccid_s.free_q);
165 usb_ep_q_init(&g_ccid_s.in_ep, "IN");
166 usb_ep_q_init(&g_ccid_s.irq_ep, "IRQ");
167 usb_ep_q_init(&g_ccid_s.out_ep, "OUT");
168
169 /* OUT endpoint read complete callback (irq context) */
170 ccid_df_register_callback(CCID_DF_CB_READ_OUT, (FUNC_PTR)&ccid_out_read_compl);
171 /* IN endpoint write complete callback (irq context) */
172 ccid_df_register_callback(CCID_DF_CB_WRITE_IN, (FUNC_PTR)&ccid_in_write_compl);
173 /* IRQ endpoint write complete callback (irq context) */
174 ccid_df_register_callback(CCID_DF_CB_WRITE_IRQ, (FUNC_PTR)&ccid_irq_write_compl);
175}
176
177/* irqsafe version of msgb_enqueue */
178struct msgb *msgb_dequeue_irqsafe(struct llist_head *q)
179{
180 struct msgb *msg;
181 CRITICAL_SECTION_ENTER()
182 msg = msgb_dequeue(q);
183 CRITICAL_SECTION_LEAVE()
184 return msg;
185}
186
Harald Welte5a8af4d2019-05-12 15:57:20 +0200187void msgb_enqueue_irqsafe(struct llist_head *q, struct msgb *msg)
188{
189 CRITICAL_SECTION_ENTER()
190 msgb_enqueue(q, msg);
191 CRITICAL_SECTION_LEAVE()
192}
193
Harald Weltec7a58ba2019-04-18 17:59:19 +0200194/* submit the next pending (if any) message for the IN EP */
195static int submit_next_in(void)
196{
197 struct usb_ep_q *ep_q = &g_ccid_s.in_ep;
198 struct msgb *msg;
199 int rc;
200
201 OSMO_ASSERT(!ep_q->in_progress);
202 msg = msgb_dequeue_irqsafe(&ep_q->list);
203 if (!msg)
204 return 0;
205
206 ep_q->in_progress = msg;
207 rc = ccid_df_write_in(msgb_data(msg), msgb_length(msg));
208 if (rc != ERR_NONE) {
209 printf("EP %s failed: %d\r\n", ep_q->name, rc);
210 return -1;
211 }
212 return 1;
213
214}
215
216/* submit the next pending (if any) message for the IRQ EP */
217static int submit_next_irq(void)
218{
219 struct usb_ep_q *ep_q = &g_ccid_s.irq_ep;
220 struct msgb *msg;
221 int rc;
222
223 OSMO_ASSERT(!ep_q->in_progress);
224 msg = msgb_dequeue_irqsafe(&ep_q->list);
225 if (!msg)
226 return 0;
227
228 ep_q->in_progress = msg;
229 rc = ccid_df_write_irq(msgb_data(msg), msgb_length(msg));
230 /* may return HALTED/ERROR/DISABLED/BUSY/ERR_PARAM/ERR_FUNC/ERR_DENIED */
231 if (rc != ERR_NONE) {
232 printf("EP %s failed: %d\r\n", ep_q->name, rc);
233 return -1;
234 }
235 return 1;
236}
237
238static int submit_next_out(void)
239{
240 struct usb_ep_q *ep_q = &g_ccid_s.out_ep;
241 struct msgb *msg;
242 int rc;
243
244 OSMO_ASSERT(!ep_q->in_progress);
245 msg = msgb_dequeue_irqsafe(&g_ccid_s.free_q);
246 if (!msg)
247 return -1;
248 ep_q->in_progress = msg;
249
250 rc = ccid_df_read_out(msgb_data(msg), msgb_tailroom(msg));
251 if (rc != ERR_NONE) {
252 /* re-add to the list of free msgb's */
253 llist_add_tail_at(&g_ccid_s.free_q, &msg->list);
254 return 0;
255 }
256 return 1;
257}
258
259/* OUT endpoint read complete callback (irq context) */
260static void ccid_out_read_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred)
261{
262 struct msgb *msg = g_ccid_s.out_ep.in_progress;
263
264 /* add just-received msg to tail of endpoint queue */
265 OSMO_ASSERT(msg);
266 /* update msgb with the amount of data received */
267 msgb_put(msg, transferred);
268 /* append to list of pending-to-be-handed messages */
269 llist_add_tail_at(&msg->list, &g_ccid_s.out_ep.list);
270
271 /* submit another [free] msgb to receive the next transfer */
272 submit_next_out();
273}
274
275/* IN endpoint write complete callback (irq context) */
276static void ccid_in_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred)
277{
278 struct msgb *msg = g_ccid_s.in_ep.in_progress;
279
280 OSMO_ASSERT(msg);
281 /* return the message back to the queue of free message buffers */
282 llist_add_tail_at(&msg->list, &g_ccid_s.free_q);
283 g_ccid_s.in_ep.in_progress = NULL;
284
285 /* submit the next pending to-be-transmitted msgb (if any) */
286 submit_next_in();
287}
288
289/* IRQ endpoint write complete callback (irq context) */
290static void ccid_irq_write_compl(const uint8_t ep, enum usb_xfer_code code, uint32_t transferred)
291{
292 struct msgb *msg = g_ccid_s.irq_ep.in_progress;
293
294 OSMO_ASSERT(msg);
295 /* return the message back to the queue of free message buffers */
296 llist_add_tail_at(&msg->list, &g_ccid_s.free_q);
297 g_ccid_s.irq_ep.in_progress = NULL;
298
299 /* submit the next pending to-be-transmitted msgb (if any) */
300 submit_next_irq();
301}
302
Harald Welte5a8af4d2019-05-12 15:57:20 +0200303#include "ccid_proto.h"
304struct msgb *ccid_gen_notify_slot_status(const uint8_t *bitmask, uint8_t bm_len)
305{
306 //struct msgb *msg = ccid_msgb_alloc();
307 struct msgb *msg = msgb_alloc(64, "IRQ");
308 struct ccid_rdr_to_pc_notify_slot_change *nsc = msgb_put(msg, sizeof(*nsc) + bm_len);
309 nsc->bMessageType = RDR_to_PC_NotifySlotChange;
310 memcpy(&nsc->bmSlotCCState, bitmask, bm_len);
311
312 return msg;
313}
314
315/* check if any card detect state has changed */
316static void poll_card_detect(void)
317{
318 uint8_t new_mask = 0;
319 struct msgb *msg;
320 unsigned int i;
321
322 for (i = 0; i < 8; i++) {
323 bool irq_level = ncn8025_interrupt_level(i);
324 if (irq_level)
325 new_mask &= ~(1 << i);
326 else
327 new_mask |= (1 << i);
328 }
329
330 /* notify the user/host about any changes */
331 if (g_ccid_s.card_insert_mask != new_mask) {
332 printf("CARD_DET 0x%02x -> 0x%02x\r\n",
333 g_ccid_s.card_insert_mask, new_mask);
334 msg = ccid_gen_notify_slot_status(&new_mask, 1);
335 msgb_enqueue_irqsafe(&g_ccid_s.irq_ep.list, msg);
336
337 g_ccid_s.card_insert_mask = new_mask;
338 }
339}
340
341
Harald Weltec7a58ba2019-04-18 17:59:19 +0200342
343/***********************************************************************
344 * Command Line interface
345 ***********************************************************************/
346
Harald Welte1b9a5b82019-02-24 23:04:45 +0100347static int validate_slotnr(int argc, char **argv, int idx)
348{
349 int slotnr;
350 if (argc < idx+1) {
351 printf("You have to specify the slot number (0..7)\r\n");
352 return -1;
353 }
354 slotnr = atoi(argv[idx]);
355 if (slotnr < 0 || slotnr > 7) {
356 printf("You have to specify the slot number (0..7)\r\n");
357 return -1;
358 }
359 return slotnr;
360}
361
Kévin Redon072951b2019-05-02 15:17:46 +0200362/** change baud rate of card slot
363 * @param[in] slotnr slot number for which the baud rate should be set
364 * @param[in] baudrate baud rate in bps to set
365 * @return if the baud rate has been set, else a parameter is out of range
366 */
367static bool slot_set_baudrate(uint8_t slotnr, uint32_t baudrate)
368{
369 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
370
371 // calculate the error corresponding to the clock sources
372 uint16_t bauds[ARRAY_SIZE(sercom_glck_freqs)];
373 double errors[ARRAY_SIZE(sercom_glck_freqs)];
374 for (uint8_t i = 0; i < ARRAY_SIZE(sercom_glck_freqs); i++) {
375 double freq = sercom_glck_freqs[i]; // remember possible SERCOM frequency
376 uint32_t min = freq / (2 * (255 + 1)); // calculate the minimum baud rate for this frequency
377 uint32_t max = freq / (2 * (0 + 1)); // calculate the maximum baud rate for this frequency
378 if (baudrate < min || baudrate > max) { // baud rate it out of supported range
379 errors[i] = NAN;
380 } else {
381 uint16_t baud = round(freq / (2 * baudrate) - 1);
382 bauds[i] = baud;
383 double actual = freq / (2 * (baud + 1));
384 errors[i] = fabs(1.0 - (actual / baudrate));
385 }
386 }
387
388 // find the smallest error
389 uint8_t best = ARRAY_SIZE(sercom_glck_freqs);
390 for (uint8_t i = 0; i < ARRAY_SIZE(sercom_glck_freqs); i++) {
391 if (isnan(errors[i])) {
392 continue;
393 }
394 if (best >= ARRAY_SIZE(sercom_glck_freqs)) {
395 best = i;
396 } else if (errors[i] < errors[best]) {
397 best = i;
398 }
399 }
400 if (best >= ARRAY_SIZE(sercom_glck_freqs)) { // found no clock supporting this baud rate
401 return false;
402 }
403
404 // set clock and baud rate
405 struct usart_async_descriptor* slot = SIM_peripheral_descriptors[slotnr]; // get slot
406 if (NULL == slot) {
407 return false;
408 }
409 printf("(%u) switching SERCOM clock to GCLK%u (freq = %lu kHz) and baud rate to %lu bps (baud = %u)\r\n", slotnr, (best + 1) * 2, (uint32_t)(round(sercom_glck_freqs[best] / 1000)), baudrate, bauds[best]);
410 while (!usart_async_is_tx_empty(slot)); // wait for transmission to complete (WARNING no timeout)
411 usart_async_disable(slot); // disable SERCOM peripheral
412 hri_gclk_clear_PCHCTRL_reg(GCLK, SIM_peripheral_GCLK_ID[slotnr], (1 << GCLK_PCHCTRL_CHEN_Pos)); // disable clock for this peripheral
413 while (hri_gclk_get_PCHCTRL_reg(GCLK, SIM_peripheral_GCLK_ID[slotnr], (1 << GCLK_PCHCTRL_CHEN_Pos))); // wait until clock is really disabled
414 // it does not seem we need to completely disable the peripheral using hri_mclk_clear_APBDMASK_SERCOMn_bit
415 hri_gclk_write_PCHCTRL_reg(GCLK, SIM_peripheral_GCLK_ID[slotnr], sercom_glck_sources[best] | (1 << GCLK_PCHCTRL_CHEN_Pos)); // set peripheral core clock and re-enable it
416 usart_async_set_baud_rate(slot, bauds[best]); // set the new baud rate
417 usart_async_enable(slot); // re-enable SERCOM peripheral
418
419 return true;
420}
421
Kévin Redon0f050722019-05-02 15:56:25 +0200422/** change ISO baud rate of card slot
423 * @param[in] slotnr slot number for which the baud rate should be set
424 * @param[in] clkdiv can clock divider
425 * @param[in] f clock rate conversion integer F
426 * @param[in] d baud rate adjustment factor D
427 * @return if the baud rate has been set, else a parameter is out of range
428 */
429static bool slot_set_isorate(uint8_t slotnr, enum ncn8025_sim_clkdiv clkdiv, uint16_t f, uint8_t d)
430{
431 // input checks
432 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
433 if (clkdiv != SIM_CLKDIV_1 && clkdiv != SIM_CLKDIV_2 && clkdiv != SIM_CLKDIV_4 && clkdiv != SIM_CLKDIV_8) {
434 return false;
435 }
436 if (!iso7816_3_valid_f(f)) {
437 return false;
438 }
439 if (!iso7816_3_valid_d(d)) {
440 return false;
441 }
442
443 // set clockdiv
444 struct ncn8025_settings settings;
445 ncn8025_get(slotnr, &settings);
446 if (settings.clkdiv != clkdiv) {
447 settings.clkdiv = clkdiv;
448 ncn8025_set(slotnr, &settings);
449 }
450
451 // calculate desired frequency
452 uint32_t freq = 20000000UL; // maximum frequency
453 switch (clkdiv) {
454 case SIM_CLKDIV_1:
455 freq /= 1;
456 break;
457 case SIM_CLKDIV_2:
458 freq /= 2;
459 break;
460 case SIM_CLKDIV_4:
461 freq /= 4;
462 break;
463 case SIM_CLKDIV_8:
464 freq /= 8;
465 break;
466 }
467
468 // set baud rate
469 uint32_t baudrate = (freq * d) / f; // calculate actual baud rate
Kévin Redon5188e9f2019-05-09 17:34:55 +0200470 return slot_set_baudrate(slotnr, baudrate); // set baud rate
471}
472
473/** write data to card
474 * @param[in] slotnr slot number on which to send data
475 * @param[in] data data to be transmitted
476 * @param[in] length length of data to be transmitted
477 * @return error code
478 */
479static int slot_card_write(uint8_t slotnr, const uint8_t* data, uint16_t length)
480{
481 // input checks
482 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
483 if (0 == length || NULL == data) {
484 return ERR_INVALID_ARG;
485 }
486
487 struct usart_async_descriptor* sim = SIM_peripheral_descriptors[slotnr];
488 ((Sercom *)sim->device.hw)->USART.CTRLB.bit.RXEN = 0; // disable receive (to avoid the echo back)
489 SIM_tx_count[slotnr] = false; // reset TX complete
490 for (uint16_t i = 0; i < length; i++) { // transmit data
491 while(!usart_async_is_tx_empty(sim)); // wait for previous byte to be transmitted (WARNING blocking)
492 if (1 != io_write(&sim->io, &data[i], 1)) { // put but in transmit buffer
493 return ERR_IO;
494 }
495 }
496 while (!SIM_tx_count[slotnr]); // wait until transmission is complete (WARNING blocking)
497 ((Sercom *)sim->device.hw)->USART.CTRLB.bit.RXEN = 1; // enable receive again
498
499 return ERR_NONE;
500}
501
502/** read data from card
503 * @param[in] slotnr slot number on which to send data
504 * @param[out] data buffer for read data to be stored
505 * @param[in] length length of data to be read
506 * @param[in] wt Waiting Time in ETU
507 * @return error code
508 * TODO fix WT/ETU duration
509 */
510static int slot_card_read(uint8_t slotnr, uint8_t* data, uint16_t length, uint32_t wt)
511{
512 // input checks
513 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
514 if (0 == length || NULL == data) {
515 return ERR_INVALID_ARG;
516 }
517
518 struct usart_async_descriptor* sim = SIM_peripheral_descriptors[slotnr];
519
520 ((Sercom *)sim->device.hw)->USART.CTRLB.bit.RXEN = 1; // ensure RX is enabled
521 uint32_t timeout = wt; // reset waiting time
522 for (uint16_t i = 0; i < length; i++) { // read all data
523 while (timeout && !usart_async_is_rx_not_empty(sim)) { // verify if data is present
524 delay_us(149); // wait for 1 ETU (372 / 1 / 2.5 MHz = 148.8 us)
525 timeout--;
526 }
527 if (0 == timeout) { // timeout reached
528 return ERR_TIMEOUT;
529 }
530 timeout = wt; // reset waiting time
531 if (1 != io_read(&sim->io, &data[i], 1)) { // read one byte
532 return ERR_IO;
533 }
534 }
535
536 return ERR_NONE;
537}
538
539/** transfer TPDU
540 * @param[in] slotnr slot number on which to transfer the TPDU
541 * @param[in] header TPDU header to send
542 * @param[io] data TPDU data to transfer
543 * @param[in] data_length length of TPDU data to transfer
544 * @param[in] write if the data should be written (true) or read (false)
545 * TODO fix WT
546 * TODO the data length can be deduce from the header
547 */
548static int slot_tpdu_xfer(uint8_t slotnr, const uint8_t* header, uint8_t* data, uint16_t data_length, bool write)
549{
550 // input checks
551 ASSERT(slotnr < ARRAY_SIZE(SIM_peripheral_descriptors));
552 if (NULL == header || (data_length > 0 && NULL == data)) {
553 return ERR_INVALID_ARG;
554 }
555
556 int rc;
557 struct usart_async_descriptor* sim = SIM_peripheral_descriptors[slotnr]; // get USART peripheral
558 usart_async_flush_rx_buffer(sim); // flush RX buffer to start from scratch
559
560 // send command header
561 printf("(%d) TPDU: ", slotnr);
562 for (uint8_t i = 0; i < 5; i++) {
563 printf("%02x ", header[i]);
564 }
565 rc = slot_card_write(slotnr, header, 5); // transmit header
566 if (ERR_NONE != rc) {
567 printf("error in command header transmit (errno = %d)\r\n", rc);
568 return rc;
569 }
570
571 // read procedure byte, and handle data
572 uint8_t pb = 0x60; // wait more procedure byte
573 uint16_t data_i = 0; // progress in the data transfer
574 while (0x60 == pb) { // wait for SW
575 rc = slot_card_read(slotnr, &pb, 1, ISO7816_3_DEFAULT_WT);
576 if (ERR_NONE != rc) {
577 printf("error while receiving PB/SW1 (errno = %d)\r\n", rc);
578 return rc;
579 }
580 printf("%02x ", pb);
581 if (0x60 == pb) { // NULL byte
582 // just wait more time
583 } else if ((0x60 == (pb & 0xf0)) || (0x90 == (pb & 0xf0))) { // SW1 byte
584 // left the rest of the code handle it
585 } else if (header[1] == pb) { // ACK byte
586 // transfer rest of the data
587 if (data_i >= data_length) {
588 printf("error no more data to transfer\r\n");
589 return ERR_INVALID_DATA;
590 }
591 if (write) { // transmit remaining command data
592 rc = slot_card_write(slotnr, &data[data_i], data_length - data_i); // transmit command data
593 if (ERR_NONE != rc) {
594 printf("error in command data transmit (errno = %d)\r\n", rc);
595 return rc;
596 }
597 } else { // receive remaining command data
598 rc = slot_card_read(slotnr, &data[data_i], data_length - data_i, ISO7816_3_DEFAULT_WT);
599 if (ERR_NONE != rc) {
600 printf("error in command data receive (errno = %d)\r\n", rc);
601 return rc;
602 }
603 }
604 for (uint16_t i = data_i; i < data_length; i++) {
605 printf("%02x ", data[i]);
606 }
607 data_i = data_length; // remember we transferred the data
608 pb = 0x60; // wait for SW1
609 } else if (header[1] == (pb ^ 0xff)) { // ACK byte
610 // transfer only one byte
611 if (data_i >= data_length) {
612 printf("error no more data to transfer\r\n");
613 return ERR_INVALID_DATA;
614 }
615 if (write) { // transmit command data byte
616 rc = slot_card_write(slotnr, &data[data_i], 1); // transmit command data
617 if (ERR_NONE != rc) {
618 printf("error in command data transmit (errno = %d)\r\n", rc);
619 return rc;
620 }
621 } else { // receive command data byte
622 rc = slot_card_read(slotnr, &data[data_i], 1, ISO7816_3_DEFAULT_WT);
623 if (ERR_NONE != rc) {
624 printf("error in command data receive (errno = %d)\r\n", rc);
625 return rc;
626 }
627 }
628 printf("%02x ", data[data_i]);
629 data_i += 1; // remember we transferred one data byte
630 pb = 0x60; // wait for SW1
631 } else { // invalid byte
632 return ERR_INVALID_DATA;
633 }
634 }
635
636 // read SW2
637 uint8_t sw2;
638 rc = slot_card_read(slotnr, &sw2, 1, ISO7816_3_DEFAULT_WT);
639 if (ERR_NONE != rc) {
640 printf("error in receiving SW2 (errno = %d)\r\n", rc);
641 return rc;
642 }
643 printf("%02x", sw2);
644
645 printf("\r\n");
646 return ERR_NONE;
Kévin Redon0f050722019-05-02 15:56:25 +0200647}
648
Harald Welte1b9a5b82019-02-24 23:04:45 +0100649DEFUN(sim_status, cmd_sim_status, "sim-status", "Get state of specified NCN8025")
650{
651 struct ncn8025_settings settings;
652 int slotnr = validate_slotnr(argc, argv, 1);
653 if (slotnr < 0)
654 return;
655 ncn8025_get(slotnr, &settings);
656 printf("SIM%d: ", slotnr);
657 ncn8025_dump(&settings);
658 printf("\r\n");
659}
660
661DEFUN(sim_power, cmd_sim_power, "sim-power", "Enable/disable SIM card power")
662{
663 struct ncn8025_settings settings;
664 int slotnr = validate_slotnr(argc, argv, 1);
665 int enable;
666
667 if (slotnr < 0)
668 return;
669
670 if (argc < 3) {
671 printf("You have to specify 0=disable or 1=enable\r\n");
672 return;
673 }
674 enable = atoi(argv[2]);
675 ncn8025_get(slotnr, &settings);
676 if (enable)
677 settings.cmdvcc = true;
678 else
679 settings.cmdvcc = false;
680 ncn8025_set(slotnr, &settings);
681}
682
683DEFUN(sim_reset, cmd_sim_reset, "sim-reset", "Enable/disable SIM reset")
684{
685 struct ncn8025_settings settings;
686 int slotnr = validate_slotnr(argc, argv, 1);
687 int enable;
688
689 if (slotnr < 0)
690 return;
691
692 if (argc < 3) {
693 printf("You have to specify 0=disable or 1=enable\r\n");
694 return;
695 }
696 enable = atoi(argv[2]);
697 ncn8025_get(slotnr, &settings);
698 if (enable)
699 settings.rstin = true;
700 else
701 settings.rstin = false;
702 ncn8025_set(slotnr, &settings);
703}
704
705DEFUN(sim_clkdiv, cmd_sim_clkdiv, "sim-clkdiv", "Set SIM clock divider (1,2,4,8)")
706{
707 struct ncn8025_settings settings;
708 int slotnr = validate_slotnr(argc, argv, 1);
709 int clkdiv;
710
711 if (slotnr < 0)
712 return;
713
714 if (argc < 3) {
715 printf("You have to specify a valid divider (1,2,4,8)\r\n");
716 return;
717 }
718 clkdiv = atoi(argv[2]);
719 if (clkdiv != 1 && clkdiv != 2 && clkdiv != 4 && clkdiv != 8) {
720 printf("You have to specify a valid divider (1,2,4,8)\r\n");
721 return;
722 }
723 ncn8025_get(slotnr, &settings);
724 switch (clkdiv) {
725 case 1:
726 settings.clkdiv = SIM_CLKDIV_1;
727 break;
728 case 2:
729 settings.clkdiv = SIM_CLKDIV_2;
730 break;
731 case 4:
732 settings.clkdiv = SIM_CLKDIV_4;
733 break;
734 case 8:
735 settings.clkdiv = SIM_CLKDIV_8;
736 break;
737 }
738 ncn8025_set(slotnr, &settings);
739}
740
741DEFUN(sim_voltage, cmd_sim_voltage, "sim-voltage", "Set SIM voltage (5/3/1.8)")
742{
743 struct ncn8025_settings settings;
744 int slotnr = validate_slotnr(argc, argv, 1);
745
746 if (slotnr < 0)
747 return;
748
749 if (argc < 3) {
750 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
751 return;
752 }
753 ncn8025_get(slotnr, &settings);
754 if (!strcmp(argv[2], "5"))
755 settings.vsel = SIM_VOLT_5V0;
756 else if (!strcmp(argv[2], "3"))
757 settings.vsel = SIM_VOLT_3V0;
758 else if (!strcmp(argv[2], "1.8"))
759 settings.vsel = SIM_VOLT_1V8;
760 else {
761 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
762 return;
763 }
764 ncn8025_set(slotnr, &settings);
765}
766
767DEFUN(sim_led, cmd_sim_led, "sim-led", "Set SIM LED (1=on, 0=off)")
768{
769 struct ncn8025_settings settings;
770 int slotnr = validate_slotnr(argc, argv, 1);
771
772 if (slotnr < 0)
773 return;
774
775 if (argc < 3) {
776 printf("You have to specify 0=disable or 1=enable\r\n");
777 return;
778 }
779 ncn8025_get(slotnr, &settings);
780 if (atoi(argv[2]))
781 settings.led = true;
782 else
783 settings.led = false;
784 ncn8025_set(slotnr, &settings);
785}
786
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200787DEFUN(sim_atr, cmd_sim_atr, "sim-atr", "Read ATR from SIM card")
788{
789 struct ncn8025_settings settings;
790 int slotnr = validate_slotnr(argc, argv, 1);
Harald Welte1b9a5b82019-02-24 23:04:45 +0100791
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200792 if (slotnr < 0 || slotnr >= ARRAY_SIZE(SIM_peripheral_descriptors) || NULL == SIM_peripheral_descriptors[slotnr]) {
793 return;
794 }
795
796 // check if card is present (and read current settings)
797 ncn8025_get(slotnr, &settings);
798 if (!settings.simpres) {
Kévin Redon096c5052019-05-09 15:01:17 +0200799 printf("(%d) error: no card present\r\n", slotnr);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200800 return;
801 }
802
803 // switch card off (assert reset and disable power)
804 // note: ISO/IEC 7816-3:2006 section 6.4 provides the deactivation sequence, but not the minimum corresponding times
805 settings.rstin = true;
806 settings.cmdvcc = false;
Harald Weltedcf57832019-04-17 17:29:41 +0200807 settings.led = true;
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200808 ncn8025_set(slotnr, &settings);
809
810 // TODO wait some time for card to be completely deactivated
811 usart_async_flush_rx_buffer(SIM_peripheral_descriptors[slotnr]); // flush RX buffer to start from scratch
812
Kévin Redon0f050722019-05-02 15:56:25 +0200813
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200814 // set clock to lowest frequency (20 MHz / 8 = 2.5 MHz)
815 // note: according to ISO/IEC 7816-3:2006 section 5.2.3 the minimum value is 1 MHz, and maximum is 5 MHz during activation
816 settings.clkdiv = SIM_CLKDIV_8;
Kévin Redon0f050722019-05-02 15:56:25 +0200817 // set USART baud rate to match the interface (f = 2.5 MHz) and card default settings (Fd = 372, Dd = 1)
818 slot_set_isorate(slotnr, settings.clkdiv, ISO7816_3_DEFAULT_FD, ISO7816_3_DEFAULT_DD);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200819 // set card voltage to 3.0 V (the most supported)
820 // note: according to ISO/IEC 7816-3:2006 no voltage should damage the card, and you should cycle from low to high
821 settings.vsel = SIM_VOLT_3V0;
822 // provide power (the NCN8025 should perform the activation according to spec)
823 // note: activation sequence is documented in ISO/IEC 7816-3:2006 section 6.2
824 settings.cmdvcc = true;
825 ncn8025_set(slotnr, &settings);
826
827 // wait for Tb=400 cycles before re-asserting reset
828 delay_us(400 * 10000 / 2500); // 400 cycles * 1000 for us, 2.5 MHz / 1000 for us
829
830 // de-assert reset to switch card back on
831 settings.rstin = false;
832 ncn8025_set(slotnr, &settings);
833
834 // wait for Tc=40000 cycles for transmission to start
835 uint32_t cycles = 40000;
836 while (cycles && !usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
837 delay_us(10);
838 cycles -= 25; // 10 us = 25 cycles at 2.5 MHz
839 }
840 if (!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
841 delay_us(12 * 372 / 1 / 2); // wait more than one byte (approximate freq down to 2 MHz)
842 }
843 // verify if one byte has been received
844 if (!usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
Kévin Redon096c5052019-05-09 15:01:17 +0200845 printf("(%d) error: card not responsive\r\n", slotnr);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200846 return;
847 }
848
849 // read ATR (just do it until there is no traffic anymore)
Kévin Redon096c5052019-05-09 15:01:17 +0200850 // TODO the ATR should be parsed to read the right number of bytes, instead we just wait until to end of WT
Harald Welte07725812019-04-17 17:30:28 +0200851 printf("(%d) ATR: ", slotnr);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200852 uint8_t atr_byte;
853 while (usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
854 if (1 == io_read(&SIM_peripheral_descriptors[slotnr]->io, &atr_byte, 1)) {
855 printf("%02x ", atr_byte);
856 }
Kévin Redon096c5052019-05-09 15:01:17 +0200857 uint16_t wt = ISO7816_3_DEFAULT_WT; // waiting time in ETU
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200858 while (wt && !usart_async_is_rx_not_empty(SIM_peripheral_descriptors[slotnr])) {
859 delay_us(149); // wait for 1 ETU (372 / 1 / 2.5 MHz = 148.8 us)
860 wt--;
861 }
862 }
863 printf("\r\n");
Harald Weltedcf57832019-04-17 17:29:41 +0200864
Kévin Redon096c5052019-05-09 15:01:17 +0200865 /* disable LED */
866 settings.led = false;
867 ncn8025_set(slotnr, &settings);
868}
869
870DEFUN(sim_iccid, cmd_sim_iccid, "sim-iccid", "Read ICCID from SIM card")
871{
872 struct ncn8025_settings settings;
873 int slotnr = validate_slotnr(argc, argv, 1);
874
875 if (slotnr < 0 || slotnr >= ARRAY_SIZE(SIM_peripheral_descriptors) || NULL == SIM_peripheral_descriptors[slotnr]) {
876 return;
877 }
878
879 // read current settings and check if card is present and powered
880 ncn8025_get(slotnr, &settings);
881 if (!settings.simpres) {
882 printf("(%d) error: no card present\r\n", slotnr);
883 return;
884 }
885 if (!settings.cmdvcc) {
886 printf("(%d) error: card not powered\r\n", slotnr);
887 return;
888 }
889 if (settings.rstin) {
890 printf("(%d) error: card under reset\r\n", slotnr);
891 return;
892 }
893
894 // enable LED
895 if (!settings.led) {
896 settings.led = true;
897 ncn8025_set(slotnr, &settings);
898 }
899
Kévin Redon5188e9f2019-05-09 17:34:55 +0200900 // select MF
901 printf("(%d) SELECT MF\r\n", slotnr);
Kévin Redon36efc6d2019-05-09 18:03:20 +0200902 const uint8_t select_header[] = {0xa0, 0xa4, 0x00, 0x00, 0x02}; // see TS 102.221 sec. 11.1.1
903 const uint8_t select_data_mf[] = {0x3f, 0x00}; // see TS 102.221 sec. 13.1
904 int rc = slot_tpdu_xfer(slotnr, select_header, (uint8_t*)select_data_mf, ARRAY_SIZE(select_data_mf), true); // transfer TPDU
Kévin Redon5188e9f2019-05-09 17:34:55 +0200905 if (ERR_NONE != rc) {
906 printf("error while SELECT MF (errno = %d)\r\n", rc);
Kévin Redon096c5052019-05-09 15:01:17 +0200907 }
Kévin Redon36efc6d2019-05-09 18:03:20 +0200908 // ignore response data
909
910 // select EF_ICCID
911 printf("(%d) SELECT EF_ICCID\r\n", slotnr);
912 const uint8_t select_data_ef_iccid[] = {0x2f, 0xe2}; // see TS 102.221 sec. 13.2
913 rc = slot_tpdu_xfer(slotnr, select_header, (uint8_t*)select_data_ef_iccid, ARRAY_SIZE(select_data_ef_iccid), true); // transfer TPDU
914 if (ERR_NONE != rc) {
915 printf("error while SELECT EF_ICCID (errno = %d)\r\n", rc);
916 }
917 // ignore response data
918
919 // read EF_ICCID
920 printf("(%d) READ EF_ICCID\r\n", slotnr);
921 uint8_t iccid[10];
922 uint8_t read_binary[] = {0xa0, 0xb0, 0x00, 0x00, ARRAY_SIZE(iccid)}; // see TS 102.221 sec. 11.1.3
923 rc = slot_tpdu_xfer(slotnr, read_binary, iccid, ARRAY_SIZE(iccid), false); // transfer TPDU
924 if (ERR_NONE != rc) {
925 printf("error while READ ICCID (errno = %d)\r\n", rc);
926 }
927 // ignore response data
928
929 printf("(%d) ICCID: ", slotnr);
930 for (uint8_t i = 0; i < ARRAY_SIZE(iccid); i++) {
931 uint8_t nibble = iccid[i] & 0xf;
932 if (0xf == nibble) {
933 break;
934 }
935 printf("%x", nibble);
936 nibble = iccid[i] >> 4;
937 if (0xf == nibble) {
938 break;
939 }
940 printf("%x", nibble);
941 }
942 printf("\r\n");
Kévin Redon096c5052019-05-09 15:01:17 +0200943
944 // disable LED
Harald Weltedcf57832019-04-17 17:29:41 +0200945 settings.led = false;
946 ncn8025_set(slotnr, &settings);
Kévin Redonc89bb8c2019-04-17 01:20:23 +0200947}
Harald Welte1b9a5b82019-02-24 23:04:45 +0100948
Harald Welte2dc67e92019-05-17 18:01:46 +0200949DEFUN(get_time, cmd_get_time, "get-time", "Read Time from RTC")
950{
951 struct calendar_date_time dt;
952 calendar_get_date_time(&CALENDAR_0, &dt);
953 printf("%04u-%02u-%02u %02u:%02u:%02u\r\n", dt.date.year, dt.date.month, dt.date.day,
954 dt.time.hour, dt.time.min, dt.time.sec);
955}
956
Harald Welte1017a752019-05-17 20:39:49 +0200957#include <osmocom/core/timer.h>
958static struct osmo_timer_list t;
959static void tmr_cb(void *data)
960{
961 printf("timer fired!\r\n");
962}
963DEFUN(test_timer, cmd_test_timer, "test-timer", "Test osmo_timer")
964{
965 printf("Setting up timer for 3s...\n\r");
966 osmo_timer_setup(&t, &tmr_cb, NULL);
967 osmo_timer_schedule(&t, 3, 0);
968}
969
Harald Welte2dc67e92019-05-17 18:01:46 +0200970
Harald Welte67b2aba2019-04-16 20:47:22 +0200971extern void testmode_init(void);
Harald Weltebdf1b352019-05-17 10:21:45 +0200972extern void libosmo_emb_init(void);
Harald Welte1017a752019-05-17 20:39:49 +0200973extern void libosmo_emb_mainloop(void);
Harald Welte1b9a5b82019-02-24 23:04:45 +0100974
Harald Welte8049d662019-04-17 21:19:18 +0200975#include "talloc.h"
Harald Weltebdf1b352019-05-17 10:21:45 +0200976#include "logging.h"
Harald Welte3304ca22019-04-17 22:08:57 +0200977#include <osmocom/core/msgb.h>
Harald Welte8049d662019-04-17 21:19:18 +0200978void *g_tall_ctx;
979
980DEFUN(_talloc_report, cmd_talloc_report, "talloc-report", "Generate a talloc report")
981{
982 talloc_report_full(g_tall_ctx, stdout);
983}
984
985DEFUN(talloc_test, cmd_talloc_test, "talloc-test", "Test the talloc allocator")
986{
987 for (int i = 0; i < 10; i++)
988 talloc_named_const(g_tall_ctx, 10, "sibling");
989}
990
991DEFUN(v_talloc_free, cmd_talloc_free, "talloc-free", "Release all memory")
992{
993 talloc_free(g_tall_ctx);
994 g_tall_ctx = NULL;
995}
996
Harald Welte65101be2019-04-18 18:30:49 +0200997/* Section 9.6 of SAMD5x/E5x Family Data Sheet */
998static int get_chip_unique_serial(uint8_t *out, size_t len)
999{
1000 uint32_t *out32 = (uint32_t *)out;
1001 if (len < 16)
1002 return -EINVAL;
1003
1004 out32[0] = *(uint32_t *)0x008061fc;
1005 out32[1] = *(uint32_t *)0x00806010;
1006 out32[2] = *(uint32_t *)0x00806014;
1007 out32[3] = *(uint32_t *)0x00806018;
1008
1009 return 0;
1010}
1011
1012/* same as get_chip_unique_serial but in hex-string format */
1013static int get_chip_unique_serial_str(char *out, size_t len)
1014{
1015 uint8_t buf[16];
1016 int rc;
1017
1018 if (len < 16*2 + 1)
1019 return -EINVAL;
1020
1021 rc = get_chip_unique_serial(buf, sizeof(buf));
1022 if (rc < 0)
1023 return rc;
1024 osmo_hexdump_buf(out, len, buf, sizeof(buf), NULL, false);
1025 return 0;
1026}
1027
Harald Welte9ab4bc82019-05-17 18:36:01 +02001028#define RSTCAUSE_STR_SIZE 64
1029static void get_rstcause_str(char *out)
1030{
1031 uint8_t val = hri_rstc_read_RCAUSE_reg(RSTC);
1032 *out = '\0';
1033 if (val & RSTC_RCAUSE_POR)
1034 strcat(out, "POR ");
1035 if (val & RSTC_RCAUSE_BODCORE)
1036 strcat(out, "BODCORE ");
1037 if (val & RSTC_RCAUSE_BODVDD)
1038 strcat(out, "BODVDD ");
1039 if (val & RSTC_RCAUSE_NVM)
1040 strcat(out, "NVM ");
1041 if (val & RSTC_RCAUSE_EXT)
1042 strcat(out, "EXT ");
1043 if (val & RSTC_RCAUSE_WDT)
1044 strcat(out, "WDT ");
1045 if (val & RSTC_RCAUSE_SYST)
1046 strcat(out, "SYST ");
1047 if (val & RSTC_RCAUSE_BACKUP)
1048 strcat(out, "BACKUP ");
1049}
1050
Kévin Redon69b92d92019-01-24 16:39:20 +01001051int main(void)
1052{
Harald Welte65101be2019-04-18 18:30:49 +02001053 char sernr_buf[16*2+1];
Harald Welte9ab4bc82019-05-17 18:36:01 +02001054 char rstcause_buf[RSTCAUSE_STR_SIZE];
Harald Welte65101be2019-04-18 18:30:49 +02001055
Kévin Redon69b92d92019-01-24 16:39:20 +01001056 atmel_start_init();
Harald Welte65101be2019-04-18 18:30:49 +02001057 get_chip_unique_serial_str(sernr_buf, sizeof(sernr_buf));
Harald Welte9ab4bc82019-05-17 18:36:01 +02001058 get_rstcause_str(rstcause_buf);
Kévin Redon78d2f442019-01-24 18:45:59 +01001059
Kévin Redon8e538002019-01-30 11:19:19 +01001060 usb_start();
1061
Harald Weltec3f170d2019-02-24 09:06:59 +01001062 board_init();
Harald Welteff9f4ce2019-02-24 22:51:09 +01001063 command_init("sysmoOCTSIM> ");
Harald Welte1b9a5b82019-02-24 23:04:45 +01001064 command_register(&cmd_sim_status);
1065 command_register(&cmd_sim_power);
1066 command_register(&cmd_sim_reset);
1067 command_register(&cmd_sim_clkdiv);
1068 command_register(&cmd_sim_voltage);
1069 command_register(&cmd_sim_led);
Kévin Redonc89bb8c2019-04-17 01:20:23 +02001070 command_register(&cmd_sim_atr);
Kévin Redon096c5052019-05-09 15:01:17 +02001071 command_register(&cmd_sim_iccid);
Harald Welte67b2aba2019-04-16 20:47:22 +02001072 testmode_init();
Harald Welte8049d662019-04-17 21:19:18 +02001073 command_register(&cmd_talloc_test);
1074 command_register(&cmd_talloc_report);
1075 command_register(&cmd_talloc_free);
Harald Welte2dc67e92019-05-17 18:01:46 +02001076 command_register(&cmd_get_time);
Harald Welte1017a752019-05-17 20:39:49 +02001077 command_register(&cmd_test_timer);
Harald Weltec3f170d2019-02-24 09:06:59 +01001078
Harald Welte729a7622019-05-17 11:02:11 +02001079 printf("\r\n\r\n"
1080 "=============================================================================\n\r"
1081 "sysmoOCTSIM firmware " GIT_VERSION "\n\r"
1082 "(C) 2018-2019 by sysmocom - s.f.m.c. GmbH and contributors\n\r"
1083 "=============================================================================\n\r");
1084 printf("Chip ID: %s\r\n", sernr_buf);
Harald Welte9ab4bc82019-05-17 18:36:01 +02001085 printf("Reset cause: %s\r\n", rstcause_buf);
Harald Weltee7aa5342019-04-16 21:11:14 +02001086
Harald Welte8049d662019-04-17 21:19:18 +02001087 talloc_enable_null_tracking();
1088 g_tall_ctx = talloc_named_const(NULL, 0, "global");
1089 printf("g_tall_ctx=%p\r\n", g_tall_ctx);
Harald Weltebdf1b352019-05-17 10:21:45 +02001090
1091 libosmo_emb_init();
1092
1093 LOGP(DUSB, LOGL_ERROR, "foobar usb\n");
Harald Welte8049d662019-04-17 21:19:18 +02001094
Harald Weltee7aa5342019-04-16 21:11:14 +02001095 command_print_prompt();
Kévin Redon8e538002019-01-30 11:19:19 +01001096 while (true) { // main loop
Harald Welteff9f4ce2019-02-24 22:51:09 +01001097 command_try_recv();
Harald Welte5a8af4d2019-05-12 15:57:20 +02001098 poll_card_detect();
Harald Welte1017a752019-05-17 20:39:49 +02001099 osmo_timers_update();
Kévin Redon8e538002019-01-30 11:19:19 +01001100 }
Kévin Redon69b92d92019-01-24 16:39:20 +01001101}