Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master bf65252ed -> 568f0c682


Fix a number of connection issues including crcinit and wait for response 
timer. Also added code to clear outstanding opcode for test project so host 
does not crash


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

Branch: refs/heads/master
Commit: 568f0c68213651336e2a0208dd696d5a1a1f1cc6
Parents: 165affb
Author: wes3 <w...@micosa.io>
Authored: Thu Dec 3 15:35:42 2015 -0800
Committer: wes3 <w...@micosa.io>
Committed: Thu Dec 3 15:35:50 2015 -0800

----------------------------------------------------------------------
 .../controller/include/controller/ble_ll.h      |  22 ++++
 net/nimble/controller/src/ble_ll.c              |  46 +++++++-
 net/nimble/controller/src/ble_ll_adv.c          |   4 +-
 net/nimble/controller/src/ble_ll_conn.c         | 113 +++++++++++++++----
 net/nimble/controller/src/ble_ll_hci.c          |  19 ++--
 net/nimble/drivers/nrf52/src/ble_phy.c          |  17 ++-
 net/nimble/host/src/host_dbg.c                  |   8 +-
 net/nimble/host/src/host_hci_cmd.c              |   2 +-
 project/bletest/src/main.c                      |  22 +++-
 9 files changed, 201 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll.h 
b/net/nimble/controller/include/controller/ble_ll.h
index aff9aab..d5f63ac 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -441,4 +441,26 @@ void ble_ll_wfr_enable(uint32_t cputime, ble_ll_wfr_func 
wfr_cb, void *arg);
 /* Disable wait for response timer */
 void ble_ll_wfr_disable(void);
 
+/* 
+ * XXX: temporary LL debug log. Will get removed once we transition to real
+ * log
+ */ 
+#undef BLE_LL_LOG
+
+#define BLE_LL_LOG_ID_RX_START          (1)
+#define BLE_LL_LOG_ID_RX_END            (2)
+#define BLE_LL_LOG_ID_CONN_EV_START     (4)
+#define BLE_LL_LOG_ID_CONN_EV_END       (5)
+#define BLE_LL_LOG_ID_PHY_SETCHAN       (200)
+#define BLE_LL_LOG_ID_PHY_DISABLE       (201)
+#define BLE_LL_LOG_ID_PHY_ISR           (202)
+#define BLE_LL_LOG_ID_PHY_RX            (220)
+#define BLE_LL_LOG_ID_PHY_TX            (221)
+
+#ifdef BLE_LL_LOG
+void ble_ll_log(uint8_t id, uint8_t arg0_8, uint8_t arg1_8, uint32_t arg0_32);
+#else
+#define ble_ll_log(m,n,o,p)
+#endif
+
 #endif /* H_LL_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/controller/src/ble_ll.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll.c 
b/net/nimble/controller/src/ble_ll.c
index 2d6d466..3196440 100644
--- a/net/nimble/controller/src/ble_ll.c
+++ b/net/nimble/controller/src/ble_ll.c
@@ -67,6 +67,44 @@ struct ble_ll_stats g_ble_ll_stats;
 struct os_task g_ble_ll_task;
 os_stack_t g_ble_ll_stack[BLE_LL_STACK_SIZE];
 
+/* XXX: temporary logging until we transition to real logging */
+#ifdef BLE_LL_LOG
+struct ble_ll_log
+{
+    uint8_t log_id;
+    uint8_t log_arg8_0;
+    uint8_t log_arg8_1;
+    uint32_t log_arg32_0;
+    uint32_t cputime;
+
+};
+
+#define BLE_LL_LOG_LEN  (128)
+
+static struct ble_ll_log g_ble_ll_log[BLE_LL_LOG_LEN];
+static uint8_t g_ble_ll_log_index;
+
+void
+ble_ll_log(uint8_t id, uint8_t arg8_0, uint8_t arg8_1, uint32_t arg32_0)
+{
+    os_sr_t sr;
+    struct ble_ll_log *le;
+
+    OS_ENTER_CRITICAL(sr);
+    le = &g_ble_ll_log[g_ble_ll_log_index];
+    le->cputime = cputime_get32();
+    le->log_id = id;
+    le->log_arg8_0 = arg8_0;
+    le->log_arg8_1 = arg8_1;
+    le->log_arg32_0 = arg32_0;
+    ++g_ble_ll_log_index;
+    if (g_ble_ll_log_index == BLE_LL_LOG_LEN) {
+        g_ble_ll_log_index = 0;
+    }
+    OS_EXIT_CRITICAL(sr);
+}
+#endif
+
 /**
  * Counts the number of advertising PDU's by type. 
  * 
@@ -247,7 +285,9 @@ ble_ll_wfr_timer_exp(void *arg)
 }
 
 /**
- * Enable the wait for response timer 
+ * Enable the wait for response timer. 
+ *  
+ * Context: Interrupt. 
  * 
  * @param cputime 
  * @param wfr_cb 
@@ -444,6 +484,8 @@ ble_ll_rx_start(struct os_mbuf *rxpdu, uint8_t chan)
     uint8_t pdu_type;
     uint8_t *rxbuf;
 
+    ble_ll_log(BLE_LL_LOG_ID_RX_START, chan, 0, (uint32_t)rxpdu);
+
     /* Check channel type */
     rxbuf = rxpdu->om_data;
     if (chan < BLE_PHY_NUM_DATA_CHANS) {
@@ -527,6 +569,8 @@ ble_ll_rx_end(struct os_mbuf *rxpdu, uint8_t chan, uint8_t 
crcok)
     uint16_t mblen;
     uint8_t *rxbuf;
 
+    ble_ll_log(BLE_LL_LOG_ID_RX_END, chan, crcok, (uint32_t)rxpdu);
+
     /* Set the rx buffer pointer to the start of the received data */
     rxbuf = rxpdu->om_data;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/controller/src/ble_ll_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_adv.c 
b/net/nimble/controller/src/ble_ll_adv.c
index 19b49f0..47326a3 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -378,7 +378,7 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
     advsm = (struct ble_ll_adv_sm *)sch->cb_arg;
 
     /* Toggle the LED */
-    gpio_toggle(LED_BLINK_PIN);
+    gpio_clear(LED_BLINK_PIN);
 
     /* Set channel */
     rc = ble_phy_setchan(advsm->adv_chan, 0, 0);
@@ -984,7 +984,7 @@ ble_ll_adv_tx_done_proc(void *arg)
         advsm->adv_pdu_start_time = advsm->adv_event_start_time;
 
         /* Toggle the LED */
-        gpio_toggle(LED_BLINK_PIN);
+        gpio_set(LED_BLINK_PIN);
     } else {
         /* 
          * Move to next advertising channel. If not in the mask, just

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/controller/src/ble_ll_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn.c 
b/net/nimble/controller/src/ble_ll_conn.c
index aee1b37..c7d9528 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -29,6 +29,7 @@
 #include "controller/ble_ll_sched.h"
 #include "controller/ble_phy.h"
 #include "hal/hal_cputime.h"
+#include "hal/hal_gpio.h"
 
 /* XXX TODO
  * 1) Add set channel map command and implement channel change procedure.
@@ -48,6 +49,8 @@
  * We might want to guarantee a IFS time as well since the next event needs
  * to be scheduled prior to the start of the event to account for the time it
  * takes to get a frame ready (which is pretty much the IFS time).
+ * 14) Look at all places where the conn_txq is accessed and make sure I
+ * am protecting it properly.
  */
 
 /* XXX: this does not belong here! Move to transport? */
@@ -258,8 +261,13 @@ struct ble_ll_conn_stats
     uint32_t rx_resent_pdus;
     uint32_t data_pdu_rx_valid;
     uint32_t data_pdu_rx_invalid;
+    uint32_t data_pdu_rx_bad_llid;
+    uint32_t data_pdu_rx_dup;
+    uint32_t data_pdu_txd;
     uint32_t data_pdu_txg;
     uint32_t data_pdu_txf;
+    uint32_t conn_req_txd;
+    uint32_t ctrl_pdu_rxd;
 };
 struct ble_ll_conn_stats g_ble_ll_conn_stats;
 
@@ -596,6 +604,7 @@ ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm, int 
beg_transition)
         ble_hdr = BLE_MBUF_HDR_PTR(m);
         ble_hdr->flags = 0;
         m->om_data[0] = BLE_LL_LLID_DATA_FRAG;
+        STAILQ_INSERT_HEAD(&connsm->conn_txq, pkthdr, omp_next);
     }
 
     /* Set SN, MD, NESN in transmit PDU */
@@ -664,6 +673,8 @@ ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm, int 
beg_transition)
                                        ble_ll_pdu_tx_time_get(m->om_len));
             ble_ll_wfr_enable(wfr_time, ble_ll_conn_wait_txend, connsm);
         }
+
+        ++g_ble_ll_conn_stats.data_pdu_txd;
     }
 
     return rc;
@@ -706,10 +717,15 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
     uint32_t wfr_time;
     struct ble_ll_conn_sm *connsm;
 
+    /* set led */
+    gpio_clear(LED_BLINK_PIN);
+
     /* Set current connection state machine */
     connsm = (struct ble_ll_conn_sm *)sch->cb_arg;
     g_ble_ll_conn_cur_sm = connsm;
 
+    ble_ll_log(BLE_LL_LOG_ID_CONN_EV_START, connsm->data_chan_index, 0, 0);
+
     /* Set LL state */
     ble_ll_state_set(BLE_LL_STATE_CONNECTION);
 
@@ -988,9 +1004,9 @@ ble_ll_conn_comp_event_send(struct ble_ll_conn_sm *connsm, 
uint8_t status)
                 memcpy(evbuf + 8, connsm->peer_addr, BLE_DEV_ADDR_LEN);
                 htole16(evbuf + 14, connsm->conn_itvl);
                 htole16(evbuf + 16, connsm->slave_latency);
-                evbuf[18] = connsm->master_sca;
+                htole16(evbuf + 18, connsm->supervision_tmo);
+                evbuf[20] = connsm->master_sca;
             }
-
             ble_ll_hci_event_send(evbuf);
         }
     }
@@ -1133,6 +1149,9 @@ ble_ll_conn_event_end(void *arg)
 
     connsm = (struct ble_ll_conn_sm *)arg;
 
+    /* Disable the PHY */
+    ble_phy_disable();
+
     /* Disable the wfr timer */
     ble_ll_wfr_disable();
 
@@ -1148,6 +1167,13 @@ ble_ll_conn_event_end(void *arg)
         connsm->slave_cur_tx_win_usecs = 0;
     }
 
+    /* 
+     * XXX: not quite sure I am interpreting slave latency correctly here.
+     * The spec says if you applied slave latency and you dont hear a packet,
+     * you dont apply slave latency. Does that mean you dont apply slave
+     * latency until you hear a packet or on the next interval if you listen
+     * and dont hear anything, can you apply slave latency?
+     */
     /* Set event counter to the next connection event that we will tx/rx in */
     itvl = connsm->conn_itvl * BLE_LL_CONN_ITVL_USECS;
     latency = 1;
@@ -1163,7 +1189,8 @@ ble_ll_conn_event_end(void *arg)
     connsm->anchor_point += cputime_usecs_to_ticks(itvl);
 
     /* Calculate data channel index of next connection event */
-    while (latency >= 0) {
+    connsm->last_unmapped_chan = connsm->unmapped_chan;
+    while (latency > 0) {
         --latency;
         connsm->data_chan_index = ble_ll_conn_calc_dci(connsm);
     }
@@ -1200,8 +1227,13 @@ ble_ll_conn_event_end(void *arg)
         connsm->slave_cur_window_widening = cur_ww;
     }
 
