ZigBee学习之6---RF收发数据

来源:互联网 发布:手机数据恢复精灵 破解 编辑:程序博客网 时间:2024/04/27 01:03

今天把TI的例子程序里的基本RF看了下,有的地方还没有完全看懂,发上来和大家分享一下



/***********************************************************************************


  Filename:     basic_rf.c


 


  Description:  Basic RF library


 


***********************************************************************************/


 


/***********************************************************************************


* INCLUDES


*/


//hal_int.h包含了控制中断的宏


#include "hal_int.h"


// Using halMcuWaitUs(),hal_mcu.h中是一些延时函数


#include "hal_mcu.h"           


 


//通道编程的宏,以及硬件RF初始化,SECURITY_CCM宏是用来控制安全MAC的


#include "hal_rf.h"


#ifdef SECURITY_CCM


#include "hal_rf_security.h"


#endif


 


//basic_rf.h中定义了RF配置用的数据结构


#include "basic_rf.h"


#ifdef SECURITY_CCM


#include "basic_rf_security.h"


#endif


 


#include "util.h"               // Using min()


#include "string.h"


 


/***********************************************************************************


* CONSTANTS AND DEFINES


*/


 


// Packet and packet part lengths


#define PKT_LEN_MIC                         8


#define PKT_LEN_SEC                         PKT_LEN_UNSEC + PKT_LEN_MIC


#define PKT_LEN_AUTH                        8


#define PKT_LEN_ENCR                        24


 


// Packet overhead ((frame control field, sequence number, PAN ID,


// destination and source) + (footer))


// Note that the length byte itself is not included included in the packet length


//MPDU的长度宏,(2字节幀控制域 + 1字节数据序列号 + 2字节PAN ID + 2字节目标地址 + 2字节源地址 + 2字节MAC尾)


#define BASIC_RF_PACKET_OVERHEAD_SIZE       ((2 + 1 + 2 + 2 + 2) + (2))


//MPDU最大有效载荷的长度(利用FIFO只有128字节空间),BASIC_RF_AUX_HDR_LENGTH 和 BASIC_RF_LEN_MIC是辅助安全头宏定义的长度分别是5和8,可以将其设置为0,即不具备安全功能


#define BASIC_RF_MAX_PAYLOAD_SIZE           (127 - BASIC_RF_PACKET_OVERHEAD_SIZE - \


    BASIC_RF_AUX_HDR_LENGTH - BASIC_RF_LEN_MIC)


//应答帧的长度宏(2字节帧首界定符 + 1字节数据序列号 + 2字节帧校验序列),不包括帧长度域


#define BASIC_RF_ACK_PACKET_SIZE            5


//MAC帧尾的长度宏


#define BASIC_RF_FOOTER_SIZE                2


#define BASIC_RF_HDR_SIZE                   10


 


// The time it takes for the acknowledgment packet to be received after the


// data packet has been transmitted.


#define BASIC_RF_ACK_DURATION               (0.5 * 32 * 2 * ((4 + 1) + (1) + (2 + 1) + (2)))


#define BASIC_RF_SYMBOL_DURATION            (32 * 0.5)


 


// The length byte


#define BASIC_RF_PLD_LEN_MASK               0x7F


 


//帧控制域


//帧类型子域:   000     信标帧


//      001     数据帧


//      010     应答帧


//      011     MAC命令


//      100~110 保留


//安全允许子域:0        帧无安全保护


//        1     帧采用安全保护


//帧待定子域:   0       传输没有附加数据的帧


//           1       当前传输后有附加的数据要发送            


//应答请求子域: 0       不需要接收器发送应答帧


//              1     需要接收器发送应答帧


//内部PAN子域:决定MAC帧是在内部还是在其他PAN之间发送


//        0     如果目的地址和源地址都存在帧将包括目标和源PAN标志域


//        1     如果目的地址和源地址都存在帧不包括源PAN标志域 


//目的地址模式子域:若为0,并且帧类型没有指定这是个应答帧或信标帧且源地址域非0,则暗含此帧发送到PAN协调器将其PAN标志作为源PAN标志域   


//源地址模式子域:若为0,且帧类型没有指定这是个应答帧或信标帧,目的地址不为0,则暗含此帧来自PAN协调器,用其PAN标志作为目的PAN标志域   


//00            PAN标志器且地址域未提交


//01            保留


//10            地址域包含16位的短地址


//11            地址域包含64位的扩展地址


// Frame control field


//16位短地址模式,数据帧,没有安全保护


#define BASIC_RF_FCF_NOACK                  0x8841


#define BASIC_RF_FCF_ACK                    0x8861


#define BASIC_RF_FCF_ACK_BM                 0x0020


#define BASIC_RF_FCF_BM                     (~BASIC_RF_FCF_ACK_BM)


#define BASIC_RF_SEC_ENABLED_FCF_BM         0x0008


 


// Frame control field LSB


#define BASIC_RF_FCF_NOACK_L                LO_UINT16(BASIC_RF_FCF_NOACK)


#define BASIC_RF_FCF_ACK_L                  LO_UINT16(BASIC_RF_FCF_ACK)


#define BASIC_RF_FCF_ACK_BM_L               LO_UINT16(BASIC_RF_FCF_ACK_BM)


#define BASIC_RF_FCF_BM_L                   LO_UINT16(BASIC_RF_FCF_BM)


#define BASIC_RF_SEC_ENABLED_FCF_BM_L       LO_UINT16(BASIC_RF_SEC_ENABLED_FCF_BM)


 


// Auxiliary Security header


#define BASIC_RF_AUX_HDR_LENGTH             5


#define BASIC_RF_LEN_AUTH                   BASIC_RF_PACKET_OVERHEAD_SIZE + \


    BASIC_RF_AUX_HDR_LENGTH - BASIC_RF_FOOTER_SIZE


#define BASIC_RF_SECURITY_M                 2


#define BASIC_RF_LEN_MIC                    8


#ifdef SECURITY_CCM


#undef BASIC_RF_HDR_SIZE


#define BASIC_RF_HDR_SIZE                   15


#endif


 


// Footer


#define BASIC_RF_CRC_OK_BM                  0x80


 


/***********************************************************************************


* TYPEDEFS


*/


// The receive struct


typedef struct {


    uint8 seqNumber;


    uint16 srcAddr;


    uint16 srcPanId;


    int8 length;


    uint8* pPayload;


    uint8 ackRequest;


    int8 rssi;


    volatile uint8 isReady;


    uint8 status;


} basicRfRxInfo_t;


 


// Tx state


typedef struct {


    uint8 txSeqNumber;


    volatile uint8 ackReceived;


    uint8 receiveOn;


    uint32 frameCounter;


} basicRfTxState_t;


 


 


//兼容IEEE 802.15.4 的MHR(MAC头)(2字节帧控制 + 1字节数据序列号 + 0~20字节地址信息)


// Basic RF packet header (IEEE 802.15.4)


typedef struct {


    uint8   packetLength;


    uint8   fcf0;           // Frame control field LSB


    uint8   fcf1;           // Frame control field MSB


    uint8   seqNumber;


    uint16  panId;


    uint16  destAddr;


    uint16  srcAddr;


    #ifdef SECURITY_CCM


    uint8   securityControl;


    uint8  frameCounter[4];


    #endif


} basicRfPktHdr_t;


 


 


/***********************************************************************************


* LOCAL VARIABLES


*/


static basicRfRxInfo_t  rxi=      { 0xFF }; // Make sure sequence numbers are


static basicRfTxState_t txState=  { 0x00 }; // initialised and distinct.


 


static basicRfCfg_t* pConfig;


static uint8 txMpdu[BASIC_RF_MAX_PAYLOAD_SIZE+BASIC_RF_PACKET_OVERHEAD_SIZE+1];


static uint8 rxMpdu[128];


 


/***********************************************************************************


* GLOBAL VARIABLES


*/


 


 


/***********************************************************************************


* LOCAL FUNCTIONS


*/


 


 


/***********************************************************************************


* @fn          basicRfBuildHeader


*


* @brief       Builds packet header according to IEEE 802.15.4 frame format


*


* @param       buffer - Pointer to buffer to write the header


*              destAddr - destination short address


*              payloadLength - length of higher layer payload


*


* @return      uint8 - length of header


*/


//构造兼容IEEE802.15.4 的帧头


static uint8 basicRfBuildHeader(uint8* buffer, uint16 destAddr, uint8 payloadLength)


{


    //声明一个指向MAC帧头结构的指针


    basicRfPktHdr_t *pHdr;


    uint16 fcf;


 


    pHdr= (basicRfPktHdr_t*)buffer;


 


    //payloadLength为有效载荷的长度,即要发送的数据的长度


    // Populate packet header


    pHdr->packetLength = payloadLength + BASIC_RF_PACKET_OVERHEAD_SIZE;


    //pConfig为基本配置数据结构


    /*typedef struct {


        uint16 myAddr;


        uint16 panId;


        uint8 channel;


        uint8 ackRequest;


        } basicRfCfg_t;


    */


    //pHdr->frameControlField = pConfig->ackRequest ? BASIC_RF_FCF_ACK : BASIC_RF_FCF_NOACK;


    fcf= pConfig->ackRequest ? BASIC_RF_FCF_ACK : BASIC_RF_FCF_NOACK;


    //分别得到16位数的低8位和高8位


    //#define HI_UINT16(a) (((uint16)(a) >> 8) & 0xFF)


    //#define LO_UINT16(a) ((uint16)(a) & 0xFF)


    pHdr->fcf0 = LO_UINT16(fcf);


    pHdr->fcf1 = HI_UINT16(fcf);


    //得到数据序列


    pHdr->seqNumber= txState.txSeqNumber;


    pHdr->panId= pConfig->panId;


    pHdr->destAddr= destAddr;


    pHdr->srcAddr= pConfig->myAddr;


 


    #ifdef SECURITY_CCM


 


    // Add security to FCF, length and security header


    pHdr->fcf0 |= BASIC_RF_SEC_ENABLED_FCF_BM_L;


    pHdr->packetLength += PKT_LEN_MIC;


    pHdr->packetLength += BASIC_RF_AUX_HDR_LENGTH;


 


    pHdr->securityControl= SECURITY_CONTROL;


    pHdr->frameCounter[0]=   LO_UINT16(LO_UINT32(txState.frameCounter));


    pHdr->frameCounter[1]=   HI_UINT16(LO_UINT32(txState.frameCounter));


    pHdr->frameCounter[2]=   LO_UINT16(HI_UINT32(txState.frameCounter));


    pHdr->frameCounter[3]=   HI_UINT16(HI_UINT32(txState.frameCounter));


 


    #endif


 


    // Make sure bytefields are network byte order


    //这里把大的字节放到了前面,但有不是完全按从大到小的顺序排列字节,不知道为什么?


    UINT16_HTON(pHdr->panId);


    UINT16_HTON(pHdr->destAddr);


    UINT16_HTON(pHdr->srcAddr);


 


    //1字节长度,2字节FCF,1字节序列,2字节PanID,2字节目的地址,2字节源地址=10


    return BASIC_RF_HDR_SIZE;


}


 


 


/***********************************************************************************


* @fn          basicRfBuildMpdu


*


* @brief       Builds mpdu (MAC header + payload) according to IEEE 802.15.4


*              frame format


*


* @param       destAddr - Destination short address


*              pPayload - pointer to buffer with payload


*              payloadLength - length of payload buffer


*


* @return      uint8 - length of mpdu


*/


//构造MPDU


static uint8 basicRfBuildMpdu(uint16 destAddr, uint8* pPayload, uint8 payloadLength)


{


    uint8 hdrLength, n;


 


    //txMpdu是定义txMpdu[BASIC_RF_MAX_PAYLOAD_SIZE+BASIC_RF_PACKET_OVERHEAD_SIZE+1]


    hdrLength = basicRfBuildHeader(txMpdu, destAddr, payloadLength);


 


    //将有效载荷数据放入MPDU单元中


    for(n=0;n<payloadLength;n++)


    {


        txMpdu[hdrLength+n] = pPayload[n];


    }


    return hdrLength + payloadLength; // total mpdu length


}


 


 


/***********************************************************************************


* @fn          basicRfRxFrmDoneIsr


*


* @brief       Interrupt service routine for received frame from radio


*              (either data or acknowlegdement)


*


* @param       rxi - file scope variable info extracted from the last incoming


*                    frame


*              txState - file scope variable that keeps tx state info


*


* @return      none


*/


//接收中断服务程序


static void basicRfRxFrmDoneIsr(void)


