📄 samcard.c
字号:
}
else
return 0;
if ( ResetLen > 1 )
{
if ( ( *(ResetData+1) & 0xf0 ) != 0xe0 )
PUB_Card_ICT01[PUB_Card_ICNO] = 0;
else
PUB_Card_ICT01[PUB_Card_ICNO] = 1;
PUB_Card_ICPCB[PUB_Card_ICNO] = 0x00;
}
return ResetLen;
}
/////////////
/************************************************************************/
/* */
/* Module title: SAM Card opration functions */
/* / _SelectSocket */
/* Author: Yesy */
/* Date: 2005-05-20 */
/* Reviewed by: */
/* Date: */
/* */
/* */
/* Functional Description: */
/* */
/* CPU Card basic opration functions */
/************************************************************************/
static void _SamSocketConfig(byte Level)//,byte SocketNo
{
int i;
word CardAB;
// CardAB=0xBE00; //1011 1110
if (Level==1)
CardAB=0x9F00; //1011 1110
else
CardAB=0x0000; //
LTC1955_LD1;
LTC1955_SCLK1;
_DelaynSec(LTC1955_tLC);
for(i=15;i>=0;i--)
{
LTC1955_SCLK0;
if((CardAB>>i) & 0x01)
LTC1955_DIN1;
else
LTC1955_DIN0;
_DelaynSec(LTC1955_tDS);
LTC1955_SCLK1;
_DelaynSec(LTC1955_tDH);
}
_DelaynSec(LTC1955_tCL);
LTC1955_LD0;
// while(1) _NOP();
return;
}
/************************************************************************
* *
* Module title: _SamCardOpen *
* Module type: subprogram *
* *
* Author: Yesy *
* Date: 2005-05-21 *
* Reviewed by: *
* Date: *
* *
* Last changed by: *
* Date: *
* *
* Functional Description: Build the connection to the CPU card, *
* and do a reset operation on the Card *
************************************************************************/
byte _SamCardOpen(void)
{
//byte ResetData[35];
//byte ResetLen;
_InitSmartCard();
_SamCardSetPower(1);
_Delay(50);
_SamCardSetClock(1);
_SamSocketConfig(1);
return 0;
// _InitSmartCard();
//for(ResetLen=0;ResetLen<35;ResetLen++)ResetData[ResetLen]=0;
//P6DIR &=~BIT1; // Enable rece
// _CardSetPower(1); //disable by Yesy
//_CardPutBit(1);
// _CardSetCLK4(1); //disable by Yesy
// _CardSetClock(1);//disable by Yesy
_Delay(1);
return 0;
//PUB_Card_Asy[PUB_Card_ICNO] = 1;
/*ResetLen = _CardSmartReset(ResetData);
if ( ResetLen != 0 )
return 0;
else
{
_CardSmartClose();
return 1;
}*/
}
/************************************************************************
* *
* Module title: _SamCardApdu *
* Module type: subprogram *
* *
* Author: Yesy *
* Date: 2005-05-21 *
* Reviewed by: *
* Date: *
* *
* Last changed by: *
* Date: *
* *
* Functional Description: Transacts exchange *
* *
* The function base on _CardSmartApdu *
************************************************************************/
byte _SamCardApdu(APDU_COMM *apducom, APDU_RESP *apduresp)
{
byte i;
byte FlagReverse;
byte Lc,Le;
byte HeaderBuff[8];
byte CmdIns,CmdIns_;
byte cc,cc2;
byte RDataPt;
byte ioff;
FlagReverse = PUB_Card_ICReverse[PUB_Card_ICNO];
Lc = (*apducom).LengthIn;
Le = (*apducom).LengthExpected;
ioff=0;
if ( PUB_Card_ICT01[PUB_Card_ICNO] == 0 )
{
memcpy(HeaderBuff,(*apducom).Command,4);
if ( Lc > 0 ) HeaderBuff[4] = Lc;
else HeaderBuff[4] = Le;
_SamCardApduSend(HeaderBuff,5,FlagReverse);//_CardSmartApduSend(HeaderBuff,5,FlagReverse)
CmdIns = HeaderBuff[1];
CmdIns_ = 0xff- CmdIns;// + 0x01;
RDataPt = 0;
for(;;)
{
//if ( _CardReadIOLimit(&cc,FlagReverse) == 0)
if ( _SamCardReadIOLimit(&cc,FlagReverse) == 0)
return 1;
if(cc==0x60)
continue;
if(((cc>0x60) && (cc<=0x6f)) || ((cc>=0x90) && (cc<=0x9f)))
{
//if ( _CardReadIOLimit(&cc2,FlagReverse) == 0 ) return 2;
if ( _SamCardReadIOLimit(&cc2,FlagReverse) == 0 ) return 2;
(*apduresp).Status = cc;
(*apduresp).Status = ((*apduresp).Status<<8) + cc2;
(*apduresp).LengthOut = RDataPt;
return 0;
}
else
{
if ( (cc != CmdIns ) && ( cc != CmdIns_) ) return 3;
else
{
if(cc==CmdIns_)
{
if(Lc>0) {
//_CardSmartApduSend((*apducom).DataIn+ioff,1,FlagReverse);
_SamCardApduSend((*apducom).DataIn+ioff,1,FlagReverse);
ioff++; Lc--;
}
else if(Le>0) {
//if(_CardReadIOLimit((*apduresp).DataOut+RDataPt,FlagReverse)==0) return 4;
if(_SamCardReadIOLimit((*apduresp).DataOut+RDataPt,FlagReverse)==0) return 4;
else { RDataPt++; Le--;}
}
}
else
{
if(Lc>0)
{
//_CardSmartApduSend((*apducom).DataIn+ioff,Lc,FlagReverse);
_SamCardApduSend((*apducom).DataIn+ioff,Lc,FlagReverse);
Lc=0;
}
else if ( Le > 0 )
{
while ( Le>0 )
{
//if ( _CardReadIOLimit((*apduresp).DataOut+RDataPt,FlagReverse) == 0 ) return 5;
if ( _SamCardReadIOLimit((*apduresp).DataOut+RDataPt,FlagReverse) == 0 ) return 5;
else { RDataPt++; Le--;}
}
}
}
}
}
}
}
else
{
HeaderBuff[0] = 0x00;
HeaderBuff[1] = PUB_Card_ICPCB[PUB_Card_ICNO];
cc = HeaderBuff[1];
for (i=0;i<4;i++)
{
HeaderBuff[3+i] = (*apducom).Command[i];
cc ^= (*apducom).Command[i];
}
cc2 = 4;
if ( Lc != 0 )
{
cc2 = cc2+ 1 + Lc;
cc ^= Lc;
for(i=0;i<Lc;i++) cc ^= *((*apducom).DataIn+i);
}
if ( Le != 0 ) { cc2++; cc ^= Le; }
cc ^= cc2;
HeaderBuff[2] = cc2;
// _DisableICRec();
_DisableSamRec();
// _CardSmartApduSend(HeaderBuff,7,FlagReverse);
_SamCardApduSend(HeaderBuff,7,FlagReverse);
if ( Lc != 0 )
{
//_CardSmartApduSend(&Lc,1,FlagReverse);
_SamCardApduSend(&Lc,1,FlagReverse);
//_CardSmartApduSend((*apducom).DataIn,Lc,FlagReverse);
_SamCardApduSend((*apducom).DataIn,Lc,FlagReverse);
}
if ( Le != 0 )
//_CardSmartApduSend(&Le,1,FlagReverse);
_SamCardApduSend(&Le,1,FlagReverse);
//_CardSmartApduSend(&cc,1,FlagReverse);
_SamCardApduSend(&cc,1,FlagReverse);
//_EnableICRec();
_EnableSamRec();
//if ( _CardReadIOLimit(&cc2,FlagReverse) == 0 ) return 86;
if ( _SamCardReadIOLimit(&cc2,FlagReverse) == 0 ) return 86;
i = cc2;
//if ( _CardReadIOLimit(&cc2,FlagReverse) == 0 ) return 87;
if ( _SamCardReadIOLimit(&cc2,FlagReverse) == 0 ) return 87;
i ^= cc2;
//if ( _CardReadIOLimit(&cc2,FlagReverse) == 0 ) return 88;
if ( _SamCardReadIOLimit(&cc2,FlagReverse) == 0 ) return 88;
i ^= cc2;
RDataPt = 0;
while ( RDataPt < cc2-2 )
{
//if ( _CardReadIOLimit(&cc,FlagReverse) == 0 ) return 89;
if ( _SamCardReadIOLimit(&cc,FlagReverse) == 0 ) return 89;
i ^= cc;
*((*apduresp).DataOut+RDataPt) = cc;
RDataPt++;
}
//if ( _CardReadIOLimit(&cc,FlagReverse) == 0 ) return 90;
if ( _SamCardReadIOLimit(&cc,FlagReverse) == 0 ) return 90;
(*apduresp).Status = cc;
i ^= cc;
//if ( _CardReadIOLimit(&cc,FlagReverse) == 0 ) return 91;
if ( _SamCardReadIOLimit(&cc,FlagReverse) == 0 ) return 91;
i ^= cc;
(*apduresp).Status = ((*apduresp).Status<<8) + cc;
(*apduresp).LengthOut = cc2 - 2;
//if ( _CardReadIOLimit(&cc,FlagReverse) == 0 ) return 92;
if ( _SamCardReadIOLimit(&cc,FlagReverse) == 0 ) return 92;
if ( i != cc ) return 13;
PUB_Card_ICPCB[PUB_Card_ICNO] ^= 0x40;
}
return 0;
}
/************************************************************************
* *
* Module title: _SamCardApduSend *
* Module type: subprogram *
* *
* Author: Yesy *
* Date: 2005-05-20 *
* Reviewed by: *
* Date: *
* *
* Last changed by: *
* Date: *
* *
* Functional Description: The function base on CardSmartApduSend *
* but changed from _CardPutBye to _SamCardPutByte because of the *
* deferent I/O *
************************************************************************/
void _SamCardApduSend(byte *SendBuff,byte SendLen,byte FlagReverse)
{
byte i;
uint j;
_DisableSamRec();
for (i=0;i<SendLen;i++)
{
if (i>0)
for (j=0;j<PUB_Card_GuardTime;j++);
if (FlagReverse)
_SamCardPutByte( _ReverseData(SendBuff[i]));
else
_SamCardPutByte(SendBuff[i]);
while ( PUB_Card_TI == 0);
}
_EnableSamRec();
return;
}
/************************************************************************
* *
* Module title: _SamCardClose *
* Module type: subprogram *
* *
* Author: Yesy *
* Date: 2005-05-21 *
* Reviewed by: *
* Date: *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -