📄 icbase.c
字号:
/*24C64子程序 */
#include "reg.h"
#include "head.h"
int GBIT(unsigned int X,int J)
{
X=X&0x000f;
X=X>>(J-1);
X=X&0x1;
return X;
}
void Delay_10_uS(void)
{
int i;
for(i=0;i<11000;i++)
{
asm("nop");
}
}
int IC_Start(void) /*初始化IC卡*/
{
*(unsigned volatile int *)SPCR0 = *(unsigned volatile int *)SPCR0 & 0xfffefffe;
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x00003000;
*(unsigned volatile int *)SPCR1 = *(unsigned volatile int *)SPCR1 & 0xfffffffe;
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x00001000;
Delay_10_uS();
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x0404; /*IC_SDA =1*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x0404; /* IC_SCK =1*/
Delay_10_uS();
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 & 0xfffffbff; /*FSRM =0*/
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 & 0xfffffbff; /*FSRM =0*/
if (GBIT((*( unsigned volatile int *)PCR1),3) == 0)
{
return 0;
}
if (GBIT((*(unsigned volatile int *)PCR0),3) == 0)
{
return 0;
}
*(unsigned volatile int *)PCR0=(*(unsigned volatile int *)PCR0 | 0x0400)&0xfffffffb; /*IC_SDA = 0*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = (*(unsigned volatile int *)PCR1 | 0x0400)&0xfffffffb; /*IC_SCK = 0*/
return 1;
}
void IC_Stop(void)
{
Delay_10_uS();
*(unsigned volatile int *)PCR0=(*(unsigned volatile int *)PCR0 | 0x0400)&0xfffffffb; /*IC_SDA = 0*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x0404; /* IC_SCK =1*/
Delay_10_uS();
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x0404; /*IC_SDA =1*/
Delay_10_uS();
}
void IC_Ack(void)
{
Delay_10_uS();
*(unsigned volatile int *)PCR0=(*(unsigned volatile int *)PCR0 | 0x0400)&0xfffffffb; /*IC_SDA = 0*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x0404; /* IC_SCK =1*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = (*(unsigned volatile int *)PCR1 | 0x0400)&0xfffffffb; /*IC_SCK = 0*/
Delay_10_uS();
}
void IC_Nack(void)
{
Delay_10_uS();
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x0404; /*IC_SDA =1*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x0404; /* IC_SCK =1*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = (*(unsigned volatile int *)PCR1 | 0x0400)&0xfffffffb; /*IC_SCK = 0*/
Delay_10_uS();
}
unsigned int IC_Send_Byte( unsigned char d)
{
int i = 8;
unsigned int bit_ack;
while( i-- )
{
Delay_10_uS();
if ( d &0x80 )
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x0404; /*IC_SDA =1*/
else
*(unsigned volatile int *)PCR0=(*(unsigned volatile int *)PCR0 | 0x0400)&0xfffffffb; /*IC_SDA = 0*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x0404; /* IC_SCK =1*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = (*(unsigned volatile int *)PCR1 | 0x0400)&0xfffffffb; /*IC_SCK = 0*/
d = d << 1;
}
Delay_10_uS();
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x0404; /*IC_SDA=1*/
Delay_10_uS();
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 & 0xfffffbff; /*FSRM =0*/
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x0404; /* IC_SCK =1*/
bit_ack = GBIT(*(unsigned volatile int *)PCR0,3);
*(unsigned volatile int *)PCR1 = (*(unsigned volatile int *)PCR1 | 0x0400)&0xfffffffb; /*IC_SCK = 0*/
return bit_ack;
}
unsigned int IC_Receive_Byte(void)
{
unsigned int i = 8, d=0;
Delay_10_uS();
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x0404; /*IC_SDA=1*/
while ( i--)
{
d = d << 1;
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 & 0xfffffbff; /*FSRM =0*/
Delay_10_uS();
*(unsigned volatile int *)PCR1 = *(unsigned volatile int *)PCR1 | 0x0404; /* IC_SCK =1*/
if ( GBIT(*(unsigned volatile int *)PCR0,3) ) d++;
Delay_10_uS();
*(unsigned volatile int *)PCR1 = (*(unsigned volatile int *)PCR1 | 0x0400)&0xfffffffb; /*IC_SCK = 0*/
}
return d;
}
/*本函数用于向IC卡写入数据
sr_addr是源地址,即DSP内存地址
ds_addr是目的地址,即IC卡内地址
count是数据长度,以字节为单位
*/
/*dsp*/ /*IC*/
int w_IC(unsigned int sr_addr,unsigned int ds_addr,unsigned int count)
{
int tmp,tmp1,val;
unsigned int n,count1,count2,count3;
*(unsigned volatile int *)SPCR0 = *(unsigned volatile int *)SPCR0 & 0xfffeffff;
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x2000; /*IC卡上电,电源置高*/
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 & 0xffffffdf;
n=(ds_addr%32+count)/32;
count1=32-ds_addr%32;
count2=(ds_addr%32+count)%32;
tmp=IC_Start();
if(tmp!=1)
return 0x30;
tmp1=IC_Send_Byte( 0xa0 );
if(tmp1!=0)
return 0x33;
//goto echo;
tmp1=IC_Send_Byte( ds_addr/256 );
if(tmp1!=0)
return 0x34;
// goto echo;
tmp1=IC_Send_Byte( ds_addr %256 );
if(tmp1!=0)
return 0x35;
//goto echo;
if(count1>=count)
{
count3=count;
}else
{
count3=count1;
}
while(count3--)
{
int tmp1;
tmp1=IC_Send_Byte( *(unsigned volatile int *)sr_addr );
if(tmp1!=0)
return 0x36;
//goto echo;
//IC_Ack();
Delay_10_uS(); /* waiting for write cycle to be completed */
sr_addr++;
ds_addr++;
}
IC_Stop();
Delay_10_uS();
if(n>1)
{
for(n;n>1;n--)
{
tmp=IC_Start();
if(tmp!=1)
return 0x30;
tmp1=IC_Send_Byte( 0xa0 );
if(tmp1!=0)
goto echo;
tmp1=IC_Send_Byte( ds_addr/256 );
if(tmp1!=0)
goto echo;
tmp1=IC_Send_Byte( ds_addr %256 );
if(tmp1!=0)
goto echo;
val=32;
while(val--)
{
int tmp1;
tmp1=IC_Send_Byte( *(unsigned volatile int *)sr_addr );
if(tmp1!=0)
goto echo;
//IC_Ack();
Delay_10_uS(); /* waiting for write cycle to be completed */
sr_addr++;
ds_addr++;
}
IC_Stop();
Delay_10_uS();
}
}
Delay_10_uS();
if(count1<count)
{
tmp=IC_Start();
if(tmp!=1)
return 0x30;
tmp1=IC_Send_Byte( 0xa0 );
if(tmp1!=0)
goto echo;
tmp1=IC_Send_Byte( ds_addr/256 );
if(tmp1!=0)
goto echo;
tmp1=IC_Send_Byte( ds_addr %256 );
if(tmp1!=0)
goto echo;
while(count2--)
{
int tmp1;
tmp1=IC_Send_Byte( *(unsigned volatile int *)sr_addr );
if(tmp1!=0)
goto echo;
//IC_Ack();
Delay_10_uS(); /* waiting for write cycle to be completed */
sr_addr++;
ds_addr++;
}
IC_Stop();
}
return 0;
echo: return 0x32;
}
/*本函数用于从IC卡读出数据
Ic_addr是源地址,即IC卡地址
dsp_addr是目的地址,即DSP内存地址
count是数据长度,以字节为单位
*/
int r_IC(unsigned int Ic_addr,unsigned int dsp_addr,unsigned int count)
{
int i=0;
int tmp,tmp1,tmp2;
*(unsigned volatile int *)SPCR0 = *(unsigned volatile int *)SPCR0 & 0xfffeffff;
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 | 0x2000; /*IC卡上电,电源置高*/
*(unsigned volatile int *)PCR0 = *(unsigned volatile int *)PCR0 & 0xffffffdf;
tmp=IC_Start();
if(tmp!=1)
return tmp;
tmp1=IC_Send_Byte( 0xa0 );
if(tmp1!=0)
goto echo;
tmp1=IC_Send_Byte( (Ic_addr-1)/256 );
if(tmp1!=0)
goto echo;
tmp1=IC_Send_Byte( (Ic_addr-1) %256 );
if(tmp1!=0)
goto echo;
IC_Start();
tmp2=IC_Send_Byte( 0xa1 );
if(tmp2!=0)
goto echo;
IC_Receive_Byte();
Delay_10_uS();
IC_Ack();
Delay_10_uS();
while(count--)
{
unsigned int retmp;
retmp= IC_Receive_Byte();
i=i+1;
i=i%4;
if(i==0)
{
retmp=retmp<<24;
*(unsigned volatile int *)dsp_addr = (*(unsigned volatile int *)dsp_addr)&0x00ffffff|retmp;
}else if(i==1)
{
*(unsigned volatile int *)dsp_addr = (*(unsigned volatile int *)dsp_addr)&0xffffff00|retmp;
}else if(i==2)
{
retmp=retmp<<8;
*(unsigned volatile int *)dsp_addr = (*(unsigned volatile int *)dsp_addr)&0xffff00ff|retmp;
}else if(i==3)
{
retmp=retmp<<16;
*(unsigned volatile int *)dsp_addr = (*(unsigned volatile int *)dsp_addr)&0xff00ffff|retmp;
}
Delay_10_uS();
IC_Ack();
Delay_10_uS();
dsp_addr++;
Ic_addr++;
}
IC_Nack();
IC_Stop();
return 0;
echo: return 0x32;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -