Skip to content

Commit e5ea1fa

Browse files
mkasenbergandrzej-kaczmarek
authored andcommitted
nimble/ll: Refactor transitions
New Bluetooth features require fast RX-to-RX and TX-to-TX transitions and use a different TIFS than 150us. Co-authored-by: Andrzej Kaczmarek <andrzej.kaczmarek@codecoup.pl>
1 parent 2625469 commit e5ea1fa

File tree

11 files changed

+354
-276
lines changed

11 files changed

+354
-276
lines changed

nimble/controller/include/controller/ble_phy.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ struct os_mbuf;
6262
#define BLE_PHY_STATE_TX (2)
6363

6464
/* BLE PHY transitions */
65-
#define BLE_PHY_TRANSITION_NONE (0)
66-
#define BLE_PHY_TRANSITION_RX_TX (1)
67-
#define BLE_PHY_TRANSITION_TX_RX (2)
68-
#define BLE_PHY_TRANSITION_TX_TX (3)
65+
#define BLE_PHY_TRANSITION_NONE (0)
66+
#define BLE_PHY_TRANSITION_TO_TX (1)
67+
#define BLE_PHY_TRANSITION_TO_RX (2)
68+
#define BLE_PHY_TRANSITION_TO_TX_ISO_SUBEVENT (3)
69+
#define BLE_PHY_TRANSITION_TO_RX_ISO_SUBEVENT (4)
6970

7071
/* PHY error codes */
7172
#define BLE_PHY_ERR_RADIO_STATE (1)
@@ -88,14 +89,8 @@ int ble_phy_init(void);
8889
int ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit);
8990
uint8_t ble_phy_chan_get(void);
9091

91-
#if MYNEWT_VAL(BLE_PHY_VARIABLE_TIFS)
92-
/* Set T_ifs time for next transition */
93-
void ble_phy_tifs_set(uint16_t tifs);
94-
#endif
95-
96-
/* Set T_ifs for tx-tx transitions. Anchor is 0 for start of previous PDU,
97-
* non-zero for end of PDU */
98-
void ble_phy_tifs_txtx_set(uint16_t usecs, uint8_t anchor);
92+
/* Set direction of the next transition */
93+
void ble_phy_transition_set(uint8_t trans, uint16_t usecs);
9994

10095
/* Set transmit start time */
10196
int ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
@@ -110,7 +105,7 @@ typedef uint8_t (*ble_phy_tx_pducb_t)(uint8_t *dptr, void *pducb_arg,
110105
uint8_t *hdr_byte);
111106

112107
/* Place the PHY into transmit mode */
113-
int ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans);
108+
int ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg);
114109

115110
/* Copies the received PHY buffer into the allocated pdu */
116111
void ble_phy_rxpdu_copy(uint8_t *dptr, struct os_mbuf *rxpdu);

nimble/controller/src/ble_ll_adv.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,6 @@ static int
11261126
ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
11271127
{
11281128
int rc;
1129-
uint8_t end_trans;
11301129
uint32_t txstart;
11311130
struct ble_ll_adv_sm *advsm;
11321131
#if MYNEWT_VAL(BLE_LL_PHY) && MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
@@ -1196,18 +1195,18 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
11961195
if ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) &&
11971196
((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) ||
11981197
(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE))) {
1199-
end_trans = BLE_PHY_TRANSITION_TX_RX;
1198+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_LL_IFS);
12001199
ble_phy_set_txend_cb(NULL, NULL);
12011200
} else {
1202-
end_trans = BLE_PHY_TRANSITION_NONE;
1201+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0);
12031202
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
12041203
}
12051204

12061205
/* Transmit advertisement */
12071206
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
1208-
rc = ble_phy_tx(ble_ll_adv_pdu_make, advsm, end_trans);
1207+
rc = ble_phy_tx(ble_ll_adv_pdu_make, advsm);
12091208
#else
1210-
rc = ble_phy_tx(ble_ll_adv_legacy_pdu_make, advsm, end_trans);
1209+
rc = ble_phy_tx(ble_ll_adv_legacy_pdu_make, advsm);
12111210
#endif
12121211
if (rc) {
12131212
goto adv_tx_done;
@@ -1281,7 +1280,6 @@ static int
12811280
ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch)
12821281
{
12831282
int rc;
1284-
uint8_t end_trans;
12851283
uint32_t txstart;
12861284
struct ble_ll_adv_sm *advsm;
12871285
ble_phy_tx_pducb_t pducb;
@@ -1340,22 +1338,22 @@ ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch)
13401338

