From 8b3e03fe72f31f135b20fea610c0f292c47c942d Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 1 Aug 2024 16:22:52 +0200 Subject: [PATCH 1/2] nimble/phy: Adjust timings for nRF5x phy Measured on nRF52 and nRF53 to have consistent results for all supported transitions. Due to rounding errors we're not able to achieve perfect 150us on all transitions, but we should be +/- 1us which is perfectly fine. --- nimble/drivers/nrf5x/src/ble_phy.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nimble/drivers/nrf5x/src/ble_phy.c b/nimble/drivers/nrf5x/src/ble_phy.c index ec5507c9fa..a6fff1f901 100644 --- a/nimble/drivers/nrf5x/src/ble_phy.c +++ b/nimble/drivers/nrf5x/src/ble_phy.c @@ -222,15 +222,15 @@ static const uint8_t g_ble_phy_t_rxenddelay[BLE_PHY_NUM_MODE] = { #else /* delay between EVENTS_READY and start of tx */ static const uint8_t g_ble_phy_t_txdelay[BLE_PHY_NUM_MODE] = { - [BLE_PHY_MODE_1M] = 4, - [BLE_PHY_MODE_2M] = 3, + [BLE_PHY_MODE_1M] = 6, + [BLE_PHY_MODE_2M] = 5, [BLE_PHY_MODE_CODED_125KBPS] = 5, [BLE_PHY_MODE_CODED_500KBPS] = 5 }; /* delay between EVENTS_END and end of txd packet */ static const uint8_t g_ble_phy_t_txenddelay[BLE_PHY_NUM_MODE] = { - [BLE_PHY_MODE_1M] = 4, - [BLE_PHY_MODE_2M] = 3, + [BLE_PHY_MODE_1M] = 6, + [BLE_PHY_MODE_2M] = 4, [BLE_PHY_MODE_CODED_125KBPS] = 9, [BLE_PHY_MODE_CODED_500KBPS] = 3 }; @@ -243,8 +243,8 @@ static const uint8_t g_ble_phy_t_rxaddrdelay[BLE_PHY_NUM_MODE] = { }; /* delay between end of rxd packet and EVENTS_END */ static const uint8_t g_ble_phy_t_rxenddelay[BLE_PHY_NUM_MODE] = { - [BLE_PHY_MODE_1M] = 6, - [BLE_PHY_MODE_2M] = 2, + [BLE_PHY_MODE_1M] = 4, + [BLE_PHY_MODE_2M] = 1, [BLE_PHY_MODE_CODED_125KBPS] = 27, [BLE_PHY_MODE_CODED_500KBPS] = 22 }; From afd00b7c9e8e0bdb8dff6c91c75bcc7631d6b5d6 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 1 Aug 2024 15:48:29 +0200 Subject: [PATCH 2/2] nimble/phy: Fix tx-tx transition timing Delay for event END was always included in tx-tx transition calculations which resulted in invalid calculations for tx-tx transition anchored at packet start. This fixes the issue and also fixes timing for event ADDRESS since it seems to be different that the one for END event. --- nimble/drivers/nrf5x/src/ble_phy.c | 38 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/nimble/drivers/nrf5x/src/ble_phy.c b/nimble/drivers/nrf5x/src/ble_phy.c index a6fff1f901..f556f65024 100644 --- a/nimble/drivers/nrf5x/src/ble_phy.c +++ b/nimble/drivers/nrf5x/src/ble_phy.c @@ -227,6 +227,13 @@ static const uint8_t g_ble_phy_t_txdelay[BLE_PHY_NUM_MODE] = { [BLE_PHY_MODE_CODED_125KBPS] = 5, [BLE_PHY_MODE_CODED_500KBPS] = 5 }; +/* delay between EVENTS_ADDRESS and txd access address */ +static const uint8_t g_ble_phy_t_txaddrdelay[BLE_PHY_NUM_MODE] = { + [BLE_PHY_MODE_1M] = 7, + [BLE_PHY_MODE_2M] = 5, + [BLE_PHY_MODE_CODED_125KBPS] = 17, + [BLE_PHY_MODE_CODED_500KBPS] = 17 +}; /* delay between EVENTS_END and end of txd packet */ static const uint8_t g_ble_phy_t_txenddelay[BLE_PHY_NUM_MODE] = { [BLE_PHY_MODE_1M] = 6, @@ -1104,25 +1111,24 @@ ble_phy_tx_end_isr(void) */ } else if (transition == BLE_PHY_TRANSITION_TX_TX) { if (g_ble_phy_data.txtx_time_anchor) { - /* Schedule next TX relative to current TX end. TX end timestamp is - * captured in CC[2]. - */ - tx_time = NRF_TIMER0->CC[2] + g_ble_phy_data.txtx_time_us; - } else { - /* Schedule next TX relative to current TX start. AA timestamp is - * captured in CC[1], we need to adjust for sync word to get TX - * start. - */ - tx_time = NRF_TIMER0->CC[1] - ble_ll_pdu_syncword_us(tx_phy_mode) + - g_ble_phy_data.txtx_time_us; - /* Adjust for delay between EVENT_ADDRESS and actual address TX time */ - /* FIXME assume this is the same as EVENT_END to end, but we should - * measure this to be sure */ + /* Calculate TX anchor relative to current TX end */ + + /* TX end timestamp is captured in CC[2] */ + tx_time = NRF_TIMER0->CC[2]; + /* Adjust for delay between EVENT_END and actual TX end time */ tx_time += g_ble_phy_t_txenddelay[tx_phy_mode]; + } else { + /* Calculate TX anchor relative to current TX start */ + + /* AA timestamp is captured in CC[1] */ + tx_time = NRF_TIMER0->CC[1]; + /* Adjust for delay between EVENT_ADDRESS and actual AA time ota */ + tx_time += g_ble_phy_t_txaddrdelay[tx_phy_mode]; + /* Adjust by sync word length to get TX start time */ + tx_time -= ble_ll_pdu_syncword_us(tx_phy_mode); } - /* Adjust for delay between EVENT_END and actual TX end time */ - tx_time += g_ble_phy_t_txenddelay[tx_phy_mode]; + tx_time += g_ble_phy_data.txtx_time_us; #if PHY_USE_FEM_PA fem_time = tx_time - MYNEWT_VAL(BLE_FEM_PA_TURN_ON_US);