blob: fd887a927a75f125b100d7f68ffdc910351c12c8 [file] [log] [blame]
Harald Welte2a6d3af2016-02-28 19:29:14 +01001#include "board.h"
2#include "card_emu.h"
3
4static const Pin pins_cardsim[] = PINS_CARDSIM;
5
6/* UART pins */
7static const Pin pins_usim1[] = {PINS_USIM1};
8static const Pin pin_usim1_rst = PIN_USIM1_nRST;
9static const Pin pin_usim1_vcc = PIN_USIM1_VCC;
10
11#ifdef CARDEMU_SECOND_UART
12static const Pin pins_usim2[] = {PINS_USIM1};
13static const Pin pin_usim2_rst = PIN_USIM2_nRST;
14static const Pin pin_usim2_vcc = PIN_USIM2_VCC;
15#endif
16
17static struct card_handle *ch1, *ch2;
18
19static struct Usart_info usart_info[] = {
20 {
21 .base = USART1,
22 .id = ID_USART1,
23 .state = USART_RCV
24 },
25#ifdef CARDEMU_SECOND_UART
26 {
27 .base = USART0,
28 .id = ID_USART0,
29 .state = USART_RCV
30 },
31#endif
32};
33
34static Usart *get_usart_by_chan(uint8_t uart_chan)
35{
36 switch (uart_chan) {
37 case 0:
38 return USART1;
39#ifdef CARDEMU_SECOND_UART
40 case 1:
41 return USART0;
42#endif
43 }
44 return NULL;
45}
46
47/***********************************************************************
48 * Call-Backs from card_emu.c
49 ***********************************************************************/
50
51/* call-back from card_emu.c to enable/disable transmit and/or receive */
52void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
53{
54 Usart *usart = get_usart_by_chan(uart_chan);
55 switch (rxtx) {
56 case ENABLE_TX:
57 USART_SetReceiverEnabled(usart, 0);
58 USART_SetTransmitterEnabled(usart, 1);
59 break;
60 case ENABLE_RX:
61 USART_SetTransmitterEnabled(usart, 0);
62 USART_SetReceiverEnabled(usart, 1);
63 break;
64 case 0:
65 default:
66 USART_SetTransmitterEnabled(usart, 0);
67 USART_SetReceiverEnabled(usart, 0);
68 break;
69 }
70}
71
72/* call-back from card_emu.c to transmit a byte */
73int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
74{
75 Usart_info *ui = &usart_info[uart_chan];
76 ISO7816_SendChar(byte, ui);
77 return 1;
78}
79
80
81/* FIXME: integrate this with actual irq handler */
82void usart_irq_rx(uint8_t uart, uint32_t csr, uint8_t byte)
83{
84 struct card_handle *ch = ch1;
85#ifdef CARDEMU_SECOND_UART
86 if (uart == 0)
87 ch = ch2;
88#endif
89
90 if (csr & US_CSR_TXRDY)
91 card_emu_tx_byte(ch);
92
93 if (csr & US_CSR_RXRDY)
94 card_emu_process_rx_byte(ch, byte);
95
96 if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE|
97 US_CSR_TIMEOUT|US_CSR_NACK|(1<<10))) {
98 TRACE_DEBUG("e 0x%x st: 0x%x\n", byte, csr);
99 }
100}
101
102
103/***********************************************************************
104 * Core USB / mainloop integration
105 ***********************************************************************/
106
107/* executed once at system boot for each config */
108void mode_cardemu_configure(void)
109{
110}
111
112/* called if config is activated */
113void mode_cardemu_init(void)
114{
115 PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim));
116
117 PIO_Configure(pins_usim1, PIO_LISTSIZE(pins_usim1));
118 ISO7816_Init(&usart_info[0], CLK_SLAVE);
119 USART_EnableIt(USART1, US_IER_RXRDY);
120 NVIC_EnableIRQ(USART1_IRQn);
121 ch1 = card_emu_init(0, 2, 0);
122
123#ifdef CARDEMU_SECOND_UART
124 PIO_Configure(pins_usim2, PIO_LISTSIZE(pins_usim2));
125 ISO7816_Init(&usart_info[1], CLK_SLAVE);
126 USART_EnableIt(USART0, US_IER_RXRDY);
127 NVIC_EnableIRQ(USART0_IRQn);
128 ch2 = card_emu_init(1, 0, 1);
129#endif
130}
131
132/* called if config is deactivated */
133void mode_cardemu_exit(void)
134{
135 NVIC_DisableIRQ(USART1_IRQn);
136 USART_SetTransmitterEnabled(USART1, 0);
137 USART_SetReceiverEnabled(USART1, 0);
138
139#ifdef CARDEMU_SECOND_UART
140 NVIC_DisableIRQ(USART0_IRQn);
141 USART_SetTransmitterEnabled(USART0, 0);
142 USART_SetReceiverEnabled(USART0, 0);
143#endif
144}
145
146/* main loop function, called repeatedly */
147void mode_cardemu_run(void)
148{
149 int rst_active, vcc_active;
150
151 /* usb_to_host() is handled by main() */
152
153 if (ch1) {
154 rst_active = PIO_Get(&pin_usim1_rst) ? 0 : 1;
155 vcc_active = PIO_Get(&pin_usim1_vcc) ? 1 : 0;
156 card_emu_io_statechg(ch1, CARD_IO_RST, rst_active);
157 card_emu_io_statechg(ch1, CARD_IO_VCC, vcc_active);
158 /* FIXME: clock ? */
159 }
160 usb_from_host(PHONE_DATAOUT);
161
162#ifdef CARDEMU_SECOND_UART
163 if (ch2) {
164 rst_active = PIO_Get(&pin_usim2_rst) ? 0 : 1;
165 vcc_active = PIO_Get(&pin_usim2_vcc) ? 1 : 0;
166 card_emu_io_statechg(ch2, CARD_IO_RST, rst_active);
167 card_emu_io_statechg(ch2, CARD_IO_VCC, vcc_active);
168 /* FIXME: clock ? */
169 }
170 usb_from_host(FIXME);
171#endif
172}