13411339
/* Set phy mode based on type of advertisement */
13421340
if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
1343-
end_trans = BLE_PHY_TRANSITION_TX_RX;
1341+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_LL_IFS);
13441342
ble_phy_set_txend_cb(NULL, NULL);
13451343
pducb = ble_ll_adv_aux_pdu_make;
13461344
} else if ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) &&
13471345
advsm->aux_first_pdu) {
1348-
end_trans = BLE_PHY_TRANSITION_TX_RX;
1346+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_LL_IFS);
13491347
ble_phy_set_txend_cb(NULL, NULL);
13501348
pducb = ble_ll_adv_aux_scannable_pdu_make;
13511349
} else {
1352-
end_trans = BLE_PHY_TRANSITION_NONE;
1350+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0);
13531351
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
13541352
pducb = ble_ll_adv_aux_pdu_make;
13551353
}
13561354

13571355
/* Transmit advertisement */
1358-
rc = ble_phy_tx(pducb, advsm, end_trans);
1356+
rc = ble_phy_tx(pducb, advsm);
13591357
if (rc) {
13601358
goto adv_aux_dropped;
13611359
}
@@ -2343,8 +2341,9 @@ ble_ll_adv_sync_tx_start_cb(struct ble_ll_sched_item *sch)
23432341
#endif
23442342

23452343
/* Transmit advertisement */
2344+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0);
23462345
ble_phy_set_txend_cb(ble_ll_adv_sync_tx_end, advsm);
2347-
rc = ble_phy_tx(ble_ll_adv_sync_pdu_make, advsm, BLE_PHY_TRANSITION_NONE);
2346+
rc = ble_phy_tx(ble_ll_adv_sync_pdu_make, advsm);
23482347
if (rc) {
23492348
goto adv_tx_done;
23502349
}
@@ -4568,6 +4567,8 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
45684567
*/
45694568
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
45704569

4570+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0);
4571+
45714572
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
45724573
if (advsm->flags & BLE_LL_ADV_SM_FLAG_SCAN_REQ_NOTIF) {
45734574
ble_ll_hci_ev_send_scan_req_recv(advsm->adv_instance, peer,
@@ -4581,12 +4582,10 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
45814582
*/
45824583

45834584
advsm->rx_ble_hdr = ble_hdr;
4584-
rc = ble_phy_tx(ble_ll_adv_scan_rsp_pdu_make, advsm,
4585-
BLE_PHY_TRANSITION_NONE);
4585+
rc = ble_phy_tx(ble_ll_adv_scan_rsp_pdu_make, advsm);
45864586
advsm->rx_ble_hdr = NULL;
45874587
#else
4588-
rc = ble_phy_tx(ble_ll_adv_scan_rsp_legacy_pdu_make, advsm,
4589-
BLE_PHY_TRANSITION_NONE);
4588+
rc = ble_phy_tx(ble_ll_adv_scan_rsp_legacy_pdu_make, advsm);
45904589
#endif
45914590

45924591
if (!rc) {
@@ -4620,9 +4619,9 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
46204619
rsp_data.peer = rxbuf + BLE_LL_PDU_HDR_LEN;
46214620
rsp_data.rxadd = rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK;
46224621

4622+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0);
46234623
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
4624-
rc = ble_phy_tx(ble_ll_adv_aux_conn_rsp_pdu_make, &rsp_data,
4625-
BLE_PHY_TRANSITION_NONE);
4624+
rc = ble_phy_tx(ble_ll_adv_aux_conn_rsp_pdu_make, &rsp_data);
46264625
if (!rc) {
46274626
ble_ll_adv_flags_set(advsm, BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD);
46284627
STATS_INC(ble_ll_stats, aux_conn_rsp_tx);
@@ -4913,6 +4912,8 @@ ble_ll_adv_rx_isr_start(uint8_t pdu_type)
49134912
*/
49144913
if (rc < 0) {
49154914
ble_ll_adv_tx_done(advsm);
4915+
} else if (rc > 0){
4916+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_TX, BLE_LL_IFS);
49164917
}
49174918

49184919
return rc;

nimble/controller/src/ble_ll_conn.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ ble_ll_conn_tx_pdu(struct ble_ll_conn_sm *connsm)
13481348
txend_func = ble_ll_conn_wait_txend;
13491349
} else {
13501350
/* Wait for a response here */
1351-
end_transition = BLE_PHY_TRANSITION_TX_RX;
1351+
end_transition = BLE_PHY_TRANSITION_TO_RX;
13521352
txend_func = NULL;
13531353
}
13541354

@@ -1436,9 +1436,11 @@ ble_ll_conn_tx_pdu(struct ble_ll_conn_sm *connsm)
14361436
}
14371437
#endif
14381438

