📄 rfid_x5577.c
字号:
// $Id: rfid_x5577.c,v 1.6 2007/01/24 21:44:15 tprescott Exp $
/*****************************************************
Project : rfid_x5577.h
Date : 9/25/2006
Author : Toby Prescott
Company : Atmel
Comments: AVR Studio GCC
Revisions:
v1.0 - Started written for WinAVR
*************************************************************/
#include "rfid_x5577.h"
//**** Configuration Block Hard Code ****//
unsigned char x5577_ConfigReg[4] EEMEM = {0x6E,0x3E,0x80,0xE8}; // X-tended mode Invert Data Off
//unsigned char x5577_ConfigReg[4] EEMEM = {0x6E,0x3E,0x80,0xEa}; // X-tended mode Invert Data On
unsigned char x5577_AFEReg[4] EEMEM = {0x9D,0xC0,0x00,0x00}; // Analog Front End Options
unsigned int x5577_SGap EEMEM = 330;
unsigned int x5577_Gap EEMEM = 220;
unsigned int x5577_Zero EEMEM = 100;
unsigned int x5577_One EEMEM = 370;
unsigned int x5577_ShortL EEMEM = 50;
unsigned int x5577_ShortH EEMEM = 210;
unsigned int x5577_LongL EEMEM = 210;
unsigned int x5577_LongH EEMEM = 350;
unsigned char x5577ResetCmd = 0x00;
//**** Load New values for Write Timings ****//
void x5577_Set_SGap(unsigned int iSGap){eeprom_write_word(&x5577_SGap,iSGap);}
void x5577_Set_Gap(unsigned int iGap){eeprom_write_word(&x5577_Gap,iGap);}
void x5577_Set_Zero(unsigned int iZero){eeprom_write_word(&x5577_Zero,iZero);}
void x5577_Set_One(unsigned int iOne){eeprom_write_word(&x5577_One,iOne);}
//**** Return Status of Write Timings ****//
unsigned int x5577_Get_SGap(void){return eeprom_read_word(&x5577_SGap);}
unsigned int x5577_Get_Gap(void){return eeprom_read_word(&x5577_Gap);}
unsigned int x5577_Get_Zero(void){return eeprom_read_word(&x5577_Zero);}
unsigned int x5577_Get_One(void){return eeprom_read_word(&x5577_One);}
//**** Load New values for Read Timings ****//
void x5577_Set_ShortL(unsigned int iShortL){eeprom_write_word(&x5577_ShortL,iShortL);}
void x5577_Set_ShortH(unsigned int iShortH){eeprom_write_word(&x5577_ShortH,iShortH);}
void x5577_Set_LongL(unsigned int iLongL){eeprom_write_word(&x5577_LongL,iLongL);}
void x5577_Set_LongH(unsigned int iLongH){eeprom_write_word(&x5577_LongH,iLongH);}
//**** Return Status of Read Timings ****//
unsigned int x5577_Get_ShortL(void){return eeprom_read_word(&x5577_ShortL);}
unsigned int x5577_Get_ShortH(void){return eeprom_read_word(&x5577_ShortH);}
unsigned int x5577_Get_LongL(void){return eeprom_read_word(&x5577_LongL);}
unsigned int x5577_Get_LongH(void){return eeprom_read_word(&x5577_LongH);}
// *******************************************************************************
// Pass in new Configuration Block data
// ******************************************************************************/
void x5577_Set_ConfigReg(unsigned char *cConfig)
{
unsigned char i;
for(i=0; i<4; i++) // Loop for all bytes in block
{
eeprom_write_byte(&x5577_ConfigReg[i],*cConfig++); // Save new byte
}
}
// *******************************************************************************
// Pass in new AFE Options data
// ******************************************************************************/
void x5577_Set_AFEReg(unsigned char *cAFEReg)
{
unsigned char i;
for(i=0; i<4; i++) // Loop for all bytes in block
{
eeprom_write_byte(&x5577_AFEReg[i],*cAFEReg++); // Save new byte
}
}
// *******************************************************************************
// Read current AFE Options data
// ******************************************************************************/
void x5577_Get_AFEReg(unsigned char *cTmpPtr)
{
unsigned char i;
for(i=0; i<4; i++) // Loop for all bytes in block
{
*cTmpPtr++ = eeprom_read_byte(&x5577_AFEReg[i]); // Save new byte
}
}
// *******************************************************************************
// Program the Configuration Block in the 5577 using specified Reader
// ******************************************************************************/
void x5577_WriteConfigReg(unsigned char Target_Reader)
{
unsigned char i,tempByte;
unsigned char writeBuff[5] = {0};
unsigned char cOpCode = 0x04; // Page0 + Lock bit = 0
writeBuff[0] = cOpCode<<5; // Load the Write Op Code into buffer
for(i=0; i<4; i++)
{
tempByte =eeprom_read_byte(&x5577_ConfigReg[i]); // Get the Configuration Block Data
writeBuff[i] |= (tempByte>>3); // Load into the buffer with shift for Op Code
writeBuff[(unsigned char)(i+1)] |= (tempByte<<5);
}
//writeBuff[4] |= 0x00<<2; // Load Address of Configuration Block in buffer
//Select Reader, Currently only supports U2270
if(Target_Reader == 70)
{
U2270_PPCWriteBlock(38, &writeBuff[0]); // Call Write Block Routine
Timer_delay_us(10000);
x5577_Reset(Target_Reader); // Send Reset Cmd
}
snd_Play(BEEPFREQ1,BEEPFREQ0,BEEPTIME); // return Audio feedback
}
// *******************************************************************************
// Program the AFE Options Block in the 5577 using specified Reader
// ******************************************************************************/
void x5577_WriteAFEReg(unsigned char Target_Reader)
{
unsigned char i,tempByte;
unsigned char writeBuff[5] = {0};
unsigned char cOpCode = 0x06; // Page1 + Lock bit = 0
unsigned char cAddr = 0x03;
writeBuff[0] = cOpCode<<5; // Load the Write Op Code into buffer
for(i=0; i<4; i++)
{
tempByte =eeprom_read_byte(&x5577_AFEReg[i]); // Get the Configuration Block Data
writeBuff[i] |= (tempByte>>3); // Load into the buffer with shift for Op Code
writeBuff[(unsigned char)(i+1)] |= (tempByte<<5);
}
writeBuff[4] |= cAddr<<2; // Load Address of Configuration Block in buffer
//Select Reader, Currently only supports U2270
if(Target_Reader == 70)
{
U2270_PPCWriteBlock(38, &writeBuff[0]); // Call Write Block Routine
Timer_delay_us(10000);
x5577_Reset(Target_Reader); // Send Reset Cmd
}
snd_Play(BEEPFREQ1,BEEPFREQ0,BEEPTIME); // return Audio feedback
}
// *******************************************************************************
// Program Block Data in the 5577 using specified Reader
// Pass in the block data and the block address
// ******************************************************************************/
unsigned char x5577_WriteBlock(unsigned char Target_Reader, unsigned char *inBuff, unsigned char inAddr, unsigned char confirm)
{
unsigned char i,tempByte;
unsigned char writeBuff[5] = {0};
unsigned char cOpCode = 0x04;
unsigned char *tmpInBuff;
unsigned char cError=1;
tmpInBuff = &(*inBuff); // Store beginning address pointer
writeBuff[0] = cOpCode<<5; // Load the Write Op Code into buffer
for(i=0; i<4; i++)
{
tempByte =*inBuff++; // Copy each byte from input data
writeBuff[i] |= (tempByte>>3); // Shift and load into the buffer
writeBuff[(unsigned char)(i+1)] |= (tempByte<<5);
}
writeBuff[4] |= inAddr<<2; // Load the correct block address into buffer
//Select Reader, Currently only supports U2270
if(Target_Reader == 70)
{
U2270_PPCWriteBlock(38, &writeBuff[0]); // Call Write Block Routine
Timer_delay_us(10000);
x5577_Reset(Target_Reader); // Send Reset Cmd
}
//IF confirm wanted check for successful write
if(confirm == 1){cError = x5577_ConfirmWrite(Target_Reader, tmpInBuff, inAddr);}
return cError;
}
// *******************************************************************************
// Program Entire Tag Data in the 5577 using specified Reader
// ******************************************************************************/
unsigned char x5577_WriteAll(unsigned char Target_Reader)
{
unsigned char block, i;
unsigned char tempBlock[4];
unsigned char cError=0;
// For Data Blocks 1-7
for(block=1; block<8; block++)
{
for(i=0; i<4; i++)
{
tempBlock[i]=Tag_Get_Block(block,i); //Read the Block Data from EEPROM
}
cError = x5577_WriteBlock(Target_Reader, &tempBlock[0], block, 1); // Call Block Write for Each Block
Timer_delay_us(10000);
if(cError == 0){block=9;}
}
return cError;
}
// *******************************************************************************
// Verify that the Block Data was correctly written
// Pass in the intended block data and the block address
// ******************************************************************************/
unsigned char x5577_ConfirmWrite(unsigned char Target_Reader, unsigned char *inBuff, unsigned char inAddr)
{
unsigned char i, k=0;
unsigned char tmpData;
unsigned char *rDatPtr;
unsigned char cError=0;
//Select Reader, Currently only supports U2270
if(Target_Reader == 70)
{
U2270_PPCWriteBlock(2, &x5577ResetCmd); // Send Reset Cmd
Timer_delay_us(10000);
cError = U2270_ReadData(67,&rDatPtr,MANCHESTER,X5557SYNC,224); // Call Read Routine (All Blocks)
}
// IF valid Read
if(cError == 0)
{
rDatPtr = rDatPtr + (unsigned char)((inAddr-1)*4); // Move pointer to requested Block Address
for(i=0; i<4; i++)
{
tmpData = *inBuff++; // Copy attempted block Data byte
if(*rDatPtr++ == tmpData){k = 1;} // Check byte against byte read from tag
else // If data does not match
{
k=0; // Flag mismatch
i=5; // Exit check
}
}
}
if(k==1)
{
snd_Play(BEEPFREQ1,BEEPFREQ0,BEEPTIME); // IF all bytes match, return Audio feedback
return 1;
}
else
{
snd_Play(BUZZFREQ1,BUZZFREQ0,BUZZTIME);
return 0;
}
}
void x5577_Reset(unsigned char Target_Reader)
{
if(Target_Reader == 70){U2270_PPCWriteBlock(2, &x5577ResetCmd);} // Send Reset Cmd
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -