blob: 344ab09cc5f72ea99e0ea3dc1081a8038f229e19 [file] [log] [blame]
Harald Welte6d1128e2017-05-05 20:23:10 +02001#include <osmocom/core/timer.h>
2#include "board.h"
3#include "utils.h"
4#include "card_pres.h"
5
6#define NUM_CARDPRES 2
7
8#define TIMER_INTERVAL_MS 500
9
10static const Pin pin_cardpres[NUM_CARDPRES] = { PIN_DET_USIM1_PRES, PIN_DET_USIM2_PRES };
11static int last_state[NUM_CARDPRES] = { -1, -1 };
12static struct osmo_timer_list cardpres_timer;
13
14/* Determine if a SIM card is present in the given slot */
15int is_card_present(int port)
16{
17 const Pin *pin;
18 int present;
19
20 if (port < 1 || port > NUM_CARDPRES)
21 return -1;
22 pin = &pin_cardpres[port-1];
23
24 /* Card present signals are low-active, as we have a switch
25 * against GND and an internal-pull-up in the SAM3 */
26 present = PIO_Get(pin) ? 0 : 1;
27
28 return present;
29}
30
31static void cardpres_tmr_cb(void *data)
32{
33 unsigned int i;
34
35 for (i = 1; i <= ARRAY_SIZE(pin_cardpres); i++) {
36 int state = is_card_present(i);
37 if (state != last_state[i-1]) {
38 TRACE_INFO("Card Detect %d Status %d -> %d\r\n", i, last_state[i], state);
39 /* FIXME: report to USB host */
40 last_state[i-1] = state;
41 }
42 }
43
44 osmo_timer_schedule(&cardpres_timer, 0, TIMER_INTERVAL_MS*1000);
45}
46
47int card_present_init(void)
48{
49 unsigned int i;
50
51 PIO_Configure(pin_cardpres, ARRAY_SIZE(pin_cardpres));
52
53 /* start timer */
54 cardpres_timer.cb = cardpres_tmr_cb;
55 osmo_timer_schedule(&cardpres_timer, 0, TIMER_INTERVAL_MS*1000);
56
57 return 2;
58}