blob: 49e6cea62e8e6448022e6309e947039f63dac08b [file] [log] [blame]
Harald Welte5c583d32017-05-09 06:46:47 +02001/* Code to switch between local (physical) and remote (emulated) SIM */
2
3#include "board.h"
4#include "trace.h"
5#include "sim_switch.h"
6
7#ifdef PIN_SIM_SWITCH1
8static const Pin pin_conn_usim1 = {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
9#endif
10#ifdef PIN_SIM_SWITCH2
11static const Pin pin_conn_usim2 = {PIO_PA28, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
12#endif
13
14static int initialized = 0;
15
16int sim_switch_use_physical(unsigned int nr, int physical)
17{
18 const Pin *pin;
19
20 if (!initialized) {
21 TRACE_ERROR("Somebody forgot to call sim_switch_init()\r\n");
22 sim_switch_init();
23 }
24
25 TRACE_INFO("Modem %d: %s SIM\n\r", nr,
26 physical ? "physical" : "virtual");
27
28 switch (nr) {
29#ifdef PIN_SIM_SWITCH1
30 case 1:
31 pin = &pin_conn_usim1;
32 break;
33#endif
34#ifdef PIN_SIM_SWITCH2
35 case 2:
36 pin = &pin_conn_usim2;
37 break;
38#endif
39 default:
40 TRACE_ERROR("Invalid SIM%u\n\r", nr);
41 return -1;
42 }
43
44 if (physical)
45 PIO_Clear(pin);
46 else
47 PIO_Set(pin);
48
49 return 0;
50}
51
52int sim_switch_init(void)
53{
54 int num_switch = 0;
55#ifdef PIN_SIM_SWITCH1
56 PIO_Configure(&pin_conn_usim1, 1);
57 num_switch++;
58#endif
59#ifdef PIN_SIM_SWITCH2
60 PIO_Configure(&pin_conn_usim2, 1);
61 num_switch++;
62#endif
63 return num_switch;
64}