📄 iccpdir2.c
字号:
{
/* TB1: 编程电压Vpp信息 */ /* T=0 and T=1 时存在 */
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
// info->vpp = inchar;
}
if (format & 0x40)
{
/* TC1: 所需额外保护时间 */ /* T=0 and T=1 时存在 */
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
// info->guardtime = inchar;
}
if (format & 0x80)
{
/* TD1: 格式字符 */ /* T=1 时存在 */
i=IccRead(&format, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= format;
buf[j++] = format;
// info->protocolid = format & 0x0f;
if (format & 0x10)
{
/* TA2: 模式选择 */
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
}
if (format & 0x20)
{
/* TB2: 编程电压 */
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
}
if (format & 0x40)
{
/* TC2: 工作等待时间 */
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
}
if (format & 0x80)
{
/* TD2: 格式字符 */ /* T=1 时存在 */
i=IccRead(&format, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= format;
buf[j++] = format;
/* SAM_T通讯协议 */
// info->protocolid = format & 0x0f;
if (format & 0x10)
{
/*TA3: IC卡信息域大小整数*/ /* T=1 时存在=0xb7 */
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
// info->infolen = inchar;
}
if (format & 0x20)
{
/*TB3: CWI和BWI*/ /* T=1 时存在=0x45 */
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
CWI = inchar >> 4;
BWI = inchar & 0x0f;
//计算CWT、BWT
}
if (format & 0x40)
{
/*TC3: 块错误检测代码类型*/
i=IccRead(&inchar, 1);
if (i < 1)
{
TIME_OUT_MS = oldtmo;
return j;
}
mylrc ^= inchar;
buf[j++] = inchar;
}
}
}
i = IccRead(buf+j, nhistory); /* 获取历史字符 */
if(i < nhistory)
{
TIME_OUT_MS = oldtmo;
return j+i;
}
/*
if (info->protocolid != 0)
{
i = IccRead(&inchar, 1);
if(i < 1)
{
TIME_OUT_MS = oldtmo;
return 0;
}
for(i=0; i<info->historylen; i++)
mylrc ^= info->history[i];
if(mylrc != inchar) // 校验错误
{
TIME_OUT_MS = oldtmo;
return 0;
}
}
*/
/*reset success*/
TIME_OUT_MS = oldtmo;
return j+nhistory;
}
/*** install etu for sam ***/
void IccInit(void)
{
// disable user power
IccUserPower(0);
// start sam clk
StartCLKO(2);
// preparing for transmiting and receiving
RxWorking = 0;
TxParity = 0;
TxNotRx = 0;
//
ICCUSEREJECT = 1; // disable eject card
ICCUSERCLS = 1; // set to input mode
ICCUSEROVLD = 1; // set to input mode
ICCIO = 1; // set to input mode
ICCRST = 0; //
USERRST = 0;
// set transmit and receive interrupt
SetIntPri(IE1_VECTOR, 3);
IT1 = 1; // 下降沿触发
}
// power on/off user card and enable/disable clock output
void IccUserPower(bit en)
{
// disable clock out
ICCUSERSCLK = 0;
ICCUSERCLKC = 0;
if(en)
{
// VCC on
ICCUSERVCCC = 0;
}
else
{
// RST = 0
USERRST = 0;
// VCC off
ICCUSERVCCC = 1;
}
}
///////////////////////////////////////////
// for user card mechanical interface
bit IccUserTest(void)
{
return ICCUSERCLS;
}
// eject card
bit IccUserEject(void)
{
unsigned char i;
ICCUSERCLKC = 0;
for(i=0; i<6; i++)
{
ICCUSEREJECT = 0;
delay(80);
ICCUSEREJECT = 1;
if(!ICCUSERCLS)
return 1;
delay(50);
}
return 0;
}
// get overload status
bit IccUserOverLoad(void)
{
static unsigned int ovtime = 0;
if(ICCUSEROVLD)
{
ovtime = GetTickCount();
return 0;
}
else
{
if(GetTickCount() - ovtime >= 20)
{
// power off and eject card
IccUserPower(0);
IccUserEject();
return 1;
}
else
return 0;
}
}
// write some bytes and read some bytes
bit IccSyncWRWithACK(void *wbuf, int wlen, void *rbuf, int rlen)
{
int i;
// start
IccSyncStart();
// write
for(i=0; i<wlen; i++)
{
if(!IccSyncPutCharWaitACK(((unsigned char *)wbuf)[i]))
return 0;;
}
// read
if(rlen > 0)
{
for(i=0; i<rlen-1; i++)
{
// get char and send ACK
((unsigned char *)rbuf)[i] = IccSyncGetCharSendACK(1);
}
// get last char and send NACK
((unsigned char *)rbuf)[i] = IccSyncGetCharSendACK(0);
}
// stop
IccSyncStop();
return 1;
}
/*
// write some bytes and read some bytes
void IccSyncWR(void *wbuf, int wlen, void *rbuf, int rlen)
{
int i;
// start
IccSyncStart();
// write
for(i=0; i<wlen; i++)
{
IccSyncPutChar(((unsigned char *)wbuf)[i]);
}
// read
if(rlen > 0)
{
for(i=0; i<rlen; i++)
{
((unsigned char *)rbuf)[i] = IccSyncGetChar();
}
}
// stop
IccSyncStop();
}
*/
// start communication with card
void IccSyncStart(void)
{
IccDisable();
ICCUSERSCLK = 1; // SCL = 1;
dummy();
ICCIO = 0; // SDA = 0;
ICCUSERSCLK = 0; // SCL = 0;
}
// stop communication with card
void IccSyncStop(void)
{
ICCIO = 0; // SDA = 0;
dummy();
ICCUSERSCLK = 1;
dummy();
ICCIO = 1;
dummy();
ICCUSERSCLK = 0;
}
// write a char to card and wait for ACK/NAK
bit IccSyncPutCharWaitACK(unsigned char ch)
{
unsigned char ci;
bit fret = 1;
IccDisable();
// send data
for(ci=0; ci<8; ci++)
{
ch <<= 1;
ICCIO = CY;
ICCUSERSCLK = 1;
dummy();
ICCUSERSCLK = 0;
}
// read ACK
ICCIO = 1;
ICCUSERSCLK = 1;
dummy();
if(ICCIO)
{
// NAK!
fret = 0;
}
// clear SCLK
ICCUSERSCLK = 0;
return fret;
}
// read a char from card and write ACK/NAK
unsigned char IccSyncGetCharSendACK(bit fack)
{
unsigned char ci;
unsigned char ch = 0;
IccDisable();
// read data
for(ci=0; ci<8; ci++)
{
ICCUSERSCLK = 1;
ch <<= 1;
if(ICCIO)
ch |= 0x01;
ICCUSERSCLK = 0;
}
// send ack or nak
ICCIO = ~fack;
ICCUSERSCLK = 1;
dummy();
ICCUSERSCLK = 0;
ICCIO = 1;
// return
return ch;
}
void IccSyncPutChar(unsigned char ch)
{
unsigned char ci;
IccDisable();
for(ci=0; ci<8; ci++)
{
ch >>= 1;
ICCIO = CY;
ICCUSERSCLK = 1;
dummy();
ICCUSERSCLK = 0;
}
}
// read a char from card
unsigned char IccSyncGetChar(void)
{
unsigned char ci;
unsigned char ch = 0;
IccDisable();
ICCUSERSCLK = 0;
for(ci=0; ci<8; ci++)
{
ICCUSERSCLK = 1;
ch >>= 1;
if(ICCIO)
ch |= 0x80;
ICCUSERSCLK = 0;
}
return ch;
}
// reset
int IccSyncReset(void *buf)
{
unsigned char ci, bi;
unsigned char ch = 0;
ICCUSERSCLK = 0;
ICCUSERCLKC = 0;
IccDisable();
ICCIO = 1; // set to input mode
USERRST = 0;
dummy();
USERRST = 1;
dummy();
ICCUSERSCLK = 1;
dummy();
ICCUSERSCLK = 0;
USERRST = 0;
for(bi=0; bi<4; bi++)
{
for(ci=0; ci<8; ci++)
{
ICCUSERSCLK = 1;
ch >>= 1;
if(ICCIO)
ch |= 0x80;
ICCUSERSCLK = 0;
}
((unsigned char *)buf)[bi] = ch;
}
return 4;
}
// polling the complete of a processing
bit IccSyncPollingIO(unsigned int tm)
{
unsigned int starttime = GetTickCount();
do
{
ICCUSERSCLK = 0;
dummy();
ICCUSERSCLK = 1;
if(GetTickCount() - starttime >= tm)
return 0;
}while(!ICCIO);
return 1;
}
// send a clock puls
void IccSyncClock(void)
{
ICCUSERSCLK = 1;
dummy();
ICCUSERSCLK = 0;
}
/*
void IccSPIWrite(void *buf, int len)
{
}
void IccSPIRead(void *buf, int len)
{
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -