近期发现技术贴少了,苦于保密问题,也不好乱发。现在发一个STM32F2XX读取RC531的DEMO。
//------------------------------------------------------------------------------------------------------
extern void SPI1_Config(void);
extern unsigned char SPI1_ReadWriteByte(unsigned char TxData);
extern unsigned char Read_RC531_IRQ(void);
/* RC531RST 引脚 */
#define RC531RST_SET() RC531_RSTPD(1) /* 置 RC500RST 为高电平 */
#define RC531RST_CLR() RC531_RSTPD(0) /* 置 RC500RST 为低电平 */
/* RC531NSS 引脚 */
#define RC531NSS_SET() RC531_CS(1) /* 置 RC500RST 为高电平 */
#define RC531NSS_CLR() RC531_CS(0) /* 置 RC500RST 为低电平 */
//全局变量
static volatile MfCmdInfo MInfo;
volatile unsigned char SerBuffer[40];
static volatile MfCmdInfo *MpIsrInfo;
static volatile unsigned char *MpIsrOut;
volatile unsigned char MLastSelectedSnr[4];
//***********************************************************************
//以下代码为1uS的延时,用视波器调整的,很准,KEIL MDK编译,优化等级为0,频率120M
void tDelay_1us(unsigned int nDly){
register unsigned char n;
while(nDly--){
n = 22;
while(n--);
}
}
/****************************************************************************
* 名 称:DelayNS()
* 功 能:长软件延时。
* 入口参数:dly 延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
void delay_1ms(unsigned long dly){
tDelay_1us(8000); //1000us
}
/****************************************************************************
* 名 称:delay_50us()
* 功 能:长软件延时。
* 入口参数:dly 延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
void delay_50us(unsigned char _50us){
tDelay_1us(40); //50us
}
//*****************************************************************************
//不改动原来函数!必须传入数据
void ResetInfo(void){
memset((unsigned char *)&MInfo,0,sizeof( MInfo));
}
/****************************************************************************
* 名 称:ReadRawIO()
* 功 能:从一个地址读出一个数据(EEPROM)。
* 入口参数:Address 地址
* 出口参数:数据
****************************************************************************/
unsigned char ReadRawIO(unsigned char Address){
unsigned char a;
RC531NSS_CLR();
SPI1_ReadWriteByte((Address<<1)|0x80);
a = SPI1_ReadWriteByte(0xff); //亚指令!
SPI1_ReadWriteByte(0x00);
RC531NSS_SET();
return a;
}
/****************************************************************************
* 名 称:WriteRawIO()
* 功 能:往一个地址写一个数据(EEPROM)。
* 入口参数:Address 地址
value 数据
* 出口参数:无
****************************************************************************/
void WriteRawIO(unsigned char Address, unsigned char value){
RC531NSS_CLR();
SPI1_ReadWriteByte((Address<<1)&0x7f);
SPI1_ReadWriteByte(value);
RC531NSS_SET();
}
/****************************************************************************
* 名 称:WriteIO()
* 功 能:往一个地址写一个数据(EEPROM)。
* 入口参数:Address 地址
value 数据
* 出口参数:无
****************************************************************************/
void WriteIO(unsigned char Address, unsigned char value){
RC531NSS_CLR();
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(GetRegPage(Address));
RC531NSS_SET();
RC531NSS_CLR();
SPI1_ReadWriteByte((Address<<1)&0x7f);
SPI1_ReadWriteByte(value);
RC531NSS_SET();
}
/****************************************************************************
* 名 称:ReadIO()
* 功 能:从一个地址读出一个数据(EEPROM)。
* 入口参数:Address 地址
* 出口参数:数据
****************************************************************************/
unsigned char ReadIO(unsigned char Address) {
unsigned char a;
RC531NSS_CLR();
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(GetRegPage(Address));
RC531NSS_SET();
RC531NSS_CLR();
SPI1_ReadWriteByte((Address<<1)|0x80);
a = SPI1_ReadWriteByte(0xff); //亚指令!
SPI1_ReadWriteByte(0x00);
RC531NSS_SET();
return a;
}
/****************************************************************************
* 名 称:RFRegWrite()
* 功 能:往一个地址写一个数据(EEPROM)。
* 入口参数:Address 地址
value 数据
* 出口参数:无
****************************************************************************/
void RFRegWrite(unsigned char Address, unsigned char value){
RC531NSS_CLR();
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(GetRegPage(Address));
RC531NSS_SET();
RC531NSS_CLR();
SPI1_ReadWriteByte((Address<<1)&0x7f);
SPI1_ReadWriteByte(value);
RC531NSS_SET();
}
/****************************************************************************
* 名 称:ReadIO()
* 功 能:从一个地址读出一个数据(EEPROM)。
* 入口参数:Address 地址
* 出口参数:数据
****************************************************************************/
unsigned char RFRegRead(unsigned char Address){
unsigned char a;
RC531NSS_CLR();
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(GetRegPage(Address));
RC531NSS_SET();
RC531NSS_CLR();
SPI1_ReadWriteByte((Address<<1)|0x80);
a = SPI1_ReadWriteByte(0xff); //亚指令!
SPI1_ReadWriteByte(0x00);
RC531NSS_SET();
return a;
}
/****************************************************************************
* 名 称:SetBitMask()
* 功 能:清一个bit
* 入口参数:reg 内存
mask俺码
* 出口参数:无
****************************************************************************/
char SetBitMask(unsigned char reg,unsigned char mask){
char tmp = 0x00;
tmp = ReadIO(reg);
WriteIO(reg,tmp | mask); // set bit mask
return 0x00;
}
/****************************************************************************
* 名 称:ClearBitMask()
* 功 能:清一个bit
* 入口参数:reg 内存
mask俺码
* 出口参数:无
****************************************************************************/
char ClearBitMask(unsigned char reg,unsigned char mask){
char tmp = 0x00;
tmp = ReadIO(reg);
WriteIO(reg,tmp & ~mask); // clear bit mask
return 0x00;
}
/****************************************************************************
* 名 称:FlushFIFO()
* 功 能:清除内部FIFO
* 入口参数:无
* 出口参数:无
****************************************************************************/
void FlushFIFO(void){
SetBitMask(RegControl,0x01);
}
/****************************************************************************
* 名 称:M500PcdCmd()
* 功 能:清一个bit
* 入口参数:unsigned char cmd,
unsigned char *rcv,
MfCmdInfo idata *info
* 出口参数:0正常,其它错误码
****************************************************************************/
char M500PcdCmd(unsigned char cmd,
unsigned char *rcv,
MfCmdInfo *info){
char status = MI_OK;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned int timecnt = 0;
char tmpStatus ;
unsigned char lastBits;
WriteIO(RegInterruptEn,0x7F); //开中断
WriteIO(RegInterruptRq,0x7F); //清中断
WriteIO(RegCommand,PCD_IDLE); //清除指令
FlushFIFO();
MpIsrInfo = info;
MpIsrOut = rcv;
info->irqSource = 0x00;
switch(cmd){
case PCD_IDLE: //0x00 无指令,清指令
irqEn = 0x00;
waitFor = 0x00;
break;
case PCD_WRITEE2: //0x01 写FIFO,并定入EEPROM
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_READE2: //0x03 从EEPROM取数据并写入FIFO
irqEn = 0x07;
waitFor = 0x04;
break;
case PCD_LOADCONFIG: //0x07 从EEPROM读数据并用于初始化
case PCD_LOADKEYE2: //0x0b 将密钥从EEPROM复制到KEY缓存
case PCD_AUTHENT1: //0x0c 执行Cryptol算法的认证过程
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_CALCCRC: //0x12 激活CRC
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_AUTHENT2: //0x14 执行Cryptol算法的认证过程二
irqEn = 0x04;
waitFor = 0x04;
break;
case PCD_RECEIVE: //0x16 接收
info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4);
irqEn = 0x06;
waitFor = 0x04;
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>