| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /******************************************************************** |
| 2 | FileName: SCpic24.c |
||
| 3 | Dependencies: See INCLUDES section |
||
| 4 | Processor: PIC24 Microcontrollers |
||
| 5 | Hardware: This demo is natively intended to be used on Exp 16 board. |
||
| 6 | This demo can be modified for use on other hardware platforms. |
||
| 7 | Complier: Microchip C30 (for PIC24) |
||
| 8 | Company: Microchip Technology, Inc. |
||
| 9 | |||
| 10 | Software License Agreement: |
||
| 11 | |||
| 12 | The software supplied herewith by Microchip Technology Incorporated |
||
| 13 | (the Company) for its PIC® Microcontroller is intended and |
||
| 14 | supplied to you, the Companys customer, for use solely and |
||
| 15 | exclusively on Microchip PIC Microcontroller products. The |
||
| 16 | software is owned by the Company and/or its supplier, and is |
||
| 17 | protected under applicable copyright laws. All rights are reserved. |
||
| 18 | Any use in violation of the foregoing restrictions may subject the |
||
| 19 | user to criminal sanctions under applicable laws, as well as to |
||
| 20 | civil liability for the breach of the terms and conditions of this |
||
| 21 | license. |
||
| 22 | |||
| 23 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
| 24 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
| 25 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
| 26 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
| 27 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
| 28 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
| 29 | |||
| 30 | ******************************************************************** |
||
| 31 | File Description: |
||
| 32 | |||
| 33 | Change History: |
||
| 34 | Rev Description |
||
| 35 | ---- ----------------------------------------- |
||
| 36 | 1.0 Initial release |
||
| 37 | 1.01 Cleaned up unnecessary variables |
||
| 38 | ********************************************************************/ |
||
| 39 | |||
| 40 | #include "string.h" |
||
| 41 | #include "GenericTypeDefs.h" |
||
| 42 | #include "sc_config.h" |
||
| 43 | #include "./Smart Card/SCpic24.h" |
||
| 44 | #if defined(__PIC24F__) |
||
| 45 | #include "./Smart Card/pps-macro.h" |
||
| 46 | #endif |
||
| 47 | |||
| 48 | unsigned long baudRate; |
||
| 49 | unsigned long scReferenceClock; // Smart Card Reference Clock |
||
| 50 | |||
| 51 | //////////////////////////////////////////////////// |
||
| 52 | //////////////////////////////////////////////////// |
||
| 53 | void SCdrv_SendTxData( BYTE data ) |
||
| 54 | { |
||
| 55 | BYTE txRetryCounter = 0; |
||
| 56 | BOOL noError = TRUE; |
||
| 57 | |||
| 58 | U1STAbits.UTXEN = 1; |
||
| 59 | U1TXREG = data; |
||
| 60 | |||
| 61 | while( !U1STAbits.TRMT ) |
||
| 62 | { |
||
| 63 | Nop(); |
||
| 64 | Nop(); |
||
| 65 | } |
||
| 66 | |||
| 67 | U1STAbits.UTXEN = 0; |
||
| 68 | |||
| 69 | U1MODEbits.UARTEN = 0; // Disable UART Module |
||
| 70 | |||
| 71 | WaitMicroSec( 1 ); |
||
| 72 | |||
| 73 | if( !SCdrv_GetRxPinData() ) // The Receiver did not like our data. it is pulling line low |
||
| 74 | { // to indicate PE or FR errors |
||
| 75 | noError = FALSE; |
||
| 76 | |||
| 77 | // WaitMicroSec((U1BRG * 170)/371); //wait two etu before repeating |
||
| 78 | |||
| 79 | U1MODEbits.UARTEN = 1; |
||
| 80 | |||
| 81 | //now retransmit the data |
||
| 82 | if( txRetryCounter < 5 ) |
||
| 83 | { |
||
| 84 | txRetryCounter++; |
||
| 85 | SCdrv_SendTxData(data); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | else |
||
| 89 | { |
||
| 90 | // WaitMicroSec((U1BRG * 140)/371); //wait 1.5 etu |
||
| 91 | } |
||
| 92 | |||
| 93 | if( noError ) //no error detected |
||
| 94 | txRetryCounter = 0; |
||
| 95 | |||
| 96 | U1MODEbits.UARTEN = 1; // Enable UART Module |
||
| 97 | |||
| 98 | U1STAbits.OERR = 0; //clear any overflow error that we caused |
||
| 99 | |||
| 100 | while(1) // remove rx data recvd from our Tx line |
||
| 101 | { |
||
| 102 | WORD temp; |
||
| 103 | if( U1STAbits.URXDA ) |
||
| 104 | temp = U1RXREG; |
||
| 105 | else |
||
| 106 | break; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | ///////////////////////////////////////////// |
||
| 111 | ///////////////////////////////////////////// |
||
| 112 | BOOL SCdrv_GetRxData( BYTE* pDat, unsigned long nTrys ) |
||
| 113 | { |
||
| 114 | //wait for data byte |
||
| 115 | while( !U1STAbits.URXDA && nTrys-- ); |
||
| 116 | |||
| 117 | if( !U1STAbits.URXDA ) |
||
| 118 | return FALSE; |
||
| 119 | |||
| 120 | if( U1STAbits.PERR ) //Parity Error detected |
||
| 121 | { |
||
| 122 | SCdrv_TxPin_Direction(0); //pull it low to tell the card that there was error receiving data |
||
| 123 | U1MODEbits.RXINV = 1; //do not recognize this low state as a valid start bit |
||
| 124 | |||
| 125 | //Read the data from UART to clear the error flag |
||
| 126 | *pDat = U1RXREG; |
||
| 127 | |||
| 128 | WaitMicroSec((U1BRG * 116)/371); //for 9600 baud, 116 us. for 250kbps, 5us |
||
| 129 | |||
| 130 | SCdrv_TxPin_Direction(1); //release RD10. Card should retransmit now. |
||
| 131 | |||
| 132 | U1MODEbits.RXINV = 0; |
||
| 133 | |||
| 134 | return SCdrv_GetRxData(pDat, 10000); //Read the data from retransmission |
||
| 135 | } |
||
| 136 | else |
||
| 137 | { |
||
| 138 | //Read the data from UART |
||
| 139 | *pDat = U1RXREG; |
||
| 140 | } |
||
| 141 | |||
| 142 | return TRUE; |
||
| 143 | } |
||
| 144 | |||
| 145 | /////////////////////////////////////////////////// |
||
| 146 | ////////////////////////////////////////////////// |
||
| 147 | void SCdrv_SetBRG( BYTE speedCode ) |
||
| 148 | { |
||
| 149 | float factorD = 1; |
||
| 150 | unsigned int factorF = 372; |
||
| 151 | BYTE tempCode; |
||
| 152 | |||
| 153 | tempCode = speedCode & 0x0F; |
||
| 154 | |||
| 155 | // Calculate Factor 'D' from TA1 value |
||
| 156 | switch(tempCode) |
||
| 157 | { |
||
| 158 | case 0x00: |
||
| 159 | case 0x07: |
||
| 160 | case 0x01: |
||
| 161 | break; |
||
| 162 | |||
| 163 | case 0x02: |
||
| 164 | factorD = 2; |
||
| 165 | break; |
||
| 166 | |||
| 167 | case 0x03: |
||
| 168 | factorD = 4; |
||
| 169 | break; |
||
| 170 | |||
| 171 | case 0x04: |
||
| 172 | factorD = 8; |
||
| 173 | break; |
||
| 174 | |||
| 175 | case 0x05: |
||
| 176 | factorD = 16; |
||
| 177 | break; |
||
| 178 | |||
| 179 | case 0x06: |
||
| 180 | factorD = 32; |
||
| 181 | break; |
||
| 182 | |||
| 183 | case 0x08: |
||
| 184 | factorD = 12; |
||
| 185 | break; |
||
| 186 | |||
| 187 | case 0x09: |
||
| 188 | factorD = 20; |
||
| 189 | break; |
||
| 190 | |||
| 191 | case 0x0A: |
||
| 192 | factorD = 0.5; |
||
| 193 | break; |
||
| 194 | |||
| 195 | case 0x0B: |
||
| 196 | factorD = 0.25; |
||
| 197 | break; |
||
| 198 | |||
| 199 | case 0x0C: |
||
| 200 | factorD = 0.125; |
||
| 201 | break; |
||
| 202 | |||
| 203 | case 0x0D: |
||
| 204 | factorD = 0.0625; |
||
| 205 | break; |
||
| 206 | |||
| 207 | case 0x0E: |
||
| 208 | factorD = 0.03125; |
||
| 209 | break; |
||
| 210 | |||
| 211 | case 0x0F: |
||
| 212 | factorD = 0.015625; |
||
| 213 | break; |
||
| 214 | } |
||
| 215 | |||
| 216 | // If you are not using internal clock in Smart Card & are |
||
| 217 | // using external clock to drive Smart Card than calculate |
||
| 218 | // factor 'F' from TA1 value |
||
| 219 | #ifdef ENABLE_SC_EXTERNAL_CLOCK |
||
| 220 | |||
| 221 | tempCode = (speedCode & 0xF0) >> 4; |
||
| 222 | |||
| 223 | // Calculate Factor 'F' from TA1 value |
||
| 224 | switch(tempCode) |
||
| 225 | { |
||
| 226 | case 0x00: |
||
| 227 | case 0x07: |
||
| 228 | case 0x08: |
||
| 229 | case 0x0E: |
||
| 230 | case 0x0F: |
||
| 231 | break; |
||
| 232 | |||
| 233 | case 0x01: |
||
| 234 | factorF = 372; |
||
| 235 | break; |
||
| 236 | |||
| 237 | case 0x02: |
||
| 238 | factorF = 558; |
||
| 239 | break; |
||
| 240 | |||
| 241 | case 0x03: |
||
| 242 | factorF = 744; |
||
| 243 | break; |
||
| 244 | |||
| 245 | case 0x04: |
||
| 246 | factorF = 1116; |
||
| 247 | break; |
||
| 248 | |||
| 249 | case 0x05: |
||
| 250 | factorF = 1488; |
||
| 251 | break; |
||
| 252 | |||
| 253 | case 0x06: |
||
| 254 | factorF = 1860; |
||
| 255 | break; |
||
| 256 | |||
| 257 | case 0x09: |
||
| 258 | factorF = 512; |
||
| 259 | break; |
||
| 260 | |||
| 261 | case 0x0A: |
||
| 262 | factorF = 768; |
||
| 263 | break; |
||
| 264 | |||
| 265 | case 0x0B: |
||
| 266 | factorF = 1024; |
||
| 267 | break; |
||
| 268 | |||
| 269 | case 0x0C: |
||
| 270 | factorF = 1536; |
||
| 271 | break; |
||
| 272 | |||
| 273 | case 0x0D: |
||
| 274 | factorF = 2048; |
||
| 275 | break; |
||
| 276 | } |
||
| 277 | |||
| 278 | if(tempCode == 0x00) // If internal clock used in Smart Card |
||
| 279 | { |
||
| 280 | U1BRG = (unsigned int)((unsigned long)((unsigned long)FCY/(4 * (unsigned long)9600 * factorD)) - 1); //Internal clk used in card |
||
| 281 | } |
||
| 282 | else // If externa; clock used to drive Smart Card |
||
| 283 | { |
||
| 284 | baudRate = (unsigned long long)((unsigned long long)((unsigned long long)scReferenceClock * factorD)/factorF); |
||
| 285 | |||
| 286 | U1BRG = (unsigned int)((unsigned long)((unsigned long)FCY/(4 * baudRate)) - 1); //10752bps with 4Mhz clk to card |
||
| 287 | } |
||
| 288 | |||
| 289 | #else // If internal clock used in Smart Card |
||
| 290 | |||
| 291 | U1BRG = (unsigned int)((unsigned long)((unsigned long)FCY/(4 * (unsigned long)9600 * factorD)) - 1); //Internal clk used in card |
||
| 292 | |||
| 293 | #endif |
||
| 294 | } |
||
| 295 | |||
| 296 | /////////////////////////////////////////////////// |
||
| 297 | ////////////////////////////////////////////////// |
||
| 298 | void SCdrv_CloseUART(void) |
||
| 299 | { |
||
| 300 | SCdrv_DisableClock(); // turn off Clock |
||
| 301 | U1MODEbits.UARTEN = 0; // Turn off UART |
||
| 302 | U1STAbits.OERR = 0;; |
||
| 303 | |||
| 304 | // Disable Pull-ups at Tx & Rx pins |
||
| 305 | SCdrv_DisableTxPinPullUp(); |
||
| 306 | SCdrv_DisableRxPinPullUp(); |
||
| 307 | } |
||
| 308 | |||
| 309 | /////////////////////////////////////////////////// |
||
| 310 | ////////////////////////////////////////////////// |
||
| 311 | void SCdrv_InitUART(void) |
||
| 312 | { |
||
| 313 | unsigned int power2Value = 1; |
||
| 314 | BYTE power2temp; |
||
| 315 | |||
| 316 | #ifdef ENABLE_SC_POWER_THROUGH_PORT_PIN |
||
| 317 | SCdrv_PowerPin_Direction(0); //set RG8 as output to power the Smart Card |
||
| 318 | #endif |
||
| 319 | |||
| 320 | SCdrv_ResetPin_Direction(0); //set RD0 as output for Smart Card Reset Pin |
||
| 321 | SCdrv_CardPresent_Direction(1); //RD3 Input Card Present - SmartCard Conn Active Hi |
||
| 322 | SCdrv_SimPresent_Direction(1); //RG12 Input Card Present - SimCard Conn Active Low |
||
| 323 | |||
| 324 | SCdrv_SetTxPinData(0); |
||
| 325 | SCdrv_TxPin_Direction(1); // use as gpio to pull the line low |
||
| 326 | |||
| 327 | //Turn on the pull-up on both RX and TX line for faster transitions. |
||
| 328 | SCdrv_EnableTxPinPullUp(); |
||
| 329 | SCdrv_EnableRxPinPullUp(); |
||
| 330 | |||
| 331 | // Enable Pull-ups for card present/sim present pins |
||
| 332 | SCdrv_EnableCardPresentPinPullUp(); |
||
| 333 | SCdrv_EnableSimPresentPinPullUp(); |
||
| 334 | |||
| 335 | #ifdef ENABLE_SC_POWER_THROUGH_PORT_PIN |
||
| 336 | SCdrv_SetSwitchCardPower(0); //Turn off power to smart card |
||
| 337 | #endif |
||
| 338 | |||
| 339 | SCdrv_SetSwitchCardReset(0); //keep card in reset state |
||
| 340 | |||
| 341 | MapUART1RxPin(); // Map UART1 Rx pin |
||
| 342 | MapUART1TxPin(); // Map UART1 Tx pin |
||
| 343 | |||
| 344 | Scdrv_ClockSet(); |
||
| 345 | |||
| 346 | // Initial Baud Rate of Smart Card for external Clock |
||
| 347 | #ifdef ENABLE_SC_EXTERNAL_CLOCK |
||
| 348 | |||
| 349 | power2temp = REF_CLOCK_POWER2_VALUE; |
||
| 350 | |||
| 351 | while(power2temp--) |
||
| 352 | { |
||
| 353 | power2Value = power2Value * (BYTE)2; |
||
| 354 | } |
||
| 355 | |||
| 356 | scReferenceClock = REF_CLOCK_CIRCUIT_INPUT_CLK/(power2Value + REF_CLOCK_DIVISOR_VALUE); |
||
| 357 | |||
| 358 | baudRate = scReferenceClock/372; |
||
| 359 | |||
| 360 | U1BRG = (unsigned int)((unsigned long)((unsigned long)FCY/(4 * baudRate)) - 1); //10752bps with 4Mhz clk to card |
||
| 361 | |||
| 362 | #else |
||
| 363 | |||
| 364 | U1BRG = (unsigned int)((unsigned long)((unsigned long)FCY/(4 * (unsigned int)9600)) - 1); //Internal clk used in card |
||
| 365 | |||
| 366 | #endif |
||
| 367 | |||
| 368 | U1MODEbits.PDSEL = 1; //8bits + even parity |
||
| 369 | U1MODEbits.STSEL = 0; //1 stop bit |
||
| 370 | U1MODEbits.BRGH = 1; |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 |
Powered by WebSVN v2.8.3