blob: 7219741b5f561c1b33496ddff810bbfe281f57df [file] [log] [blame]
Christina Quast32a90ac2015-03-10 16:18:16 +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 * \file
32 *
33 * \section Purpose
34 *
35 * ISO 7816 driver
36 *
37 * \section Usage
38 *
39 * Explanation on the usage of the code made available through the header file.
40 */
41
42/*------------------------------------------------------------------------------
43 * Headers
44 *------------------------------------------------------------------------------*/
45
46#include "board.h"
47
48/*------------------------------------------------------------------------------
49 * Definitions
50 *------------------------------------------------------------------------------*/
51/** Case for APDU commands*/
52#define CASE1 1
53#define CASE2 2
54#define CASE3 3
55
56/** Flip flop for send and receive char */
57#define USART_SEND 0
58#define USART_RCV 1
59
Christina Quast32a90ac2015-03-10 16:18:16 +010060/*-----------------------------------------------------------------------------
61 * Internal variables
62 *-----------------------------------------------------------------------------*/
Christina Quast32a90ac2015-03-10 16:18:16 +010063/** Pin reset master card */
Christina Quast2fcef412015-03-10 15:51:21 +010064static Pin *st_pinIso7816RstMC;
Christina Quast32a90ac2015-03-10 16:18:16 +010065
Christina Quastec9c09e2015-04-16 10:45:39 +020066struct Usart_info usart_sim = {.base = USART_SIM, .id = ID_USART_SIM, .state = USART_RCV};
Christina Quaste24b9ac2015-04-10 17:44:49 +020067
Christina Quast32a90ac2015-03-10 16:18:16 +010068/*----------------------------------------------------------------------------
69 * Internal functions
70 *----------------------------------------------------------------------------*/
71
72/**
73 * Get a character from ISO7816
74 * \param pCharToReceive Pointer for store the received char
75 * \return 0: if timeout else status of US_CSR
76 */
Christina Quaste24b9ac2015-04-10 17:44:49 +020077uint32_t ISO7816_GetChar( uint8_t *pCharToReceive, Usart_info *usart)
Christina Quast32a90ac2015-03-10 16:18:16 +010078{
79 uint32_t status;
80 uint32_t timeout=0;
81
Christina Quaste24b9ac2015-04-10 17:44:49 +020082 Usart *us_base = usart->base;
83 uint32_t us_id = usart->id;
84
Christina Quastec9c09e2015-04-16 10:45:39 +020085 if( usart->state == USART_SEND ) {
Christina Quaste24b9ac2015-04-10 17:44:49 +020086 while((us_base->US_CSR & US_CSR_TXEMPTY) == 0) {}
87 us_base->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Christina Quastec9c09e2015-04-16 10:45:39 +020088 usart->state = USART_RCV;
Christina Quast32a90ac2015-03-10 16:18:16 +010089 }
90
91 /* Wait USART ready for reception */
Christina Quaste24b9ac2015-04-10 17:44:49 +020092 while( ((us_base->US_CSR & US_CSR_RXRDY) == 0) ) {
Harald Welte0633b252017-11-28 22:47:09 +010093 WDT_Restart(WDT);
Christina Quast32a90ac2015-03-10 16:18:16 +010094 if(timeout++ > 12000 * (BOARD_MCK/1000000)) {
95 TRACE_WARNING("TimeOut\n\r");
96 return( 0 );
97 }
98 }
99
100 /* At least one complete character has been received and US_RHR has not yet been read. */
101
102 /* Get a char */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200103 *pCharToReceive = ((us_base->US_RHR) & 0xFF);
Christina Quast32a90ac2015-03-10 16:18:16 +0100104
Christina Quaste24b9ac2015-04-10 17:44:49 +0200105 status = (us_base->US_CSR&(US_CSR_OVRE|US_CSR_FRAME|
Christina Quast32a90ac2015-03-10 16:18:16 +0100106 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
107 (1<<10)));
108
109 if (status != 0 ) {
110 TRACE_DEBUG("R:0x%" PRIX32 "\n\r", status);
Christina Quaste24b9ac2015-04-10 17:44:49 +0200111 TRACE_DEBUG("R:0x%" PRIX32 "\n\r", us_base->US_CSR);
112 TRACE_DEBUG("Nb:0x%" PRIX32 "\n\r", us_base->US_NER );
113 us_base->US_CR = US_CR_RSTSTA;
Christina Quast32a90ac2015-03-10 16:18:16 +0100114 }
115
116 /* Return status */
117 return( status );
118}
119
120
121/**
122 * Send a char to ISO7816
123 * \param CharToSend char to be send
124 * \return status of US_CSR
125 */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200126uint32_t ISO7816_SendChar( uint8_t CharToSend, Usart_info *usart )
Christina Quast32a90ac2015-03-10 16:18:16 +0100127{
128 uint32_t status;
129
Christina Quaste24b9ac2015-04-10 17:44:49 +0200130 Usart *us_base = usart->base;
131 uint32_t us_id = usart->id;
132
Christina Quastec9c09e2015-04-16 10:45:39 +0200133 if( usart->state == USART_RCV ) {
Christina Quaste24b9ac2015-04-10 17:44:49 +0200134 us_base->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Christina Quastec9c09e2015-04-16 10:45:39 +0200135 usart->state = USART_SEND;
Christina Quast32a90ac2015-03-10 16:18:16 +0100136 }
137
138 /* Wait USART ready for transmit */
Christina Quast68cc8592015-04-16 11:08:32 +0200139 int i = 0;
140 while((us_base->US_CSR & (US_CSR_TXRDY)) == 0) {
141 i++;
142 if (!(i%1000000)) {
Harald Welte40901a02016-03-03 10:42:45 +0100143 printf("s: %x ", us_base->US_CSR);
144 printf("s: %x\r\n", us_base->US_RHR & 0xFF);
Christina Quast68cc8592015-04-16 11:08:32 +0200145 us_base->US_CR = US_CR_RSTTX;
146 us_base->US_CR = US_CR_RSTRX;
147 }
148 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100149 /* There is no character in the US_THR */
150
151 /* Transmit a char */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200152 us_base->US_THR = CharToSend;
Christina Quast32a90ac2015-03-10 16:18:16 +0100153
Harald Welte40901a02016-03-03 10:42:45 +0100154 TRACE_ERROR("Sx%02X\r\n", CharToSend);
155
Christina Quaste24b9ac2015-04-10 17:44:49 +0200156 status = (us_base->US_CSR&(US_CSR_OVRE|US_CSR_FRAME|
Christina Quast32a90ac2015-03-10 16:18:16 +0100157 US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
158 (1<<10)));
159
160 if (status != 0 ) {
Christina Quast68cc8592015-04-16 11:08:32 +0200161 TRACE_INFO("******* status: 0x%" PRIX32 " (Overrun: %" PRIX32
Christina Quaste24b9ac2015-04-10 17:44:49 +0200162 ", NACK: %" PRIX32 ", Timeout: %" PRIX32 ", underrun: %" PRIX32 ")\n\r",
163 status, ((status & US_CSR_OVRE)>> 5), ((status & US_CSR_NACK) >> 13),
Christina Quast32a90ac2015-03-10 16:18:16 +0100164 ((status & US_CSR_TIMEOUT) >> 8), ((status & (1 << 10)) >> 10));
Christina Quast68cc8592015-04-16 11:08:32 +0200165 TRACE_INFO("E (USART CSR reg):0x%" PRIX32 "\n\r", us_base->US_CSR);
166 TRACE_INFO("Nb (Number of errors):0x%" PRIX32 "\n\r", us_base->US_NER );
Christina Quaste24b9ac2015-04-10 17:44:49 +0200167 us_base->US_CR = US_CR_RSTSTA;
Christina Quast32a90ac2015-03-10 16:18:16 +0100168 }
169
170 /* Return status */
171 return( status );
172}
173
174
175/**
176 * Iso 7816 ICC power on
177 */
178static void ISO7816_IccPowerOn( void )
179{
180 /* Set RESET Master Card */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200181 if (st_pinIso7816RstMC) {
182 PIO_Set(st_pinIso7816RstMC);
183 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100184}
185
186/*----------------------------------------------------------------------------
187 * Exported functions
188 *----------------------------------------------------------------------------*/
189
190/**
191 * Iso 7816 ICC power off
192 */
193void ISO7816_IccPowerOff( void )
194{
195 /* Clear RESET Master Card */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200196 if (st_pinIso7816RstMC) {
197 PIO_Clear(st_pinIso7816RstMC);
198 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100199}
200
201/**
Christina Quast73c2b642015-04-07 13:49:26 +0200202 * Transfert Block TPDU T=0
203 * \param pAPDU APDU buffer
204 * \param pMessage Message buffer
205 * \param wLength Block length
206 * \param indexMsg Message index
207 * \return 0 on success, content of US_CSR otherwise
Christina Quast32a90ac2015-03-10 16:18:16 +0100208 */
Christina Quast73c2b642015-04-07 13:49:26 +0200209uint32_t ISO7816_XfrBlockTPDU_T0(const uint8_t *pAPDU,
Christina Quast32a90ac2015-03-10 16:18:16 +0100210 uint8_t *pMessage,
Christina Quast73c2b642015-04-07 13:49:26 +0200211 uint16_t wLength,
212 uint16_t *retlen )
Christina Quast32a90ac2015-03-10 16:18:16 +0100213{
214 uint16_t NeNc;
215 uint16_t indexApdu = 4;
Christina Quast73c2b642015-04-07 13:49:26 +0200216 uint16_t indexMsg = 0;
Christina Quast32a90ac2015-03-10 16:18:16 +0100217 uint8_t SW1 = 0;
218 uint8_t procByte;
219 uint8_t cmdCase;
Christina Quast73c2b642015-04-07 13:49:26 +0200220 uint32_t status = 0;
Christina Quast32a90ac2015-03-10 16:18:16 +0100221
Christina Quastc63df592015-03-10 23:32:45 +0100222 TRACE_INFO("pAPDU[0]=0x%X\n\r",pAPDU[0]);
223 TRACE_INFO("pAPDU[1]=0x%X\n\r",pAPDU[1]);
224 TRACE_INFO("pAPDU[2]=0x%X\n\r",pAPDU[2]);
225 TRACE_INFO("pAPDU[3]=0x%X\n\r",pAPDU[3]);
226 TRACE_INFO("pAPDU[4]=0x%X\n\r",pAPDU[4]);
227 TRACE_INFO("pAPDU[5]=0x%X\n\r",pAPDU[5]);
228 TRACE_INFO("wlength=%d\n\r",wLength);
Christina Quast32a90ac2015-03-10 16:18:16 +0100229
Christina Quaste24b9ac2015-04-10 17:44:49 +0200230 ISO7816_SendChar( pAPDU[0], &usart_sim ); /* CLA */
231 ISO7816_SendChar( pAPDU[1], &usart_sim ); /* INS */
232 ISO7816_SendChar( pAPDU[2], &usart_sim ); /* P1 */
233 ISO7816_SendChar( pAPDU[3], &usart_sim ); /* P2 */
234 ISO7816_SendChar( pAPDU[4], &usart_sim ); /* P3 */
Christina Quast32a90ac2015-03-10 16:18:16 +0100235
236 /* Handle the four structures of command APDU */
Christina Quastc63df592015-03-10 23:32:45 +0100237 indexApdu = 5;
Christina Quast32a90ac2015-03-10 16:18:16 +0100238
239 if( wLength == 4 ) {
240 cmdCase = CASE1;
241 NeNc = 0;
242 }
243 else if( wLength == 5) {
244 cmdCase = CASE2;
245 NeNc = pAPDU[4]; /* C5 */
246 if (NeNc == 0) {
247 NeNc = 256;
248 }
249 }
250 else if( wLength == 6) {
251 NeNc = pAPDU[4]; /* C5 */
252 cmdCase = CASE3;
253 }
254 else if( wLength == 7) {
255 NeNc = pAPDU[4]; /* C5 */
256 if( NeNc == 0 ) {
257 cmdCase = CASE2;
258 NeNc = (pAPDU[5]<<8)+pAPDU[6];
259 }
260 else {
261 cmdCase = CASE3;
262 }
263 }
264 else {
265 NeNc = pAPDU[4]; /* C5 */
266 if( NeNc == 0 ) {
267 cmdCase = CASE3;
268 NeNc = (pAPDU[5]<<8)+pAPDU[6];
269 }
270 else {
271 cmdCase = CASE3;
272 }
273 }
274
275 TRACE_DEBUG("CASE=0x%X NeNc=0x%X\n\r", cmdCase, NeNc);
276
277 /* Handle Procedure Bytes */
278 do {
Christina Quaste24b9ac2015-04-10 17:44:49 +0200279 status = ISO7816_GetChar(&procByte, &usart_sim);
Christina Quast73c2b642015-04-07 13:49:26 +0200280 if (status != 0) {
281 return status;
282 }
Christina Quast2fcef412015-03-10 15:51:21 +0100283 TRACE_INFO("procByte: 0x%X\n\r", procByte);
Christina Quast32a90ac2015-03-10 16:18:16 +0100284 /* Handle NULL */
285 if ( procByte == ISO_NULL_VAL ) {
Christina Quast2fcef412015-03-10 15:51:21 +0100286 TRACE_INFO("INS\n\r");
Christina Quast32a90ac2015-03-10 16:18:16 +0100287 continue;
288 }
289 /* Handle SW1 */
290 else if ( ((procByte & 0xF0) ==0x60) || ((procByte & 0xF0) ==0x90) ) {
Christina Quast2fcef412015-03-10 15:51:21 +0100291 TRACE_INFO("SW1\n\r");
Christina Quast32a90ac2015-03-10 16:18:16 +0100292 SW1 = 1;
293 }
294 /* Handle INS */
295 else if ( pAPDU[1] == procByte) {
Christina Quast2fcef412015-03-10 15:51:21 +0100296 TRACE_INFO("HdlINS\n\r");
Christina Quast32a90ac2015-03-10 16:18:16 +0100297 if (cmdCase == CASE2) {
298 /* receive data from card */
299 do {
Christina Quaste24b9ac2015-04-10 17:44:49 +0200300 status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim);
Christina Quast73c2b642015-04-07 13:49:26 +0200301 } while(( 0 != --NeNc) && (status == 0) );
302 if (status != 0) {
303 return status;
304 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100305 }
306 else {
307 /* Send data */
308 do {
Christina Quastc63df592015-03-10 23:32:45 +0100309 TRACE_INFO("Send %X", pAPDU[indexApdu]);
Christina Quaste24b9ac2015-04-10 17:44:49 +0200310 ISO7816_SendChar(pAPDU[indexApdu++], &usart_sim);
Christina Quast32a90ac2015-03-10 16:18:16 +0100311 } while( 0 != --NeNc );
312 }
313 }
314 /* Handle INS ^ 0xff */
Christina Quast767fdeb2015-04-07 13:50:20 +0200315 else
316 #pragma GCC diagnostic push
317 #pragma GCC diagnostic ignored "-Wsign-compare"
318 if ( pAPDU[1] == (procByte ^ 0xff)) {
319 #pragma GCC diagnostic pop
Christina Quast2fcef412015-03-10 15:51:21 +0100320 TRACE_INFO("HdlINS+\n\r");
Christina Quast32a90ac2015-03-10 16:18:16 +0100321 if (cmdCase == CASE2) {
322 /* receive data from card */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200323 status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim);
Christina Quast73c2b642015-04-07 13:49:26 +0200324 if (status != 0) {
325 return status;
326 }
327 TRACE_INFO("Rcv: 0x%X\n\r", pMessage[indexMsg-1]);
Christina Quast32a90ac2015-03-10 16:18:16 +0100328 }
329 else {
Christina Quaste24b9ac2015-04-10 17:44:49 +0200330 status = ISO7816_SendChar(pAPDU[indexApdu++], &usart_sim);
Christina Quast73c2b642015-04-07 13:49:26 +0200331 if (status != 0) {
332 return status;
333 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100334 }
335 NeNc--;
336 }
337 else {
338 /* ?? */
Christina Quast2fcef412015-03-10 15:51:21 +0100339 TRACE_INFO("procByte=0x%X\n\r", procByte);
Christina Quast32a90ac2015-03-10 16:18:16 +0100340 break;
341 }
342 } while (NeNc != 0);
343
344 /* Status Bytes */
345 if (SW1 == 0) {
Christina Quaste24b9ac2015-04-10 17:44:49 +0200346 status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim); /* SW1 */
Christina Quast73c2b642015-04-07 13:49:26 +0200347 if (status != 0) {
348 return status;
349 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100350 }
351 else {
Christina Quast73c2b642015-04-07 13:49:26 +0200352 pMessage[indexMsg++] = procByte;
Christina Quast32a90ac2015-03-10 16:18:16 +0100353 }
Christina Quaste24b9ac2015-04-10 17:44:49 +0200354 status = ISO7816_GetChar(&pMessage[indexMsg++], &usart_sim); /* SW2 */
Christina Quast73c2b642015-04-07 13:49:26 +0200355 if (status != 0) {
356 return status;
357 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100358
Christina Quast73c2b642015-04-07 13:49:26 +0200359 TRACE_WARNING("SW1=0x%X, SW2=0x%X\n\r", pMessage[indexMsg-2], pMessage[indexMsg-1]);
Christina Quast2fcef412015-03-10 15:51:21 +0100360
Christina Quast73c2b642015-04-07 13:49:26 +0200361 *retlen = indexMsg;
362 return status;
Christina Quast32a90ac2015-03-10 16:18:16 +0100363
364}
365
366/**
367 * Escape ISO7816
368 */
369void ISO7816_Escape( void )
370{
371 TRACE_DEBUG("For user, if needed\n\r");
372}
373
374/**
375 * Restart clock ISO7816
376 */
377void ISO7816_RestartClock( void )
378{
379 TRACE_DEBUG("ISO7816_RestartClock\n\r");
Christina Quaste24b9ac2015-04-10 17:44:49 +0200380 USART_SIM->US_BRGR = 13;
Christina Quast32a90ac2015-03-10 16:18:16 +0100381}
382
383/**
384 * Stop clock ISO7816
385 */
386void ISO7816_StopClock( void )
387{
388 TRACE_DEBUG("ISO7816_StopClock\n\r");
Christina Quaste24b9ac2015-04-10 17:44:49 +0200389 USART_SIM->US_BRGR = 0;
Christina Quast32a90ac2015-03-10 16:18:16 +0100390}
391
392/**
393 * T0 APDU
394 */
395void ISO7816_toAPDU( void )
396{
397 TRACE_DEBUG("ISO7816_toAPDU\n\r");
398 TRACE_DEBUG("Not supported at this time\n\r");
399}
400
401/**
402 * Answer To Reset (ATR)
403 * \param pAtr ATR buffer
404 * \param pLength Pointer for store the ATR length
405 * \return 0: if timeout else status of US_CSR
406 */
407uint32_t ISO7816_Datablock_ATR( uint8_t* pAtr, uint8_t* pLength )
408{
409 uint32_t i;
410 uint32_t j;
411 uint32_t y;
412 uint32_t status = 0;
413
414 *pLength = 0;
415
416 /* Read ATR TS */
417 // FIXME: There should always be a check for the GetChar return value..0 means timeout
Christina Quaste24b9ac2015-04-10 17:44:49 +0200418 status = ISO7816_GetChar(&pAtr[0], &usart_sim);
Christina Quast73c2b642015-04-07 13:49:26 +0200419 if (status != 0) {
Christina Quast32a90ac2015-03-10 16:18:16 +0100420 return status;
Christina Quast73c2b642015-04-07 13:49:26 +0200421 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100422
423 /* Read ATR T0 */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200424 status = ISO7816_GetChar(&pAtr[1], &usart_sim);
Christina Quast73c2b642015-04-07 13:49:26 +0200425 if (status != 0) {
426 return status;
427 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100428 y = pAtr[1] & 0xF0;
429 i = 2;
430
431 /* Read ATR Ti */
Christina Quast73c2b642015-04-07 13:49:26 +0200432 while (y && (status == 0)) {
Christina Quast32a90ac2015-03-10 16:18:16 +0100433
434 if (y & 0x10) { /* TA[i] */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200435 status = ISO7816_GetChar(&pAtr[i++], &usart_sim);
Christina Quast32a90ac2015-03-10 16:18:16 +0100436 }
437 if (y & 0x20) { /* TB[i] */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200438 status = ISO7816_GetChar(&pAtr[i++], &usart_sim);
Christina Quast32a90ac2015-03-10 16:18:16 +0100439 }
440 if (y & 0x40) { /* TC[i] */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200441 status = ISO7816_GetChar(&pAtr[i++], &usart_sim);
Christina Quast32a90ac2015-03-10 16:18:16 +0100442 }
443 if (y & 0x80) { /* TD[i] */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200444 status = ISO7816_GetChar(&pAtr[i], &usart_sim);
Christina Quast32a90ac2015-03-10 16:18:16 +0100445 y = pAtr[i++] & 0xF0;
446 }
447 else {
448 y = 0;
449 }
450 }
Christina Quast73c2b642015-04-07 13:49:26 +0200451 if (status != 0) {
452 return status;
453 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100454
455 /* Historical Bytes */
456 y = pAtr[1] & 0x0F;
Christina Quast73c2b642015-04-07 13:49:26 +0200457 for( j=0; (j < y) && (status == 0); j++ ) {
Christina Quaste24b9ac2015-04-10 17:44:49 +0200458 status = ISO7816_GetChar(&pAtr[i++], &usart_sim);
Christina Quast73c2b642015-04-07 13:49:26 +0200459 }
460
461 if (status != 0) {
462 return status;
Christina Quast32a90ac2015-03-10 16:18:16 +0100463 }
464
465 *pLength = i;
Christina Quast73c2b642015-04-07 13:49:26 +0200466 return status;
Christina Quast32a90ac2015-03-10 16:18:16 +0100467}
468
469/**
470 * Set data rate and clock frequency
471 * \param dwClockFrequency ICC clock frequency in KHz.
472 * \param dwDataRate ICC data rate in bpd
473 */
474void ISO7816_SetDataRateandClockFrequency( uint32_t dwClockFrequency, uint32_t dwDataRate )
475{
476 uint8_t ClockFrequency;
477
478 /* Define the baud rate divisor register */
479 /* CD = MCK / SCK */
480 /* SCK = FIDI x BAUD = 372 x 9600 */
481 /* BOARD_MCK */
482 /* CD = MCK/(FIDI x BAUD) = 48000000 / (372x9600) = 13 */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200483 USART_SIM->US_BRGR = BOARD_MCK / (dwClockFrequency*1000);
Christina Quast32a90ac2015-03-10 16:18:16 +0100484
Christina Quaste24b9ac2015-04-10 17:44:49 +0200485 ClockFrequency = BOARD_MCK / USART_SIM->US_BRGR;
Christina Quast32a90ac2015-03-10 16:18:16 +0100486
Christina Quaste24b9ac2015-04-10 17:44:49 +0200487 USART_SIM->US_FIDI = (ClockFrequency)/dwDataRate;
Christina Quast32a90ac2015-03-10 16:18:16 +0100488
489}
490
491/**
492 * Pin status for ISO7816 RESET
493 * \return 1 if the Pin RstMC is high; otherwise 0.
494 */
495uint8_t ISO7816_StatusReset( void )
496{
Christina Quaste24b9ac2015-04-10 17:44:49 +0200497 if (st_pinIso7816RstMC) {
498 return PIO_Get(st_pinIso7816RstMC);
499 }
500 return 0;
Christina Quast32a90ac2015-03-10 16:18:16 +0100501}
502
503/**
504 * cold reset
505 */
506void ISO7816_cold_reset( void )
507{
508 volatile uint32_t i;
509
Christina Quastc870b522015-04-09 13:31:58 +0200510 /* tb: wait ??? cycles*/
511 for( i=0; i<(400*(BOARD_MCK/1000000)); i++ ) {
Christina Quast32a90ac2015-03-10 16:18:16 +0100512 }
513
Christina Quaste24b9ac2015-04-10 17:44:49 +0200514 USART_SIM->US_RHR;
515 USART_SIM->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Christina Quast32a90ac2015-03-10 16:18:16 +0100516
517 ISO7816_IccPowerOn();
518}
519
520/**
521 * Warm reset
522 */
523void ISO7816_warm_reset( void )
524{
525 volatile uint32_t i;
526
527// Clears Reset
528 ISO7816_IccPowerOff();
529
Christina Quastc870b522015-04-09 13:31:58 +0200530 /* tb: wait ??? cycles */
531 for( i=0; i<(400*(BOARD_MCK/1000000)); i++ ) {
Christina Quast32a90ac2015-03-10 16:18:16 +0100532 }
533
Christina Quaste24b9ac2015-04-10 17:44:49 +0200534 USART_SIM->US_RHR;
535 USART_SIM->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Christina Quast32a90ac2015-03-10 16:18:16 +0100536
537// Sets Reset
538 ISO7816_IccPowerOn();
539}
540
541/**
542 * Decode ATR trace
543 * \param pAtr pointer on ATR buffer
544 */
545void ISO7816_Decode_ATR( uint8_t* pAtr )
546{
547 uint32_t i;
548 uint32_t j;
549 uint32_t y;
550 uint8_t offset;
551
552 printf("\n\r");
553 printf("ATR: Answer To Reset:\n\r");
554 printf("TS = 0x%X Initial character ",pAtr[0]);
555 if( pAtr[0] == 0x3B ) {
556
557 printf("Direct Convention\n\r");
558 }
559 else {
560 if( pAtr[0] == 0x3F ) {
561
562 printf("Inverse Convention\n\r");
563 }
564 else {
565 printf("BAD Convention\n\r");
566 }
567 }
568
569 printf("T0 = 0x%X Format caracter\n\r",pAtr[1]);
570 printf(" Number of historical bytes: K = %d\n\r", pAtr[1]&0x0F);
571 printf(" Presence further interface byte:\n\r");
572 if( pAtr[1]&0x80 ) {
573 printf("TA ");
574 }
575 if( pAtr[1]&0x40 ) {
576 printf("TB ");
577 }
578 if( pAtr[1]&0x20 ) {
579 printf("TC ");
580 }
581 if( pAtr[1]&0x10 ) {
582 printf("TD ");
583 }
584 if( pAtr[1] != 0 ) {
585 printf(" present\n\r");
586 }
587
588 i = 2;
589 y = pAtr[1] & 0xF0;
590
591 /* Read ATR Ti */
592 offset = 1;
593 while (y) {
594
595 if (y & 0x10) { /* TA[i] */
596 printf("TA[%d] = 0x%X ", offset, pAtr[i]);
597 if( offset == 1 ) {
598 printf("FI = %d ", (pAtr[i]>>8));
599 printf("DI = %d", (pAtr[i]&0x0F));
600 }
601 printf("\n\r");
602 i++;
603 }
604 if (y & 0x20) { /* TB[i] */
605 printf("TB[%d] = 0x%X\n\r", offset, pAtr[i]);
606 i++;
607 }
608 if (y & 0x40) { /* TC[i] */
609 printf("TC[%d] = 0x%X ", offset, pAtr[i]);
610 if( offset == 1 ) {
611 printf("Extra Guard Time: N = %d", pAtr[i]);
612 }
613 printf("\n\r");
614 i++;
615 }
616 if (y & 0x80) { /* TD[i] */
617 printf("TD[%d] = 0x%X\n\r", offset, pAtr[i]);
618 y = pAtr[i++] & 0xF0;
619 }
620 else {
621 y = 0;
622 }
623 offset++;
624 }
625
626 /* Historical Bytes */
627 printf("Historical bytes:\n\r");
628 y = pAtr[1] & 0x0F;
629 for( j=0; j < y; j++ ) {
630 printf(" 0x%X", pAtr[i]);
631 i++;
632 }
633 printf("\n\r\n\r");
634
635}
636
Christina Quaste24b9ac2015-04-10 17:44:49 +0200637void ISO7816_Set_Reset_Pin(const Pin *pPinIso7816RstMC) {
638 /* Pin ISO7816 initialize */
639 st_pinIso7816RstMC = (Pin *)pPinIso7816RstMC;
640}
641
Christina Quast32a90ac2015-03-10 16:18:16 +0100642/** Initializes a ISO driver
643 * \param pPinIso7816RstMC Pin ISO 7816 Rst MC
644 */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200645void ISO7816_Init( Usart_info *usart, bool master_clock )
Christina Quast32a90ac2015-03-10 16:18:16 +0100646{
Christina Quast3eab56e2015-04-10 15:38:49 +0200647 uint32_t clk;
Christina Quast32a90ac2015-03-10 16:18:16 +0100648 TRACE_DEBUG("ISO_Init\n\r");
649
Christina Quaste24b9ac2015-04-10 17:44:49 +0200650 Usart *us_base = usart->base;
651 uint32_t us_id = usart->id;
Christina Quast32a90ac2015-03-10 16:18:16 +0100652
Christina Quast3eab56e2015-04-10 15:38:49 +0200653 if (master_clock == true) {
654 clk = US_MR_USCLKS_MCK;
655 } else {
656 clk = US_MR_USCLKS_SCK;
657 }
658
Christina Quaste24b9ac2015-04-10 17:44:49 +0200659 USART_Configure( us_base,
Christina Quast32a90ac2015-03-10 16:18:16 +0100660 US_MR_USART_MODE_IS07816_T_0
Christina Quast3eab56e2015-04-10 15:38:49 +0200661 | clk
Christina Quast32a90ac2015-03-10 16:18:16 +0100662 | US_MR_NBSTOP_1_BIT
663 | US_MR_PAR_EVEN
664 | US_MR_CHRL_8_BIT
665 | US_MR_CLKO
Christina Quast68cc8592015-04-16 11:08:32 +0200666 | US_MR_INACK /* Inhibit errors */
667 | (3<<24), /* MAX_ITERATION */
Christina Quast32a90ac2015-03-10 16:18:16 +0100668 1,
669 0);
670
Christina Quast32a90ac2015-03-10 16:18:16 +0100671 /* Disable interrupts */
Christina Quaste24b9ac2015-04-10 17:44:49 +0200672 us_base->US_IDR = (uint32_t) -1;
Christina Quast32a90ac2015-03-10 16:18:16 +0100673
Christina Quast68cc8592015-04-16 11:08:32 +0200674 /* Configure USART */
675 PMC_EnablePeripheral(us_id);
676
Christina Quaste24b9ac2015-04-10 17:44:49 +0200677 us_base->US_FIDI = 372; /* by default */
Christina Quast32a90ac2015-03-10 16:18:16 +0100678 /* Define the baud rate divisor register */
679 /* CD = MCK / SCK */
680 /* SCK = FIDI x BAUD = 372 x 9600 */
681 /* BOARD_MCK */
682 /* CD = MCK/(FIDI x BAUD) = 48000000 / (372x9600) = 13 */
Christina Quast4b814932015-04-17 18:47:34 +0200683 if (master_clock == true) {
684 us_base->US_BRGR = BOARD_MCK / (372*9600);
685 } else {
686 us_base->US_BRGR = US_BRGR_CD(1);
687 }
Christina Quast32a90ac2015-03-10 16:18:16 +0100688}
689