From c33701ae80cfe3d23ba7ae02d6328fec2b4ec209 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Tue, 6 Mar 2018 10:20:04 +0100 Subject: [PATCH] nimble/phy: Fix receiver configuration on late RX If we are late when scheduling RX, receiver is enabled anyway since we may just want to scan for "anything". However, current code only enables receiver but does not configure it so the results seem unpredictable. This patch fixes this by ensuring that radio is properly configured for RX in both cases. X-Original-Commit: 068c09a8da97bc62eddd5084fa1e40f910abf7e8 --- nimble/drivers/nrf52/src/ble_phy.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/nimble/drivers/nrf52/src/ble_phy.c b/nimble/drivers/nrf52/src/ble_phy.c index adae7f054..85909293a 100644 --- a/nimble/drivers/nrf52/src/ble_phy.c +++ b/nimble/drivers/nrf52/src/ble_phy.c @@ -1443,6 +1443,7 @@ ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs) int ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs) { + bool late = false; int rc = 0; /* XXX: This should not be necessary, but paranoia is good! */ @@ -1451,16 +1452,29 @@ ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs) if (ble_phy_set_start_time(cputime, rem_usecs, false) != 0) { STATS_INC(ble_phy_stats, rx_late); + + /* + * Disable PPI so ble_phy_rx() will start RX immediately after + * configuring receiver. + */ NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk; - NRF_RADIO->TASKS_RXEN = 1; - rc = BLE_PHY_ERR_RX_LATE; + late = true; } else { /* Enable PPI to automatically start RXEN */ NRF_PPI->CHENSET = PPI_CHEN_CH21_Msk; - - /* Start rx */ - rc = ble_phy_rx(); } + + /* Start rx */ + rc = ble_phy_rx(); + + /* + * If we enabled receiver but were late, let's return proper error code so + * caller can handle this. + */ + if (!rc && late) { + rc = BLE_PHY_ERR_RX_LATE; + } + return rc; }