{


    basicRfPktHdr_t *pHdr;


    uint8 *pStatusWord;


    #ifdef SECURITY_CCM


    uint8 authStatus="0";


    #endif


 


    // Map header to packet buffer


    pHdr= (basicRfPktHdr_t*)rxMpdu;


 


    //IM_FIFOP中断禁止,禁止RF总中断


    // Clear interrupt and disable new RX frame done interrupt


    halRfDisableRxInterrupt();


 


    //打开总中断


    // Enable all other interrupt sources (enables interrupt nesting)


    halIntOn();


/*读取接收缓存,length指定要读取的字节数


void halRfReadRxBuf(uint8* pData, uint8 length)


{


    while (length>0) {


        *pData++= RFD;


        length--;


    }


}


*/


    //读取MPDU的长度


    // Read payload length.


    halRfReadRxBuf(&pHdr->packetLength,1);


    //指定MPDU的长度字节的高位默认固定为0,所以去掉高位,得到长度真实值


    pHdr->packetLength &= BASIC_RF_PLD_LEN_MASK; // Ignore MSB


   


    // Is this an acknowledgment packet?


    // Only ack packets may be 5 bytes in total.


    if (pHdr->packetLength == BASIC_RF_ACK_PACKET_SIZE) {


    //如果只有5个字节长度,则此帧为应答帧


        // Read the packet


        halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength);


 


        // Make sure byte fields are changed from network to host byte order


    UINT16_NTOH(pHdr->panId);


    UINT16_NTOH(pHdr->destAddr);


    UINT16_NTOH(pHdr->srcAddr);


        #ifdef SECURITY_CCM


        UINT32_NTOH(pHdr->frameCounter);


        #endif


 


        rxi.ackRequest = !!(pHdr->fcf0 & BASIC_RF_FCF_ACK_BM_L);


 


        //条过RSSI指到CRC校验


        // Read the status word and check for CRC OK


        pStatusWord= rxMpdu + 4;


 


        // Indicate the successful ACK reception if CRC and sequence number OK


        if ((pStatusWord[1] & BASIC_RF_CRC_OK_BM) && (pHdr->seqNumber == txState.txSeqNumber)) {


            txState.ackReceived = TRUE;


        }


 


        // No, it is data


    } else {


 


        // It is assumed that the radio rejects packets with invalid length.


        // Subtract the number of bytes in the frame overhead to get actual payload.


 


        rxi.length = pHdr->packetLength - BASIC_RF_PACKET_OVERHEAD_SIZE;


 


        #ifdef SECURITY_CCM


        rxi.length -= (BASIC_RF_AUX_HDR_LENGTH + BASIC_RF_LEN_MIC);


        authStatus = halRfReadRxBufSecure(&rxMpdu[1], pHdr->packetLength, rxi.length,


                                        BASIC_RF_LEN_AUTH, BASIC_RF_SECURITY_M);


        #else


        halRfReadRxBuf(&rxMpdu[1], pHdr->packetLength);


        #endif


 


        // Make sure byte fields are changed from network to host byte order


    UINT16_NTOH(pHdr->panId);


    UINT16_NTOH(pHdr->destAddr);


    UINT16_NTOH(pHdr->srcAddr);


        #ifdef SECURITY_CCM


        UINT32_NTOH(pHdr->frameCounter);


        #endif


 


        rxi.ackRequest = !!(pHdr->fcf0 & BASIC_RF_FCF_ACK_BM_L);


 


        // Read the source address


        rxi.srcAddr= pHdr->srcAddr;


 


        // Read the packet payload


        rxi.pPayload = rxMpdu + BASIC_RF_HDR_SIZE;


 


        // Read the FCS to get the RSSI and CRC


        pStatusWord= rxi.pPayload+rxi.length;


        #ifdef SECURITY_CCM


        pStatusWord+= BASIC_RF_LEN_MIC;


        #endif


        rxi.rssi = pStatusWord[0];


 


        // Notify the application about the received data packet if the CRC is OK


        // Throw packet if the previous packet had the same sequence number


        if( (pStatusWord[1] & BASIC_RF_CRC_OK_BM) && (rxi.seqNumber != pHdr->seqNumber) ) {


            // If security is used check also that authentication passed


            #ifdef SECURITY_CCM


            if( authStatus==SUCCESS ) {


                if ( (pHdr->fcf0 & BASIC_RF_FCF_BM_L) ==


                    (BASIC_RF_FCF_NOACK_L | BASIC_RF_SEC_ENABLED_FCF_BM_L)) {


                        rxi.isReady = TRUE;


                }


            }


            #else


            if ( ((pHdr->fcf0 & (BASIC_RF_FCF_BM_L)) == BASIC_RF_FCF_NOACK_L) ) {


                rxi.isReady = TRUE;


            }             


            #endif


        }


        rxi.seqNumber = pHdr->seqNumber;


    }


 


    // Enable RX frame done interrupt again


    halIntOff();


    halRfEnableRxInterrupt();


}


 


 


