📄 mac_radio.c
字号:
*
* @return none
**************************************************************************************************
*/
void macRadioUpdateTxPower(void)
{
halIntState_t s;
/*
* If the requested power setting is different from the actual radio setting,
* attempt to udpate to the new power setting.
*/
HAL_ENTER_CRITICAL_SECTION(s);
if (reqTxPower != macPhyTxPower)
{
/*
* Radio power cannot be updated when the radio is physically transmitting.
* If there is a possibility radio is transmitting, do not change the power
* setting. This function will be called again after the current transmit
* completes.
*/
if (!macRxOutgoingAckFlag && !MAC_TX_IS_PHYSICALLY_ACTIVE())
{
/*
* Set new power level; update the shadow value and write
* the new value to the radio hardware.
*/
macPhyTxPower = reqTxPower;
MAC_RADIO_SET_TX_POWER(macPhyTxPower);
}
}
HAL_EXIT_CRITICAL_SECTION(s);
}
/**************************************************************************************************
* @fn macRadioSetChannel
*
* @brief Set radio channel.
*
* @param channel - channel number, valid range is 11 through 26
*
* @return none
**************************************************************************************************
*/
void macRadioSetChannel(uint8 channel)
{
halIntState_t s;
MAC_ASSERT((channel >= 11) && (channel <= 28)); /* illegal channel */
/* critical section to make sure transmit does not start while updating channel */
HAL_ENTER_CRITICAL_SECTION(s);
/* set requested channel */
reqChannel = channel;
/*
* If transmit is not active, update the radio hardware immediately. If transmit is active,
* the channel will be updated at the end of the current transmit.
*/
if (!macTxActive)
{
macRadioUpdateChannel();
}
HAL_EXIT_CRITICAL_SECTION(s);
}
/**************************************************************************************************
* @fn macRadioUpdateChannel
*
* @brief Update the radio channel if a new channel has been requested.
*
* @param none
*
* @return none
**************************************************************************************************
*/
void macRadioUpdateChannel(void)
{
halIntState_t s;
MAC_ASSERT(!macTxActive); /* cannot change channel during a transmit */
/* if the channel has changed, set the radio to the new channel */
HAL_ENTER_CRITICAL_SECTION(s);
if (reqChannel != macPhyChannel)
{
macPhyChannel = reqChannel;
HAL_EXIT_CRITICAL_SECTION(s);
/* changing the channel stops any receive in progress */
macRxOff();
MAC_RADIO_SET_CHANNEL(macPhyChannel);
macRxOnRequest();
}
else
{
HAL_EXIT_CRITICAL_SECTION(s);
}
}
/**************************************************************************************************
* @fn macRadioStartScan
*
* @brief Puts radio into selected scan mode.
*
* @param scanMode - scan mode, see #defines in .h file
*
* @return none
**************************************************************************************************
*/
void macRadioStartScan(uint8 scanMode)
{
MAC_ASSERT(macSleepState == MAC_SLEEP_STATE_AWAKE); /* radio must be awake */
MAC_ASSERT(macRxFilter == RX_FILTER_OFF); /* all filtering must be off to start scan */
/* set the receive filter based on the selected scan mode */
if (scanMode == MAC_SCAN_ED)
{
macRxFilter = RX_FILTER_ALL;
}
else if (scanMode == MAC_SCAN_ORPHAN)
{
macRxFilter = RX_FILTER_NON_COMMAND_FRAMES;
}
else
{
MAC_ASSERT((scanMode == MAC_SCAN_ACTIVE) || (scanMode == MAC_SCAN_PASSIVE)); /* invalid scan type */
macRxFilter = RX_FILTER_NON_BEACON_FRAMES;
/* for active and passive scans, per spec the pan ID must be 0xFFFF */
MAC_RADIO_SET_PAN_ID(0xFFFF);
}
}
/**************************************************************************************************
* @fn macRadioStopScan
*
* @brief Takes radio out of scan mode. Note can be called if
*
* @param none
*
* @return none
**************************************************************************************************
*/
void macRadioStopScan(void)
{
macRxFilter = RX_FILTER_OFF;
/* restore the pan ID (passive and active scans set pan ID to 0xFFFF) */
MAC_RADIO_SET_PAN_ID(macPib.panId);
}
/**************************************************************************************************
* @fn macRadioEnergyDetectStart
*
* @brief Initiates energy detect. The highest energy detected is recorded from the time
* when this function is called until the energy detect is stopped.
*
* @param none
*
* @return none
**************************************************************************************************
*/
void macRadioEnergyDetectStart(void)
{
MAC_RADIO_RECORD_MAX_RSSI_START();
}
/**************************************************************************************************
* @fn macRadioEnergyDetectStop
*
* @brief Called at completion of an energy detect. Note: can be called even if energy
* detect is already stopped (needed by reset).
*
* @param none
*
* @return highest energy detect measurement
**************************************************************************************************
*/
uint8 macRadioEnergyDetectStop(void)
{
uint8 rssiDbm;
uint8 energyDetectMeasurement;
rssiDbm = MAC_RADIO_RECORD_MAX_RSSI_STOP() + MAC_RADIO_RSSI_OFFSET;
energyDetectMeasurement = radioComputeED(rssiDbm);
return(energyDetectMeasurement);
}
/*=================================================================================================
* @fn radioComputeED
*
* @brief Compute energy detect measurement.
*
* @param rssi - raw RSSI value from radio hardware
*
* @return energy detect measurement in the range of 0x00-0xFF
*=================================================================================================
*/
static uint8 radioComputeED(int8 rssiDbm)
{
uint8 ed;
/*
* Keep RF power between minimum and maximum values.
* This min/max range is derived from datasheet and specification.
*/
if (rssiDbm < ED_RF_POWER_MIN_DBM)
{
rssiDbm = ED_RF_POWER_MIN_DBM;
}
else if (rssiDbm > ED_RF_POWER_MAX_DBM)
{
rssiDbm = ED_RF_POWER_MAX_DBM;
}
/*
* Create energy detect measurement by normalizing and scaling RF power level.
*
* Note : The division operation below is designed for maximum accuracy and
* best granularity. This is done by grouping the math operations to
* compute the entire numerator before doing any division.
*/
ed = (MAC_SPEC_ED_MAX * (rssiDbm - ED_RF_POWER_MIN_DBM)) / (ED_RF_POWER_MAX_DBM - ED_RF_POWER_MIN_DBM);
return(ed);
}
/**************************************************************************************************
* @fn macRadioComputeLQI
*
* @brief Compute link quality indication.
*
* @param rssi - raw RSSI value from radio hardware
* corr - correlation value from radio hardware
*
* @return link quality indicator value
**************************************************************************************************
*/
uint8 macRadioComputeLQI(int8 rssiDbm, uint8 corr)
{
(void) corr; /* suppress compiler warning of unused parameter */
/*
* Note : Currently the LQI value is simply the energy detect measurement.
* A more accurate value could be derived by using the correlation
* value along with the RSSI value.
*/
return(radioComputeED(rssiDbm));
}
/**************************************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -