diff --git a/c3.conf b/application-c3wireless_em_ble.conf similarity index 100% rename from c3.conf rename to application-c3wireless_em_ble.conf diff --git a/c3-mcuboot.conf b/mcuboot-application-c3wireless_em_ble.conf similarity index 100% rename from c3-mcuboot.conf rename to mcuboot-application-c3wireless_em_ble.conf diff --git a/mcuboot-application-nrf52_sparkfun.conf b/mcuboot-application-nrf52_sparkfun.conf new file mode 100644 index 0000000..9e5d2b4 --- /dev/null +++ b/mcuboot-application-nrf52_sparkfun.conf @@ -0,0 +1,25 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_UART_CONSOLE=n +CONFIG_GPIO=y +CONFIG_SERIAL=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_MAIN_STACK_SIZE=512 +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=512 +CONFIG_BT=y +CONFIG_BT_HCI_RAW=y +CONFIG_BT_MAX_CONN=16 +CONFIG_BT_TINYCRYPT_ECC=n +CONFIG_BT_CTLR_DTM_HCI=y +CONFIG_BT_CTLR_ASSERT_HANDLER=y + +# Bootloader stuff +CONFIG_BOOTLOADER_MCUBOOT=y +CONFIG_REBOOT=y + +# Console +CONFIG_HAS_SEGGER_RTT=y +CONFIG_USE_SEGGER_RTT=y +CONFIG_LOG=y +CONFIG_LOG_DEFAULT_LEVEL=4 +CONFIG_RTT_CONSOLE=y +CONFIG_CONSOLE=y diff --git a/src/main.c b/src/main.c index 96b9994..6b45ee9 100644 --- a/src/main.c +++ b/src/main.c @@ -113,6 +113,12 @@ static bool reliable_packet(u8_t type) #define H5_SET_LEN(hdr, len) (((hdr)[1] |= ((len) & 0x0f) << 4), \ ((hdr)[2] |= (len) >> 4)) +typedef enum { + UNINIT, + INIT, + ACTIVE, +} linkstate_t; + static struct h5 { //struct net_buf *rx_buf; @@ -127,11 +133,7 @@ static struct h5 { u8_t rx_ack; - enum { - UNINIT, - INIT, - ACTIVE, - } link_state; + linkstate_t link_state; enum { START, @@ -554,7 +556,7 @@ static void tx_thread(void) static void h5_init(void) { - LOG_DBG(""); + LOG_DBG("h5_init"); h5.link_state = UNINIT; h5.rx_state = START; @@ -628,23 +630,23 @@ void bt_ctlr_assert_handle(char *file, u32_t line) DEVICE_INIT(hci_uart, "hci_uart", &h5_open, NULL, NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); -bool _link_ctrl_memcmp(struct net_buf const * const buf, u8_t const * const ref) { +bool _link_ctrl_memcmp(struct net_buf const * buf, u8_t const * const ref) { return !memcmp(buf->data, ref, 2); } -bool packet_is_sync(struct net_buf *buf) { +bool packet_is_sync(struct net_buf const *buf) { return _link_ctrl_memcmp(buf, sync_req); } -bool packet_is_sync_response(struct net_buf *buf) { +bool packet_is_sync_response(struct net_buf const *buf) { return _link_ctrl_memcmp(buf, sync_rsp); } -bool packet_is_config(struct net_buf *buf) { +bool packet_is_config(struct net_buf const *buf) { return _link_ctrl_memcmp(buf, conf_req); } -static bool packet_is_config_response(struct net_buf *buf) { +static bool packet_is_config_response(struct net_buf const *buf) { return _link_ctrl_memcmp(buf, conf_rsp); } @@ -699,6 +701,57 @@ void gpio_init() { if (ret) { printk("Error enabling callback!\n"); } + +} + +static linkstate_t do_uninit(struct net_buf const *buf) { + if (packet_is_sync_response(buf)) { + h5_send_config(); + return INIT; + } else { + /* SYNC is the answer to any non-SYNC_RESP packets in UNINIT state */ + h5_send_sync(); + return UNINIT; + } +} + +static linkstate_t do_init(struct net_buf const *buf) { + if (packet_is_config(buf)) { + h5_send_config_response(); + return INIT; + } else if (packet_is_config_response(buf)) { + h5.tx_win = conf_rsp[2] & 0x7; + h5.tx_seq = 0; + h5.tx_ack = 0; + LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win); + return ACTIVE; + } +} + +static linkstate_t do_active(struct net_buf const *buf) { + if (packet_is_config(buf)) { + h5_send_config_response(); + return ACTIVE; + } + + if (packet_is_config_response(buf)) { + return ACTIVE; + } + + if (packet_is_sync_response(buf) || packet_is_config(buf)) { + h5_send_sync(); + return UNINIT; + + } + + // Presumably something from the controller + u8_t type = bt_buf_get_type(buf); + if (type == BT_BUF_EVT) { + LOG_HEXDUMP_DBG(buf->data, buf->len, "CTRL -> HOST"); + h5_send(buf->data, HCI_EVENT_PKT, buf->len); + } else { + LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue"); + } } void main(void) @@ -716,45 +769,18 @@ void main(void) h5.link_state = UNINIT; h5_send_sync_response(); goto next; - } + } - if (h5.link_state == UNINIT) { - if (packet_is_sync_response(buf)) { - h5.link_state = INIT; - h5_send_config(); - } else { - /* SYNC is the answer to any non-SYNC_RESP packets in UNINIT - state */ - h5_send_sync(); - } - } else if (h5.link_state == INIT) { - if (packet_is_config(buf)) { - h5_send_config_response(); - } else if (packet_is_config_response(buf)) { - h5.link_state = ACTIVE; - h5.tx_win = conf_rsp[2] & 0x7; - h5.tx_seq = 0; - h5.tx_ack = 0; - LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win); - } - } else if (h5.link_state == ACTIVE) { - if (packet_is_config(buf)) { - h5_send_config_response(); - } else if (packet_is_config_response(buf)) { - goto next; - } else if (packet_is_sync_response(buf) || packet_is_config(buf)) { - h5.link_state = UNINIT; - h5_send_sync(); - } else { - // Presumably something from the controller - u8_t type = bt_buf_get_type(buf); - if (type == BT_BUF_EVT) { - LOG_HEXDUMP_DBG(buf->data, buf->len, "CTRL -> HOST"); - h5_send(buf->data, HCI_EVENT_PKT, buf->len); - } else { - LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue"); - } - } + switch (h5.link_state) { + case UNINIT: + h5.link_state = do_uninit(buf); + break; + case INIT: + h5.link_state = do_init(buf); + break; + case ACTIVE: + h5.link_state = do_active(buf); + break; } next: net_buf_unref(buf);