From f2fa3ca12ff2be3ed48e5c3208fc3975229766c8 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Tue, 15 Dec 2020 13:32:07 +0100 Subject: [PATCH] radio/nrf802154: fix set_cca_threshold range This commit fixes the CCA threshold range for set_cca_threshold. Without this commit the threshold overflows when using values below the receiver sensitivity. --- cpu/nrf52/radio/nrf802154/nrf802154_radio.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/cpu/nrf52/radio/nrf802154/nrf802154_radio.c b/cpu/nrf52/radio/nrf802154/nrf802154_radio.c index fc359df69b..db17851e38 100644 --- a/cpu/nrf52/radio/nrf802154/nrf802154_radio.c +++ b/cpu/nrf52/radio/nrf802154/nrf802154_radio.c @@ -279,15 +279,6 @@ static int _request_cca(ieee802154_dev_t *dev) return 0; } -/** - * @brief Set CCA threshold value in internal represetion - */ -static void _set_cca_thresh(uint8_t thresh) -{ - NRF_RADIO->CCACTRL &= ~RADIO_CCACTRL_CCAEDTHRES_Msk; - NRF_RADIO->CCACTRL |= thresh << RADIO_CCACTRL_CCAEDTHRES_Pos; -} - /** * @brief Convert from dBm to the internal representation, when the * radio operates as a IEEE802.15.4 transceiver. @@ -300,7 +291,15 @@ static inline uint8_t _dbm_to_ieee802154_hwval(int8_t dbm) static int set_cca_threshold(ieee802154_dev_t *dev, int8_t threshold) { (void) dev; - _set_cca_thresh(_dbm_to_ieee802154_hwval(threshold)); + + if (threshold < ED_RSSIOFFS) { + return -EINVAL; + } + + uint8_t hw_val = _dbm_to_ieee802154_hwval(threshold); + + NRF_RADIO->CCACTRL &= ~RADIO_CCACTRL_CCAEDTHRES_Msk; + NRF_RADIO->CCACTRL |= hw_val << RADIO_CCACTRL_CCAEDTHRES_Pos; return 0; }