// receive a paket data form cc2500 u8 CC2500_ReceivePacket(u8 *rxBuffer) { u8 status; u8 LEN=0,i=0; status =CC2500_ReadStatus(CCxxx0_RXBYTES); //RX FIFO 等于0 CRC校验失败 如果大于0CRC校验OK数据能读出 if((status & 0x7F)==0x00) { status = CC2500_ReadStatus(CCxxx0_MARCSTATE); if((status!=0x0D)&&(status!=0x08)) //0x0D为RX状态 { CC2500_SetRxd(); //激活(接收或传送) } return 0; } //LEN = CC2500_ReadReg(CCxxx0_RXFIFO_one); LEN = PACKET_LEN; //采用定长模式 if(LEN > 0) { if(LEN<=RFTXDBUFFSIZE) { CC2500_Select(); CC2500_WriteByte(CCxxx0_RXFIFO_Muti); for(i=0;i<LEN;i++) { rxBuffer[i] = CC2500_ReadByte(); } CC2500_NoSelect(); CC2500_SetRxd(); return LEN;//(status & CRC_OK); } else { CC2500_Select(); CC2500_WriteByte(CCxxx0_RXFIFO_Muti); for(i=0;i<LEN;i++) { rxBuffer[i] = CC2500_ReadByte(); } CC2500_NoSelect(); CC2500_SetRxd(); return 0; } } else { CC2500_SetRxd(); return 0; } }
void SelfSend() { sPacketbuf[DES_NUM] = SELFADDRESS; sPacketbuf[SRC_NUM] = SELFADDRESS; CC2500_SeleChannel(12); CC2500_SetRecvAddr(SELFADDRESS); CC2500_SetRxd(); CC2500_SendPacket(sPacketbuf,PACKET_LEN); CC2500_SetRxd(); sPacketbuf[DES_NUM]=0x00; }
bool CC2500_PacketCheck() { u8 i; u32 *pu32 = NULL; RSSI = CC2500_ReadStatus(CCxxx0_RSSI); LQI = CC2500_ReadStatus(CCxxx0_LQI); CC2500_ReceivePacket(gPacketbuf); ReceiveTime = Timer2Counter; CC2500_SetRxd(); pu32 = (u32*) &gPacketbuf[1]; CRC_ResetDR(); if (CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1) == pu32[CRC_LENGTH - 1]) { if (NodeTimestamp[gPacketbuf[SRC_NUM]] != pu32[CRC_LENGTH - 2]) { NodeTimestamp[gPacketbuf[SRC_NUM]] = pu32[CRC_LENGTH - 2]; return TRUE; } else { return FALSE; } } else { return FALSE; } }
void CC2500_SetRecvAddr(CC2500_RECV_ADDR AddrMode,u8 SpecAddr) { CC2500_WriteReg(CCxxx0_ADDR,SpecAddr); CC2500_WriteReg(CCxxx0_PKTCTRL1,AddrMode);//地址检查寄存器 CC2500_SetRxd(); }
// rf communication void CC2500_Process(void) { u8 i,j; #define GDO0_ #ifdef GDO0_ if(!GDO0_IN()) { return; } while(GDO0_IN()); #else if(!(CC2500_ReadStatus(0x38)&0x01)) { return; } while((CC2500_ReadStatus(0x38)&0x01)); #endif Debug("Have a packet!\n\r"); i = CC2500_ReadRSSIValue(); for(i=0;i<sizeof(bTempBuf);i++) bTempBuf[i]=0; CC2500_ReceivePacket(bTempBuf); Debug("Receive over!\n\r"); CC2500_SetRxd(); // CC2500_REnter_WOR(); #if 1//def RF_DEBUG_ Debug("\n\rRX Data:"); for(j=0;j<sizeof(bTempBuf);j++) { Debug("0x%x ",bTempBuf[j]); } Debug("\n\r"); #endif }
/* This function runs before the OS start. */ void NetworkConnecting() { int i, p, test, test1; int sendcounter = 0; u32 *pu32 = NULL; test1 = Timer2Counter; test = Timer2Counter; sPacketbuf[DES_NUM] = SELFADDRESS; sPacketbuf[SRC_NUM] = SELFADDRESS; CC2500_SeleChannel(0); CC2500_SetRecvAddr(SELFADDRESS); CC2500_SetRxd(); CC2500_SendPacket(sPacketbuf,PACKET_LEN); CC2500_SetRxd(); sPacketbuf[DES_NUM]=0x00; NbrList[0]=NbrCount; // while(RESET==RNG_GetFlagStatus(RNG_FLAG_DRDY)); // Delay_100us(RNG_GetRandomNumber() & 0x000000f); pu32 = (u32*) &sPacketbuf[1]; while (0 == NodeFlag[SELFADDRESS]) { if (ISIDLE) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_MESSAGE; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); p = Bernoulli(1.0/(MAXNUM-NbrCount+1)); //Delay_100us(1); if (ISIDLE) { if (0 == DelayFlag) { if (p/*Bernoulli(1.0/(MAXNUM-NbrCount+1))*/) { CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); DelayFlag=1; } } else { Preemptive_Delay_100us(25, &DelayFlag); DelayFlag=0; } } } } test1 = Timer2Counter - test1; while(MAXNUM > NbrCount) { if (0 != DiscoveryHelp) { if (ISIDLE) { Delay_100us(1); if (ISIDLE) { if (Bernoulli(1.0/(MAXNUM-NbrCount+1))) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_MESSAGE; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); } } } DiscoveryHelp = 0; } } test = Timer2Counter - test; while(RESET==RNG_GetFlagStatus(RNG_FLAG_DRDY)); Delay_100us(RNG_GetRandomNumber() & 0x000000f); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. sendcounter = 3; while (0 != sendcounter) { if (ISIDLE) { Delay_100us(1); if (ISIDLE) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_ACCOMPLISHED; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); sendcounter--; } } Delay_100us(10); } printf("NeighbourList:\r\n"); for (i=0;i<MAXNUM;i++) { printf("%#x\r\n",NbrList[1+i]); } printf("t1 = %d\r\n", test1); printf("t = %d\r\n", test); }
bool CC2500_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; u8 i = 0; u16 Id; //CS GPIO_InitStructure.GPIO_Pin = CC_CS_IO_PIN; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(CC_CS_IO_GROUP, &GPIO_InitStructure); SPI2_Init(); CC2500_Reset(); if((Id=CC2500_GetChipId())!=0x8003) { Debug("Error CC2500 ID: %04x\n\r",Id); return FALSE; } CC2500_RfSettings(); CC2500_Select(); CC2500_WriteByte(CCxxx0_PATABLE|WRITE_BURST); for(i=0;i<8;i++) { CC2500_WriteByte(paTable_CC2500[i]); } CC2500_NoSelect(); CC2500_SetRxd(); //GDO0 GPIO_InitStructure.GPIO_Pin = CC_GDO0_IO_PIN; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(CC_GDO0_IO_GROUP, &GPIO_InitStructure); #if QXW_LCM_ID == 210 || QXW_LCM_ID == 211 || QXW_LCM_ID == 220 || QXW_LCM_ID == 221 NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = EXTI3_IRQn_Priority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE NVIC_Init(&NVIC_InitStructure); EXTI_InitStructure.EXTI_Line = EXTI_Line3; //外部中断线 ,使用第2根 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//中断触发方式 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //打开中断 EXTI_Init(&EXTI_InitStructure); //调用库函数给寄存器复制 CC2500_InterruptEnable(FALSE); GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3); #elif QXW_LCM_ID == 212 NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = EXTI9_5_IRQn_Priority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE NVIC_Init(&NVIC_InitStructure); EXTI_InitStructure.EXTI_Line = EXTI_Line6; //外部中断线 ,使用第2根 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//中断触发方式 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //打开中断 EXTI_Init(&EXTI_InitStructure); //调用库函数给寄存器复制 CC2500_InterruptEnable(FALSE); GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource6); #endif return TRUE; }