/***********************************************************************************


* GLOBAL FUNCTIONS


*/


 


/***********************************************************************************


* @fn          basicRfInit


*


* @brief       Initialise basic RF datastructures. Sets channel, short address and


*              PAN id in the chip and configures interrupt on packet reception


*


* @param       pRfConfig - pointer to BASIC_RF_CONFIG struct.


*                          This struct must be allocated by higher layer


*              txState - file scope variable that keeps tx state info


*              rxi - file scope variable info extracted from the last incoming


*                    frame


*


* @return      none


*/


//几个重要的底层函数:


/*RF初始化,调用hal_rf.c中的函数


uint8 halRfInit(void)


{


    uint8 i;


 


    // turning on power to analog part of radio and waiting for voltage regulator.


    //模拟稳压器上电,延时250uS上电


    RFPWR = 0x04;


    //等待ADI_RADIO_PD为0,也可以通过检测中断标志RFIF.IRQ_RREG_ON为1来等待稳压器稳定


    while( RFPWR & 0x10 );


 


    //根据需要设置RF功能,自动CRC,自动应答0x03


    // Setting for AUTO CRC and AUTOACK


    MDMCTRL0L |= (AUTO_CRC | AUTO_ACK);


 


    //打开自动收发转换,接收包后12个符号超时,接受应答包控制


    // Turning on AUTO_TX2RX


    FSMTC1 = ((FSMTC1 & (~AUTO_TX2RX_OFF & ~RX2RX_TIME_OFF))  | ACCEPT_ACKPKT);


 


    //当SRXON发送时包接收不终止


    // Turning off abortRxOnSrxon.


    FSMTC1 &= ~0x20;


 


    //FIFO门限设为最大值127


    // Set FIFOP threshold to maximum


    IOCFG0 = 0x7F;


    //增益这部分没看懂,好像是用来优化的


    // tuning adjustments for optimal radio performance; details available in datasheet */


    RXCTRL0H = 0x32;


    RXCTRL0L = 0xF5;


 


    // Turning on receiver to get output from IF-ADC


    ISRXON();


    halMcuWaitUs(1);


 


    //运行随机数发生器


    // Enable random generator


    ADCCON1 &= ~0x0C;


 


    for(i = 0 ; i < 32 ; i++)


    {


        RNDH = ADCTSTH;


        // Clock random generator


        ADCCON1 |= 0x04;


    }


    ISRFOFF();


 


    // Enable CC2591 with High Gain Mode


    halPaLnaInit();


 


    //打开接收中断


    halRfEnableRxInterrupt();


 


    return SUCCESS;


}


 


//设置2.4G的RF通道。


void halRfSetChannel(uint8 channel)


{


    uint16 freqMHz;


 


    //MIN_CHANNEL为11,CHANNEL_SPACING为5,在hal_rf.h中定义


    freqMHz= 2405 + ((channel - MIN_CHANNEL) * CHANNEL_SPACING); // Calculate frequency


    freqMHz -= (uint32)2048;              // Subtract; datasheet sect 14.16


 


    FSCTRLL = LO_UINT16(freqMHz);


    FSCTRLH &= ~0x03;


    FSCTRLH |= (HI_UINT16(freqMHz) & 0x03);


}


 


//设置16位短地址


void halRfSetShortAddr(uint16 shortAddr)


{


    SHORTADDRL= LO_UINT16(shortAddr);


    SHORTADDRH= HI_UINT16(shortAddr);


}


 


//设置PANID


void halRfSetPanId(uint16 panId)


{


    PANIDL= LO_UINT16(panId);


    PANIDH= HI_UINT16(panId);


}


*/


uint8 basicRfInit(basicRfCfg_t* pRfConfig)