+    ble_ll_log(BLE_LL_LOG_ID_CONN_EV_END, 0, 0, connsm->event_cntr);
+
     /* Schedule the next connection event */
     ble_ll_conn_sched_set(connsm);
+
+    /* turn led off */
+    gpio_set(LED_BLINK_PIN);
 }
 
 /**
@@ -1277,9 +1309,9 @@ ble_ll_conn_req_pdu_make(struct ble_ll_conn_sm *connsm)
 
     /* Access address */
     htole32(dptr, connsm->access_addr);
-    dptr[4] = connsm->crcinit >> 16;
-    dptr[5] = connsm->crcinit >> 8;
-    dptr[6] = (uint8_t)connsm->crcinit;
+    dptr[4] = (uint8_t)connsm->crcinit;
+    dptr[5] = (uint8_t)(connsm->crcinit >> 8);
+    dptr[6] = (uint8_t)(connsm->crcinit >> 16);
     dptr[7] = connsm->tx_win_size;
     htole16(dptr + 8, connsm->tx_win_off);
     htole16(dptr + 10, connsm->conn_itvl);
@@ -1625,6 +1657,7 @@ ble_ll_init_rx_pdu_end(struct os_mbuf *rxpdu)
         rc = ble_ll_conn_request_send(addr_type, adv_addr);
         if (!rc) {
             ble_hdr->flags |= BLE_MBUF_HDR_F_CONN_REQ_TXD;
+            ++g_ble_ll_conn_stats.conn_req_txd;
         }
     }
 
@@ -1677,6 +1710,13 @@ ble_ll_conn_rx_pdu_start(void)
 {
     struct ble_ll_conn_sm *connsm;
 
+    /* 
+     * Disable wait for response timer since we receive a response. We dont
+     * care if this is the response we were waiting for or not; the code
+     * called at receive end will deal with ending the connection event
+     * if needed
+     */ 
+    ble_ll_wfr_disable();
     connsm = g_ble_ll_conn_cur_sm;
     if (connsm) {
         connsm->rsp_rxd = 1;
@@ -1726,13 +1766,22 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, uint8_t 
crcok)
             tmo = connsm->supervision_tmo * BLE_HCI_CONN_SPVN_TMO_UNITS * 1000;
             cputime_timer_relative(&connsm->conn_spvn_timer, tmo);
 
+            rxbuf = rxpdu->om_data;
+            hdr_byte = rxbuf[0];
+            acl_len = rxbuf[1];
+            acl_hdr = hdr_byte & BLE_LL_DATA_HDR_LLID_MASK;
+
+            /* Check that the LLID is reasonable */
+            if ((hdr_byte == 0) || 
+                ((hdr_byte == BLE_LL_LLID_DATA_START) && (acl_len == 0))) {
+                ++g_ble_ll_conn_stats.data_pdu_rx_bad_llid;
+                goto conn_rx_data_pdu_end;
+            }
+
             /* 
              * If we are a slave, we can only start to use slave latency
              * once we have received a NESN of 1 from the master
              */ 
-            rxbuf = rxpdu->om_data;
-            hdr_byte = rxbuf[0];
-            acl_len = rxbuf[1];
             if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
                 if (hdr_byte & BLE_LL_DATA_HDR_NESN_MASK) {
                     connsm->allow_slave_latency = 1;
@@ -1748,18 +1797,29 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, uint8_t 
crcok)
                 /* Update last rxd sn */
                 connsm->last_rxd_sn = rxd_sn;
 
+                /* No need to do anything if empty pdu */
+                if ((acl_hdr == BLE_LL_LLID_DATA_FRAG) && (acl_len == 0)) {
+                    goto conn_rx_data_pdu_end;
+                }
+
+                if (acl_hdr == BLE_LL_LLID_CTRL) {
+                    /* XXX: Process control frame! For now just free */
+                    ++g_ble_ll_conn_stats.ctrl_pdu_rxd;
+                    goto conn_rx_data_pdu_end;
+                }
+
                 /* NOTE: there should be at least two bytes available */
                 assert(OS_MBUF_LEADINGSPACE(rxpdu) >= 2);
                 os_mbuf_prepend(rxpdu, 2);
                 rxbuf = rxpdu->om_data;
 
-                /* Set ACL data packet header and send to host */
-                acl_hdr = (hdr_byte & BLE_LL_DATA_HDR_LLID_MASK);
                 acl_hdr = (acl_hdr << 12) | connsm->conn_handle;
                 htole16(rxbuf, acl_hdr);
                 htole16(rxbuf + 2, acl_len);
                 ble_hs_rx_data(rxpdu);
                 return;
+            } else {
+                ++g_ble_ll_conn_stats.data_pdu_rx_dup;
             }
         } else {
             ++g_ble_ll_conn_stats.no_conn_sm;
@@ -1769,6 +1829,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, uint8_t 
crcok)
     }
 
     /* Free buffer */
+conn_rx_data_pdu_end:
     os_mbuf_free(rxpdu);
 }
 
@@ -1858,6 +1919,10 @@ ble_ll_conn_rx_pdu_end(struct os_mbuf *rxpdu, uint8_t 
crcok)
         hdr_nesn = hdr_byte & BLE_LL_DATA_HDR_NESN_MASK;
         conn_sn = connsm->tx_seqnum;
         if ((hdr_nesn && conn_sn) || (!hdr_nesn && !conn_sn)) {
+            /*
+             * XXX: if we are a slave, our first received data pdu will cause
+             * us to increment failed tx data pdus. This should be fixed.s
+             */
             /* We did not get an ACK. Must retry the PDU */
             ++g_ble_ll_conn_stats.data_pdu_txf;
         } else {
@@ -2022,11 +2087,9 @@ ble_ll_conn_slave_start(uint8_t *rxbuf)
 
     /* Set connection state machine information */
     connsm->access_addr = le32toh(dptr);
-    crcinit = dptr[4];
-    crcinit <<= 16;
-    crcinit |= dptr[5];
-    crcinit <<= 8;
-    crcinit |= dptr[6];
+    crcinit = dptr[6];
+    crcinit = (crcinit << 8) | dptr[5];
+    crcinit = (crcinit << 8) | dptr[4];
     connsm->crcinit = crcinit;
     connsm->tx_win_size = dptr[7];
     connsm->tx_win_off = le16toh(dptr + 8);
@@ -2126,6 +2189,16 @@ ble_ll_conn_init(void)
         assert(connsm != NULL);
         connsm->conn_handle = i;
         STAILQ_INSERT_TAIL(&g_ble_ll_conn_free_list, connsm, free_stqe);
+
+        /* Initialize empty pdu */
+        m = (struct os_mbuf *)&connsm->conn_empty_pdu;
+        m->om_data = (uint8_t *)&connsm->conn_empty_pdu[0];
+        m->om_data += BLE_MBUF_PKT_OVERHEAD;
+        m->om_pkthdr_len = sizeof(struct ble_mbuf_hdr) + 
+            sizeof(struct os_mbuf_pkthdr);
+        m->om_len = 2;
+        OS_MBUF_PKTHDR(m)->omp_len = 2;
+        m->om_data[0] = BLE_LL_LLID_DATA_FRAG;
     }
 
     /* Configure the global LL parameters */
@@ -2141,13 +2214,5 @@ ble_ll_conn_init(void)
     maxbytes = BLE_LL_CFG_CONN_INIT_MAX_TX_BYTES + BLE_LL_DATA_MAX_OVERHEAD;
     conn_params->conn_init_max_tx_time = ble_ll_pdu_tx_time_get(maxbytes);
     conn_params->conn_init_max_tx_octets = BLE_LL_CFG_SUPP_MAX_TX_BYTES;
