📄 fk4428.c
字号:
BYTE i;
for(i=0;i<_len;i++)
{
*_dt=SPI_RdByt();
_dt++;
ICCLK=1;
delay_15us_NOP();
ICIO=1;
*_dt++=(ICIO==1?0xff:0);
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
}
return (OK);
}
void SPI_WtByt(BYTE wt_data)
{
int i;
for(i=0;i<8;i++)
{
wt_data>>=1;
ICIO=CY;
delay_15us_NOP();
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
}
}
void SPI_Cmd(BYTE _cntrl,BYTE _addr,BYTE _dt)
{
ICRST=1;
delay_15us_NOP();
ICCLK=0;
SPI_WtByt( _cntrl);
delay_15us_NOP();
SPI_WtByt( _addr);
delay_15us_NOP();
SPI_WtByt( _dt);
delay_15us_NOP();
ICRST=0;
delay_15us_NOP();
}
void wrwpb_4428(int offset, int len, BYTE *databuff)//protect
{
BYTE cw1,cw2,i,j;
for(j=0;j<len;j++)
{
cw1=((BYTE)(offset>>2)&0xc0)+0x31;
cw2=(BYTE)offset;
SPI_Cmd(cw1,cw2,databuff[j]);
for(i=0;i<255;i++)
{
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
ICIO=1;
if(ICIO==0)break;
}
offset++;
}
}
void swr_4428(int offset, int len, BYTE *w_string)
{
BYTE cw1,cw2,i,j;
for(j=0;j<len;j++)
{
cw1=((BYTE)(offset>>2)&0xc0);
cw1+=0x33;
cw2=(BYTE)offset;
SPI_Cmd(cw1,cw2,w_string[j]);
for(i=0;i<255;i++)
{
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
ICIO=1;
if(ICIO==0)break;
}
offset++;
}
}
void pwr_4428(int offset, int len, BYTE *w_string)
{
BYTE cw1,cw2,i,j;
for(j=0;j<len;j++)
{
cw1=((BYTE)(offset>>2)&0xc0);
cw1+=0x30;
cw2=(BYTE)offset;
SPI_Cmd(cw1,cw2,w_string[j]);
for(i=0;i<255;i++)
{
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
ICIO=1;
if(ICIO==0)break;
}
offset++;
}
}
void wct_4428(int offset, int len, BYTE *w_string)
{
BYTE cw1,cw2,i,j;
for(j=0;j<len;j++)
{
cw1=((BYTE)(offset>>2)&0xc0);
cw1+=0x32;
cw2=(BYTE)offset;
SPI_Cmd(cw1,cw2,w_string[j]);
for(i=0;i<255;i++)
{
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
ICIO=1;
if(ICIO==0)break;
}
offset++;
}
}
void csc_4428(BYTE* p_string)
{
BYTE cw1,cw2,i,j;
int offset=1022;
for(j=0;j<2;j++)
{
cw1=((BYTE)(offset>>2)&0xc0)+0x0d;//?
cw2=(BYTE)offset;
SPI_Cmd(cw1,cw2,p_string[j]);
for(i=0;i<255;i++)
{
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
ICIO=1;
if(ICIO==0)break;
}
offset++;
}
}
void rdwpb_4428(int offset, int len, BYTE *databuff)
{
BYTE cw1,cw2;
cw1=((BYTE)(offset>>2)&0xc0)+0x0c;
cw2=(BYTE)offset;
SPI_Cmd(cw1,cw2,0);
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
SPI_RdMMwpb(0, len, databuff);
}
void srd_4428(int offset, int len, BYTE *r_string)
{
BYTE cw1,cw2;
cw1=((BYTE)(offset>>2)&0xc0)+0x0e;
cw2=(BYTE)offset;
SPI_Cmd(cw1,cw2,0);
delay_15us_NOP();
ICCLK=1;
delay_15us_NOP();
ICCLK=0;
delay_15us_NOP();
SPI_RdMM(0, len, r_string);
}
/****************************************************************************
* Function: cmd_execution *
****************************************************************************/
void cmd_execution(void)
{
BYTE Status;
BYTE len;
BYTE cmd;
WORD Ret;
BYTE idata tmp[16];
BYTE idata snr[4];
BYTE idata size;
BYTE idata tt[2];
BYTE adr;
BYTE tmp1,tmp2,tmp3,i;
len = SerBuffer[LENGTH];
SerBuffer[LENGTH] = 0;
Status=OK;
cmd = SerBuffer[COMMAND];
switch (cmd)
{
case 0x40: // HL-Authentication
Status = mifs_request(SerBuffer[HLREQMODE] | ALL, tt);
if (Status != OK)
break;
Status = mifs_select(&SerBuffer[HL_SERNR], &size);
if (Status != OK)
break;
Status = mifs_authentication(SerBuffer[HLAUTHMODE], SerBuffer[HLSECNR]);
break;
case 0x41: // Request
Status = mifs_request(SerBuffer[MODE], &SerBuffer[TAGTYPE]);
if (Status == OK)
{
SerBuffer[LENGTH] = 2;
}
break;
case 0x42: // Anticoll
Status = mifs_anticoll(SerBuffer[BCNT], &SerBuffer[SERNR]);
if (Status == OK)
{
SerBuffer[LENGTH] = 4;
}
break;
case 0x43: // Select
Status = mifs_select(&SerBuffer[SERNR], &SerBuffer[SIZE]);
if (Status == OK)
{
SerBuffer[LENGTH] = 1;
}
break;
case 0x44: // Authentication
Status = mifs_authentication(SerBuffer[MODE], SerBuffer[SECNR]);
break;
case 0x45: // Halt
Status = mifs_halt();
break;
case 0x46: // Read
Status = mifs_read(SerBuffer[ADR], &SerBuffer[ADR]);
if (Status == OK)
{
SerBuffer[LENGTH] = 16;
}
break;
case 0x47: // Write
Status = mifs_write(SerBuffer[ADR], &SerBuffer[DATABYTES]);
break;
case 0x48: // Increment
Status = mifs_increment(SerBuffer[ADR], &SerBuffer[VALUE]);
break;
case 0x49: // Decrement
Status = mifs_decrement(SerBuffer[ADR], &SerBuffer[VALUE]);
break;
case 0x4A: // Restore
Status = mifs_restore(SerBuffer[ADR]);
break;
case 0x4B: // Transfer
Status = mifs_transfer(SerBuffer[ADR]);
break;
case 0x4C: // Load Key
adr = (len == 18)? NKEY8 : NKEY6;
Status = mifs_load_key(SerBuffer[MODE], SerBuffer[SECNR],
(KEY idata *)&SerBuffer[TKEY],
(KEY idata *)&SerBuffer[adr]);
break;
case 0x4E: // Reset
mifs_reset(&SerBuffer[TIME]);
break;
case 0x52: // Config
mifs_config(SerBuffer[MODE], SerBuffer[BAUD]);
break;
case 0x53: // Check Write
Status = MIS_CHK_FAILED;
if (!(SerBuffer[SERNR]&0x0f)) //authentication necessary
{
if (mifs_request(ALL, tt) != OK)
{
if (mifs_request(ALL, tt) != OK)
{
break;
}
}
if (mifs_select(&SerBuffer[SERNR], &size) != OK)
{
if (mifs_request(ALL, tt) != OK)
{
break;
}
if (mifs_select(&SerBuffer[SERNR], &size) != OK)
{
break;
}
}
if (mifs_authentication(SerBuffer[AUTHMODE],
SerBuffer[ADRCHKWR] >> 2) != OK)
{
break;
}
}
if (mifs_read(SerBuffer[ADRCHKWR], tmp) != OK)
{
break;
}
if (memcmp(&SerBuffer[DATACHKWR], tmp, 16) != OK)
{
Status = MIS_CHK_COMPERR;
}else{
Status = MIS_CHK_OK;
}
break;
case 0x56: // Authentication with additional trailer address
Status = mifs_authentication_2(SerBuffer[MODE], SerBuffer[SECNR], SerBuffer[AUTHADD]);
break;
case 0x57: // Decrement+Transfer
Status = mifs_decrement_transfer(SerBuffer[ADR], &SerBuffer[VALUE]);
break;
case 0x5f: // Set Port
beep = SerBuffer[PORTBYTE]&0x01;
break;
case 0x60:
case 0x64:
bUseSAM=(cmd==0x64);
if(!bUseSAM)
{
Status=Check_ICSW();
if(Status!=OK)
break;
}
Reset(&SerBuffer[LENGTH], &SerBuffer[ADR]); //Insert Card or Warm Reset,CPU Card Reset
break;
case 0x61: //CPUC_Cmd
case 0x65:
bUseSAM=(cmd==0x65);
if(!bUseSAM)
{
Status=Check_ICSW();
if(Status!=OK)
break;
}
if(!RdVCC)
Reset(tmp, tmp); //Insert Card or Warm Reset,CPU Card Reset
SerBuffer[len+3]=0x00;
Ret=CPUC_Cmd(len,&SerBuffer[ADR], &SerBuffer[LENGTH], &SerBuffer[ADR]);
len=SerBuffer[LENGTH];
*((WORD *)(&SerBuffer[3+len]))=Ret;
SerBuffer[LENGTH]+=2;
break;
case 0x62: //Remove Card
Power_off();
break;
case 0x63: //Get Interface Device Status
SerBuffer[ADR] =ICSW==ON? 0x00: 0x05;
SerBuffer[LENGTH]=1;
break;
case 0x80: //4428 reset
Status=Check_ICSW();
if(Status!=OK)
break;
reset_4428();
break;
case 0x81:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
swr_4428(*(int *)&SerBuffer[ADR],*(int *)&SerBuffer[ADR+2], &SerBuffer[ADR+4]);
break;
case 0x82:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
len=SerBuffer[ADR+3];
srd_4428(*(int *)&SerBuffer[ADR],*(int *)&SerBuffer[ADR+2], &SerBuffer[ADR]);
SerBuffer[LENGTH]=len;
break;
case 0x83:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
wrwpb_4428(*(int *)&SerBuffer[ADR],*(int *)&SerBuffer[ADR+2], &SerBuffer[ADR+4]);
break;
case 0x84:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
len=SerBuffer[ADR+3];
rdwpb_4428(*(int *)&SerBuffer[ADR],*(int *)&SerBuffer[ADR+2], &SerBuffer[ADR]);
SerBuffer[LENGTH]=2*len;
break;
case 0x85:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
rdwpb_4428(1021,1,&tmp1);
if(tmp1==0)
{
Status=4;
}else{
tmp2=tmp1;
for(i=0;i<8;i++)
{
tmp2=tmp2<<1;
if(CY==1)break;
}
tmp3=(0x80>>i);
tmp1=tmp1-tmp3;
wct_4428(1021,1,&tmp1);
csc_4428(&SerBuffer[ADR]);
tmp1=0xff;
swr_4428(1021,1,&tmp1);
}
break;
case 0x86:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
srd_4428(1022,2, &SerBuffer[ADR]);
SerBuffer[LENGTH]=2;
break;
case 0x87:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
swr_4428(1022,2, &SerBuffer[ADR]);
break;
case 0x88:
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
rdwpb_4428(1021,1,&SerBuffer[ADR]);
SerBuffer[LENGTH]=1;
break;
case 0x89: //
Status=Check_ICSW();
if(Status!=OK)
break;
if(ICVCC!=1)
reset_4428();
pwr_4428(*(int *)&SerBuffer[ADR], *(int *)&SerBuffer[ADR+2], &SerBuffer[ADR+4]);
break;
default:
Status = W_CMD_NY_IMPL;
break;
} // end switch (Command)
SerBuffer[STATUS] = Status;
}
/****************************************************************************
* Function: main *
****************************************************************************/
void main(void)
{
BYTE i;
init();
/* for(i=0;i<8;i++)
{
SerBuffer[COMMAND]=0x75;
cmd_execution();
}
reset_4428();
SPI_RdMM(0,4,SerBuffer);
rd_4428(1,8,SerBuffer);
rdwpb_4428(1,8,SerBuffer);
rd_4428(1021,3,SerBuffer);
rdwpb_4428(1021,3,SerBuffer);
SerBuffer[10]=0x7f;
wct_4428(1021,1,&SerBuffer[10]);
SerBuffer[0]=0x24;SerBuffer[1]=0x10;
csc_4428(2,SerBuffer);
rd_4428(1021,1,&SerBuffer[10]);
SerBuffer[10]=0xff;
wr_4428(1021,1,&SerBuffer[10]);
SerBuffer[0]=0x24;
SerBuffer[1]=0x10;
wr_4428(1022,2,SerBuffer);
SerBuffer[0]=225;
SerBuffer[1]=234;
wr_4428(0x20,2,&SerBuffer[0]);
rd_4428(0x20,2,&SerBuffer[2]);
SerBuffer[0]=0x55;
wrwpb_4428(0x10,1,&SerBuffer[0]);
rd_4428(1021,3,SerBuffer);
rdwpb_4428(1021,3,SerBuffer);
SerBuffer[0]=0x55;
wr_4428(0x11,1,&SerBuffer[0]);
SerBuffer[0]=0x55;
wpbwdc_4428(0x11,1,&SerBuffer[0]);
SerBuffer[11]=0xff;
wr_4428(1021,1,&SerBuffer[11]);
wr_4428(0x20,2,SerBuffer);*/
while(1)
{
TOGGLE_WD();
Check_cmd();
Check_ICSW();
}while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -