Skip to content

Commit b4af225

Browse files
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 <[email protected]>
1 parent c29b277 commit b4af225

File tree

14 files changed

+440
-284
lines changed

14 files changed

+440
-284
lines changed

nimble/controller/include/controller/ble_phy.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ struct os_mbuf;
6363

6464
/* BLE PHY transitions */
6565
#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)
66+
#define BLE_PHY_TRANSITION_TO_TX (1)
67+
#define BLE_PHY_TRANSITION_TO_RX (2)
68+
69+
/* TIFS anchor point */
70+
#define BLE_PHY_TIFS_ANCHOR_START (0)
71+
#define BLE_PHY_TIFS_ANCHOR_END (1)
6972

7073
/* PHY error codes */
7174
#define BLE_PHY_ERR_RADIO_STATE (1)
@@ -80,6 +83,7 @@ struct os_mbuf;
8083

8184
/* Wait for response timer */
8285
typedef void (*ble_phy_tx_end_func)(void *arg);
86+
typedef void (*ble_phy_rx_end_func)(void *arg);
8387

8488
/* Initialize the PHY */
8589
int ble_phy_init(void);
@@ -88,14 +92,11 @@ int ble_phy_init(void);
8892
int ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit);
8993
uint8_t ble_phy_chan_get(void);
9094

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+
/* Set T_ifs for the next transition */
96+
void ble_phy_tifs_set(uint16_t usecs, uint8_t anchor);
9597

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);
98+
/* Set direction of the next transition */
99+
void ble_phy_transition_set(uint8_t trans, uint8_t anchor, uint16_t usecs);
99100

100101
/* Set transmit start time */
101102
int ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs);
@@ -110,7 +111,7 @@ typedef uint8_t (*ble_phy_tx_pducb_t)(uint8_t *dptr, void *pducb_arg,
110111
uint8_t *hdr_byte);
111112

112113
/* Place the PHY into transmit mode */
113-
int ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans);
114+
int ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg);
114115

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

nimble/controller/src/ble_ll_adv.c

+16-17
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,6 @@ static int
11211121
ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
11221122
{
11231123
int rc;
1124-
uint8_t end_trans;
11251124
uint32_t txstart;
11261125
struct ble_ll_adv_sm *advsm;
11271126

@@ -1183,18 +1182,18 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
11831182
if ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_LEGACY) &&
11841183
((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) ||
11851184
(advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE))) {
1186-
end_trans = BLE_PHY_TRANSITION_TX_RX;
1185+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_PHY_TIFS_ANCHOR_END, BLE_LL_IFS);
11871186
ble_phy_set_txend_cb(NULL, NULL);
11881187
} else {
1189-
end_trans = BLE_PHY_TRANSITION_NONE;
1188+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0, 0);
11901189
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
11911190
}
11921191

11931192
/* Transmit advertisement */
11941193
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
1195-
rc = ble_phy_tx(ble_ll_adv_pdu_make, advsm, end_trans);
1194+
rc = ble_phy_tx(ble_ll_adv_pdu_make, advsm);
11961195
#else
1197-
rc = ble_phy_tx(ble_ll_adv_legacy_pdu_make, advsm, end_trans);
1196+
rc = ble_phy_tx(ble_ll_adv_legacy_pdu_make, advsm);
11981197
#endif
11991198
if (rc) {
12001199
goto adv_tx_done;
@@ -1268,7 +1267,6 @@ static int
12681267
ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch)
12691268
{
12701269
int rc;
1271-
uint8_t end_trans;
12721270
uint32_t txstart;
12731271
struct ble_ll_adv_sm *advsm;
12741272
ble_phy_tx_pducb_t pducb;
@@ -1319,22 +1317,22 @@ ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch)
13191317

13201318
/* Set phy mode based on type of advertisement */
13211319
if (advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_CONNECTABLE) {
1322-
end_trans = BLE_PHY_TRANSITION_TX_RX;
1320+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_PHY_TIFS_ANCHOR_END, BLE_LL_IFS);
13231321
ble_phy_set_txend_cb(NULL, NULL);
13241322
pducb = ble_ll_adv_aux_pdu_make;
13251323
} else if ((advsm->props & BLE_HCI_LE_SET_EXT_ADV_PROP_SCANNABLE) &&
13261324
advsm->aux_first_pdu) {
1327-
end_trans = BLE_PHY_TRANSITION_TX_RX;
1325+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_PHY_TIFS_ANCHOR_END, BLE_LL_IFS);
13281326
ble_phy_set_txend_cb(NULL, NULL);
13291327
pducb = ble_ll_adv_aux_scannable_pdu_make;
13301328
} else {
1331-
end_trans = BLE_PHY_TRANSITION_NONE;
1329+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0, 0);
13321330
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
13331331
pducb = ble_ll_adv_aux_pdu_make;
13341332
}
13351333

13361334
/* Transmit advertisement */
1337-
rc = ble_phy_tx(pducb, advsm, end_trans);
1335+
rc = ble_phy_tx(pducb, advsm);
13381336
if (rc) {
13391337
goto adv_aux_dropped;
13401338
}
@@ -2322,8 +2320,9 @@ ble_ll_adv_sync_tx_start_cb(struct ble_ll_sched_item *sch)
23222320
#endif
23232321