-
-    /* Initialize empty pdu */
-    m = (struct os_mbuf *)&connsm->conn_empty_pdu;
-    m->om_data = (uint8_t *)&connsm->conn_empty_pdu[0];
-    m->om_data += BLE_MBUF_PKT_OVERHEAD;
-    m->om_len = 2;
-    OS_MBUF_PKTHDR(m)->omp_len = 2;
-    m->om_data[0] = BLE_LL_LLID_DATA_FRAG;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/controller/src/ble_ll_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci.c 
b/net/nimble/controller/src/ble_ll_hci.c
index 31cace8..c162227 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -323,15 +323,8 @@ ble_ll_hci_cmd_proc(struct os_event *ev)
         break;
     }
 
-    /* Make sure valid error code */
-    assert(rc >= 0);
-    if (rc) {
-        ++g_ble_ll_stats.hci_cmd_errs;
-    } else {
-        ++g_ble_ll_stats.hci_cmds;
-    }
-
     /* If no response is generated, we free the buffers */
+    assert(rc >= 0);
     if (rc <= BLE_ERR_MAX) {
         /* Create a command complete event with status from command */
         cmdbuf[0] = BLE_HCI_EVCODE_COMMAND_COMPLETE;
@@ -341,13 +334,21 @@ ble_ll_hci_cmd_proc(struct os_event *ev)
         cmdbuf[5] = (uint8_t)rc;
     } else {
         /* Create a command complete event with status from command */
+        rc -= (BLE_ERR_MAX + 1);
         cmdbuf[0] = BLE_HCI_EVCODE_COMMAND_STATUS;
         cmdbuf[1] = 4;
-        cmdbuf[2] = (uint8_t)(rc - (BLE_ERR_MAX + 1));
+        cmdbuf[2] = (uint8_t)rc;
         cmdbuf[3] = ble_ll_hci_get_num_cmd_pkts();
         htole16(cmdbuf + 4, opcode);
     }
 
+    /* Count commands and those in error */
+    if (rc) {
+        ++g_ble_ll_stats.hci_cmd_errs;
+    } else {
+        ++g_ble_ll_stats.hci_cmds;
+    }
+
     /* Send the event (events cannot be masked) */
     ble_ll_hci_event_send(cmdbuf);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/drivers/nrf52/src/ble_phy.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/nrf52/src/ble_phy.c 
b/net/nimble/drivers/nrf52/src/ble_phy.c
index 4d99869..d6278a3 100644
--- a/net/nimble/drivers/nrf52/src/ble_phy.c
+++ b/net/nimble/drivers/nrf52/src/ble_phy.c
@@ -137,8 +137,12 @@ ble_phy_isr(void)
     struct os_mbuf *rxpdu;
     struct ble_mbuf_hdr *ble_hdr;
 
-    /* Check for disabled event. This only happens for transmits now */
+    /* Read irq register to determine which interrupts are enabled */
     irq_en = NRF_RADIO->INTENCLR;
+
+    ble_ll_log(BLE_LL_LOG_ID_PHY_ISR, 0, 0, irq_en);
+
+    /* 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);
@@ -537,11 +541,6 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, 
uint32_t crcinit)
         return BLE_PHY_ERR_INV_PARAM;
     }
 
-    /* If the current channel is set, just return */
-    if (g_ble_phy_data.phy_chan == chan) {
-        return 0;
-    }
-
     /* Get correct nrf52 frequency */
     if (chan < BLE_PHY_NUM_DATA_CHANS) {
         if (chan < 11) {
@@ -585,6 +584,8 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, 
uint32_t crcinit)
     NRF_RADIO->FREQUENCY = freq;
     NRF_RADIO->DATAWHITEIV = chan;
 
+    ble_ll_log(BLE_LL_LOG_ID_PHY_SETCHAN, chan, freq, access_addr);
+
     return 0;
 }
 
@@ -598,10 +599,14 @@ ble_phy_setchan(uint8_t chan, uint32_t access_addr, 
uint32_t crcinit)
 void
 ble_phy_disable(void)
 {
+    ble_ll_log(BLE_LL_LOG_ID_PHY_DISABLE, g_ble_phy_data.phy_state, 0, 0);
+
     NRF_RADIO->INTENCLR = NRF52_RADIO_IRQ_MASK_ALL;
     NRF_RADIO->SHORTS = 0;
     NRF_RADIO->TASKS_DISABLE = 1;
     g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
+
+    /* XXX: do I need to clear any pending state in the cortex-M possibly? */
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/host/src/host_dbg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_dbg.c b/net/nimble/host/src/host_dbg.c
index 977f309..b71677c 100644
--- a/net/nimble/host/src/host_dbg.c
+++ b/net/nimble/host/src/host_dbg.c
@@ -70,9 +70,9 @@ host_hci_dbg_le_event_disp(uint8_t subev, uint8_t len, 
uint8_t *evdata)
                            "latency=%u spvn_tmo=%u mca=%u\n", 
                            le16toh(evdata + 1), evdata[3], evdata[4], 
                            evdata[10], evdata[9], evdata[8], evdata[7],
-                           evdata[6], evdata[5], 
-                           le16toh(evdata + 11), le16toh(evdata + 13), 
-                           evdata[14]);
+                           evdata[6], evdata[5], le16toh(evdata + 11), 
+                           le16toh(evdata + 13), le16toh(evdata + 15), 
+                           evdata[17]);
         } else {
             console_printf("LE connection complete. FAIL 
(status=%u)\n",status);
         }
@@ -101,7 +101,7 @@ host_hci_dbg_cmd_complete_disp(uint8_t *evdata, uint8_t len)
     if (ogf == BLE_HCI_OGF_LE) {
         switch (ocf) {
         case BLE_HCI_OCF_LE_SET_ADV_DATA:
-            snprintf(parmbuf, 12, "status=%-3d ", evdata[3]);
+            snprintf(parmbuf, 12, "status=%u ", evdata[3]);
             break;
         default:
             break;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/net/nimble/host/src/host_hci_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci_cmd.c 
b/net/nimble/host/src/host_hci_cmd.c
index 0ea6799..5d28f62 100644
--- a/net/nimble/host/src/host_hci_cmd.c
+++ b/net/nimble/host/src/host_hci_cmd.c
@@ -47,7 +47,7 @@ host_hci_le_cmd_send(uint16_t ocf, uint8_t len, void *cmddata)
     uint16_t opcode;
 
     /* Don't allow multiple commands "in flight." */
-    //assert(host_hci_outstanding_opcode == 0);
+    assert(host_hci_outstanding_opcode == 0);
 
     rc = -1;
     cmd = os_memblock_get(&g_hci_cmd_pool);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/568f0c68/project/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/project/bletest/src/main.c b/project/bletest/src/main.c
index 78fd953..78f8c7a 100755
--- a/project/bletest/src/main.c
+++ b/project/bletest/src/main.c
@@ -62,10 +62,10 @@ os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE];
 #define BLETEST_ROLE_ADVERTISER         (0)
 #define BLETEST_ROLE_SCANNER            (1)
 #define BLETEST_ROLE_INITIATOR          (2)
-#define BLETEST_CFG_ROLE                (BLETEST_ROLE_SCANNER)
+#define BLETEST_CFG_ROLE                (BLETEST_ROLE_INITIATOR)
 #define BLETEST_CFG_FILT_DUP_ADV        (0)
 #define BLETEST_CFG_ADV_ITVL            (500000 / BLE_HCI_ADV_ITVL)
-#define BLETEST_CFG_ADV_TYPE            BLE_HCI_ADV_TYPE_ADV_SCAN_IND
+#define BLETEST_CFG_ADV_TYPE            BLE_HCI_ADV_TYPE_ADV_IND
 #define BLETEST_CFG_ADV_FILT_POLICY     (BLE_HCI_ADV_FILT_NONE)
 #define BLETEST_CFG_SCAN_ITVL           (700000 / BLE_HCI_SCAN_ITVL)
 #define BLETEST_CFG_SCAN_WINDOW         (650000 / BLE_HCI_SCAN_ITVL)
@@ -111,6 +111,7 @@ bletest_inc_adv_pkt_num(void)
 
     rc = host_hci_cmd_le_set_adv_data(g_host_adv_data, g_host_adv_len);
     assert(rc == 0);
+    host_hci_outstanding_opcode = 0;
 }
 
 uint8_t
@@ -179,14 +180,18 @@ bletest_init_advertising(void)
     adv.adv_itvl_max = adv_itvl;
     rc = host_hci_cmd_le_set_adv_params(&adv);
     assert(rc == 0);
+    host_hci_outstanding_opcode = 0;
+
 
     /* Set advertising data */
     rc = host_hci_cmd_le_set_adv_data(&g_host_adv_data[0], adv_len);
     assert(rc == 0);
+    host_hci_outstanding_opcode = 0;
 
     /* Set scan response data */
     rc = host_hci_cmd_le_set_scan_rsp_data(&g_host_adv_data[0], adv_len);
     assert(rc == 0);
+    host_hci_outstanding_opcode = 0;
 }
 
 #if (BLETEST_CFG_ROLE == BLETEST_ROLE_SCANNER)
