Christina Quast | 32906bb | 2015-02-24 11:35:19 +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 | |
| 42 | /** Maximum ucSize in bytes of the smartcard answer to a command.*/ |
| 43 | #define MAX_ANSWER_SIZE 10 |
| 44 | |
| 45 | /** Maximum ATR ucSize in bytes.*/ |
| 46 | #define MAX_ATR_SIZE 55 |
| 47 | |
| 48 | /** USB states */ |
| 49 | /// Use for power management |
| 50 | #define STATE_IDLE 0 |
| 51 | /// The USB device is in suspend state |
| 52 | #define STATE_SUSPEND 4 |
| 53 | /// The USB device is in resume state |
| 54 | #define STATE_RESUME 5 |
| 55 | |
| 56 | /*------------------------------------------------------------------------------ |
| 57 | * Internal variables |
| 58 | *------------------------------------------------------------------------------*/ |
| 59 | /** USB state: suspend, resume, idle */ |
| 60 | unsigned char USBState = STATE_IDLE; |
| 61 | |
| 62 | /** ISO7816 pins */ |
| 63 | static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE}; |
| 64 | /** ISO7816 RST pin */ |
| 65 | static const Pin pinIso7816RstMC = PIN_ISO7816_RST_PHONE; |
| 66 | static uint8_t sim_inserted = 0; |
| 67 | |
| 68 | static const Pin pPwr[] = { |
| 69 | /* Enable power converter 4.5-6V to 3.3V; low: off */ |
| 70 | {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}, |
| 71 | |
| 72 | /* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */ |
| 73 | {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT} |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | #define ISO7816_PHONE_RST {PIO_PA24, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP | PIO_DEBOUNCE | PIO_DEGLITCH | PIO_IT_EDGE } |
| 78 | static const Pin pinPhoneRST = ISO7816_PHONE_RST; |
| 79 | //#define ISO7816_PHONE_CLK {PIO_PA23A_SCK1, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP | PIO_DEBOUNCE | PIO_DEGLITCH | PIO_IT_EDGE } |
| 80 | //static const Pin pinPhoneClk = ISO7816_PHONE_CLK; |
| 81 | |
| 82 | /* ===================================================*/ |
| 83 | /* Taken from iso7816_4.c */ |
| 84 | /* ===================================================*/ |
| 85 | /** Flip flop for send and receive char */ |
| 86 | #define USART_SEND 0 |
| 87 | #define USART_RCV 1 |
| 88 | /*----------------------------------------------------------------------------- |
| 89 | * Internal variables |
| 90 | *-----------------------------------------------------------------------------*/ |
| 91 | /** Variable for state of send and receive froom USART */ |
| 92 | static uint8_t StateUsartGlobal = USART_RCV; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 93 | |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 94 | extern uint32_t char_stat; |
| 95 | extern uint8_t rcvdChar; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 96 | |
| 97 | /*----------------------------------------------------------------------------- |
| 98 | * Interrupt routines |
| 99 | *-----------------------------------------------------------------------------*/ |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 100 | |
| 101 | static void ISR_PhoneRST( const Pin *pPin) |
| 102 | { |
| 103 | TRACE_DEBUG("+++++++++ Interrupt!! ISR:0x%x, CSR:0x%x\n\r", pinPhoneRST.pio->PIO_ISR, USART1->US_CSR); |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 104 | // FIXME: What to do on reset? |
| 105 | // PIO_DisableIt( &pinPhoneRST ) ; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void Config_PhoneRST_IrqHandler() |
| 109 | { |
| 110 | PIO_Configure( &pinPhoneRST, 1); |
| 111 | // PIO_Configure( &pinPhoneClk, 1); |
| 112 | PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ; |
| 113 | // PIO_ConfigureIt( &pinPhoneClk, ISR_PhoneRST ) ; |
| 114 | PIO_EnableIt( &pinPhoneRST ) ; |
| 115 | // PIO_EnableIt( &pinPhoneClk ) ; |
| 116 | NVIC_EnableIRQ( PIOA_IRQn ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get a character from ISO7816 |
| 121 | * \param pCharToReceive Pointer for store the received char |
| 122 | * \return 0: if timeout else status of US_CSR |
| 123 | */ |
| 124 | /* FIXME: This code is taken from cciddriver.c |
| 125 | --> Reuse the code!!! */ |
| 126 | uint32_t _ISO7816_GetChar( uint8_t *pCharToReceive ) |
| 127 | { |
| 128 | uint32_t status; |
| 129 | uint32_t timeout=0; |
| 130 | |
| 131 | TRACE_DEBUG("--"); |
| 132 | |
| 133 | if( StateUsartGlobal == USART_SEND ) { |
| 134 | while((USART_PHONE->US_CSR & US_CSR_TXEMPTY) == 0) {} |
| 135 | USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 136 | StateUsartGlobal = USART_RCV; |
| 137 | } |
| 138 | |
| 139 | /* Wait USART ready for reception */ |
| 140 | while( ((USART_PHONE->US_CSR & US_CSR_RXRDY) == 0) ) { |
| 141 | if(timeout++ > 12000 * (BOARD_MCK/1000000)) { |
| 142 | TRACE_DEBUG("TimeOut\n\r"); |
| 143 | return( 0 ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* At least one complete character has been received and US_RHR has not yet been read. */ |
| 148 | |
| 149 | /* Get a char */ |
| 150 | *pCharToReceive = ((USART_PHONE->US_RHR) & 0xFF); |
| 151 | |
| 152 | status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME| |
| 153 | US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| |
| 154 | (1<<10))); |
| 155 | |
| 156 | if (status != 0 ) { |
| 157 | TRACE_DEBUG("R:0x%X\n\r", status); |
| 158 | TRACE_DEBUG("R:0x%X\n\r", USART_PHONE->US_CSR); |
| 159 | TRACE_DEBUG("Nb:0x%X\n\r", USART_PHONE->US_NER ); |
| 160 | USART_PHONE->US_CR = US_CR_RSTSTA; |
| 161 | } |
| 162 | |
| 163 | /* Return status */ |
| 164 | return( status ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Send a char to ISO7816 |
| 169 | * \param CharToSend char to be send |
| 170 | * \return status of US_CSR |
| 171 | */ |
| 172 | uint32_t _ISO7816_SendChar( uint8_t CharToSend ) |
| 173 | { |
| 174 | uint32_t status; |
| 175 | |
| 176 | TRACE_DEBUG("********** Send char: %c (0x%X)\n\r", CharToSend, CharToSend); |
| 177 | |
| 178 | if( StateUsartGlobal == USART_RCV ) { |
| 179 | USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 180 | StateUsartGlobal = USART_SEND; |
| 181 | } |
| 182 | |
| 183 | /* Wait USART ready for transmit */ |
| 184 | while((USART_PHONE->US_CSR & US_CSR_TXRDY) == 0) {} |
| 185 | /* There is no character in the US_THR */ |
| 186 | |
| 187 | /* Transmit a char */ |
| 188 | USART_PHONE->US_THR = CharToSend; |
| 189 | |
| 190 | status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME| |
| 191 | US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| |
| 192 | (1<<10))); |
| 193 | |
| 194 | if (status != 0 ) { |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 195 | TRACE_DEBUG("******* status: 0x%X (Overrun: %lu, NACK: %lu, Timeout: %lu, underrun: %lu)\n\r", |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 196 | status, ((status & US_CSR_OVRE)>> 5), ((status & US_CSR_NACK) >> 13), |
| 197 | ((status & US_CSR_TIMEOUT) >> 8), ((status & (1 << 10)) >> 10)); |
| 198 | |
| 199 | TRACE_DEBUG("E (USART CSR reg):0x%X\n\r", USART_PHONE->US_CSR); |
| 200 | TRACE_DEBUG("Nb (Number of errors):0x%X\n\r", USART_PHONE->US_NER ); |
| 201 | USART_PHONE->US_CR = US_CR_RSTSTA; |
| 202 | } |
| 203 | |
| 204 | /* Return status */ |
| 205 | return( status ); |
| 206 | } |
| 207 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 208 | void Phone_Master_Init( void ) { |
| 209 | |
| 210 | PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ; |
| 211 | Config_PhoneRST_IrqHandler(); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 212 | |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 213 | _ISO7816_Init(); |
| 214 | |
| 215 | USART_SetTransmitterEnabled(USART_PHONE, 1); |
| 216 | USART_SetReceiverEnabled(USART_PHONE, 1); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame^] | 217 | |
| 218 | /* Configure ISO7816 driver */ |
| 219 | // FIXME: PIO_Configure(pPwr, PIO_LISTSIZE( pPwr )); |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 224 | // FIXME: Or do I need to call VBUS_CONFIGURE() here instead, which will call USBD_Connect() later? |
| 225 | // USBD_Connect(); |
| 226 | // FIXME: USB clock? USB PMC? |
| 227 | // NVIC_EnableIRQ( UDP_IRQn ); |
| 228 | |
| 229 | // USART_EnableIt( USART_PHONE, US_IER_RXRDY) ; |
| 230 | |
| 231 | // FIXME: At some point USBD_IrqHandler() should get called and set USBD_STATE_CONFIGURED |
| 232 | /* while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 233 | int i = 1; |
| 234 | if ((i%10000) == 0) { |
| 235 | TRACE_DEBUG("%d: USB State: %x\n\r", i, USBD_GetState()); |
| 236 | } |
| 237 | i++; |
| 238 | } |
| 239 | */ |
| 240 | |
| 241 | } |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame^] | 242 | |
| 243 | void Phone_run( void ) |
| 244 | { |
| 245 | // FIXME: Function Phone_run not implemented yet |
| 246 | |
| 247 | /* Send and receive chars */ |
| 248 | // ISO7816_GetChar(&rcv_char); |
| 249 | // ISO7816_SendChar(char_to_send); |
| 250 | } |