23242322
/* Transmit advertisement */
2323+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0, 0);
23252324
ble_phy_set_txend_cb(ble_ll_adv_sync_tx_end, advsm);
2326-
rc = ble_phy_tx(ble_ll_adv_sync_pdu_make, advsm, BLE_PHY_TRANSITION_NONE);
2325+
rc = ble_phy_tx(ble_ll_adv_sync_pdu_make, advsm);
23272326
if (rc) {
23282327
goto adv_tx_done;
23292328
}
@@ -4547,6 +4546,8 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
45474546
*/
45484547
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
45494548

4549+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0, 0);
4550+
45504551
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
45514552
if (advsm->flags & BLE_LL_ADV_SM_FLAG_SCAN_REQ_NOTIF) {
45524553
ble_ll_hci_ev_send_scan_req_recv(advsm->adv_instance, peer,
@@ -4560,12 +4561,10 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
45604561
*/
45614562

45624563
advsm->rx_ble_hdr = ble_hdr;
4563-
rc = ble_phy_tx(ble_ll_adv_scan_rsp_pdu_make, advsm,
4564-
BLE_PHY_TRANSITION_NONE);
4564+
rc = ble_phy_tx(ble_ll_adv_scan_rsp_pdu_make, advsm);
45654565
advsm->rx_ble_hdr = NULL;
45664566
#else
4567-
rc = ble_phy_tx(ble_ll_adv_scan_rsp_legacy_pdu_make, advsm,
4568-
BLE_PHY_TRANSITION_NONE);
4567+
rc = ble_phy_tx(ble_ll_adv_scan_rsp_legacy_pdu_make, advsm);
45694568
#endif
45704569

45714570
if (!rc) {
@@ -4599,9 +4598,9 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
45994598
rsp_data.peer = rxbuf + BLE_LL_PDU_HDR_LEN;
46004599
rsp_data.rxadd = rxbuf[0] & BLE_ADV_PDU_HDR_TXADD_MASK;
46014600

4601+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0, 0);
46024602
ble_phy_set_txend_cb(ble_ll_adv_tx_done, advsm);
4603-
rc = ble_phy_tx(ble_ll_adv_aux_conn_rsp_pdu_make, &rsp_data,
4604-
BLE_PHY_TRANSITION_NONE);
4603+
rc = ble_phy_tx(ble_ll_adv_aux_conn_rsp_pdu_make, &rsp_data);
46054604
if (!rc) {
46064605
ble_ll_adv_flags_set(advsm, BLE_LL_ADV_SM_FLAG_CONN_RSP_TXD);
46074606
STATS_INC(ble_ll_stats, aux_conn_rsp_tx);

nimble/controller/src/ble_ll_conn.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ ble_ll_conn_tx_pdu(struct ble_ll_conn_sm *connsm)
10831083
int rc;
10841084
uint8_t md;
10851085
uint8_t hdr_byte;
1086-
uint8_t end_transition;
1086+
uint8_t transition;
10871087
uint8_t cur_txlen;
10881088
uint16_t next_txlen;
10891089
uint16_t cur_offset;
@@ -1343,11 +1343,11 @@ ble_ll_conn_tx_pdu(struct ble_ll_conn_sm *connsm)
13431343
((connsm->last_rxd_hdr_byte & BLE_LL_DATA_HDR_MD_MASK) == 0) &&
13441344
!ble_ll_ctrl_is_terminate_ind(hdr_byte, m->om_data[0]))) {
13451345
/* We will end the connection event */
1346-
end_transition = BLE_PHY_TRANSITION_NONE;
1346+
transition = BLE_PHY_TRANSITION_NONE;
13471347
txend_func = ble_ll_conn_wait_txend;
13481348
} else {
13491349
/* Wait for a response here */
1350-
end_transition = BLE_PHY_TRANSITION_TX_RX;
1350+
transition = BLE_PHY_TRANSITION_TO_RX;
13511351
txend_func = NULL;
13521352
}
13531353

@@ -1435,9 +1435,9 @@ ble_ll_conn_tx_pdu(struct ble_ll_conn_sm *connsm)
14351435
}
14361436
#endif
14371437

1438-
/* Set transmit end callback */
1438+
ble_phy_transition_set(transition, BLE_PHY_TIFS_ANCHOR_END, BLE_LL_IFS);
14391439
ble_phy_set_txend_cb(txend_func, connsm);
1440-
rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, m, end_transition);
1440+
rc = ble_phy_tx(ble_ll_tx_mbuf_pducb, m);
14411441
if (!rc) {
14421442
/* Log transmit on connection state */
14431443
cur_txlen = ble_hdr->txinfo.pyld_len;
@@ -3269,9 +3269,11 @@ ble_ll_conn_send_connect_req(struct os_mbuf *rxpdu,
32693269
ble_ll_conn_prepare_connect_ind(connsm, ble_ll_scan_get_pdu_data(), addrd,
32703270
rxhdr->rxinfo.channel);
32713271

3272+
ble_phy_transition_set(ext ? BLE_PHY_TRANSITION_TO_RX :
3273+
BLE_PHY_TRANSITION_NONE,
3274+
BLE_PHY_TIFS_ANCHOR_END, BLE_LL_IFS);
32723275
ble_phy_set_txend_cb(NULL, NULL);
3273-
rc = ble_phy_tx(ble_ll_conn_tx_connect_ind_pducb, connsm,
3274-
ext ? BLE_PHY_TRANSITION_TX_RX : BLE_PHY_TRANSITION_NONE);
3276+
rc = ble_phy_tx(ble_ll_conn_tx_connect_ind_pducb, connsm);
32753277
if (rc) {
32763278
ble_ll_conn_send_connect_req_cancel();
32773279
return -1;

nimble/controller/src/ble_ll_dtm.c

+2-1
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, 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

+9-10
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ ble_ll_iso_big_control_tx(struct ble_ll_iso_big *big)
688688
ble_phy_set_txend_cb(ble_ll_iso_big_control_txend_cb, big);
689689
ble_phy_setchan(chan_idx, big->ctrl_aa, big->crc_init << 8);
690690

691-
rc = ble_phy_tx(ble_ll_iso_big_control_pdu_cb, big, BLE_PHY_TRANSITION_NONE);
691+
ble_phy_transition_set(BLE_PHY_TRANSITION_NONE, 0, 0);
692+
rc = ble_phy_tx(ble_ll_iso_big_control_pdu_cb, big);
692693

693694
return rc;
694695
}
@@ -773,9 +774,13 @@ ble_ll_iso_big_subevent_tx(struct ble_ll_iso_big *big)
773774

774775
to_tx = (big->tx.subevents_rem > 1) || big->cstf;
775776

776-
rc = ble_phy_tx(ble_ll_iso_big_subevent_pdu_cb, big,
777-
to_tx ? BLE_PHY_TRANSITION_TX_TX
778-
: BLE_PHY_TRANSITION_NONE);
777+
ble_phy_transition_set(to_tx ? BLE_PHY_TRANSITION_TO_TX :
778+
BLE_PHY_TRANSITION_NONE,
779+
BLE_PHY_TIFS_ANCHOR_START,
780+
big->interleaved ? big->bis_spacing :
781+
big->sub_interval);
782+
783+
rc = ble_phy_tx(ble_ll_iso_big_subevent_pdu_cb, big);
779784
return rc;
780785
}
781786

@@ -874,12 +879,6 @@ ble_ll_iso_big_event_sched_cb(struct ble_ll_sched_item *sch)
874879
big->tx.bis = STAILQ_FIRST(&big->bis_q);
875880
big->tx.bis->tx.subevent_num = 1;
876881

877-
if (big->interleaved) {
878-
ble_phy_tifs_txtx_set(big->bis_spacing, 0);
879-
} else {
880-
ble_phy_tifs_txtx_set(big->sub_interval, 0);
881-
}
882-
883882
rc = ble_phy_tx_set_start_time(sch->start_time + g_ble_ll_sched_offset_ticks,
884883
sch->remainder);
885884
if (rc) {

nimble/controller/src/ble_ll_scan.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,8 @@ ble_ll_scan_send_scan_req(uint8_t pdu_type, uint8_t *rxbuf,
15631563
ble_ll_scan_req_pdu_prepare(scansm, addrd->adva, addrd->adva_type,
15641564
rpa_index);
15651565

1566-
rc = ble_phy_tx(ble_ll_scan_req_tx_pdu_cb, scansm, BLE_PHY_TRANSITION_TX_RX);
1566+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_PHY_TIFS_ANCHOR_END, BLE_LL_IFS);
1567+
rc = ble_phy_tx(ble_ll_scan_req_tx_pdu_cb, scansm);
15671568
if (rc) {
15681569
return false;
15691570
}

nimble/controller/src/ble_ll_scan_aux.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1226,8 +1226,8 @@ ble_ll_scan_aux_send_scan_req(struct ble_ll_scan_aux_data *aux,
12261226
* interrupted if scheduler kicks in.
12271227
*/
12281228

1229-
rc = ble_phy_tx(ble_ll_scan_aux_scan_req_tx_pdu_cb, aux,
1230-
BLE_PHY_TRANSITION_TX_RX);
1229+
ble_phy_transition_set(BLE_PHY_TRANSITION_TO_RX, BLE_PHY_TIFS_ANCHOR_END, BLE_LL_IFS);
1230+
rc = ble_phy_tx(ble_ll_scan_aux_scan_req_tx_pdu_cb, aux);
12311231
if (rc) {
12321232
return false;
12331233
}

nimble/drivers/dialog_cmac/src/ble_phy.c

+10-6
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, uint8_t anchor, uint16_t usecs)
1833+
{
1834+
g_ble_phy_data.end_transition = trans;
1835+
}

nimble/drivers/native/src/ble_phy.c

+4-6
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, uint8_t anchor, uint16_t usecs)
666663
{
664+
g_ble_phy_data.phy_transition = trans;
667665
}

0 commit comments

Comments
 (0)