From 220a1caa0555bc9ff98e41c95314230d7378d9fb Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Tue, 17 Oct 2017 13:33:23 +0200 Subject: [PATCH 1/3] nimble/ll: Set phy mode before scheduling tx We need to set phy mode before scheduling tx as otherwise calculations in ble_phy will use invalid phy and can be off few usecs. X-Original-Commit: 9dc306abccf7d5ecd13e63bb9b681aa420776a85 --- nimble/controller/src/ble_ll_adv.c | 33 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/nimble/controller/src/ble_ll_adv.c b/nimble/controller/src/ble_ll_adv.c index 4f55f7f45..e134e6fcf 100644 --- a/nimble/controller/src/ble_ll_adv.c +++ b/nimble/controller/src/ble_ll_adv.c @@ -844,6 +844,19 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch) rc = ble_phy_setchan(advsm->adv_chan, BLE_ACCESS_ADDR_ADV, BLE_LL_CRCINIT_ADV); assert(rc == 0); +#if (BLE_LL_BT5_PHY_SUPPORTED == 1) + /* Set phy mode */ +#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV) + if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) { + ble_phy_mode_set(BLE_PHY_MODE_1M, BLE_PHY_MODE_1M); + } else { + ble_phy_mode_set(advsm->pri_phy, advsm->pri_phy); + } +#else + ble_phy_mode_set(BLE_PHY_MODE_1M, BLE_PHY_MODE_1M); +#endif +#endif + /* Set transmit start time. */ txstart = sch->start_time + g_ble_ll_sched_offset_ticks; rc = ble_phy_tx_set_start_time(txstart, sch->remainder); @@ -887,19 +900,8 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch) #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV) ble_ll_adv_pdu_make(advsm, adv_pdu); - -#if (BLE_LL_BT5_PHY_SUPPORTED == 1) - if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) { - ble_phy_mode_set(BLE_PHY_MODE_1M, BLE_PHY_MODE_1M); - } else { - ble_phy_mode_set(advsm->pri_phy, advsm->pri_phy); - } -#endif #else ble_ll_adv_legacy_pdu_make(advsm, adv_pdu); -#if (BLE_LL_BT5_PHY_SUPPORTED == 1) - ble_phy_mode_set(BLE_PHY_MODE_1M, BLE_PHY_MODE_1M); -#endif #endif /* Transmit advertisement */ @@ -999,6 +1001,11 @@ ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch) BLE_LL_CRCINIT_ADV); assert(rc == 0); +#if (BLE_LL_BT5_PHY_SUPPORTED == 1) + /* Set phy mode */ + ble_phy_mode_set(advsm->sec_phy, advsm->sec_phy); +#endif + /* Set transmit start time. */ txstart = sch->start_time + g_ble_ll_sched_offset_ticks; rc = ble_phy_tx_set_start_time(txstart, sch->remainder); @@ -1040,10 +1047,6 @@ ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch) ble_ll_adv_pdu_make(advsm, adv_pdu); -#if (BLE_LL_BT5_PHY_SUPPORTED == 1) - ble_phy_mode_set(advsm->sec_phy, advsm->sec_phy); -#endif - /* Transmit advertisement */ rc = ble_phy_tx(adv_pdu, end_trans); os_mbuf_free_chain(adv_pdu); From d9a7fb75e103c09ba077ec7257115170b136f5d1 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 19 Oct 2017 16:18:42 +0200 Subject: [PATCH 2/3] nimble/ll: Add stat for dropped adv event X-Original-Commit: f92d765ac68ff83a910287b2bea975e761b3d279 --- nimble/controller/include/controller/ble_ll.h | 1 + nimble/controller/src/ble_ll.c | 1 + nimble/controller/src/ble_ll_adv.c | 2 ++ 3 files changed, 4 insertions(+) diff --git a/nimble/controller/include/controller/ble_ll.h b/nimble/controller/include/controller/ble_ll.h index 7be6e4dd7..53e14e91e 100644 --- a/nimble/controller/include/controller/ble_ll.h +++ b/nimble/controller/include/controller/ble_ll.h @@ -156,6 +156,7 @@ STATS_SECT_START(ble_ll_stats) STATS_SECT_ENTRY(adv_txg) STATS_SECT_ENTRY(adv_late_starts) STATS_SECT_ENTRY(adv_resched_pdu_fail) + STATS_SECT_ENTRY(adv_drop_event) STATS_SECT_ENTRY(sched_state_conn_errs) STATS_SECT_ENTRY(sched_state_adv_errs) STATS_SECT_ENTRY(scan_starts) diff --git a/nimble/controller/src/ble_ll.c b/nimble/controller/src/ble_ll.c index ed36a8212..81fccc778 100644 --- a/nimble/controller/src/ble_ll.c +++ b/nimble/controller/src/ble_ll.c @@ -176,6 +176,7 @@ STATS_NAME_START(ble_ll_stats) STATS_NAME(ble_ll_stats, adv_txg) STATS_NAME(ble_ll_stats, adv_late_starts) STATS_NAME(ble_ll_stats, adv_resched_pdu_fail) + STATS_NAME(ble_ll_stats, adv_drop_event) STATS_NAME(ble_ll_stats, sched_state_conn_errs) STATS_NAME(ble_ll_stats, sched_state_adv_errs) STATS_NAME(ble_ll_stats, scan_starts) diff --git a/nimble/controller/src/ble_ll_adv.c b/nimble/controller/src/ble_ll_adv.c index e134e6fcf..e6c9d44ef 100644 --- a/nimble/controller/src/ble_ll_adv.c +++ b/nimble/controller/src/ble_ll_adv.c @@ -2616,6 +2616,8 @@ ble_ll_adv_rx_isr_start(uint8_t pdu_type) static void ble_ll_adv_drop_event(struct ble_ll_adv_sm *advsm) { + STATS_INC(ble_ll_stats, adv_drop_event); + ble_ll_sched_rmv_elem(&advsm->adv_sch); #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV) ble_ll_sched_rmv_elem(&advsm->adv_secondary_sch); From 4424cde7b31f3a12dd33949c4dcc27a32b0a19c0 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 19 Oct 2017 16:19:07 +0200 Subject: [PATCH 3/3] nimble/ll: Drop event if adv ext is delayed beyond aux Sometimes if things are delayed we may try to send adv ext after scheduled aux - in this case just drop entire event as it won't make much sense for receiver anyway. This can easily happen when connecting debugger to running platform. X-Original-Commit: 949fa18456278f7774016e68cdc6f294a955d3c6 --- nimble/controller/src/ble_ll_adv.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nimble/controller/src/ble_ll_adv.c b/nimble/controller/src/ble_ll_adv.c index e6c9d44ef..99bb24704 100644 --- a/nimble/controller/src/ble_ll_adv.c +++ b/nimble/controller/src/ble_ll_adv.c @@ -2782,6 +2782,16 @@ ble_ll_adv_done(struct ble_ll_adv_sm *advsm) */ advsm->adv_pdu_start_time = os_cputime_get32() + g_ble_ll_sched_offset_ticks; + +#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV) + /* If we're past aux (unlikely, but can happen), just drop an event */ + if (!(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) && + (advsm->adv_pdu_start_time > advsm->adv_secondary_start_time)) { + ble_ll_adv_drop_event(advsm); + return; + } +#endif + resched_pdu = 1; }