blob: 2a16bd109d2966af738a6bacd111a500924a139e [file] [log] [blame]
Christina Quastdb7b1ab2015-03-03 12:34:36 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/*------------------------------------------------------------------------------
31 * Headers
32 *------------------------------------------------------------------------------*/
33
34#include "board.h"
35
36#include <string.h>
37
38/*------------------------------------------------------------------------------
39 * Internal definitions
40 *------------------------------------------------------------------------------*/
41/** Maximum ucSize in bytes of the smartcard answer to a command.*/
42#define MAX_ANSWER_SIZE 10
43
44/** Maximum ATR ucSize in bytes.*/
45#define MAX_ATR_SIZE 55
46
47
48/*------------------------------------------------------------------------------
49 * Internal variables
50 *------------------------------------------------------------------------------*/
51
52/** ISO7816 pins */
53static const Pin pinsISO7816[] = {PINS_ISO7816};
Christina Quast53a76082015-03-04 19:01:34 +010054/** Bus switch pins */
55static const Pin pinsBus[] = {PINS_BUS_DEFAULT};
56/* SIMcard power pin */
57static const Pin pinsPower[] = {PWR_PINS};
Christina Quastdb7b1ab2015-03-03 12:34:36 +010058/** ISO7816 RST pin */
59static const Pin pinIso7816RstMC = PIN_ISO7816_RSTMC;
60static uint8_t sim_inserted = 0;
61
Christina Quaste24b9ac2015-04-10 17:44:49 +020062static struct Usart_info usart_info = {.base = USART_SIM, .id = ID_USART_SIM};
63
Christina Quastdb7b1ab2015-03-03 12:34:36 +010064/*------------------------------------------------------------------------------
65 * Optional smartcard detection
66 *------------------------------------------------------------------------------*/
67
68#ifdef SMARTCARD_CONNECT_PIN
69
70/** Smartcard detection pin.*/
71static const Pin pinSmartCard = SMARTCARD_CONNECT_PIN;
72
73/**
74 * PIO interrupt service routine. Checks if the smartcard has been connected
75 * or disconnected.
76 */
77static void ISR_PioSmartCard( const Pin *pPin )
78{
79/* FIXME: why is pinSmartCard.pio->PIO_ISR the wrong number?
80 printf("+++++ Trying to check for pending interrupts (PIO ISR: 0x%X)\n\r", pinSmartCard.pio->PIO_ISR);
81 printf("+++++ Mask: 0x%X\n\r", pinSmartCard.mask);
82Output:
83 +++++ Trying to check for pending interrupts (PIO ISR: 0x400)) = 1<<10
84 +++++ Mask: 0x100 = 1<<8
85*/
86 // PA10 is DTXD, which is the debug uart transmit pin
87
88 printf("Interrupt!!\n\r");
89 /* Check all pending interrupts */
90 // FIXME: this if condition is not always true...
91// if ( (pinSmartCard.pio->PIO_ISR & pinSmartCard.mask) != 0 )
92 {
93 /* Check current level on pin */
94 if ( PIO_Get( &pinSmartCard ) == 0 )
95 {
96 sim_inserted = 1;
97 printf( "-I- Smartcard inserted\n\r" ) ;
98 CCID_Insertion();
99 }
100 else
101 {
102 sim_inserted = 0;
103 printf( "-I- Smartcard removed\n\r" ) ;
104 CCID_Removal();
105 }
106 }
107}
108
109/**
110 * Configures the smartcard detection pin to trigger an interrupt.
111 */
112static void ConfigureCardDetection( void )
113{
114 printf("+++++ Configure PIOs\n\r");
115 PIO_Configure( &pinSmartCard, 1 ) ;
116 NVIC_EnableIRQ( PIOA_IRQn );
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100117 PIO_EnableIt( &pinSmartCard ) ;
118}
119
120#else
121
122/**
123 * Dummy implementation.
124 */
125static void ConfigureCardDetection( void )
126{
127 printf( "-I- Smartcard detection not supported.\n\r" ) ;
128}
129
130#endif
131
132
133/*-----------------------------------------------------------------------------
134 * Initialization and run
135 *-----------------------------------------------------------------------------*/
Christina Quast53a76082015-03-04 19:01:34 +0100136extern CCIDDriverConfigurationDescriptors configurationDescriptorCCID;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100137
Christina Quast95d66162015-04-09 22:38:47 +0200138void CCID_configure ( void ) {
139 CCIDDriver_Initialize();
140// FIXME: Do we need to set priority?: NVIC_SetPriority( PIOA_IRQn, 10);
141 PIO_ConfigureIt( &pinSmartCard, ISR_PioSmartCard ) ;
142}
143
144void CCID_exit ( void ) {
145 PIO_DisableIt( &pinSmartCard ) ;
146}
147
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100148void CCID_init( void )
149{
Christina Quast53a76082015-03-04 19:01:34 +0100150 uint8_t pAtr[MAX_ATR_SIZE];
151 uint8_t ucSize ;
152
Christina Quast53a76082015-03-04 19:01:34 +0100153 // FIXME: do we want to print ATR?
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100154 /* Initialize Atr buffer */
155 memset( pAtr, 0, sizeof( pAtr ) ) ;
156
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100157 ConfigureCardDetection() ;
158
Christina Quast53a76082015-03-04 19:01:34 +0100159 // Configure ISO7816 driver
160 PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816));
161 PIO_Configure(pinsBus, PIO_LISTSIZE(pinsBus));
162 PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower));
163
164 /* power up the card */
165// PIO_Set(&pinsPower[0]);
166
Christina Quaste24b9ac2015-04-10 17:44:49 +0200167 ISO7816_Init(&usart_info, CLK_MASTER);
168 ISO7816_Set_Reset_Pin(&pinIso7816RstMC);
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100169 /* Read ATR */
170 ISO7816_warm_reset() ;
171
172 ISO7816_Datablock_ATR( pAtr, &ucSize ) ;
173
174 /* Decode ATR and print it */
175 ISO7816_Decode_ATR( pAtr ) ;
176
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100177 // FIXME. what if smcard is not inserted?
178 if(PIO_Get(&pinSmartCard) == 0) {
179 printf("SIM card inserted\n\r");
180 CCID_Insertion();
181 }
182}
183
184void CCID_run( void )
185{
Christina Quast53a76082015-03-04 19:01:34 +0100186
187 //if (USBD_Read(INT, pBuffer, dLength, fCallback, pArgument);
188
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100189 CCID_SmartCardRequest();
190}