[34/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-284: fix nrf51 radio state errors

2016-04-27 Thread marko
MYNEWT-284: fix nrf51 radio state errors


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/27705013
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/27705013
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/27705013

Branch: refs/heads/master
Commit: 27705013e505dc6b90a7a946e7ce7f8e064ef0f4
Parents: 88356a0
Author: William San Filippo 
Authored: Thu Apr 21 14:46:17 2016 -0700
Committer: William San Filippo 
Committed: Thu Apr 21 14:46:24 2016 -0700

--
 net/nimble/drivers/nrf51/src/ble_phy.c | 272 +---
 net/nimble/drivers/nrf52/src/ble_phy.c | 272 +---
 2 files changed, 249 insertions(+), 295 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27705013/net/nimble/drivers/nrf51/src/ble_phy.c
--
diff --git a/net/nimble/drivers/nrf51/src/ble_phy.c 
b/net/nimble/drivers/nrf51/src/ble_phy.c
index bc4a258..d95329a 100644
--- a/net/nimble/drivers/nrf51/src/ble_phy.c
+++ b/net/nimble/drivers/nrf51/src/ble_phy.c
@@ -202,6 +202,11 @@ ble_phy_rxpdu_get(void)
 return m;
 }
 
+/**
+ * Called when we want to wait if the radio is in either the rx or tx
+ * disable states. We want to wait until that state is over before doing
+ * anything to the radio
+ */
 static void
 nrf_wait_disabled(void)
 {
@@ -220,112 +225,135 @@ nrf_wait_disabled(void)
 }
 }
 
+/**
+ * Setup transceiver for receive.
+ */
 static void
-ble_phy_isr(void)
+ble_phy_rx_xcvr_setup(void)
 {
-int rc;
-uint8_t transition;
-uint8_t crcok;
-uint32_t irq_en;
-uint32_t state;
-uint32_t wfr_time;
 #ifdef BLE_LL_CFG_FEAT_LE_ENCRYPTION
-uint8_t *dptr;
+if (g_ble_phy_data.phy_encrypted) {
+NRF_RADIO->PACKETPTR = (uint32_t)_ble_phy_enc_buf[0];
+NRF_CCM->INPTR = (uint32_t)_ble_phy_enc_buf[0];
+NRF_CCM->OUTPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+NRF_CCM->SCRATCHPTR = (uint32_t)_nrf_encrypt_scratchpad[0];
+NRF_CCM->MODE = CCM_MODE_MODE_Decryption;
+NRF_CCM->CNFPTR = (uint32_t)_nrf_ccm_data;
+NRF_CCM->SHORTS = 0;
+NRF_CCM->EVENTS_ERROR = 0;
+NRF_CCM->EVENTS_ENDCRYPT = 0;
+NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk | PPI_CHEN_CH25_Msk;
+} else {
+NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+}
+#else
+NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
 #endif
-struct os_mbuf *rxpdu;
-struct ble_mbuf_hdr *ble_hdr;
 
-/* Read irq register to determine which interrupts are enabled */
-irq_en = NRF_RADIO->INTENCLR;
+/* Reset the rx started flag. Used for the wait for response */
+g_ble_phy_data.phy_rx_started = 0;
+g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
 
-/* Check for disabled event. This only happens for transmits now */
-if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
-/* Better be in TX state! */
-assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
+/* I want to know when 1st byte received (after address) */
+NRF_RADIO->BCC = 8; /* in bits */
+NRF_RADIO->EVENTS_ADDRESS = 0;
+NRF_RADIO->EVENTS_DEVMATCH = 0;
+NRF_RADIO->EVENTS_BCMATCH = 0;
+NRF_RADIO->EVENTS_RSSIEND = 0;
+NRF_RADIO->SHORTS = RADIO_SHORTS_END_DISABLE_Msk |
+RADIO_SHORTS_READY_START_Msk |
+RADIO_SHORTS_DISABLED_TXEN_Msk |
+RADIO_SHORTS_ADDRESS_BCSTART_Msk |
+RADIO_SHORTS_ADDRESS_RSSISTART_Msk |
+RADIO_SHORTS_DISABLED_RSSISTOP_Msk;
 
-ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, (g_ble_phy_txrx_buf[0] >> 8) & 
0xFF,
-   g_ble_phy_data.phy_encrypted, NRF_TIMER0->CC[2]);
+NRF_RADIO->INTENSET = RADIO_INTENSET_ADDRESS_Msk;
+}
 
-/* Clear events and clear interrupt on disabled event */
-NRF_RADIO->EVENTS_DISABLED = 0;
-NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
-NRF_RADIO->EVENTS_END = 0;
-state = NRF_RADIO->SHORTS;
+/**
+ * Called from interrupt context when the transmit ends
+ *
+ */
+static void
+ble_phy_tx_end_isr(void)
+{
+uint8_t transition;
+uint32_t wfr_time;
+
+/* Better be in TX state! */
+assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
+
+/* Log the event */
+ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, (g_ble_phy_txrx_buf[0] >> 8) & 0xFF,
+   g_ble_phy_data.phy_encrypted, NRF_TIMER0->CC[2]);
+
+/* Clear events and clear interrupt on disabled event */
+NRF_RADIO->EVENTS_DISABLED = 0;
+NRF_RADIO->INTENCLR = 

incubator-mynewt-core git commit: MYNEWT-284: fix nrf51 radio state errors

2016-04-21 Thread wes3
Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 88356a0df -> 27705013e


MYNEWT-284: fix nrf51 radio state errors


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/27705013
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/27705013
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/27705013

Branch: refs/heads/develop
Commit: 27705013e505dc6b90a7a946e7ce7f8e064ef0f4
Parents: 88356a0
Author: William San Filippo 
Authored: Thu Apr 21 14:46:17 2016 -0700
Committer: William San Filippo 
Committed: Thu Apr 21 14:46:24 2016 -0700

--
 net/nimble/drivers/nrf51/src/ble_phy.c | 272 +---
 net/nimble/drivers/nrf52/src/ble_phy.c | 272 +---
 2 files changed, 249 insertions(+), 295 deletions(-)
--


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27705013/net/nimble/drivers/nrf51/src/ble_phy.c
--
diff --git a/net/nimble/drivers/nrf51/src/ble_phy.c 
b/net/nimble/drivers/nrf51/src/ble_phy.c
index bc4a258..d95329a 100644
--- a/net/nimble/drivers/nrf51/src/ble_phy.c
+++ b/net/nimble/drivers/nrf51/src/ble_phy.c
@@ -202,6 +202,11 @@ ble_phy_rxpdu_get(void)
 return m;
 }
 
+/**
+ * Called when we want to wait if the radio is in either the rx or tx
+ * disable states. We want to wait until that state is over before doing
+ * anything to the radio
+ */
 static void
 nrf_wait_disabled(void)
 {
@@ -220,112 +225,135 @@ nrf_wait_disabled(void)
 }
 }
 
+/**
+ * Setup transceiver for receive.
+ */
 static void
-ble_phy_isr(void)
+ble_phy_rx_xcvr_setup(void)
 {
-int rc;
-uint8_t transition;
-uint8_t crcok;
-uint32_t irq_en;
-uint32_t state;
-uint32_t wfr_time;
 #ifdef BLE_LL_CFG_FEAT_LE_ENCRYPTION
-uint8_t *dptr;
+if (g_ble_phy_data.phy_encrypted) {
+NRF_RADIO->PACKETPTR = (uint32_t)_ble_phy_enc_buf[0];
+NRF_CCM->INPTR = (uint32_t)_ble_phy_enc_buf[0];
+NRF_CCM->OUTPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+NRF_CCM->SCRATCHPTR = (uint32_t)_nrf_encrypt_scratchpad[0];
+NRF_CCM->MODE = CCM_MODE_MODE_Decryption;
+NRF_CCM->CNFPTR = (uint32_t)_nrf_ccm_data;
+NRF_CCM->SHORTS = 0;
+NRF_CCM->EVENTS_ERROR = 0;
+NRF_CCM->EVENTS_ENDCRYPT = 0;
+NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk | PPI_CHEN_CH25_Msk;
+} else {
+NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+}
+#else
+NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
 #endif
-struct os_mbuf *rxpdu;
-struct ble_mbuf_hdr *ble_hdr;
 
-/* Read irq register to determine which interrupts are enabled */
-irq_en = NRF_RADIO->INTENCLR;
+/* Reset the rx started flag. Used for the wait for response */
+g_ble_phy_data.phy_rx_started = 0;
+g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
 
-/* Check for disabled event. This only happens for transmits now */
-if ((irq_en & RADIO_INTENCLR_DISABLED_Msk) && NRF_RADIO->EVENTS_DISABLED) {
-/* Better be in TX state! */
-assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
+/* I want to know when 1st byte received (after address) */
+NRF_RADIO->BCC = 8; /* in bits */
+NRF_RADIO->EVENTS_ADDRESS = 0;
+NRF_RADIO->EVENTS_DEVMATCH = 0;
+NRF_RADIO->EVENTS_BCMATCH = 0;
+NRF_RADIO->EVENTS_RSSIEND = 0;
+NRF_RADIO->SHORTS = RADIO_SHORTS_END_DISABLE_Msk |
+RADIO_SHORTS_READY_START_Msk |
+RADIO_SHORTS_DISABLED_TXEN_Msk |
+RADIO_SHORTS_ADDRESS_BCSTART_Msk |
+RADIO_SHORTS_ADDRESS_RSSISTART_Msk |
+RADIO_SHORTS_DISABLED_RSSISTOP_Msk;
 
-ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, (g_ble_phy_txrx_buf[0] >> 8) & 
0xFF,
-   g_ble_phy_data.phy_encrypted, NRF_TIMER0->CC[2]);
+NRF_RADIO->INTENSET = RADIO_INTENSET_ADDRESS_Msk;
+}
 
-/* Clear events and clear interrupt on disabled event */
-NRF_RADIO->EVENTS_DISABLED = 0;
-NRF_RADIO->INTENCLR = RADIO_INTENCLR_DISABLED_Msk;
-NRF_RADIO->EVENTS_END = 0;
-state = NRF_RADIO->SHORTS;
+/**
+ * Called from interrupt context when the transmit ends
+ *
+ */
+static void
+ble_phy_tx_end_isr(void)
+{
+uint8_t transition;
+uint32_t wfr_time;
+
+/* Better be in TX state! */
+assert(g_ble_phy_data.phy_state == BLE_PHY_STATE_TX);
+
+/* Log the event */
+ble_ll_log(BLE_LL_LOG_ID_PHY_TXEND, (g_ble_phy_txrx_buf[0] >> 8) & 0xFF,
+   g_ble_phy_data.phy_encrypted, NRF_TIMER0->CC[2]);
+
+/* Clear events and clear interrupt on