void delay_ns(unsigned int ns)
{
unsigned int i;
for(i=0;i<ns;i++)
{
nop();
nop();
nop();
}
}
//------------------------------------------
// 讀SPI數(shù)據(jù)
//------------------------------------------
unsigned char SPIReadByte(void)
{
unsigned char SPICount; // Counter used to clock out the data
unsigned char SPIData;
SPIData = 0;
for (SPICount = 0; SPICount < 8; SPICount++) // Prepare to clock in the data to be read
{
SPIData <<=1; // Rotate the data
CLR_SPI_CK; //nop();//nop(); // Raise the clock to clock the data out of the MAX7456
if(STU_SPI_MISO)
{
SPIData|=0x01;
}
SET_SPI_CK; //nop();//nop(); // Drop the clock ready for the next bit
} // and loop back
return (SPIData); // Finally return the read data
}
//------------------------------------------
// 寫SPI數(shù)據(jù)
//------------------------------------------
void SPIWriteByte(unsigned char SPIData)
{
unsigned char SPICount; // Counter used to clock out the data
for (SPICount = 0; SPICount < 8; SPICount++)
{
if (SPIData & 0x80)
{
SET_SPI_MOSI;
}
else
{
CLR_SPI_MOSI;
}
nop();nop();
CLR_SPI_CK;nop();nop();
SET_SPI_CK;nop();nop();
SPIData <<= 1;
}
}
for (i=0; i<InLenByte; i++)
{
WriteRawRC(FIFODataReg, pInData[i]);
}
WriteRawRC(CommandReg, Command);
if (Command == PCD_TRANSCEIVE)
{
SetBitMask(BitFramingReg,0x80);
}
//i = 600;//根據(jù)時(shí)鐘頻率調(diào)整,操作M1卡最大等待時(shí)間25ms
i = 2000;
do
{
n = ReadRawRC(ComIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitFor));
ClearBitMask(BitFramingReg,0x80);
if (i!=0)
{
if(!(ReadRawRC(ErrorReg)&0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{ status = MI_NOTAGERR; }
if (Command == PCD_TRANSCEIVE)
{
n = ReadRawRC(FIFOLevelReg);
lastBits = ReadRawRC(ControlReg) & 0x07;
if (lastBits)
{
*pOutLenBit = (n-1)*8 + lastBits;
}
else
{
*pOutLenBit = n*8;
}
if (n == 0)
{
n = 1;
}
if (n > MAXRLEN)
{
n = MAXRLEN;
}
for (i=0; i<n; i++)
{
pOutData[i] = ReadRawRC(FIFODataReg);
}
}
}
else
{
status = MI_ERR;
}
}
SetBitMask(ControlReg,0x80); // stop timer now
WriteRawRC(CommandReg,PCD_IDLE);
return status;
}