diff --git a/examples/platforms/nrf52840/radio.c b/examples/platforms/nrf52840/radio.c index a3c016ddc..64beac67e 100644 --- a/examples/platforms/nrf52840/radio.c +++ b/examples/platforms/nrf52840/radio.c @@ -188,6 +188,34 @@ void nrf5RadioInit(void) nrf_drv_radio802154_init(); } +PhyState otPlatRadioGetState(otInstance *aInstance) +{ + (void) aInstance; + + if (sDisabled) + { + return kStateDisabled; + } + + switch (nrf_drv_radio802154_state_get()) + { + case NRF_DRV_RADIO802154_STATE_SLEEP: + return kStateSleep; + + case NRF_DRV_RADIO802154_STATE_RECEIVE: + case NRF_DRV_RADIO802154_STATE_ENERGY_DETECTION: + return kStateReceive; + + case NRF_DRV_RADIO802154_STATE_TRANSMIT: + return kStateTransmit; + + default: + assert(false); // Make sure driver returned valid state. + } + + return kStateReceive; // It is the default state. Return it in case of unknown. +} + ThreadError otPlatRadioEnable(otInstance *aInstance) { (void) aInstance; diff --git a/include/openthread/platform/radio.h b/include/openthread/platform/radio.h index 3c45e696a..3837de214 100644 --- a/include/openthread/platform/radio.h +++ b/include/openthread/platform/radio.h @@ -200,6 +200,19 @@ void otPlatRadioSetShortAddress(otInstance *aInstance, uint16_t aShortAddress); * */ +/** + * Get current state of the radio. + * + * This function is not required by OpenThread. It may be used for debugging and/or application-specific purposes. + * + * @note This function may be not implemented. It does not affect OpenThread. + * + * @param[in] aInstance The OpenThread instance structure. + * + * @return Current state of the radio. + */ +PhyState otPlatRadioGetState(otInstance *aInstance); + /** * Enable the radio. * diff --git a/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.c b/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.c index 37dd98eed..a01449e7e 100644 --- a/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.c +++ b/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.c @@ -915,6 +915,31 @@ void nrf_drv_radio802154_init(void) irq_init(); } +nrf_drv_radio802154_state_t nrf_drv_radio802154_state_get(void) +{ + switch (m_state) + { + case RADIO_STATE_SLEEP: + return NRF_DRV_RADIO802154_STATE_SLEEP; + + case RADIO_STATE_WAITING_RX_FRAME: + case RADIO_STATE_RX_HEADER: + case RADIO_STATE_RX_FRAME: + case RADIO_STATE_TX_ACK: + return NRF_DRV_RADIO802154_STATE_RECEIVE; + + case RADIO_STATE_CCA: + case RADIO_STATE_TX_FRAME: + case RADIO_STATE_RX_ACK: + return NRF_DRV_RADIO802154_STATE_TRANSMIT; + + case RADIO_STATE_ED: + return NRF_DRV_RADIO802154_STATE_ENERGY_DETECTION; + } + + return NRF_DRV_RADIO802154_STATE_INVALID; +} + bool nrf_drv_radio802154_sleep(void) { bool result = true; diff --git a/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.h b/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.h index b32e9c931..89f804c70 100644 --- a/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.h +++ b/third_party/NordicSemiconductor/drivers/nrf_drv_radio802154.h @@ -43,6 +43,18 @@ extern "C" { #endif +/** + * @brief States of the driver. + */ +typedef enum +{ + NRF_DRV_RADIO802154_STATE_INVALID, + NRF_DRV_RADIO802154_STATE_SLEEP, + NRF_DRV_RADIO802154_STATE_RECEIVE, + NRF_DRV_RADIO802154_STATE_TRANSMIT, + NRF_DRV_RADIO802154_STATE_ENERGY_DETECTION, +} nrf_drv_radio802154_state_t; + /** * @brief Initialize 802.15.4 driver. * @@ -90,7 +102,7 @@ void nrf_drv_radio802154_short_address_set(const uint8_t *p_short_address); /** - * @section Functions to request FSM transitions. + * @section Functions to request FSM transitions and check current state. * * receive() transmit() * --------> --------> @@ -103,6 +115,11 @@ void nrf_drv_radio802154_short_address_set(const uint8_t *p_short_address); * Energy detection */ +/** + * @brief Get current state of the radio. + */ +nrf_drv_radio802154_state_t nrf_drv_radio802154_state_get(void); + /** * @brief Change radio state to Sleep. *