Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ll: Rework how TX power is configured #1502

Merged
merged 2 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions nimble/controller/include/controller/ble_phy.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int ble_phy_init(void);

/* Set the PHY channel */
int ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit);
uint8_t ble_phy_chan_get(void);

#if MYNEWT_VAL(BLE_PHY_VARIABLE_TIFS)
/* Set T_ifs time for next transition */
Expand Down
14 changes: 12 additions & 2 deletions nimble/controller/src/ble_ll.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@

/* This is TX power on PHY (or FEM PA if enabled) */
int8_t g_ble_ll_tx_power;
static int8_t g_ble_ll_tx_power_phy_current;
int8_t g_ble_ll_tx_power_compensation;
int8_t g_ble_ll_rx_power_compensation;

Expand Down Expand Up @@ -1365,7 +1366,7 @@ ble_ll_task(void *arg)
/* Set output power to default */
g_ble_ll_tx_power = ble_ll_tx_power_round(MIN(MYNEWT_VAL(BLE_LL_TX_PWR_DBM),
MYNEWT_VAL(BLE_LL_TX_PWR_MAX_DBM)));
ble_ll_tx_power_set(g_ble_ll_tx_power);
g_ble_ll_tx_power_phy_current = INT8_MAX;

/* Tell the host that we are ready to receive packets */
ble_ll_hci_send_noop();
Expand Down Expand Up @@ -1617,7 +1618,7 @@ ble_ll_reset(void)
/* Set output power to default */
g_ble_ll_tx_power = ble_ll_tx_power_round(MIN(MYNEWT_VAL(BLE_LL_TX_PWR_DBM),
MYNEWT_VAL(BLE_LL_TX_PWR_MAX_DBM)));
ble_ll_tx_power_set(g_ble_ll_tx_power);
g_ble_ll_tx_power_phy_current = INT8_MAX;

/* FLush all packets from Link layer queues */
ble_ll_flush_pkt_queue(&g_ble_ll_data.ll_tx_pkt_q);
Expand Down Expand Up @@ -2011,6 +2012,15 @@ ble_ll_tx_power_set(int tx_power)
tx_power -= MYNEWT_VAL(BLE_FEM_PA_GAIN);
#endif
#endif

/* If current TX power configuration matches requested one we don't need
* to update PHY tx power.
*/
if (g_ble_ll_tx_power_phy_current == tx_power) {
return;
}

g_ble_ll_tx_power_phy_current = tx_power;
ble_phy_tx_power_set(tx_power);
}

Expand Down
26 changes: 9 additions & 17 deletions nimble/controller/src/ble_ll_adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,9 +1030,6 @@ ble_ll_adv_tx_done(void *arg)
{
struct ble_ll_adv_sm *advsm;

/* reset power to default after advertising */
ble_ll_tx_power_set(g_ble_ll_tx_power);

advsm = (struct ble_ll_adv_sm *)arg;

ble_ll_trace_u32x2(BLE_LL_TRACE_ID_ADV_TXDONE, advsm->adv_instance,
Expand Down Expand Up @@ -1111,9 +1108,6 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
goto adv_tx_done;
}

/* Set the power */
ble_ll_tx_power_set(advsm->tx_power);

/* Set channel */
rc = ble_phy_setchan(advsm->adv_chan, BLE_ACCESS_ADDR_ADV, BLE_LL_CRCINIT_ADV);
BLE_LL_ASSERT(rc == 0);
Expand All @@ -1131,6 +1125,9 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
#endif
#endif

/* Set the power */
ble_ll_tx_power_set(advsm->tx_power);

/* Set transmit start time. */
txstart = sch->start_time + g_ble_ll_sched_offset_ticks;
rc = ble_phy_tx_set_start_time(txstart, sch->remainder);
Expand Down Expand Up @@ -1255,9 +1252,6 @@ ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch)

ble_ll_adv_active_chanset_set_sec(advsm);

/* Set the power */
ble_ll_tx_power_set(advsm->tx_power);

/* Set channel */
aux = AUX_CURRENT(advsm);
rc = ble_phy_setchan(aux->chan, BLE_ACCESS_ADDR_ADV,
Expand All @@ -1269,6 +1263,9 @@ ble_ll_adv_secondary_tx_start_cb(struct ble_ll_sched_item *sch)
ble_phy_mode_set(advsm->sec_phy, advsm->sec_phy);
#endif

/* Set the power */
ble_ll_tx_power_set(advsm->tx_power);

/* Set transmit start time. */
txstart = sch->start_time + g_ble_ll_sched_offset_ticks;
rc = ble_phy_tx_set_start_time(txstart, sch->remainder);
Expand Down Expand Up @@ -1741,8 +1738,6 @@ ble_ll_adv_halt(void)

ble_ll_trace_u32(BLE_LL_TRACE_ID_ADV_HALT, advsm->adv_instance);

ble_ll_tx_power_set(g_ble_ll_tx_power);

#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PERIODIC_ADV)
if (advsm->flags & BLE_LL_ADV_SM_FLAG_PERIODIC_SYNC_SENDING) {
ble_ll_adv_flags_clear(advsm,
Expand Down Expand Up @@ -2176,9 +2171,6 @@ ble_ll_adv_sync_pdu_make(uint8_t *dptr, void *pducb_arg, uint8_t *hdr_byte)
static void
ble_ll_adv_sync_tx_done(struct ble_ll_adv_sm *advsm)
{
/* reset power to default after advertising */
ble_ll_tx_power_set(g_ble_ll_tx_power);

/* for sync we trace a no pri nor sec set */
ble_ll_trace_u32x2(BLE_LL_TRACE_ID_ADV_TXDONE, advsm->adv_instance, 0);

Expand Down Expand Up @@ -2232,9 +2224,6 @@ ble_ll_adv_sync_tx_start_cb(struct ble_ll_sched_item *sch)
ble_ll_adv_active_chanset_clear(advsm);
ble_ll_adv_flags_set(advsm, BLE_LL_ADV_SM_FLAG_PERIODIC_SYNC_SENDING);

/* Set the power */
ble_ll_tx_power_set(advsm->tx_power);

/* Set channel */
sync = SYNC_CURRENT(advsm);
rc = ble_phy_setchan(sync->chan, advsm->periodic_access_addr,
Expand All @@ -2247,6 +2236,9 @@ ble_ll_adv_sync_tx_start_cb(struct ble_ll_sched_item *sch)
ble_phy_mode_set(advsm->sec_phy, advsm->sec_phy);
#endif

/* Set the power */
ble_ll_tx_power_set(advsm->tx_power);

/* Set transmit start time. */
txstart = sch->start_time + g_ble_ll_sched_offset_ticks;
rc = ble_phy_tx_set_start_time(txstart, sch->remainder);
Expand Down
3 changes: 3 additions & 0 deletions nimble/controller/src/ble_ll_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,9 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
ble_phy_mode_set(connsm->phy_data.tx_phy_mode, connsm->phy_data.rx_phy_mode);
#endif

/* Set the power */
ble_ll_tx_power_set(g_ble_ll_tx_power);

switch (connsm->conn_role) {
#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL)
case BLE_LL_CONN_ROLE_CENTRAL:
Expand Down
2 changes: 0 additions & 2 deletions nimble/controller/src/ble_ll_hci_vs.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ ble_ll_hci_vs_set_tx_power(uint16_t ocf, const uint8_t *cmdbuf, uint8_t cmdlen,
g_ble_ll_tx_power_compensation);
}

ble_ll_tx_power_set(g_ble_ll_tx_power);

rsp->tx_power = g_ble_ll_tx_power + g_ble_ll_tx_power_compensation;
*rsplen = sizeof(*rsp);

Expand Down
7 changes: 7 additions & 0 deletions nimble/controller/src/ble_ll_scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,13 @@ ble_ll_scan_start(struct ble_ll_scan_sm *scansm)
ble_phy_mode_set(phy_mode, phy_mode);
#endif

/* if scan is not passive we need to set tx power as we may end up sending
* package
*/
if (scansm->scanp->scan_type != BLE_SCAN_TYPE_PASSIVE) {
ble_ll_tx_power_set(g_ble_ll_tx_power);
}

rc = ble_phy_rx_set_start_time(ble_ll_tmr_get() +
g_ble_ll_sched_offset_ticks, 0);
if (!rc || rc == BLE_PHY_ERR_RX_LATE) {
Expand Down
8 changes: 8 additions & 0 deletions nimble/controller/src/ble_ll_scan_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ ble_ll_scan_aux_sched_cb(struct ble_ll_sched_item *sch)
ble_phy_mode_set(phy_mode, phy_mode);
#endif

/* if scan is not passive we need to set tx power as we may end up sending
* package
*/
/* TODO do this only on first AUX? */
if (aux->scan_type != BLE_SCAN_TYPE_PASSIVE) {
ble_ll_tx_power_set(g_ble_ll_tx_power);
}

rc = ble_phy_rx_set_start_time(sch->start_time + g_ble_ll_sched_offset_ticks,
sch->remainder);
if (rc != 0 && rc != BLE_PHY_ERR_RX_LATE) {
Expand Down
6 changes: 6 additions & 0 deletions nimble/drivers/dialog_cmac/src/ble_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,12 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crc_init)
return 0;
}

uint8_t
ble_phy_chan_get(void)
{
return g_ble_phy_data.channel;
}

void
ble_phy_restart_rx(void)
{
Expand Down
6 changes: 6 additions & 0 deletions nimble/drivers/native/src/ble_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
return 0;
}

uint8_t
ble_phy_chan_get(void)
{
return g_ble_phy_data.phy_chan;
}

/**
* Disable the PHY. This will do the following:
* -> Turn off all phy interrupts.
Expand Down
6 changes: 6 additions & 0 deletions nimble/drivers/nrf51/src/ble_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,12 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
return 0;
}

uint8_t
ble_phy_chan_get(void)
{
return g_ble_phy_data.phy_chan;
}

/**
* Stop the timer used to count microseconds when using RTC for cputime
*/
Expand Down
6 changes: 6 additions & 0 deletions nimble/drivers/nrf5x/src/ble_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,12 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crcinit)
return 0;
}

uint8_t
ble_phy_chan_get(void)
{
return g_ble_phy_data.phy_chan;
}

/**
* Stop the timer used to count microseconds when using RTC for cputime
*/
Expand Down