Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 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 */ |
| 53 | static const Pin pinsISO7816[] = {PINS_ISO7816}; |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 54 | /** Bus switch pins */ |
| 55 | static const Pin pinsBus[] = {PINS_BUS_DEFAULT}; |
| 56 | /* SIMcard power pin */ |
| 57 | static const Pin pinsPower[] = {PWR_PINS}; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 58 | /** ISO7816 RST pin */ |
| 59 | static const Pin pinIso7816RstMC = PIN_ISO7816_RSTMC; |
| 60 | static uint8_t sim_inserted = 0; |
| 61 | |
Christina Quast | ec9c09e | 2015-04-16 10:45:39 +0200 | [diff] [blame] | 62 | static struct Usart_info usart_info = {.base = USART_SIM, .id = ID_USART_SIM, .state = USART_RCV}; |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 63 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 64 | /*------------------------------------------------------------------------------ |
| 65 | * Optional smartcard detection |
| 66 | *------------------------------------------------------------------------------*/ |
| 67 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 68 | /** Smartcard detection pin.*/ |
| 69 | static const Pin pinSmartCard = SMARTCARD_CONNECT_PIN; |
| 70 | |
| 71 | /** |
| 72 | * PIO interrupt service routine. Checks if the smartcard has been connected |
| 73 | * or disconnected. |
| 74 | */ |
| 75 | static 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); |
| 80 | Output: |
| 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 | */ |
| 110 | static void ConfigureCardDetection( void ) |
| 111 | { |
| 112 | printf("+++++ Configure PIOs\n\r"); |
| 113 | PIO_Configure( &pinSmartCard, 1 ) ; |
| 114 | NVIC_EnableIRQ( PIOA_IRQn ); |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 115 | PIO_EnableIt( &pinSmartCard ) ; |
| 116 | } |
| 117 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 118 | /*----------------------------------------------------------------------------- |
| 119 | * Initialization and run |
| 120 | *-----------------------------------------------------------------------------*/ |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 121 | extern CCIDDriverConfigurationDescriptors configurationDescriptorCCID; |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 122 | |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 123 | void CCID_configure ( void ) { |
| 124 | CCIDDriver_Initialize(); |
| 125 | // FIXME: Do we need to set priority?: NVIC_SetPriority( PIOA_IRQn, 10); |
| 126 | PIO_ConfigureIt( &pinSmartCard, ISR_PioSmartCard ) ; |
| 127 | } |
| 128 | |
| 129 | void CCID_exit ( void ) { |
| 130 | PIO_DisableIt( &pinSmartCard ) ; |
Christina Quast | 4b81493 | 2015-04-17 18:47:34 +0200 | [diff] [blame^] | 131 | USART_SetTransmitterEnabled(usart_info.base, 0); |
| 132 | USART_SetReceiverEnabled(usart_info.base, 0); |
Christina Quast | 95d6616 | 2015-04-09 22:38:47 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 135 | void CCID_init( void ) |
| 136 | { |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 137 | uint8_t pAtr[MAX_ATR_SIZE]; |
| 138 | uint8_t ucSize ; |
| 139 | |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 140 | // FIXME: do we want to print ATR? |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 141 | /* Initialize Atr buffer */ |
| 142 | memset( pAtr, 0, sizeof( pAtr ) ) ; |
| 143 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 144 | ConfigureCardDetection() ; |
| 145 | |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 146 | // Configure ISO7816 driver |
| 147 | PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816)); |
| 148 | PIO_Configure(pinsBus, PIO_LISTSIZE(pinsBus)); |
| 149 | PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower)); |
| 150 | |
| 151 | /* power up the card */ |
| 152 | // PIO_Set(&pinsPower[0]); |
| 153 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 154 | ISO7816_Init(&usart_info, CLK_MASTER); |
Christina Quast | 4b81493 | 2015-04-17 18:47:34 +0200 | [diff] [blame^] | 155 | USART_SetTransmitterEnabled(usart_info.base, 1); |
| 156 | USART_SetReceiverEnabled(usart_info.base, 1); |
| 157 | |
Christina Quast | e24b9ac | 2015-04-10 17:44:49 +0200 | [diff] [blame] | 158 | ISO7816_Set_Reset_Pin(&pinIso7816RstMC); |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 159 | /* Read ATR */ |
| 160 | ISO7816_warm_reset() ; |
| 161 | |
| 162 | ISO7816_Datablock_ATR( pAtr, &ucSize ) ; |
| 163 | |
| 164 | /* Decode ATR and print it */ |
| 165 | ISO7816_Decode_ATR( pAtr ) ; |
| 166 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 167 | // FIXME. what if smcard is not inserted? |
| 168 | if(PIO_Get(&pinSmartCard) == 0) { |
| 169 | printf("SIM card inserted\n\r"); |
| 170 | CCID_Insertion(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void CCID_run( void ) |
| 175 | { |
Christina Quast | 53a7608 | 2015-03-04 19:01:34 +0100 | [diff] [blame] | 176 | |
| 177 | //if (USBD_Read(INT, pBuffer, dLength, fCallback, pArgument); |
| 178 | |
Christina Quast | db7b1ab | 2015-03-03 12:34:36 +0100 | [diff] [blame] | 179 | CCID_SmartCardRequest(); |
| 180 | } |