From 16f56abfad9b69fdd3869dc65167f3e35be581b6 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 1 Nov 2015 17:45:29 +0000 Subject: [PATCH] Wait for a valid RSSI reading in CC13xx/CC26xx RF drivers As discussed in #1341, the current CC13xx/CC26xx IEEE mode driver sends `CMD_GET_RSSI` once and returns the RSSI reading uncondtionally. This happens within the `get_rssi()` function. This logic is broken if `get_rssi()` is called with the radio off. The function will make sure to turn on the radio first, but it does not make sure the RSSI reading is valid, which only happens a number of symbol periods after the radio enters RX. The outcome is that `NETSTACK_RADIO.get_value(RADIO_PARAM_RSSI, ...)` will always return -128 (meaning that RSSI is unavailable) if the radio was off at the time of calling the function. The same condition affects the prop mode driver. This commit changes the logic of `get_rssi()`: * For PROP mode, if `CMD_GET_RSSI` returns an invalid RSSI, we send it again. For unknown reasons, `CMD_GET_RSSI` on occasion returns 0, so we ignore that value too. * For IEEE mode, we use `CMD_IEEE_CCA_REQ` and we inspect the value of `ccaInfo.ccaEnergy` of the return structure. If the value is 0x02 (Invalid), we send the command again. Fixes #1341 --- cpu/cc26xx-cc13xx/rf-core/ieee-mode.c | 21 +++++++++++++-------- cpu/cc26xx-cc13xx/rf-core/prop-mode.c | 18 ++++++++++++------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/cpu/cc26xx-cc13xx/rf-core/ieee-mode.c b/cpu/cc26xx-cc13xx/rf-core/ieee-mode.c index f76fe2beb..3480ba86d 100644 --- a/cpu/cc26xx-cc13xx/rf-core/ieee-mode.c +++ b/cpu/cc26xx-cc13xx/rf-core/ieee-mode.c @@ -384,9 +384,8 @@ static radio_value_t get_rssi(void) { uint32_t cmd_status; - int8_t rssi; uint8_t was_off = 0; - rfc_CMD_GET_RSSI_t cmd; + rfc_CMD_IEEE_CCA_REQ_t cmd; /* If we are off, turn on first */ if(!rf_is_on()) { @@ -398,13 +397,19 @@ get_rssi(void) } memset(&cmd, 0x00, sizeof(cmd)); - cmd.commandNo = CMD_GET_RSSI; + cmd.ccaInfo.ccaEnergy = RF_CMD_CCA_REQ_CCA_STATE_INVALID; - rssi = RF_CMD_CCA_REQ_RSSI_UNKNOWN; + while(cmd.ccaInfo.ccaEnergy == RF_CMD_CCA_REQ_CCA_STATE_INVALID) { + memset(&cmd, 0x00, sizeof(cmd)); + cmd.commandNo = CMD_IEEE_CCA_REQ; - if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_OK) { - /* Current RSSI in bits 23:16 of cmd_status */ - rssi = (cmd_status >> 16) & 0xFF; + if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_ERROR) { + PRINTF("get_rssi: CMDSTA=0x%08lx\n", cmd_status); + + /* Make sure to return RSSI unknown */ + cmd.currentRssi = RF_CMD_CCA_REQ_RSSI_UNKNOWN; + break; + } } /* If we were off, turn back off */ @@ -412,7 +417,7 @@ get_rssi(void) off(); } - return rssi; + return cmd.currentRssi; } /*---------------------------------------------------------------------------*/ /* Returns the current TX power in dBm */ diff --git a/cpu/cc26xx-cc13xx/rf-core/prop-mode.c b/cpu/cc26xx-cc13xx/rf-core/prop-mode.c index 94aef33b9..902eb04ea 100644 --- a/cpu/cc26xx-cc13xx/rf-core/prop-mode.c +++ b/cpu/cc26xx-cc13xx/rf-core/prop-mode.c @@ -271,6 +271,7 @@ get_rssi(void) { uint32_t cmd_status; int8_t rssi; + uint8_t attempts = 0; uint8_t was_off = 0; rfc_CMD_GET_RSSI_t cmd; @@ -283,14 +284,19 @@ get_rssi(void) } } - memset(&cmd, 0x00, sizeof(cmd)); - cmd.commandNo = CMD_GET_RSSI; - rssi = RF_CMD_CCA_REQ_RSSI_UNKNOWN; - if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_OK) { - /* Current RSSI in bits 23:16 of cmd_status */ - rssi = (cmd_status >> 16) & 0xFF; + while((rssi == RF_CMD_CCA_REQ_RSSI_UNKNOWN || rssi == 0) && ++attempts < 10) { + memset(&cmd, 0x00, sizeof(cmd)); + cmd.commandNo = CMD_GET_RSSI; + + if(rf_core_send_cmd((uint32_t)&cmd, &cmd_status) == RF_CORE_CMD_ERROR) { + PRINTF("get_rssi: CMDSTA=0x%08lx\n", cmd_status); + break; + } else { + /* Current RSSI in bits 23:16 of cmd_status */ + rssi = (cmd_status >> 16) & 0xFF; + } } /* If we were off, turn back off */