1439+
ble_phy_transition_set(end_transition, BLE_LL_IFS);
1440+
14391441
/* Set transmit end callback */
14401442
ble_phy_set_txend_cb(txend_func, connsm);
1441-
rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, m, end_transition);
1443+
rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, m);
14421444
if (!rc) {
14431445
/* Log transmit on connection state */
14441446
cur_txlen = ble_hdr->txinfo.pyld_len;
@@ -3275,9 +3277,11 @@ ble_ll_conn_send_connect_req(struct os_mbuf *rxpdu,
32753277
ble_ll_conn_prepare_connect_ind(connsm, ble_ll_scan_get_pdu_data(), addrd,
32763278
rxhdr->rxinfo.channel);
32773279

3280+
ble_phy_transition_set(ext ? BLE_PHY_TRANSITION_TO_RX :
3281+
BLE_PHY_TRANSITION_NONE,
3282+
BLE_LL_IFS);
32783283
ble_phy_set_txend_cb(NULL, NULL);
3279-
rc = ble_phy_tx(ble_ll_conn_tx_connect_ind_pducb, connsm,
3280-
ext ? BLE_PHY_TRANSITION_TX_RX : BLE_PHY_TRANSITION_NONE);
3284+
rc = ble_phy_tx(ble_ll_conn_tx_connect_ind_pducb, connsm);
32813285
if (rc) {
32823286
ble_ll_conn_send_connect_req_cancel();
32833287
return -1;

nimble/controller/src/ble_ll_dtm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ ble_ll_dtm_tx_sched_cb(struct ble_ll_sched_item *sch)
244244
goto resched;
245245
}
246246

247-
rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, ctx->om, BLE_PHY_TRANSITION_NONE);
247+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0);
248+
rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, ctx->om);
248249
if (rc) {
249250
goto resched;
250251
}

nimble/controller/src/ble_ll_iso_big.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ ble_ll_iso_big_control_tx(struct ble_ll_iso_big *big)
652652
ble_phy_set_txend_cb(ble_ll_iso_big_control_txend_cb, big);
653653
ble_phy_setchan(chan_idx, big->ctrl_aa, big->crc_init << 8);
654654

655-
rc = ble_phy_tx(ble_ll_iso_big_control_pdu_cb, big, BLE_PHY_TRANSITION_NONE);
655+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0);
656+
rc = ble_phy_tx(ble_ll_iso_big_control_pdu_cb, big);
656657

657658
return rc;
658659
}
@@ -737,9 +738,12 @@ ble_ll_iso_big_subevent_tx(struct ble_ll_iso_big *big)
737738

738739
to_tx = (big->tx.subevents_rem > 1) || big->cstf;
739740

740-
rc = ble_phy_tx(ble_ll_iso_big_subevent_pdu_cb, big,
741-
to_tx ? BLE_PHY_TRANSITION_TX_TX
742-
: BLE_PHY_TRANSITION_NONE);
741+
ble_phy_transition_set(to_tx ? BLE_PHY_TRANSITION_TO_TX_ISO_SUBEVENT :
742+
BLE_PHY_TRANSITION_NONE,
743+
big->interleaved ? big->bis_spacing :
744+
big->sub_interval);
745+
746+
rc = ble_phy_tx(ble_ll_iso_big_subevent_pdu_cb, big);
743747
return rc;
744748
}
745749

@@ -836,12 +840,6 @@ ble_ll_iso_big_event_sched_cb(struct ble_ll_sched_item *sch)
836840
big->tx.bis = STAILQ_FIRST(&big->bis_q);
837841
big->tx.bis->tx.subevent_num = 1;
838842

