diff --git a/c3-mcuboot.conf b/c3-mcuboot.conf new file mode 100644 index 0000000..9e5d2b4 --- /dev/null +++ b/c3-mcuboot.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/c3.conf b/c3.conf index 9e5d2b4..2bdf9d1 100644 --- a/c3.conf +++ b/c3.conf @@ -13,7 +13,6 @@ CONFIG_BT_CTLR_DTM_HCI=y CONFIG_BT_CTLR_ASSERT_HANDLER=y # Bootloader stuff -CONFIG_BOOTLOADER_MCUBOOT=y CONFIG_REBOOT=y # Console diff --git a/src/main.c b/src/main.c index 76bfd15..0c407d7 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,11 @@ #include #include #include + +#define LOG_MODULE_NAME hci_uart_h5 #include + + #include #include @@ -232,37 +236,6 @@ static void h5_print_header(const u8_t *hdr, const char *str) } */ -static void hexdump(const char *str, const u8_t *packet, size_t length) -{ - int n = 0; - - if (!length) { - printk("%s zero-length signal packet\n", str); - return; - } - - while (length--) { - if (n % 16 == 0) { - printk("%s %08X ", str, n); - } - - printk("%02X ", *packet++); - - n++; - if (n % 8 == 0) { - if (n % 16 == 0) { - printk("\n"); - } else { - printk(" "); - } - } - } - - if (n % 16) { - printk("\n"); - } -} - static u8_t h5_slip_byte(u8_t byte) { switch (byte) { @@ -448,12 +421,18 @@ int pull_header(struct net_buf *buf, u8_t *hdr) { return 0; } +void reboot_if_not(struct net_buf *buf) { + if (!buf) { + LOG_ERR("Failed alloc at %s:%d", __FILE__, __LINE__); + sys_reboot(SYS_REBOOT_COLD); + } +} + static void unproc_thread(void) { struct net_buf *buf; while (true) { buf = net_buf_get(&h5.unprocessed_queue, K_FOREVER); - //hexdump("Packet: ", buf->data, buf->len); u8_t hdr[4]; if (pull_header(buf, hdr) < 0) { @@ -464,42 +443,28 @@ static void unproc_thread(void) { struct net_buf *rx_buf = NULL; switch (H5_HDR_PKT_TYPE(hdr)) { - case HCI_ACLDATA_PKT: - rx_buf = net_buf_alloc(&acl_tx_pool, K_NO_WAIT); - if (!rx_buf) { - LOG_WRN("No available data buffers"); - sys_reboot(SYS_REBOOT_COLD); - goto next; - } - bt_buf_set_type(rx_buf, BT_BUF_ACL_OUT); - break; - case HCI_COMMAND_PKT: - rx_buf = net_buf_alloc(&cmd_tx_pool, K_NO_WAIT); - if (!rx_buf) { - LOG_WRN("No available data buffers"); - sys_reboot(SYS_REBOOT_COLD); - goto next; - } - bt_buf_set_type(rx_buf, BT_BUF_CMD); - break; - case HCI_3WIRE_ACK_PKT: - LOG_DBG("ACK PACKET"); - h5.rx_ack = H5_HDR_ACK(hdr); - goto next; - break; - case HCI_3WIRE_LINK_PKT: - rx_buf = net_buf_alloc(&h5_pool, K_NO_WAIT); - if (!rx_buf) { - LOG_WRN("No available signal buffers"); - sys_reboot(SYS_REBOOT_COLD); - goto next; - } - LOG_DBG("ALLOC %p", rx_buf); - break; - default: - LOG_ERR("Wrong packet type from host: %u", H5_HDR_PKT_TYPE(hdr)); - goto next; - } + case HCI_ACLDATA_PKT: + rx_buf = net_buf_alloc(&acl_tx_pool, K_NO_WAIT); + reboot_if_not(rx_buf); + bt_buf_set_type(rx_buf, BT_BUF_ACL_OUT); + break; + case HCI_COMMAND_PKT: + rx_buf = net_buf_alloc(&cmd_tx_pool, K_NO_WAIT); + reboot_if_not(rx_buf); + bt_buf_set_type(rx_buf, BT_BUF_CMD); + break; + case HCI_3WIRE_ACK_PKT: + h5.rx_ack = H5_HDR_ACK(hdr); + goto next; + break; + case HCI_3WIRE_LINK_PKT: + rx_buf = net_buf_alloc(&h5_pool, K_NO_WAIT); + reboot_if_not(rx_buf); + break; + default: + LOG_ERR("Wrong packet type from host: %u", H5_HDR_PKT_TYPE(hdr)); + goto next; + } int byte; while ((byte = unslip_next_byte(buf)) >= 0) { @@ -511,46 +476,37 @@ static void unproc_thread(void) { goto next; } - //h5_print_header(hdr, "From Host =>"); - //hexdump("\tDecoded: ", rx_buf->data, rx_buf->len); - /* Check when full packet is received, it can be done - * when parsing packet header but we need to receive - * full packet anyway to clear UART. - */ - if (H5_HDR_RELIABLE(hdr) && + if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5.tx_ack) { - LOG_ERR("Seq expected %u got %u. Drop packet", h5.tx_ack, - H5_HDR_SEQ(hdr)); - goto next; - } + LOG_ERR("Seq expected %u got %u. Drop packet", h5.tx_ack, H5_HDR_SEQ(hdr)); + goto next; + } h5.rx_ack = H5_HDR_ACK(hdr); if (reliable_packet(H5_HDR_PKT_TYPE(hdr))) { /* For reliable packet increment next transmit ack number */ - h5.tx_ack = (h5.tx_ack + 1) % 8; - /* Submit delayed work to ack the packet */ - k_delayed_work_submit(&ack_work, H5_RX_ACK_TIMEOUT); + h5.tx_ack = (h5.tx_ack + 1) % 8; + /* Submit delayed work to ack the packet */ + k_delayed_work_submit(&ack_work, H5_RX_ACK_TIMEOUT); } - //process_unack(); - - switch (H5_HDR_PKT_TYPE(hdr)) { - case HCI_3WIRE_ACK_PKT: - // No further action required - break; - case HCI_3WIRE_LINK_PKT: - net_buf_put(&h5.host_queue, rx_buf); - break; - case HCI_COMMAND_PKT: - case HCI_ACLDATA_PKT: - //LOG_DBG("Adding to controller queue\n"); - net_buf_put(&h5.controller_queue, rx_buf); - break; - default: - LOG_WRN("Unknown packet type %u\n", H5_HDR_PKT_TYPE(hdr)); - break; - } + switch (H5_HDR_PKT_TYPE(hdr)) { + case HCI_3WIRE_ACK_PKT: + // No further action required + break; + case HCI_3WIRE_LINK_PKT: + net_buf_put(&h5.host_queue, rx_buf); + break; + case HCI_COMMAND_PKT: + case HCI_ACLDATA_PKT: + //LOG_DBG("Adding to controller queue\n"); + net_buf_put(&h5.controller_queue, rx_buf); + break; + default: + LOG_WRN("Unknown packet type %u\n", H5_HDR_PKT_TYPE(hdr)); + break; + } next: net_buf_unref(buf); } @@ -579,8 +535,8 @@ static void tx_thread(void) if (!buf) { break; } - hexdump("TX_QUEUE -> CTRL", buf->data, buf->len); - bt_send(buf); + //LOG_HEXDUMP_DBG(buf->data, buf->len, "TX_QUEUE -> CTRL"); + bt_send(buf); /* buf is dequeued from tx_queue and queued to unack * queue. @@ -715,9 +671,8 @@ void gpio_callback(struct device *port, sys_reboot(SYS_REBOOT_COLD); } -#define BOOTLOADER_REQ_GPIO_PIN 9 -#define BOOTLOADER_STATUS_GPIO_PIN 9 - +#define BOOTLOADER_REQ_GPIO_PIN 8 +#define BOOTLOADER_STATUS_GPIO_PIN 7 void gpio_init() { int ret; gpio_dev = device_get_binding("GPIO_0"); @@ -790,11 +745,10 @@ void main(void) // Presumably something from the controller u8_t type = bt_buf_get_type(buf); if (type == BT_BUF_EVT) { - //hexdump("From CTRL To HOST => ", buf->data, buf->len); + LOG_HEXDUMP_DBG(buf->data, buf->len, "CTRL -> HOST"); h5_send(buf->data, HCI_EVENT_PKT, buf->len); } else { - LOG_DBG("WTF is this (%u): ", bt_buf_get_type(buf)); - hexdump("CTRL WTF: ", buf->data, buf->len); + LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue"); } } }