blob: f17cfeae2d6a3d147636ce6f7a3bd1e1b8154447 [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
62/*------------------------------------------------------------------------------
63 * Optional smartcard detection
64 *------------------------------------------------------------------------------*/
65
66#ifdef SMARTCARD_CONNECT_PIN
67
68/** Smartcard detection pin.*/
69static const Pin pinSmartCard = SMARTCARD_CONNECT_PIN;
70
71/**
72 * PIO interrupt service routine. Checks if the smartcard has been connected
73 * or disconnected.
74 */
75static void ISR_PioSmartCard( const Pin *pPin )
76{
77/* FIXME: why is pinSmartCard.pio->PIO_ISR the wrong number?
78 printf("+++++ Trying to check for pending interrupts (PIO ISR: 0x%X)\n\r", pinSmartCard.pio->PIO_ISR);
79 printf("+++++ Mask: 0x%X\n\r", pinSmartCard.mask);
80Output:
81 +++++ Trying to check for pending interrupts (PIO ISR: 0x400)) = 1<<10
82 +++++ Mask: 0x100 = 1<<8
83*/
84 // PA10 is DTXD, which is the debug uart transmit pin
85
86 printf("Interrupt!!\n\r");
87 /* Check all pending interrupts */
88 // FIXME: this if condition is not always true...
89// if ( (pinSmartCard.pio->PIO_ISR & pinSmartCard.mask) != 0 )
90 {
91 /* Check current level on pin */
92 if ( PIO_Get( &pinSmartCard ) == 0 )
93 {
94 sim_inserted = 1;
95 printf( "-I- Smartcard inserted\n\r" ) ;
96 CCID_Insertion();
97 }
98 else
99 {
100 sim_inserted = 0;
101 printf( "-I- Smartcard removed\n\r" ) ;
102 CCID_Removal();
103 }
104 }
105}
106
107/**
108 * Configures the smartcard detection pin to trigger an interrupt.
109 */
110static void ConfigureCardDetection( void )
111{
112 printf("+++++ Configure PIOs\n\r");
113 PIO_Configure( &pinSmartCard, 1 ) ;
114 NVIC_EnableIRQ( PIOA_IRQn );
115// FIXME: Do we need to set priority?: NVIC_SetPriority( PIOA_IRQn, 10);
116 PIO_ConfigureIt( &pinSmartCard, ISR_PioSmartCard ) ;
117 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 +0100136static const CCIDDriverConfigurationDescriptors *configDescCCID;
137extern CCIDDriverConfigurationDescriptors configurationDescriptorCCID;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100138
139void CCID_init( void )
140{
Christina Quast53a76082015-03-04 19:01:34 +0100141 uint8_t pAtr[MAX_ATR_SIZE];
142 uint8_t ucSize ;
143
144 configDescCCID = &configurationDescriptorCCID;
145
146 // FIXME: do we want to print ATR?
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100147 /* Initialize Atr buffer */
148 memset( pAtr, 0, sizeof( pAtr ) ) ;
149
150 /* Configure IT on Smart Card */
151 ConfigureCardDetection() ;
152
Christina Quast53a76082015-03-04 19:01:34 +0100153 // Configure ISO7816 driver
154 PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816));
155 PIO_Configure(pinsBus, PIO_LISTSIZE(pinsBus));
156 PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower));
157
158 /* power up the card */
159// PIO_Set(&pinsPower[0]);
160
161 ISO7816_Init( &pinIso7816RstMC ) ;
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100162
163 CCIDDriver_Initialize();
164
165 /* Read ATR */
166 ISO7816_warm_reset() ;
167
168 ISO7816_Datablock_ATR( pAtr, &ucSize ) ;
169
170 /* Decode ATR and print it */
171 ISO7816_Decode_ATR( pAtr ) ;
172
173// We have our own USB initialization routine
174// CCIDDriver_Initialize();
175
176 // FIXME. what if smcard is not inserted?
177 if(PIO_Get(&pinSmartCard) == 0) {
178 printf("SIM card inserted\n\r");
179 CCID_Insertion();
180 }
181}
182
Christina Quast5ce38ab2015-03-10 15:16:13 +0100183/* FIXME: Remove this testcode */
184void send_select_file() {
185 #define MAX_ANSWER_SIZE 10
186 static const uint8_t testCommand4[] = {0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00};
187 uint8_t pMessage[MAX_ANSWER_SIZE];
188 uint8_t ucSize ;
189 int i;
190
191 printf("Write msg\n\r");
192 ucSize = ISO7816_XfrBlockTPDU_T0( testCommand4, pMessage, sizeof( testCommand4 ) ) ;
193 // Output smartcard answer
194 if ( ucSize > 0 )
195 {
196 printf( "\n\rAnswer: " ) ;
197 for ( i=0 ; i < ucSize ; i++ )
198 {
199 printf( "0x%02X ", pMessage[i] ) ;
200 }
201 printf( "\n\r" ) ;
202 }
203}
204
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100205void CCID_run( void )
206{
Christina Quast53a76082015-03-04 19:01:34 +0100207
208 //if (USBD_Read(INT, pBuffer, dLength, fCallback, pArgument);
209
Christina Quastdb7b1ab2015-03-03 12:34:36 +0100210 CCID_SmartCardRequest();
211}