z-stack驱动RC522读卡器
时间:10-02
整理:3721RD
点击:
我的是cc2530芯片,有没有人做过z-stack驱动MFRC522读卡器,我现在遇到了一个问题,就是裸机程序(没用z-stack)可以完美驱动MFRC522读卡器,但是把程序加入到z-stack中就无法寻卡,查了好多资料都不行,有没人可以帮帮忙,万分感谢。
与MFRC522的接口是哪种?SPI ,I2C,uart?
普通IO模拟的SPI接口
自己顶一下
相关代码如下:
主要在GenericApp.c添加了200行和395行,其他没改
IC_w_r.c.c
#include"rc522.h" #include"UART.h" #include"iocc2530.h" //宏定义 #define IC_CS P1_7 #define IC_SCK P0_0 #define IC_MOSI P1_2 #define IC_MISO P0_4 #define IC_REST P0_5 #define uint unsigned int #define uchar unsigned char //void Delay_I_1us(unsigned int k) //{ // uint i,j; // for(i=0;i<k;i++) // for(j=0;j<32;j++); //} //引脚初始化 void Initial(void) { /* IC_CS P1_7 */ P1DIR |= 1<<7; P1INP |= 1<<7; P1SEL &= ~(1<<7); /* IC_SCK P0_0 */ P0DIR |= 1<<0; P0INP |= 1<<0; P0SEL &= ~(1<<0); /* IC_MOSI P1_2 */ P1DIR |= 1<<2; P1INP |= 1<<2; P1SEL &= ~(1<<2); /* IC_MISO P0_4 */ P0DIR &= ~(1<<4); P0INP &= ~(1<<4); P0SEL &= ~(1<<4); /* IC_REST P0_5 */ P0DIR |= 1<<5; P0INP |= 1<<5; P0SEL &= ~(1<<5); IC_SCK = 1; IC_CS = 1; } //延迟函数 void Delay_I_1us(unsigned int k) { while(k--) { asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop"); asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop"); asm("nop");asm("nop"); } } void SPIWriteByte(uchar infor) { unsigned int counter; for(counter=0;counter<8;counter++) { if(infor&0x80) IC_MOSI = 1; else IC_MOSI = 0; Delay_I_1us(3); IC_SCK = 0; Delay_I_1us(1); IC_SCK = 1; Delay_I_1us(3); infor <<= 1; } } unsigned char SPIReadByte() { unsigned int counter; unsigned char SPI_Data; for(counter=0;counter<8;counter++) { SPI_Data<<=1; IC_SCK = 0; Delay_I_1us(3); if(IC_MISO == 1) SPI_Data |= 0x01; Delay_I_1us(2); IC_SCK = 1; Delay_I_1us(3); } return SPI_Data; } ///////////////////////////////////////////////////////////////////// //功 能:读RC632寄存器 //参数说明:Address[IN]:寄存器地址 //返 回:读出的值 ///////////////////////////////////////////////////////////////////// unsigned char ReadRawRC(unsigned char Address) { unsigned char ucAddr; unsigned char ucResult=0; IC_CS = 0; ucAddr = ((Address<<1)&0x7E)|0x80;//地址变换,SPI的读写地址有要求 SPIWriteByte(ucAddr); ucResult=SPIReadByte(); IC_CS = 1; return ucResult; } ///////////////////////////////////////////////////////////////////// //功 能:写RC632寄存器 //参数说明:Address[IN]:寄存器地址 // value[IN]:写入的值 ///////////////////////////////////////////////////////////////////// void WriteRawRC(unsigned char Address, unsigned char value) { unsigned char ucAddr; Address <<= 1; ucAddr = (Address&0x7e); IC_CS = 0; SPIWriteByte(ucAddr); SPIWriteByte(value); IC_CS = 1; } ///////////////////////////////////////////////////////////////////// //功 能:置RC522寄存器位 //参数说明:reg[IN]:寄存器地址 // mask[IN]:置位值 ///////////////////////////////////////////////////////////////////// void SetBitMask(unsigned char reg,unsigned char mask) { char tmp = 0x0; tmp = ReadRawRC(reg); WriteRawRC(reg,tmp | mask); // set bit mask } ///////////////////////////////////////////////////////////////////// //功 能:清RC522寄存器位 //参数说明:reg[IN]:寄存器地址 // mask[IN]:清位值 ///////////////////////////////////////////////////////////////////// void ClearBitMask(unsigned char reg,unsigned char mask) { char tmp = 0x0; tmp = ReadRawRC(reg); WriteRawRC(reg, tmp & ~mask); // clear bit mask } ///////////////////////////////////////////////////////////////////// //开启天线 //每次启动或关闭天险发射之间应至少有1ms的间隔 ///////////////////////////////////////////////////////////////////// void PcdAntennaOn(void) { unsigned char i; i = ReadRawRC(TxControlReg); if (!(i & 0x03)) { SetBitMask(TxControlReg, 0x03); } } ///////////////////////////////////////////////////////////////////// //关闭天线 ///////////////////////////////////////////////////////////////////// void PcdAntennaOff(void) { ClearBitMask(TxControlReg, 0x03); } ///////////////////////////////////////////////////////////////////// //功 能:复位RC522 //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// void PcdReset(void) { //PORTD|=(1<<RC522RST); IC_REST = 1; Delay_I_1us(1); //PORTD&=~(1<<RC522RST); IC_REST = 0; Delay_I_1us(1); //PORTD|=(1<<RC522RST); IC_REST = 1; Delay_I_1us(1); WriteRawRC(0x01,0x0f); while(ReadRawRC(0x01)&0x10); Delay_I_1us(10); WriteRawRC(ModeReg,0x3D); //定义发送和接收常用模式 和Mifare卡通讯,CRC初始值0x6363 WriteRawRC(TReloadRegL,30); //16位定时器低位 WriteRawRC(TReloadRegH,0); //16位定时器高位 WriteRawRC(TModeReg,0x8D); //定义内部定时器的设置 WriteRawRC(TPrescalerReg,0x3E); //设置定时器分频系数 WriteRawRC(TxAutoReg,0x40); // 调制发送信号为100%ASK //return MI_OK; } ////////////////////////////////////////////////////////////////////// //设置RC632的工作方式 ////////////////////////////////////////////////////////////////////// void M500PcdConfigISOType(unsigned char type) { if (type == 'A') //ISO14443_A { ClearBitMask(Status2Reg,0x08); WriteRawRC(ModeReg,0x3D);//3F WriteRawRC(RxSelReg,0x86);//84 WriteRawRC(RFCfgReg,0x7F); //4F WriteRawRC(TReloadRegL,30);//tmoLength);// TReloadVal = 'h6a =tmoLength(dec) WriteRawRC(TReloadRegH,0); WriteRawRC(TModeReg,0x8D); WriteRawRC(TPrescalerReg,0x3E); Delay_I_1us(2); PcdAntennaOn();//开天线 } // else return (-1); //return MI_OK; } ///////////////////////////////////////////////////////////////////// //功 能:通过RC522和ISO14443卡通讯 //参数说明:Command[IN]:RC522命令字 // pInData[IN]:通过RC522发送到卡片的数据 // InLenByte[IN]:发送数据的字节长度 // pOutData[OUT]:接收到的卡片返回数据 // *pOutLenBit[OUT]:返回数据的位长度 ///////////////////////////////////////////////////////////////////// char PcdComMF522(unsigned char Command, //RC522命令字 unsigned char *pInData, //通过RC522发送到卡片的数据 unsigned char InLenByte, //发送数据的字节长度 unsigned char *pOutData, //接收到的卡片返回数据 unsigned int *pOutLenBit) //返回数据的位长度 { char status = MI_ERR; unsigned char irqEn = 0x00; unsigned char waitFor = 0x00; unsigned char lastBits; unsigned char n; unsigned int i; switch (Command) { case PCD_AUTHENT: //Mifare认证 irqEn = 0x12; //允许错误中断请求ErrIEn 允许空闲中断IdleIEn waitFor = 0x10; //认证寻卡等待时候 查询空闲中断标志位 break; case PCD_TRANSCEIVE: //接收发送 发送接收 irqEn = 0x77; //允许TxIEn RxIEn IdleIEn LoAlertIEn ErrIEn TimerIEn waitFor = 0x30; //寻卡等待时候 查询接收中断标志位与 空闲中断标志位 break; default: break; } WriteRawRC(ComIEnReg,irqEn|0x80); //IRqInv置位管脚IRQ与Status1Reg的IRq位的值相反 ClearBitMask(ComIrqReg,0x80); //Set1该位清零时,CommIRqReg的屏蔽位清零 WriteRawRC(CommandReg,PCD_IDLE); //写空闲命令 SetBitMask(FIFOLevelReg,0x80); //置位FlushBuffer清除内部FIFO的读和写指针以及ErrReg的BufferOvfl标志位被清除 for (i=0; i<InLenByte; i++) { WriteRawRC(FIFODataReg, pInData[i]); } //写数据进FIFOdata WriteRawRC(CommandReg, Command); //写命令 if (Command == PCD_TRANSCEIVE) { SetBitMask(BitFramingReg,0x80); } //StartSend置位启动数据发送 该位与收发命令使用时才有效 i = 1000;//根据时钟频率调整,操作M1卡最大等待时间25ms do //认证 与寻卡等待时间 { n = ReadRawRC(ComIrqReg); //查询事件中断 i--; } while ((i!=0) && !(n&0x01) && !(n&waitFor)); //退出条件i=0,定时器中断,与写空闲命令 ClearBitMask(BitFramingReg,0x80); //清理允许StartSend位 if (i!=0) { if(!(ReadRawRC(ErrorReg)&0x1B)) //读错误标志寄存器BufferOfI CollErr ParityErr ProtocolErr { status = MI_OK; if (n & irqEn & 0x01) //是否发生定时器中断 { status = MI_NOTAGERR; } if (Command == PCD_TRANSCEIVE) { n = ReadRawRC(FIFOLevelReg); //读FIFO中保存的字节数 lastBits = ReadRawRC(ControlReg) & 0x07; //最后接收到得字节的有效位数 if (lastBits) { *pOutLenBit = (n-1)*8 + lastBits; } //N个字节数减去1(最后一个字节)+最后一位的位数 读取到的数据总位数 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; } ///////////////////////////////////////////////////////////////////// //功 能:寻卡 //参数说明: req_code[IN]:寻卡方式 // 0x52 = 寻感应区内所有符合14443A标准的卡 // 0x26 = 寻未进入休眠状态的卡 // pTagType[OUT]:卡片类型代码 // 0x4400 = Mifare_UltraLight // 0x0400 = Mifare_One(S50) // 0x0200 = Mifare_One(S70) // 0x0800 = Mifare_Pro(X) // 0x4403 = Mifare_DESFire //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// char PcdRequest(unsigned char req_code,unsigned char *pTagType) { char status; //uint i; unsigned int unLen; unsigned char ucComMF522Buf[MAXRLEN]; ClearBitMask(Status2Reg,0x08); //清理指示MIFARECyptol单元接通以及所有卡的数据通信被加密的情况 WriteRawRC(BitFramingReg,0x07); // 发送的最后一个字节的 七位 SetBitMask(TxControlReg,0x03); //TX1,TX2管脚的输出信号传递经发送调制的13.56的能量载波信号 ucComMF522Buf[0] = req_code; //存入 卡片命令字 status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen); //寻卡 if ((status == MI_OK) && (unLen == 0x10)) //寻卡成功返回卡类型 { *pTagType = ucComMF522Buf[0]; *(pTagType+1) = ucComMF522Buf[1]; } else { status = MI_ERR; } return status; } ///////////////////////////////////////////////////////////////////// //功 能:防冲撞 //参数说明: pSnr[OUT]:卡片序列号,4字节 //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// char PcdAnticoll(unsigned char *pSnr) { char status; unsigned char i,snr_check=0; unsigned int unLen; unsigned char ucComMF522Buf[MAXRLEN]; ClearBitMask(Status2Reg,0x08); //清MFCryptol On位 只有成功执行MFAuthent命令后,该位才能置位 WriteRawRC(BitFramingReg,0x00); //清理寄存器 停止收发 ClearBitMask(CollReg,0x80); //清ValuesAfterColl所有接收的位在冲突后被清除 // WriteRawRC(BitFramingReg,0x07); // 发送的最后一个字节的 七位 // SetBitMask(TxControlReg,0x03); //TX1,TX2管脚的输出信号传递经发送调制的13.56的能量载波信号 ucComMF522Buf[0] = 0x93; //卡片防冲突命令 ucComMF522Buf[1] = 0x20; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);//与卡片通信 if (status == MI_OK) //通信成功 { for (i=0; i<4; i++) { *(pSnr+i) = ucComMF522Buf[i]; //读出UID snr_check ^= ucComMF522Buf[i]; } if (snr_check != ucComMF522Buf[i]) { status = MI_ERR; } } SetBitMask(CollReg,0x80); return status; } ///////////////////////////////////////////////////////////////////// //用MF522计算CRC16函数 ///////////////////////////////////////////////////////////////////// void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData) { unsigned char i,n; ClearBitMask(DivIrqReg,0x04); WriteRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80); for (i=0; i<len; i++) { WriteRawRC(FIFODataReg, *(pIndata+i)); } WriteRawRC(CommandReg, PCD_CALCCRC); i = 0xFF; do { n = ReadRawRC(DivIrqReg); i--; } while ((i!=0) && !(n&0x04)); pOutData[0] = ReadRawRC(CRCResultRegL); pOutData[1] = ReadRawRC(CRCResultRegM); } ///////////////////////////////////////////////////////////////////// //功 能:选定卡片 //参数说明: pSnr[IN]:卡片序列号,4字节 //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// char PcdSelect(unsigned char *pSnr) { char status; unsigned char i; unsigned int unLen; unsigned char ucComMF522Buf[MAXRLEN]; ucComMF522Buf[0] = PICC_ANTICOLL1; ucComMF522Buf[1] = 0x70; ucComMF522Buf[6] = 0; for (i=0; i<4; i++) { ucComMF522Buf[i+2] = *(pSnr+i); ucComMF522Buf[6] ^= *(pSnr+i); } CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]); ClearBitMask(Status2Reg,0x08); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen); if ((status == MI_OK) && (unLen == 0x18)) { status = MI_OK; } else { status = MI_ERR; } return status; } ///////////////////////////////////////////////////////////////////// //功 能:验证卡片密码 //参数说明: auth_mode[IN]: 密码验证模式 // 0x60 = 验证A密钥 // 0x61 = 验证B密钥 // addr[IN]:块地址 // pKey[IN]:密码 // pSnr[IN]:卡片序列号,4字节 //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr) { char status; unsigned int unLen; unsigned char i,ucComMF522Buf[MAXRLEN]; ucComMF522Buf[0] = auth_mode; ucComMF522Buf[1] = addr; for (i=0; i<6; i++) { ucComMF522Buf[i+2] = *(pKey+i); } for (i=0; i<6; i++) { ucComMF522Buf[i+8] = *(pSnr+i); } // memcpy(&ucComMF522Buf[2], pKey, 6); // memcpy(&ucComMF522Buf[8], pSnr, 4); status = PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen); if ((status != MI_OK) || (!(ReadRawRC(Status2Reg) & 0x08))) { status = MI_ERR; } return status; } ///////////////////////////////////////////////////////////////////// //功 能:写数据到M1卡一块 //参数说明: addr[IN]:块地址 // pData[IN]:写入的数据,16字节 //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// char PcdWrite(unsigned char addr,unsigned char *pData) { char status; unsigned int unLen; unsigned char i,ucComMF522Buf[MAXRLEN]; ucComMF522Buf[0] = PICC_WRITE; ucComMF522Buf[1] = addr; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A)) { status = MI_ERR; } if (status == MI_OK) { //memcpy(ucComMF522Buf, pData, 16); for (i=0; i<16; i++) { ucComMF522Buf[i] = *(pData+i); } CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen); if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A)) { status = MI_ERR; } } return status; } ///////////////////////////////////////////////////////////////////// //功 能:读取M1卡一块数据 //参数说明: addr[IN]:块地址 // pData[OUT]:读出的数据,16字节 //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// char PcdRead(unsigned char addr,unsigned char *pData) { char status; unsigned int unLen; unsigned char i,ucComMF522Buf[MAXRLEN]; ucComMF522Buf[0] = PICC_READ; ucComMF522Buf[1] = addr; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]); status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); if ((status == MI_OK) && (unLen == 0x90)) // { memcpy(pData, ucComMF522Buf, 16); } { for (i=0; i<16; i++) { *(pData+i) = ucComMF522Buf[i]; } } else { status = MI_ERR; } return status; } ///////////////////////////////////////////////////////////////////// //功 能:命令卡片进入休眠状态 //返 回: 成功返回MI_OK ///////////////////////////////////////////////////////////////////// char PcdHalt(void) { // char status; unsigned int unLen; unsigned char ucComMF522Buf[MAXRLEN]; ucComMF522Buf[0] = PICC_HALT; ucComMF522Buf[1] = 0; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]); PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); // status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen); return MI_OK; } //void IC_CMT(uchar *UID,uchar *KEY,uchar RW,char *Dat) //{ // uchar status = 0xab; // uchar qq[16]=0;//IC卡的类型 // uchar IC_uid[16]=0;//IC卡的UID // // UartSend(PcdRequest(0x52,qq));//寻卡 // UartSend(PcdAnticoll(IC_uid));//防冲撞 // // UartSend(PcdSelect(UID));//选定卡 // // UartSend(PcdAuthState(0x60,0x10,KEY,UID));//校验 // if(RW)//读写选择,1是读,0是写 // { // UartSend (PcdRead(0x10,Dat)); // } // else // { // UartSend(PcdWrite(0x10,Dat)); // } // UartSend(PcdHalt()); //} //测试读写功能 unsigned char g_ucTempbuf[20]={0}; unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; void test(void) { unsigned char status; status = PcdRequest(PICC_REQIDL, g_ucTempbuf); //寻天线区内全部卡 //////////////////////////////////////////////////////////////////////////////// if (status != MI_OK) { return; } ///////////////////////////////////////////////////////////////////// status = PcdAnticoll(g_ucTempbuf);//防冲撞 ////////////////////////////////////////////////////////////////////// if (status != MI_OK) { return; } ///////////////////////////////////////////////////////////////////// status = PcdSelect(g_ucTempbuf);//选卡 if (status != MI_OK) { return; } //////////////////////////////////////////////////////////////////////////// status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//校验密码 if (status != MI_OK) { return; } ///////////////////////////////////////////////////////////////////////////// // status = PcdWrite(1, data1);//写块区 // if (status != MI_OK) // { // return; // } ///////////////////////////////////////////////////////////////////////////// status = PcdRead(1, g_ucTempbuf);//读块区 if (status != MI_OK) { return; } UartSend_String((char*)g_ucTempbuf,16); PcdHalt(); //命令卡片进入休眠状态 } //初始化IC卡 void IC_Init(void) { Initial(); PcdReset(); M500PcdConfigISOType('A');//设置工作方式 }
GenericApp.c
/********************************************************************* * INCLUDES */ #include "OSAL.h" #include "AF.h" #include "ZDApp.h" #include "ZDObject.h" #include "ZDProfile.h" #include "GenericApp.h" #include "DebugTrace.h" #if !defined( WIN32 ) || defined( ZBIT ) #include "OnBoard.h" #endif /* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "hal_uart.h" /* RTOS */ #if defined( IAR_ARMCM3_LM ) #include "RTOS_App.h" #endif #include "IC_w_r.h" /********************************************************************* * MACROS */ /********************************************************************* * CONSTANTS */ /********************************************************************* * TYPEDEFS */ /********************************************************************* * GLOBAL VARIABLES */ // This list should be filled with Application specific Cluster IDs. const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] = { GENERICAPP_CLUSTERID }; const SimpleDescriptionFormat_t GenericApp_SimpleDesc = { GENERICAPP_ENDPOINT, // int Endpoint; GENERICAPP_PROFID, // uint16 AppProfId[2]; GENERICAPP_DEVICEID, // uint16 AppDeviceId[2]; GENERICAPP_DEVICE_VERSION, // int AppDevVer:4; GENERICAPP_FLAGS, // int AppFlags:4; GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters; (cId_t *)GenericApp_ClusterList, // byte *pAppInClusterList; GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters; (cId_t *)GenericApp_ClusterList // byte *pAppInClusterList; }; // This is the Endpoint/Interface description. It is defined here, but // filled-in in GenericApp_Init(). Another way to go would be to fill // in the structure here and make it a "const" (in code space). The // way it's defined in this sample app it is define in RAM. endPointDesc_t GenericApp_epDesc; /********************************************************************* * EXTERNAL VARIABLES */ /********************************************************************* * EXTERNAL FUNCTIONS */ /********************************************************************* * LOCAL VARIABLES */ byte GenericApp_TaskID; // Task ID for internal task/event processing // This variable will be received when // GenericApp_Init() is called. devStates_t GenericApp_NwkState; byte GenericApp_TransID; // This is the unique message ID (counter) afAddrType_t GenericApp_DstAddr; // Number of recieved messages static uint16 rxMsgCount; // Time interval between sending messages static uint32 txMsgDelay = GENERICAPP_SEND_MSG_TIMEOUT; /********************************************************************* * LOCAL FUNCTIONS */ static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg ); static void GenericApp_HandleKeys( byte shift, byte keys ); static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); static void GenericApp_SendTheMessage( void ); #if defined( IAR_ARMCM3_LM ) static void GenericApp_ProcessRtosMessage( void ); #endif /********************************************************************* * NETWORK LAYER CALLBACKS */ /********************************************************************* * PUBLIC FUNCTIONS */ /********************************************************************* * @fn GenericApp_Init * * @brief Initialization function for the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void GenericApp_Init( uint8 task_id ) { GenericApp_TaskID = task_id; GenericApp_NwkState = DEV_INIT; GenericApp_TransID = 0; IC_Init(); // Device hardware initialization can be added here or in main() (Zmain.c). // If the hardware is application specific - add it here. // If the hardware is other parts of the device add it in main(). GenericApp_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent; GenericApp_DstAddr.endPoint = 0; GenericApp_DstAddr.addr.shortAddr = 0; // Fill out the endpoint description. GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT; GenericApp_epDesc.task_id = &GenericApp_TaskID; GenericApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc; GenericApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &GenericApp_epDesc ); // Register for all key events - This app will handle all key events RegisterForKeys( GenericApp_TaskID ); // Update the display #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "GenericApp", HAL_LCD_LINE_1 ); #endif ZDO_RegisterForZDOMsg( GenericApp_TaskID, End_Device_Bind_rsp ); ZDO_RegisterForZDOMsg( GenericApp_TaskID, Match_Desc_rsp ); #if defined( IAR_ARMCM3_LM ) // Register this task with RTOS task initiator RTOS_RegisterApp( task_id, GENERICAPP_RTOS_MSG_EVT ); #endif } /********************************************************************* * @fn GenericApp_ProcessEvent * * @brief Generic Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return none */ uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; afDataConfirm_t *afDataConfirm; // Data Confirmation message fields byte sentEP; ZStatus_t sentStatus; byte sentTransID; // This should match the value sent (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { case ZDO_CB_MSG: GenericApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt ); break; case KEY_CHANGE: GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; case AF_DATA_CONFIRM_CMD: // This message is received as a confirmation of a data packet sent. // The status is of ZStatus_t type [defined in ZComDef.h] // The message fields are defined in AF.h afDataConfirm = (afDataConfirm_t *)MSGpkt; sentEP = afDataConfirm->endpoint; (void)sentEP; // This info not used now sentTransID = afDataConfirm->transID; (void)sentTransID; // This info not used now sentStatus = afDataConfirm->hdr.status; // Action taken when confirmation is received. if ( sentStatus != ZSuccess ) { // The data wasn't delivered -- Do something } break; case AF_INCOMING_MSG_CMD: GenericApp_MessageMSGCB( MSGpkt ); break; case ZDO_STATE_CHANGE: GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if ( (GenericApp_NwkState == DEV_ZB_COORD) || (GenericApp_NwkState == DEV_ROUTER) || (GenericApp_NwkState == DEV_END_DEVICE) ) { // Start sending "the" message in a regular interval. osal_start_timerEx( GenericApp_TaskID, GENERICAPP_SEND_MSG_EVT, txMsgDelay ); } break; default: break; } // Release the memory osal_msg_deallocate( (uint8 *)MSGpkt ); // Next MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID ); } // return unprocessed events return (events ^ SYS_EVENT_MSG); } // Send a message out - This event is generated by a timer // (setup in GenericApp_Init()). if ( events & GENERICAPP_SEND_MSG_EVT ) { // Send "the" message //GenericApp_SendTheMessage(); test(); // Setup to send message again osal_start_timerEx( GenericApp_TaskID, GENERICAPP_SEND_MSG_EVT, txMsgDelay ); // return unprocessed events return (events ^ GENERICAPP_SEND_MSG_EVT); } #if defined( IAR_ARMCM3_LM ) // Receive a message from the RTOS queue if ( events & GENERICAPP_RTOS_MSG_EVT ) { // Process message from RTOS queue GenericApp_ProcessRtosMessage(); // return unprocessed events return (events ^ GENERICAPP_RTOS_MSG_EVT); } #endif // Discard unknown events return 0; } /********************************************************************* * Event Generation Functions */ /********************************************************************* * @fn GenericApp_ProcessZDOMsgs() * * @brief Process response messages * * @param none * * @return none */ static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg ) { switch ( inMsg->clusterID ) { case End_Device_Bind_rsp: if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess ) { // Light LED HalLedSet( HAL_LED_4, HAL_LED_MODE_ON ); } #if defined( BLINK_LEDS ) else { // Flash LED to show failure HalLedSet ( HAL_LED_4, HAL_LED_MODE_FLASH ); } #endif break; case Match_Desc_rsp: { ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( inMsg ); if ( pRsp ) { if ( pRsp->status == ZSuccess && pRsp->cnt ) { GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; GenericApp_DstAddr.addr.shortAddr = pRsp->nwkAddr; // Take the first endpoint, Can be changed to search through endpoints GenericApp_DstAddr.endPoint = pRsp->epList[0]; // Light LED HalLedSet( HAL_LED_4, HAL_LED_MODE_ON ); } osal_mem_free( pRsp ); } } break; } } /********************************************************************* * @fn GenericApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_4 * HAL_KEY_SW_3 * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ static void GenericApp_HandleKeys( uint8 shift, uint8 keys ) { zAddrType_t dstAddr; // Shift is used to make each button/switch dual purpose. if ( shift ) { if ( keys & HAL_KEY_SW_1 ) { } if ( keys & HAL_KEY_SW_2 ) { } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { } } else { if ( keys & HAL_KEY_SW_1 ) { #if defined( SWITCH1_BIND ) // We can use SW1 to simulate SW2 for devices that only have one switch, keys |= HAL_KEY_SW_2; #elif defined( SWITCH1_MATCH ) // or use SW1 to simulate SW4 for devices that only have one switch keys |= HAL_KEY_SW_4; #else // Normally, SW1 changes the rate that messages are sent if ( txMsgDelay > 100 ) { // Cut the message TX delay in half txMsgDelay /= 2; } else { // Reset to the default txMsgDelay = GENERICAPP_SEND_MSG_TIMEOUT; } #endif } if ( keys & HAL_KEY_SW_2 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate an End Device Bind Request for the mandatory endpoint dstAddr.addrMode = Addr16Bit; dstAddr.addr.shortAddr = 0x0000; // Coordinator ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(), GenericApp_epDesc.endPoint, GENERICAPP_PROFID, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, FALSE ); } if ( keys & HAL_KEY_SW_3 ) { } if ( keys & HAL_KEY_SW_4 ) { HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF ); // Initiate a Match Description Request (Service Discovery) dstAddr.addrMode = AddrBroadcast; dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR; ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR, GENERICAPP_PROFID, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList, FALSE ); } } } /********************************************************************* * LOCAL FUNCTIONS */ /********************************************************************* * @fn GenericApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return none */ static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { switch ( pkt->clusterId ) { case GENERICAPP_CLUSTERID: rxMsgCount += 1; // Count this message HalLedSet ( HAL_LED_4, HAL_LED_MODE_BLINK ); // Blink an LED #if defined( LCD_SUPPORTED ) HalLcdWriteString( (char*)pkt->cmd.Data, HAL_LCD_LINE_1 ); HalLcdWriteStringValue( "Rcvd:", rxMsgCount, 10, HAL_LCD_LINE_2 ); #elif defined( WIN32 ) WPRINTSTR( pkt->cmd.Data ); #endif break; } } /********************************************************************* * @fn GenericApp_SendTheMessage * * @brief Send "the" message. * * @param none * * @return none */ static void GenericApp_SendTheMessage( void ) { char theMessageData[] = "Hello World"; if ( AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc, GENERICAPP_CLUSTERID, (byte)osal_strlen( theMessageData ) + 1, (byte *)&theMessageData, &GenericApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { // Successfully requested to be sent. } else { // Error occurred in request to send. } } #if defined( IAR_ARMCM3_LM ) /********************************************************************* * @fn GenericApp_ProcessRtosMessage * * @brief Receive message from RTOS queue, send response back. * * @param none * * @return none */ static void GenericApp_ProcessRtosMessage( void ) { osalQueue_t inMsg; if ( osal_queue_receive( OsalQueue, &inMsg, 0 ) == pdPASS ) { uint8 cmndId = inMsg.cmnd; uint32 counter = osal_build_uint32( inMsg.cbuf, 4 ); switch ( cmndId ) { case CMD_INCR: counter += 1; /* Increment the incoming counter */ /* Intentionally fall through next case */ case CMD_ECHO: { userQueue_t outMsg; outMsg.resp = RSP_CODE | cmndId; /* Response ID */ osal_buffer_uint32( outMsg.rbuf, counter ); /* Increment counter */ osal_queue_send( UserQueue1, &outMsg, 0 ); /* Send back to UserTask */ break; } default: break; /* Ignore unknown command */ } } } #endif /********************************************************************* */
SPI的话,建议不要使用模拟的。ZSTACK里有带SPI的驱动,并且使用了DMA.
osal系统里面不要有delay这样的操作。