@@ -204,6 +209,7 @@ bletest_init_scanner(void)
                                          BLE_HCI_ADV_OWN_ADDR_PUBLIC,
                                          BLETEST_CFG_SCAN_FILT_POLICY);
     assert(rc == 0);
+    host_hci_outstanding_opcode = 0;
 
     filter_policy = BLETEST_CFG_SCAN_FILT_POLICY;
     if (filter_policy & 1) {
@@ -216,6 +222,7 @@ bletest_init_scanner(void)
         dev_addr[5] = 0x08;
         rc = host_hci_cmd_le_add_to_whitelist(dev_addr, BLE_ADDR_TYPE_PUBLIC);
         assert(rc == 0);
+        host_hci_outstanding_opcode = 0;
     }
 }
 #endif
@@ -241,15 +248,16 @@ bletest_init_initiator(void)
     hcc->peer_addr[0] = 0x00;
     hcc->peer_addr[1] = 0x00;
     hcc->peer_addr[2] = 0x00;
-    hcc->peer_addr[3] = 0x99;
-    hcc->peer_addr[4] = 0x99;
-    hcc->peer_addr[5] = 0x09;
+    hcc->peer_addr[3] = 0x88;
+    hcc->peer_addr[4] = 0x88;
+    hcc->peer_addr[5] = 0x08;
     hcc->own_addr_type = BLE_HCI_CONN_PEER_ADDR_PUBLIC;
     hcc->min_ce_len = BLETEST_CFG_MIN_CE_LEN;
     hcc->max_ce_len = BLETEST_CFG_MAX_CE_LEN;
 
     rc = host_hci_cmd_le_create_connection(hcc);
     assert(rc == 0);
+    host_hci_outstanding_opcode = 0;
 }
 #endif
 
@@ -263,10 +271,12 @@ bletest_execute(void)
     if ((int32_t)(os_time_get() - g_next_os_time) >= 0) {
         if (g_bletest_state) {
             rc = host_hci_cmd_le_set_adv_enable(0);
+            host_hci_outstanding_opcode = 0;
             assert(rc == 0);
             g_bletest_state = 0;
         } else {
             rc = host_hci_cmd_le_set_adv_enable(1);
+            host_hci_outstanding_opcode = 0;
             assert(rc == 0);
             g_bletest_state = 1;
         }
@@ -279,10 +289,12 @@ bletest_execute(void)
         if (g_bletest_state) {
             rc = host_hci_cmd_le_set_scan_enable(0, BLETEST_CFG_FILT_DUP_ADV);
             assert(rc == 0);
+            host_hci_outstanding_opcode = 0;
             g_bletest_state = 0;
         } else {
             rc = host_hci_cmd_le_set_scan_enable(1, BLETEST_CFG_FILT_DUP_ADV);
             assert(rc == 0);
+            host_hci_outstanding_opcode = 0;
             g_bletest_state = 1;
         }
         g_next_os_time += (OS_TICKS_PER_SEC * 60);

Reply via email to