Skip to content

Commit 67d30b6

Browse files
nimble/phy/nrf: Move rx_end to disabled event
This moves handling of PDU RX end from EVENT_END to EVENT_DISABLED. This will allow to run our phy on BabbleSim and also seems to simplify code a bit. EVENT_DISABLED happens almost immediately after EVENT_END, so on real it happens while ble_phy_isr is being executed and is simply ignored. However, on BabbleSim code is executed as if time was paused which means that no other event can occur when ble_phy_isr is being executed, i.e. it will be called after we return from isr and simulation code can trigger subsequent events. If rx_end configures transition to TX and thus enables interrupt on EVENT_DISABLED, we will handle that during an event that is triggered for RX - basically we handle tx_end before TX even started. Using EVENT_DISABLED for both TX and RX resolves this issue. We still use timer capture on EVENT_END for transition timing so this has no effect on that calculations, and since EVENT_DISABLED occurs just a fraction of microsecond after EVENT_END, execution timings are also not affected.
1 parent a600943 commit 67d30b6

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

nimble/drivers/nrf52/src/ble_phy.c

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,6 @@ ble_phy_wfr_enable(int txrx, uint8_t tx_phy_mode, uint32_t wfr_usecs)
730730
/* Enable wait for response PPI */
731731
NRF_PPI->CHENSET = (PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk);
732732

733-
/* Enable the disabled interrupt so we time out on events compare */
734-
NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
735-
736733
/*
737734
* It may happen that if CPU is halted for a brief moment (e.g. during flash
738735
* erase or write), TIMER0 already counted past CC[3] and thus wfr will not
@@ -860,7 +857,8 @@ ble_phy_rx_xcvr_setup(void)
860857
RADIO_SHORTS_ADDRESS_RSSISTART_Msk |
861858
RADIO_SHORTS_DISABLED_RSSISTOP_Msk;
862859

863-
NRF_RADIO->INTENSET = RADIO_INTENSET_ADDRESS_Msk;
860+
NRF_RADIO->INTENSET = RADIO_INTENSET_ADDRESS_Msk |
861+
RADIO_INTENSET_DISABLED_Msk;
864862
}
865863

866864
/**
@@ -874,7 +872,6 @@ ble_phy_tx_end_isr(void)
874872
uint8_t was_encrypted;
875873
uint8_t transition;
876874
uint32_t rx_time;
877-
uint32_t wfr_time;
878875

879876
/* Store PHY on which we've just transmitted smth */
880877
tx_phy_mode = g_ble_phy_data.phy_cur_phy_mode;
@@ -886,13 +883,6 @@ ble_phy_tx_end_isr(void)
886883
/* Better be in TX state! */
887884
assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
888885

889-
/* Clear events and clear interrupt on disabled event */
890-
NRF_RADIO->EVENTS_DISABLED = 0;
891-
NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
892-
NRF_RADIO->EVENTS_END = 0;
893-
wfr_time = NRF_RADIO->SHORTS;
894-
(void)wfr_time;
895-
896886
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
897887
/*
898888
* XXX: not sure what to do. We had a HW error during transmission.
@@ -982,10 +972,6 @@ ble_phy_rx_end_isr(void)
982972
uint32_t tx_time;
983973
struct ble_mbuf_hdr *ble_hdr;
984974

985-
/* Clear events and clear interrupt */
986-
NRF_RADIO->EVENTS_END = 0;
987-
NRF_RADIO->INTENCLR = RADIO_INTENCLR_END_Msk;
988-
989975
/* Disable automatic RXEN */
990976
NRF_PPI->CHENCLR = PPI_CHEN_CH21_Msk;
991977

@@ -1116,9 +1102,9 @@ ble_phy_rx_start_isr(void)
11161102

11171103
/* Clear events and clear interrupt */
11181104
NRF_RADIO->EVENTS_ADDRESS = 0;
1105+
NRF_RADIO->INTENCLR = RADIO_INTENCLR_ADDRESS_Msk;
11191106

1120-
/* Clear wfr timer channels and DISABLED interrupt */
1121-
NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk | RADIO_INTENCLR_ADDRESS_Msk;
1107+
/* Clear wfr timer channels */
11221108
NRF_PPI->CHENCLR = PPI_CHEN_CH4_Msk | PPI_CHEN_CH5_Msk;
11231109

11241110
/* Initialize the ble mbuf header */
@@ -1210,7 +1196,6 @@ ble_phy_rx_start_isr(void)
12101196
if (rc >= 0) {
12111197
/* Set rx started flag and enable rx end ISR */
12121198
g_ble_phy_data.phy_rx_started = 1;
1213-
NRF_RADIO->INTENSET = RADIO_INTENSET_END_Msk;
12141199
} else {
12151200
/* Disable PHY */
12161201
ble_phy_disable();
@@ -1256,28 +1241,36 @@ ble_phy_isr(void)
12561241
}
12571242
}
12581243

1259-
/* Check for disabled event. This only happens for transmits now */
1244+
/* Handle disabled event. This is enabled for both TX and RX. On RX, we
1245+
* need to check phy_rx_started flag to make sure we actually were receiving
1246+
* a PDU, otherwise this is due to wfr.
1247+
*/
12601248
if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
1261-
if (g_ble_phy_data.phy_state == BLE_PHY_STATE_RX) {
1262-
NRF_RADIO->EVENTS_DISABLED = 0;
1263-
ble_ll_wfr_timer_exp(NULL);
1264-
} else if (g_ble_phy_data.phy_state == BLE_PHY_STATE_IDLE) {
1265-
assert(0);
1266-
} else {
1249+
BLE_LL_ASSERT(NRF_RADIO->EVENTS_END ||
1250+
((g_ble_phy_data.phy_state == BLE_PHY_STATE_RX) &&
1251+
!g_ble_phy_data.phy_rx_started));
1252+
NRF_RADIO->EVENTS_END = 0;
1253+
NRF_RADIO->EVENTS_DISABLED = 0;
1254+
NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
1255+
1256+
switch (g_ble_phy_data.phy_state) {
1257+
case BLE_PHY_STATE_RX:
1258+
if (g_ble_phy_data.phy_rx_started) {
1259+
ble_phy_rx_end_isr();
1260+
} else {
1261+
ble_ll_wfr_timer_exp(NULL);
1262+
}
1263+
break;
1264+
case BLE_PHY_STATE_TX:
12671265
ble_phy_tx_end_isr();
1266+
break;
1267+
default:
1268+
BLE_LL_ASSERT(0);
12681269
}
12691270
}
12701271

1271-
/* Receive packet end (we dont enable this for transmit) */
1272-
if ((irq_en & RADIO_INTENCLR_END_Msk) && NRF_RADIO->EVENTS_END) {
1273-
ble_phy_rx_end_isr();
1274-
}
1275-
12761272
g_ble_phy_data.phy_transition_late = 0;
12771273

1278-
/* Ensures IRQ is cleared */
1279-
irq_en = NRF_RADIO->SHORTS;
1280-
12811274
/* Count # of interrupts */
12821275
STATS_INC(ble_phy_stats, phy_isrs);
12831276

0 commit comments

Comments
 (0)