{


    if (halRfInit()==FAILED)


        return FAILED;


    //关闭所有中断


    halIntOff();


 


    // Set the protocol configuration


    pConfig = pRfConfig;


    rxi.pPayload   = NULL;


 


    txState.receiveOn = TRUE;


    txState.frameCounter = 0;


 


    //设置通道


    // Set channel


    halRfSetChannel(pConfig->channel);


 


    //设置短地址和PAN id


    // Write the short address and the PAN ID to the CC2520 RAM


    halRfSetShortAddr(pConfig->myAddr);


    halRfSetPanId(pConfig->panId);


 


    // if security is enabled, write key and nonce


    #ifdef SECURITY_CCM


    basicRfSecurityInit(pConfig);


    #endif


 


/*


//设置接收中断函数


在hal_types.h中有中断函数的定义:


#define HAL_ISR_FUNC_DECLARATION(f,v)   \


    _PRAGMA(vector=v) __near_func __interrupt void f(void)


#define HAL_ISR_FUNC_PROTOTYPE(f,v)     \


    _PRAGMA(vector=v) __near_func __interrupt void f(void)


#define HAL_ISR_FUNCTION(f,v)           \


    HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v)


可以看出HAL_ISR_FUNCTION(f,v)其实就定义了中断函数了。然后中断函数如下:


HAL_ISR_FUNCTION( macMcuRfIsr, RF_VECTOR )


{


    uint8 rfim;


    uint8 x;


 


    HAL_INT_LOCK(x);


 


    rfim = RFIM;


 


    if ((RFIF & IRQ_FIFOP) & rfim)


    {


        (pfISR)();                  // Execute the custom ISR


        S1CON= 0;


        RFIF&= ~IRQ_FIFOP;


    }


    HAL_INT_UNLOCK(x);


}


进入中断后会检查中断类型,然后执行pfISR指针指向的中断函数。


typedef void (*ISR_FUNC_PTR)(void);


//定义中断服务程序的函数指针


static ISR_FUNC_PTR pfISR= NULL;


void halRfRxInterruptConfig(ISR_FUNC_PTR pf)


{


    uint8 x;


    HAL_INT_LOCK(x);


    pfISR= pf;


    HAL_INT_UNLOCK(x);


//#define HAL_INT_LOCK(x)    st( (x) = EA; EA = 0; )


//#define HAL_INT_UNLOCK(x)  st( EA = (x); )


//中断锁。实际上就是在操作之前关闭总的中断,等操作完成后再打开中断,避免产生不必要的中断


}


*/


    // Set up receive interrupt (received data or acknowlegment)


    halRfRxInterruptConfig(basicRfRxFrmDoneIsr);


 


    //开所有中断


    halIntOn();


 


    return SUCCESS;


}


 


 


/***********************************************************************************


* @fn          basicRfSendPacket


*


* @brief       Send packet


*


* @param       destAddr - destination short address


*              pPayload - pointer to payload buffer. This buffer must be


*                         allocated by higher layer.


*              length - length of payload


*              txState - file scope variable that keeps tx state info


*              mpdu - file scope variable. Buffer for the frame to send


*


* @return      basicRFStatus_t - SUCCESS or FAILED


*/


/*发送函数


uint8 halRfTransmit(void)


{


    uint8 status;


 


    ISTXON(); // Sending


 


    // Waiting for transmission to finish


    while(!(RFIF & IRQ_TXDONE) );


 


    RFIF = ~IRQ_TXDONE;


    status= SUCCESS;


 


    return status;


}


*/


uint8 basicRfSendPacket(uint16 destAddr, uint8* pPayload, uint8 length)