839-
if (big->interleaved) {
840-
ble_phy_tifs_txtx_set(big->bis_spacing, 0);
841-
} else {
842-
ble_phy_tifs_txtx_set(big->sub_interval, 0);
843-
}
844-
845843
rc = ble_phy_tx_set_start_time(sch->start_time + g_ble_ll_sched_offset_ticks,
846844
sch->remainder);
847845
if (rc) {

nimble/controller/src/ble_ll_scan.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,8 @@ ble_ll_scan_send_scan_req(uint8_t pdu_type, uint8_t *rxbuf,
15691569
ble_ll_scan_req_pdu_prepare(scansm, addrd->adva, addrd->adva_type,
15701570
rpa_index);
15711571

1572-
rc = ble_phy_tx(ble_ll_scan_req_tx_pdu_cb, scansm, BLE_PHY_TRANSITION_TX_RX);
1572+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_LL_IFS);
1573+
rc = ble_phy_tx(ble_ll_scan_req_tx_pdu_cb, scansm);
15731574
if (rc) {
15741575
return false;
15751576
}

nimble/controller/src/ble_ll_scan_aux.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,8 @@ ble_ll_scan_aux_send_scan_req(struct ble_ll_scan_aux_data *aux,
12061206
* interrupted if scheduler kicks in.
12071207
*/
12081208

1209-
rc = ble_phy_tx(ble_ll_scan_aux_scan_req_tx_pdu_cb, aux,
1210-
BLE_PHY_TRANSITION_TX_RX);
1209+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_LL_IFS);
1210+
rc = ble_phy_tx(ble_ll_scan_aux_scan_req_tx_pdu_cb, aux);
12111211
if (rc) {
12121212
return false;
12131213
}

nimble/drivers/dialog_cmac/src/ble_phy.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ ble_phy_irq_field_tx_exc_bs_start_4this(void)
580580
{
581581
CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_BS_START_4THIS_Msk;
582582

583-
if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TX_RX) {
583+
if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TO_RX) {
584584
/*
585585
* Setup 2nd frame that will start after current one.
586586
* -2us offset to adjust for allowed active clock accuracy.
@@ -642,7 +642,7 @@ ble_phy_irq_frame_tx_exc_bs_stop(void)
642642
/* Clear latched timestamp so we do not have error on next frame */
643643
(void)CMAC->CM_TS1_REG;
644644

645-
if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TX_RX) {
645+
if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TO_RX) {
646646
#if MYNEWT_VAL(BLE_LL_PHY)
647647
ble_phy_mode_apply(g_ble_phy_data.phy_mode_rx);
648648
#endif
@@ -665,7 +665,7 @@ ble_phy_irq_frame_tx_exc_phy_to_idle_4this(void)
665665
{
666666
CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_PHY_TO_IDLE_4THIS_Msk;
667667

668-
if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TX_RX) {
668+
if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TO_RX) {
669669
ble_phy_rx_setup_xcvr();
670670

671671
g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
@@ -1403,7 +1403,7 @@ ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
14031403
}
14041404

14051405
int
1406-
ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
1406+
ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg)
14071407
{
14081408
uint8_t *txbuf = g_ble_phy_tx_buf;
14091409
int rc;
@@ -1412,8 +1412,6 @@ ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
14121412

14131413
assert(CMAC->CM_FRAME_1_REG & CMAC_CM_FRAME_1_REG_FRAME_TX_Msk);
14141414

1415-
g_ble_phy_data.end_transition = end_trans;
1416-
14171415
/*
14181416
* Program required fields now so in worst case TX can continue while we
14191417
* are still preparing header and payload.
@@ -1829,3 +1827,9 @@ ble_phy_dtm_carrier(uint8_t rf_channel)
18291827
}
18301828
#endif
18311829
#endif
1830+
1831+
void
1832+
ble_phy_transition_set(uint8_t trans, uint16_t usecs)
1833+
{
1834+
g_ble_phy_data.end_transition = trans;
1835+
}

nimble/drivers/native/src/ble_phy.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ ble_phy_isr(void)
241241
ble_xcvr_clear_irq(BLE_XCVR_IRQ_F_TX_END);
242242

243243
transition = g_ble_phy_data.phy_transition;
244-
if (transition == BLE_PHY_TRANSITION_TX_RX) {
244+
if (transition == BLE_PHY_TRANSITION_TO_RX) {
245245
/* Disable the phy */
246246
/* XXX: count no bufs? */
247247
ble_phy_disable();
@@ -422,7 +422,7 @@ ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
422422

423423

424424
int
425-
ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
425+
ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg)
426426
{
427427
uint8_t hdr_byte;
428428
int rc;
@@ -440,9 +440,6 @@ ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
440440
} else {
441441
}
442442

443-
/* Set the PHY transition */
444-
g_ble_phy_data.phy_transition = end_trans;
445-
446443
/* Set phy state to transmitting and count packet statistics */
447444
g_ble_phy_data.phy_state = BLE_PHY_STATE_TX;
448445
++g_ble_phy_stats.tx_good;
@@ -662,6 +659,7 @@ ble_phy_rfclk_disable(void)
662659
}
663660

664661
void
665-
ble_phy_tifs_txtx_set(uint16_t usecs, uint8_t anchor)
662+
ble_phy_transition_set(uint8_t trans, uint16_t usecs)
666663
{
664+
g_ble_phy_data.phy_transition = trans;
667665
}

0 commit comments

Comments
 (0)