rf_receive.c
来自「CC2420+PIC单片机基础实验代码、CC2420无线收发实验代码」· C语言 代码 · 共 122 行
C
122 行
#include <include.h>
extern volatile BASIC_RF_SETTINGS rfSettings;
extern void halWait(UINT16 timeout);
extern BASIC_RF_RX_INFO* basicRfReceivePacket(BASIC_RF_RX_INFO *pRRI);
extern unsigned char recive;
//-------------------------------------------------------------------------------------------------------
// void halRfReceiveOn(void)
//
// DESCRIPTION:
// Enables the CC2420 receiver and the FIFOP interrupt. When a packet is received through this
// interrupt, it will call halRfReceivePacket(...), which must be defined by the application
//-------------------------------------------------------------------------------------------------------
void basicRfReceiveOn(void) {
rfSettings.receiveOn = TRUE;
FASTSPI_STROBE(CC2420_SRXON);
FASTSPI_STROBE(CC2420_SFLUSHRX);
FIFOP_INT_INIT();
ENABLE_FIFOP_INT();
} // basicRfReceiveOn
//-------------------------------------------------------------------------------------------------------
// void halRfReceiveOff(void)
//
// DESCRIPTION:
// Disables the CC2420 receiver and the FIFOP interrupt.
//-------------------------------------------------------------------------------------------------------
void basicRfReceiveOff(void) {
rfSettings.receiveOn = FALSE;
FASTSPI_STROBE(CC2420_SRFOFF);
DISABLE_FIFOP_INT();
} // basicRfReceiveOff
//******************************************************************************
// Interrupt Vectors
//******************************************************************************
void HighISR();
#pragma code high_vector=0x08
void high_interrupt (void)
{
_asm GOTO HighISR _endasm
}
#pragma code
#pragma interruptlow HighISR
void HighISR()
{
WORD frameControlField;
INT8 length;
BYTE pFooter[2];
CLEAR_FIFOP_INT();
// Clean up and exit in case of FIFO overflow, which is indicated by FIFOP = 1 and FIFO = 0
if((FIFOP_IS_1) && (!(FIFO_IS_1))) {
FASTSPI_STROBE(CC2420_SFLUSHRX);
FASTSPI_STROBE(CC2420_SFLUSHRX);
return;
}
// Payload length
FASTSPI_READ_FIFO_BYTE(length);
length &= BASIC_RF_LENGTH_MASK; // Ignore MSB
// Ignore the packet if the length is too short
if (length < BASIC_RF_ACK_PACKET_SIZE) {
FASTSPI_READ_FIFO_GARBAGE(length);
// Otherwise, if the length is valid, then proceed with the rest of the packet
} else {
// Register the payload length
rfSettings.pRxInfo->length = length - BASIC_RF_PACKET_OVERHEAD_SIZE;
// Read the frame control field and the data sequence number
FASTSPI_READ_FIFO_NO_WAIT((BYTE*) &frameControlField, 2);
rfSettings.pRxInfo->ackRequest = !!(frameControlField & BASIC_RF_FCF_ACK_BM);
FASTSPI_READ_FIFO_BYTE(rfSettings.pRxInfo->seqNumber);
// Is this an acknowledgment packet?
if ((length == BASIC_RF_ACK_PACKET_SIZE) && (frameControlField == BASIC_RF_ACK_FCF) && (rfSettings.pRxInfo->seqNumber == rfSettings.txSeqNumber)) {
// Read the footer and check for CRC OK
FASTSPI_READ_FIFO_NO_WAIT((BYTE*) pFooter, 2);
// Indicate the successful ack reception (this flag is polled by the transmission routine)
if (pFooter[1] & BASIC_RF_CRC_OK_BM) rfSettings.ackReceived = TRUE;
// Too small to be a valid packet?
} else if (length < BASIC_RF_PACKET_OVERHEAD_SIZE) {
FASTSPI_READ_FIFO_GARBAGE(length - 3);
return;
// Receive the rest of the packet
}
else
{
// Skip the destination PAN and address (that's taken care of by harware address recognition!)
FASTSPI_READ_FIFO_GARBAGE(4);
// Read the source address
FASTSPI_READ_FIFO_NO_WAIT((BYTE*) &rfSettings.pRxInfo->srcAddr, 2);
// Read the packet payload
FASTSPI_READ_FIFO_NO_WAIT(rfSettings.pRxInfo->pPayload, rfSettings.pRxInfo->length);
// Read the footer to get the RSSI value
FASTSPI_READ_FIFO_NO_WAIT((BYTE*) pFooter, 2);
rfSettings.pRxInfo->rssi = pFooter[0];
// Notify the application about the received _data_ packet if the CRC is OK
if (((frameControlField & (BASIC_RF_FCF_BM)) == BASIC_RF_FCF_NOACK) && (pFooter[1] & BASIC_RF_CRC_OK_BM)) {
rfSettings.pRxInfo = basicRfReceivePacket(rfSettings.pRxInfo);recive=1;
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?