{


    uint8 mpduLength;


    uint8 status;


 


/*


打开接收


#define FLUSH_RX_FIFO()         st( ISFLUSHRX(); ISFLUSHRX(); )


void halRfReceiveOn(void)


{


//发送CPS指令


    FLUSH_RX_FIFO();


    ISRXON();


}


*/


    // Turn on receiver if its not on


    if(!txState.receiveOn) {


        halRfReceiveOn();


    }


 


    // Check packet length


    length = min(length, BASIC_RF_MAX_PAYLOAD_SIZE);


 


/*等待传输完成


void halRfWaitTransceiverReady(void)


{


    while (RFSTATUS & (1<<1) | (1<<4) ));


}


*/


    // Wait until the transceiver is idle


    halRfWaitTransceiverReady();


 


    // Turn off RX frame done interrupt to avoid interference on the SPI interface


    halRfDisableRxInterrupt();


 


    mpduLength = basicRfBuildMpdu(destAddr, pPayload, length);


 


    #ifdef SECURITY_CCM


    halRfWriteTxBufSecure(txMpdu, mpduLength, length, BASIC_RF_LEN_AUTH, BASIC_RF_SECURITY_M);


    txState.frameCounter++;     // Increment frame counter field


    #else


    halRfWriteTxBuf(txMpdu, mpduLength);


    #endif


 


    // Turn on RX frame done interrupt for ACK reception


    halRfEnableRxInterrupt();


 


    // Send frame with CCA. return FAILED if not successful


    if(halRfTransmit() != SUCCESS) {


        status = FAILED;


    }


 


    // Wait for the acknowledge to be received, if any


    if (pConfig->ackRequest) {


        txState.ackReceived = FALSE;


 


        // We'll enter RX automatically, so just wait until we can be sure that the ack reception should have finished


        // The timeout consists of a 12-symbol turnaround time, the ack packet duration, and a small margin


       //BASIC_RF_SYMBOL_DURATION为0.5us,因为2MChip/s的速度是固定的


        halMcuWaitUs((12 * BASIC_RF_SYMBOL_DURATION) + (BASIC_RF_ACK_DURATION) + (2 * BASIC_RF_SYMBOL_DURATION) + 10);


 


        // If an acknowledgment has been received (by RxFrmDoneIsr), the ackReceived flag should be set


        status = txState.ackReceived ? SUCCESS : FAILED;


 


    } else {


        status = SUCCESS;


    }


 


    // Turn off the receiver if it should not continue to be enabled


    if (!txState.receiveOn) {


        halRfReceiveOff();


    }


 


    if(status == SUCCESS) {


        txState.txSeqNumber++;


    }


#ifdef SECURITY_CCM


    halRfIncNonceTx();          // Increment nonce value


#endif

    return status;

}



/***********************************************************************************


* @fn          basicRfPacketIsReady


*


* @brief       Check if a new packet is ready to be read by next higher layer


*


* @param       none


*


* @return      uint8 - TRUE if a packet is ready to be read by higher layer


*/


//检查包是否已准备好被上层读取


uint8 basicRfPacketIsReady(void)

{

    return rxi.isReady;

}


/**********************************************************************************


* @fn          basicRfReceive


*


* @brief       Copies the payload of the last incoming packet into a buffer


*


* @param       pRxData - pointer to data buffer to fill. This buffer must be


*                        allocated by higher layer.


*              len - Number of bytes to read in to buffer


*              rxi - file scope variable holding the information of the last


*                    incoming packet


*


* @return      uint8 - number of bytes actually copied into buffer


*/


uint8 basicRfReceive(uint8* pRxData, uint8 len, int16* pRssi)


{


    // Accessing shared variables -> this is a critical region


    // Critical region start


    halIntOff();


    memcpy(pRxData, rxi.pPayload, min(rxi.length, len));


    if(pRssi != NULL) {


        if(rxi.rssi < 128){


            *pRssi = rxi.rssi - halRfGetRssiOffset();


        }


        else{


            *pRssi = (rxi.rssi - 256) - halRfGetRssiOffset();


        }


    }


    rxi.isReady = FALSE;


    halIntOn();


    // Critical region end


    return min(rxi.length, len);


}


/**********************************************************************************


* @fn          basicRfGetRssi


*


* @brief       Copies the payload of the last incoming packet into a buffer


*


* @param       none


 


* @return      int8 - RSSI value


*/


int8 basicRfGetRssi(void)


{


    if(rxi.rssi < 128){


    //CC2430的rssi便宜为45左右


        return rxi.rssi - halRfGetRssiOffset();


    }


    else{


        return (rxi.rssi - 256) - halRfGetRssiOffset();


    }


}


 


/***********************************************************************************


* @fn          basicRfReceiveOn


*


* @brief       Turns on receiver on radio


*


* @param       txState - file scope variable


*


* @return      none


*/


void basicRfReceiveOn(void)


{


    txState.receiveOn = TRUE;


    halRfReceiveOn();


}


/***********************************************************************************


* @fn          basicRfReceiveOff


*


* @brief       Turns off receiver on radio


*


* @param       txState - file scope variable


*


* @return      none


*/


void basicRfReceiveOff(void)


{


    txState.receiveOn = FALSE;


    halRfReceiveOff();


}


/***********************************************************************************


  Copyright 2007 Texas Instruments Incorporated. All rights reserved.


*/


原创粉丝点击