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

📄 hal_rf.c

📁 cc2430编程实例,非常适合初学者学习。
💻 C
📖 第 1 页 / 共 2 页
字号:
*/
void halRfSetPanId(uint16 panId)
{
    PANIDL= LOBYTE(panId);
    PANIDH= HIBYTE(panId);
}


/***********************************************************************************
* @fn      halRfSetPower
*
* @brief   Set TX output power
*
* @param   uint8 power - power level: TXPOWER_MIN_4_DBM, TXPOWER_0_DBM,
*                        TXPOWER_4_DBM
*
* @return  uint8 - SUCCESS or FAILED
*/
uint8 halRfSetTxPower(uint8 power)
{
    uint8 v;

    switch(power)
    {
#ifdef INCLUDE_PA
      case HAL_RF_TXPOWER_0_DBM:
        v = 0x06;
        break;
    case HAL_RF_TXPOWER_13_DBM:
        v = 0x13;
        break;
    case HAL_RF_TXPOWER_15_DBM:
        v = 0x17;
        break;
    case HAL_RF_TXPOWER_18_DBM:
        v = 0x5F;
        break;
    case HAL_RF_TXPOWER_19_DBM:
        v = 0xFF;
        break;
#else
    case HAL_RF_TXPOWER_0_DBM:
        v = 0x5F;
        break;
    case HAL_RF_TXPOWER_MIN_4_DBM:
        v = 0x17;
        break;
#endif
    default:
        return FAILED;
    }

    // Set TX power
    TXCTRLL&= ~PA_LEVEL_MASK;
    TXCTRLL|= v;

    return SUCCESS;
}


/***********************************************************************************
* @fn      halRfSetGain
*
* @brief   Set gain mode - only applicable for units with CC2590/91.
*
* @param   uint8 - gain mode
*
* @return  none
*/
void halRfSetGain(uint8 gainMode)
{
    if (gainMode==HAL_RF_GAIN_LOW) {
        HAL_PA_LNA_RX_LGM();
        rssiOffset = RSSI_OFFSET_LNA_LOWGAIN;
    } else {
        HAL_PA_LNA_RX_HGM();
        rssiOffset = RSSI_OFFSET_LNA_HIGHGAIN;
    }
}

/***********************************************************************************
* @fn      halRfWriteTxBuf
*
* @brief   Write to TX buffer
*
* @param   uint8* pData - buffer to write
*          uint8 length - number of bytes
*
* @return  none
*/
void halRfWriteTxBuf(uint8* pData, uint8 length)
{
    uint8 i;

    ISFLUSHTX();          // Making sure that the TX FIFO is empty.

    RFIF = ~IRQ_TXDONE;   // Clear TX done interrupt

    // Insert data
    for(i=0;i<length;i++){
        RFD = pData[i];
    }

}


/***********************************************************************************
* @fn      halRfAppendTxBuf
*
* @brief   Write to TX buffer
*
* @param   uint8* pData - buffer to write
*          uint8 length - number of bytes
*
* @return  none
*/
void halRfAppendTxBuf(uint8* pData, uint8 length)
{
    uint8 i;

    // Insert data
    for(i=0;i<length;i++){
        RFD = pData[i];
    }
}


/***********************************************************************************
* @fn      halRfReadRxBuf
*
* @brief   Read RX buffer
*
* @param   uint8* pData - data buffer. This must be allocated by caller.
*          uint8 length - number of bytes
*
* @return  none
*/
void halRfReadRxBuf(uint8* pData, uint8 length)
{
    while (length>0) {
        *pData++= RFD;
        length--;
    }
}



/***********************************************************************************
* @fn      halRfTransmit
*
* @brief   Transmit frame with Clear Channel Assessment.
*
* @param   none
*
* @return  uint8 - SUCCESS or FAILED
*/
uint8 halRfTransmit(void)
{
    uint8 status;

    ISTXON(); // Sending

    // Waiting for transmission to finish
    while(!(RFIF & IRQ_TXDONE) );

    RFIF = ~IRQ_TXDONE;
    status= SUCCESS;

    // TBD: use CCA
    return status;
}



/***********************************************************************************
* @fn      halRfReceiveOn
*
* @brief   Turn receiver on
*
* @param   none
*
* @return  none
*/
void halRfReceiveOn(void)
{
    FLUSH_RX_FIFO();
    ISRXON();
}

/***********************************************************************************
* @fn      halRfReceiveOff
*
* @brief   Turn receiver off
*
* @param   none
*
* @return  none
*/
void halRfReceiveOff(void)
{
    ISRFOFF();
    FLUSH_RX_FIFO();
}


/***********************************************************************************
* @fn      halRfDisableRxInterrupt
*
* @brief   Clear and disable RX interrupt.
*
* @param   none
*
* @return  none
*/
void halRfDisableRxInterrupt(void)
{
  // disable RX_FIFOP interrupt
  RFIM &= ~BV(5);
  // disable general RF interrupts
  IEN2 &= ~BV(0);
}


/***********************************************************************************
* @fn      halRfEnableRxInterrupt
*
* @brief   Enable RX interrupt.
*
* @param   none
*
* @return  none
*/
void halRfEnableRxInterrupt(void)
{
  // enable RX_FIFOP interrupt
  RFIM |= BV(5);
  // enable general RF interrupts
  IEN2 |= BV(0);
}


/***********************************************************************************
* @fn      halRfRxInterruptConfig
*
* @brief   Configure RX interrupt.
*
* @param   none
*
* @return  none
*/
void halRfRxInterruptConfig(ISR_FUNC_PTR pf)
{
    uint8 x;
    HAL_INT_LOCK(x);
    pfISR= pf;
    HAL_INT_UNLOCK(x);
}

/***********************************************************************************
* @fn      halRfWaitTransceiverReady
*
* @brief   Wait until the transciever is ready (SFD inactive).
*
* @param   none
*
* @return  none
*/
void halRfWaitTransceiverReady(void)
{
    while (RFSTATUS & (BV(1) | BV(4) ));
}

/************************************************************************************
 * @fn          macMcuRfIsr
 *
 * @brief       Interrupt service routine that handles FIFOP interrupts.
 *
 * @param       none
 *
 * @return      none
 */
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);
}

/***********************************************************************************
* LOCAL FUNCTIONS
*/
static void halPaLnaInit(void)
{
#ifdef INCLUDE_PA  
    /* Initialize CC2591 to RX high gain mode */
    static uint8 fFirst= TRUE;
    
    if(fFirst) {
        uint8 i; 
        P1SEL&= ~0x02; 
        P1DIR|= 0x02; 
        P1_1= 1; 
        
        for (i=0; i<8; i++) { 
            asm("NOP");
        }
        fFirst = FALSE;
        rssiOffset = RSSI_OFFSET_LNA_HIGHGAIN;
    }
    
#else // do nothing
#endif
}



/***********************************************************************************
  Copyright 2007 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED 揂S IS

⌨️ 快捷键说明

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