⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rfreceivepacket.c

📁 ti-Chipcon CC1010 1G以下Soc源码库。包括rf,powermodes,clockmodes,flashRW,interrupts,timer,pwm,uart...所有底层驱动源码
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                            CHIPCON CC1010                    *
 *      ***   + +   ***                   HAL - RFReceivePacket              *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 *                                                                           *
 *****************************************************************************
 * Author:              ROH                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: RFReceivePacket.c,v $
 * Revision 1.3  2003/02/25 10:12:24  tos
 * Bugfix: lock average filter immediately after sync byte received.
 *
 * Revision 1.2  2002/11/20 15:22:22  tos
 * Corrected inconsitency in header text.
 *
 * Revision 1.1  2002/10/14 13:04:34  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/


#include <chipcon/hal.h>

#define CRC16_POLY 0x1021
#define CRC16_INIT 0xFFFF
#define CRC_OK     0

//----------------------------------------------------------------------------
//  void halRFReceivePacket(...)
//
//  Description:
//      Used to receive a packet sent using halSendPacket on another CC1010.
//      RECOMMENDED modem/RF configuration is used. Note that this overrides
//      settling time and average filter lock control. RX is assumed to be
//      powered up. The function waits for a valid syncronization byte
//      (RF_SUITABLE_SYNC_BYTE) for up to _timeOut_ ms. If timed out without
//      receiving the sync. byte the function returns 0. If a sync. byte is
//      received in time the number of bytes indicated by the packet is received
//      (max _maxLength_ bytes) and put in the buffer pointed to by _packetData_,
//      after which the function returns the number of bytes received.
//      THIS FUNCTION ASSUMES THAT TIMER3 IS AVAILABLE!
//
//  Arguments:
//      byte timeOut
//          Timeout for reception of valid synchronization byte in tens of ms.
//			A value of zero gives an infinite timeout.
//      byte* packetData
//          A pointer to a buffer for the received data.
//      byte maxLength
//          The maximum number of bytes to receive (max packet size 253 bytes).
//      char* rssiByte
//          If this pointer is different from NULL an RSSI measurement is
//          performed after receiving the sync byte using the
//          halReadRSSI(...) function and its return value stored in the
//          byte pointed to by _rssiByte_.
//      word clkFreq
//          The XOSC clock frequency in kHz.
//
//  Return value:
//      byte
//          The actual number of bytes received or 0 if timed out / CRC error.
//----------------------------------------------------------------------------
byte halRFReceivePacket(byte timeOut, byte* packetData, byte maxLength, char* rssiByte, word clkFreq) {
    byte receivedBytes, pkgLen, crcData, i; 
    word crcReg;

    halConfigTimer23(TIMER3|TIMER23_NO_INT_TIMER, 10000, clkFreq);   

    INT_SETFLAG(INUM_TIMER3, INT_CLR);
    TIMER3_RUN(TRUE);
    
    RF_SET_PREAMBLE_COUNT(16);
    RF_SET_SYNC_BYTE(RF_SUITABLE_SYNC_BYTE);   
    MODEM1=(MODEM1&0x03)|0x24;  // Make sure avg filter is free-running + 22 baud settling time
    INT_ENABLE(INUM_RF, INT_OFF);
    INT_SETFLAG(INUM_RF, INT_CLR);
    RF_START_RX();
    
    while (1) {
        // Check if 10 ms have passed
        if (INT_GETFLAG(INUM_TIMER3)) {
            // Clear interrupt flag and decrement timeout value
            INT_SETFLAG(INUM_TIMER3, INT_CLR);
			if (timeOut && !--timeOut) {
				timeOut=255;
                break;                      // Timeout
			}
        }
        // Check if sync byte received
        if (INT_GETFLAG(INUM_RF)) {
            EXIF &= ~0x10; // Clear the flag
            break;            
        }
    }        
    
    receivedBytes=0;
    // Timeout or sync byte received?
    if (timeOut!=255) {
        // Lock average filter and perform RSSI reading if desired
        RF_LOCK_AVERAGE_FILTER(TRUE);

        // Get length of package
        RF_WAIT_AND_RECEIVE_BYTE( pkgLen );
        pkgLen+=2;                  // Add the two CRC bytes 

        if (rssiByte)
            *rssiByte=halRFReadRSSI();

        // Initialize CRC-16
        crcReg=CRC16_INIT;
        
        // Receive as many bytes as packet specifies + 2 crc bytes
        while (pkgLen--) {
            RF_WAIT_AND_RECEIVE_BYTE( crcData );

            // If there is space in the buffer at _packetData_ and we're not
            // currently receiving CRC, store the byte
            if ( pkgLen>=2 && maxLength ) {
                *packetData++=crcData;
                receivedBytes++;
                maxLength--;    
            }

            // Calculate CRC-16 (CCITT)
        	for (i=0; i<8; i++) {
        		if ( ((crcReg&0x8000)>>8) ^ (crcData&0x80) )
        			crcReg=(crcReg<<1)^CRC16_POLY;
        		else
        			crcReg=(crcReg<<1);
        		crcData<<=1;
        	}            
        }

        // Check if CRC is OK
        if (crcReg != CRC_OK)
            receivedBytes=0;        
    }         

    TIMER3_RUN(FALSE);
    RF_SET_PREAMBLE_COUNT(RF_PREDET_OFF);   
    return receivedBytes;
}
//----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -