SmartCard reader. PC is interface through USB or TCPport. SmartCard is interfaced through UART@ 1MHz, DIV372

Dependencies:   EthernetNetIf mbed

Committer:
bcalin1984
Date:
Sun Feb 27 22:20:40 2011 +0000
Revision:
0:5bf6fcf71548

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcalin1984 0:5bf6fcf71548 1 /*
bcalin1984 0:5bf6fcf71548 2 This file encapsulates logic for SmartCard Reader
bcalin1984 0:5bf6fcf71548 3
bcalin1984 0:5bf6fcf71548 4 Author: Calin Bira
bcalin1984 0:5bf6fcf71548 5 Date: 28.02.2011
bcalin1984 0:5bf6fcf71548 6 */
bcalin1984 0:5bf6fcf71548 7
bcalin1984 0:5bf6fcf71548 8 #include "SmartCardReader.h"
bcalin1984 0:5bf6fcf71548 9
bcalin1984 0:5bf6fcf71548 10 SmartCardReader::SmartCardReader(Serial *_dut, DigitalOut *_vcc, DigitalOut *_rst,PwmOut *_clock, char *_format)
bcalin1984 0:5bf6fcf71548 11 {
bcalin1984 0:5bf6fcf71548 12 int databits,stopbits;
bcalin1984 0:5bf6fcf71548 13 Serial::Parity parity;
bcalin1984 0:5bf6fcf71548 14
bcalin1984 0:5bf6fcf71548 15 freq = 1000*1000;
bcalin1984 0:5bf6fcf71548 16 divfact = 372;
bcalin1984 0:5bf6fcf71548 17
bcalin1984 0:5bf6fcf71548 18 dut = _dut;
bcalin1984 0:5bf6fcf71548 19 rst = _rst;
bcalin1984 0:5bf6fcf71548 20 vcc = _vcc;
bcalin1984 0:5bf6fcf71548 21 clk = _clock;
bcalin1984 0:5bf6fcf71548 22
bcalin1984 0:5bf6fcf71548 23 BytesToIgnore = 0;
bcalin1984 0:5bf6fcf71548 24 ReceiveBufferIndex = 0;
bcalin1984 0:5bf6fcf71548 25
bcalin1984 0:5bf6fcf71548 26 databits = _format[0] - '0';
bcalin1984 0:5bf6fcf71548 27 switch (_format[1])
bcalin1984 0:5bf6fcf71548 28 {
bcalin1984 0:5bf6fcf71548 29 case 'E': {parity = Serial::Even; break;}
bcalin1984 0:5bf6fcf71548 30 case 'O': {parity = Serial::Odd; break;}
bcalin1984 0:5bf6fcf71548 31 default: {parity = Serial::None; break;}
bcalin1984 0:5bf6fcf71548 32 }
bcalin1984 0:5bf6fcf71548 33
bcalin1984 0:5bf6fcf71548 34 stopbits = _format[2] - '0';
bcalin1984 0:5bf6fcf71548 35 dut->format(databits,parity,stopbits);
bcalin1984 0:5bf6fcf71548 36
bcalin1984 0:5bf6fcf71548 37 setClock(freq);
bcalin1984 0:5bf6fcf71548 38 clk->write(0.5);// 50% duty cycle
bcalin1984 0:5bf6fcf71548 39
bcalin1984 0:5bf6fcf71548 40
bcalin1984 0:5bf6fcf71548 41 }
bcalin1984 0:5bf6fcf71548 42
bcalin1984 0:5bf6fcf71548 43 void SmartCardReader::setClock(int Hz)
bcalin1984 0:5bf6fcf71548 44 {
bcalin1984 0:5bf6fcf71548 45 freq = Hz;
bcalin1984 0:5bf6fcf71548 46 clk->period_us(1000*1000/freq);
bcalin1984 0:5bf6fcf71548 47 dut->baud(freq/divfact);
bcalin1984 0:5bf6fcf71548 48 }
bcalin1984 0:5bf6fcf71548 49
bcalin1984 0:5bf6fcf71548 50 void SmartCardReader::cold_reset()
bcalin1984 0:5bf6fcf71548 51 {
bcalin1984 0:5bf6fcf71548 52 *vcc = 0;
bcalin1984 0:5bf6fcf71548 53 *rst = 0;
bcalin1984 0:5bf6fcf71548 54 wait(0.1);
bcalin1984 0:5bf6fcf71548 55 *vcc = 1;
bcalin1984 0:5bf6fcf71548 56 wait(0.1);
bcalin1984 0:5bf6fcf71548 57
bcalin1984 0:5bf6fcf71548 58 while (dut->readable())
bcalin1984 0:5bf6fcf71548 59 dut->getc();
bcalin1984 0:5bf6fcf71548 60 *rst = 1;
bcalin1984 0:5bf6fcf71548 61 }
bcalin1984 0:5bf6fcf71548 62
bcalin1984 0:5bf6fcf71548 63 void SmartCardReader::warm_reset()
bcalin1984 0:5bf6fcf71548 64 {
bcalin1984 0:5bf6fcf71548 65 *rst = 0;
bcalin1984 0:5bf6fcf71548 66 wait(0.1);
bcalin1984 0:5bf6fcf71548 67 *rst = 1;
bcalin1984 0:5bf6fcf71548 68 }
bcalin1984 0:5bf6fcf71548 69
bcalin1984 0:5bf6fcf71548 70 /*
bcalin1984 0:5bf6fcf71548 71 void SmartCardReader::Relay_PC_SC()
bcalin1984 0:5bf6fcf71548 72 {
bcalin1984 0:5bf6fcf71548 73 static char ignore=0;
bcalin1984 0:5bf6fcf71548 74
bcalin1984 0:5bf6fcf71548 75 while (dut->readable()) //if there is data from smartcard
bcalin1984 0:5bf6fcf71548 76 {
bcalin1984 0:5bf6fcf71548 77 if (ignore>0)
bcalin1984 0:5bf6fcf71548 78 {
bcalin1984 0:5bf6fcf71548 79 dut->getc();//ignore data: this is data from pc
bcalin1984 0:5bf6fcf71548 80 ignore--;
bcalin1984 0:5bf6fcf71548 81 }
bcalin1984 0:5bf6fcf71548 82 else pc->putc(dut->getc());//send to pc
bcalin1984 0:5bf6fcf71548 83 }
bcalin1984 0:5bf6fcf71548 84
bcalin1984 0:5bf6fcf71548 85 while (pc->readable()) //if there is data from pc
bcalin1984 0:5bf6fcf71548 86 {
bcalin1984 0:5bf6fcf71548 87 ignore++;
bcalin1984 0:5bf6fcf71548 88 dut->putc(pc->getc()); //send data to smartcard
bcalin1984 0:5bf6fcf71548 89 }
bcalin1984 0:5bf6fcf71548 90 }
bcalin1984 0:5bf6fcf71548 91 */
bcalin1984 0:5bf6fcf71548 92 void SmartCardReader::send(char *buf, int len)
bcalin1984 0:5bf6fcf71548 93 {
bcalin1984 0:5bf6fcf71548 94 while(len-- >0)
bcalin1984 0:5bf6fcf71548 95 {
bcalin1984 0:5bf6fcf71548 96 BytesToIgnore++;
bcalin1984 0:5bf6fcf71548 97 dut->putc(*buf++);
bcalin1984 0:5bf6fcf71548 98 }
bcalin1984 0:5bf6fcf71548 99 }
bcalin1984 0:5bf6fcf71548 100
bcalin1984 0:5bf6fcf71548 101 int SmartCardReader::receive(char *buf)
bcalin1984 0:5bf6fcf71548 102 {
bcalin1984 0:5bf6fcf71548 103 int OldIndex = ReceiveBufferIndex;
bcalin1984 0:5bf6fcf71548 104 for (int i=0; i < ReceiveBufferIndex; i++)
bcalin1984 0:5bf6fcf71548 105 {
bcalin1984 0:5bf6fcf71548 106 buf[i] = ReceiveBuffer[i];
bcalin1984 0:5bf6fcf71548 107 }
bcalin1984 0:5bf6fcf71548 108 ReceiveBufferIndex = 0;
bcalin1984 0:5bf6fcf71548 109 return OldIndex;
bcalin1984 0:5bf6fcf71548 110 }
bcalin1984 0:5bf6fcf71548 111
bcalin1984 0:5bf6fcf71548 112 void SmartCardReader::doEvents()
bcalin1984 0:5bf6fcf71548 113 {
bcalin1984 0:5bf6fcf71548 114 //Relay_PC_SC();
bcalin1984 0:5bf6fcf71548 115
bcalin1984 0:5bf6fcf71548 116 while (dut->readable()) //if there is data from smartcard
bcalin1984 0:5bf6fcf71548 117 {
bcalin1984 0:5bf6fcf71548 118 if (BytesToIgnore > 0)
bcalin1984 0:5bf6fcf71548 119 {
bcalin1984 0:5bf6fcf71548 120 dut->getc();//ignore data: this is data from previous send
bcalin1984 0:5bf6fcf71548 121 BytesToIgnore--;
bcalin1984 0:5bf6fcf71548 122 }
bcalin1984 0:5bf6fcf71548 123 else
bcalin1984 0:5bf6fcf71548 124 {
bcalin1984 0:5bf6fcf71548 125 if (ReceiveBufferIndex < ReceiveBufferLen)
bcalin1984 0:5bf6fcf71548 126 ReceiveBuffer[ReceiveBufferIndex++] = dut->getc();
bcalin1984 0:5bf6fcf71548 127 else dut->getc();//discard data, no space in buffer
bcalin1984 0:5bf6fcf71548 128 }
bcalin1984 0:5bf6fcf71548 129 }
bcalin1984 0:5bf6fcf71548 130 }