www.pudn.com > CC2420_TRX.rar > basic_rf_receive.c
/******************************************************************************************************* * * * ********** * * ************ * * *** *** * * *** +++ *** * * *** + + *** * * *** + CHIPCON CC2420 BASIC RF LIBRARY * * *** + + *** Packet reception * * *** +++ *** * * *** *** * * ************ * * ********** * * * ******************************************************************************************************* * This file contains functions used to enable and disable for packet reception, and the FIFOP * * interrupt which handles the packet reception. The application must define the * * basicRfReceivePacket() function, which is called when a packet has been successfully received. * * * * More information can be found in basic_rf.h * ******************************************************************************************************* * Compiler: AVR-GCC * * Target platform: CC2420DB, CC2420 + any MCU with very few modifications required * ******************************************************************************************************* * Revision history: * * $Log: basic_rf_receive.c,v $ * Revision 1.4 2004/07/26 11:28:10 mbr * Modified RXFIFO flushing by strobing CC2420_SFLUSHRX * * Revision 1.3 2004/03/30 14:59:22 mbr * Release for web * * * *******************************************************************************************************/ #include//------------------------------------------------------------------------------------------------------- // 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); //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 //------------------------------------------------------------------------------------------------------- // SIGNAL(SIG_INTERRUPT0) - CC2420 FIFOP interrupt service routine // // DESCRIPTION: // When a packet has been completely received, this ISR will extract the data from the RX FIFO, put // it into the active BASIC_RF_RX_INFO structure, and call basicRfReceivePacket() (defined by the // application). FIFO overflow and illegally formatted packets is handled by this routine. // // Note: Packets are acknowledged automatically by CC2420 through the auto-acknowledgment feature. //------------------------------------------------------------------------------------------------------- SIGNAL(SIG_INTERRUPT0) { WORD frameControlField; INT8 length; BYTE pFooter[2]; // 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 reset 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); //FASTSPI_READ_FIFO_NO_WAIT((BYTE*) &rfSettings.panId, 2); //FASTSPI_READ_FIFO_NO_WAIT((BYTE*) &rfSettings.myAddr, 2); //FASTSPI_READ_FIFO_NO_WAIT((BYTE*) &destaddr, 2); // 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]; SET_GLED(); halWait(50000); CLR_GLED(); // halWait(50000); // Notify the application about the received _data_ packet if the CRC is OK if (((frameControlField & (BASIC_RF_FCF_BM)) == BASIC_RF_FCF_ACK) && (pFooter[1] & BASIC_RF_CRC_OK_BM)) { rfSettings.pRxInfo = basicRfReceivePacket(rfSettings.pRxInfo); } } } // SET_YLED(); //halWait(50000); //CLR_YLED(); //pe4 } // SIGNAL(SIG_INTERRUPT0)