diff --git a/CMakeLists.txt b/CMakeLists.txt index 667fb61..592f707 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,7 @@ +cmake_minimum_required(VERSION 3.13.1) set(CONF_FILE c3.conf) include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) -project(NONE) - -target_link_libraries(app subsys__bluetooth) +project(hci_uart_h5) target_sources(app PRIVATE src/main.c) diff --git a/c3.conf b/c3.conf index adb48a2..9e5d2b4 100644 --- a/c3.conf +++ b/c3.conf @@ -3,7 +3,6 @@ CONFIG_UART_CONSOLE=n CONFIG_GPIO=y CONFIG_SERIAL=y CONFIG_UART_INTERRUPT_DRIVEN=y -CONFIG_UART_NRF5_BAUD_RATE=115200 CONFIG_MAIN_STACK_SIZE=512 CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=512 CONFIG_BT=y @@ -14,12 +13,13 @@ CONFIG_BT_CTLR_DTM_HCI=y CONFIG_BT_CTLR_ASSERT_HANDLER=y # Bootloader stuff -CONFIG_TEXT_SECTION_OFFSET=0x200 +CONFIG_BOOTLOADER_MCUBOOT=y CONFIG_REBOOT=y # Console CONFIG_HAS_SEGGER_RTT=y -CONFIG_SYS_LOG=y -CONFIG_SYS_LOG_DEFAULT_LEVEL=4 +CONFIG_USE_SEGGER_RTT=y +CONFIG_LOG=y +CONFIG_LOG_DEFAULT_LEVEL=4 CONFIG_RTT_CONSOLE=y -CONFIG_CONSOLE=y \ No newline at end of file +CONFIG_CONSOLE=y diff --git a/src/main.c b/src/main.c index a6fa145..76bfd15 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ #include #include #include -#include +#include #include #include @@ -28,7 +28,7 @@ #include #include -#include +//#include #include #include "common/log.h" @@ -54,10 +54,10 @@ NET_BUF_POOL_DEFINE(cmd_tx_pool, CONFIG_BT_HCI_CMD_COUNT, CMD_BUF_SIZE, NET_BUF_POOL_DEFINE(acl_tx_pool, TX_BUF_COUNT, BT_BUF_ACL_SIZE, BT_BUF_USER_DATA_MIN, NULL); -static BT_STACK_NOINIT(tx_stack, 256); +static K_THREAD_STACK_DEFINE(tx_stack, 256); static struct k_thread tx_thread_data; -static BT_STACK_NOINIT(unproc_stack, 256); +static K_THREAD_STACK_DEFINE(unproc_stack, 256); static struct k_thread unproc_thread_data; static struct k_delayed_work ack_work; @@ -167,6 +167,7 @@ static inline void bt_uart_drain(struct device *dev) } } +/* static void process_unack(void) { u8_t next_seq = h5.tx_seq; @@ -176,39 +177,39 @@ static void process_unack(void) return; } - SYS_LOG_DBG("rx_ack %u tx_ack %u tx_seq %u unack_queue_len %u", + LOG_DBG("rx_ack %u tx_ack %u tx_seq %u unack_queue_len %u", h5.rx_ack, h5.tx_ack, h5.tx_seq, unack_queue_len); while (unack_queue_len > 0) { if (next_seq == h5.rx_ack) { - /* Next sequence number is the same as last received - * ack number - */ + // Next sequence number is the same as last received + // ack number + // break; } number_removed--; - /* Similar to (n - 1) % 8 with unsigned conversion */ + // Similar to (n - 1) % 8 with unsigned conversion next_seq = (next_seq - 1) & 0x07; } if (next_seq != h5.rx_ack) { - SYS_LOG_ERR("Wrong sequence: rx_ack %u tx_seq %u next_seq %u", + LOG_ERR("Wrong sequence: rx_ack %u tx_seq %u next_seq %u", h5.rx_ack, h5.tx_seq, next_seq); } - SYS_LOG_DBG("Need to remove %u packet from the queue", number_removed); + LOG_DBG("Need to remove %u packet from the queue", number_removed); while (number_removed) { struct net_buf *buf = net_buf_get(&h5.unack_queue, K_NO_WAIT); if (!buf) { - SYS_LOG_ERR("Unack queue is empty"); + LOG_ERR("Unack queue is empty"); break; } - /* TODO: print or do something with packet */ - SYS_LOG_DBG("Remove buf from the unack_queue"); + // TODO: print or do something with packet + LOG_DBG("Remove buf from the unack_queue"); //net_buf_unref(buf); unack_queue_len--; @@ -219,16 +220,17 @@ static void process_unack(void) static void h5_print_header(const u8_t *hdr, const char *str) { if (H5_HDR_RELIABLE(hdr)) { - SYS_LOG_DBG("%s REL: seq %u ack %u crc %u type %u len %u", + LOG_DBG("%s REL: seq %u ack %u crc %u type %u len %u", str, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), H5_HDR_CRC(hdr), H5_HDR_PKT_TYPE(hdr), H5_HDR_LEN(hdr)); } else { - SYS_LOG_DBG("%s UNREL: ack %u crc %u type %u len %u", + LOG_DBG("%s UNREL: ack %u crc %u type %u len %u", str, H5_HDR_ACK(hdr), H5_HDR_CRC(hdr), H5_HDR_PKT_TYPE(hdr), H5_HDR_LEN(hdr)); } } +*/ static void hexdump(const char *str, const u8_t *packet, size_t length) { @@ -323,7 +325,7 @@ static void retx_timeout(struct k_work *work) { ARG_UNUSED(work); - SYS_LOG_DBG("unack_queue_len %u", unack_queue_len); + LOG_DBG("unack_queue_len %u", unack_queue_len); if (unack_queue_len) { struct k_fifo tmp_queue; @@ -355,7 +357,7 @@ static void ack_timeout(struct k_work *work) { ARG_UNUSED(work); - SYS_LOG_DBG(""); + LOG_DBG(""); h5_send(NULL, HCI_3WIRE_ACK_PKT, 0); @@ -382,7 +384,7 @@ int unslip_next_byte(struct net_buf *buf) { if (next == SLIP_ESC_DELIM) { return SLIP_DELIMITER; } - SYS_LOG_WRN("Bad Escape Seqence: %02X %02X", SLIP_ESC, next); + LOG_WRN("Bad Escape Seqence: %02X %02X", SLIP_ESC, next); return -2; } @@ -440,7 +442,7 @@ int pull_header(struct net_buf *buf, u8_t *hdr) { // Checksum if (((hdr[3] + hdr[0] + hdr[1] + hdr[2]) & 0xff) != 0xff) { - SYS_LOG_WRN("Invalid Header Checksum\n"); + LOG_WRN("Invalid Header Checksum\n"); } return 0; @@ -465,7 +467,7 @@ static void unproc_thread(void) { case HCI_ACLDATA_PKT: rx_buf = net_buf_alloc(&acl_tx_pool, K_NO_WAIT); if (!rx_buf) { - SYS_LOG_WRN("No available data buffers"); + LOG_WRN("No available data buffers"); sys_reboot(SYS_REBOOT_COLD); goto next; } @@ -474,28 +476,28 @@ static void unproc_thread(void) { case HCI_COMMAND_PKT: rx_buf = net_buf_alloc(&cmd_tx_pool, K_NO_WAIT); if (!rx_buf) { - SYS_LOG_WRN("No available data buffers"); + 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: - SYS_LOG_DBG("ACK PACKET"); + 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) { - SYS_LOG_WRN("No available signal buffers"); + LOG_WRN("No available signal buffers"); sys_reboot(SYS_REBOOT_COLD); goto next; } - SYS_LOG_DBG("ALLOC %p", rx_buf); + LOG_DBG("ALLOC %p", rx_buf); break; default: - SYS_LOG_ERR("Wrong packet type from host: %u", H5_HDR_PKT_TYPE(hdr)); + LOG_ERR("Wrong packet type from host: %u", H5_HDR_PKT_TYPE(hdr)); goto next; } @@ -505,7 +507,7 @@ static void unproc_thread(void) { } if (H5_HDR_LEN(hdr) != rx_buf->len) { - SYS_LOG_ERR("Payload too short\n"); + LOG_ERR("Payload too short\n"); goto next; } @@ -517,7 +519,7 @@ static void unproc_thread(void) { */ if (H5_HDR_RELIABLE(hdr) && H5_HDR_SEQ(hdr) != h5.tx_ack) { - SYS_LOG_ERR("Seq expected %u got %u. Drop packet", h5.tx_ack, + LOG_ERR("Seq expected %u got %u. Drop packet", h5.tx_ack, H5_HDR_SEQ(hdr)); goto next; } @@ -542,11 +544,11 @@ static void unproc_thread(void) { break; case HCI_COMMAND_PKT: case HCI_ACLDATA_PKT: - //SYS_LOG_DBG("Adding to controller queue\n"); + //LOG_DBG("Adding to controller queue\n"); net_buf_put(&h5.controller_queue, rx_buf); break; default: - SYS_LOG_WRN("Unknown packet type %u\n", H5_HDR_PKT_TYPE(hdr)); + LOG_WRN("Unknown packet type %u\n", H5_HDR_PKT_TYPE(hdr)); break; } next: @@ -559,7 +561,7 @@ static void h5_send_config(void); static void tx_thread(void) { - SYS_LOG_DBG("TX Thread is alive."); + LOG_DBG("TX Thread is alive."); while (true) { struct net_buf *buf; @@ -596,7 +598,7 @@ static void tx_thread(void) static void h5_init(void) { - SYS_LOG_DBG(""); + LOG_DBG(""); h5.link_state = UNINIT; h5.rx_state = START; @@ -635,7 +637,7 @@ static void h5_init(void) static int h5_open(struct device *unused) { - SYS_LOG_DBG("Open"); + LOG_DBG("Open"); hci_uart_dev = device_get_binding(CONFIG_BT_CTLR_TO_HOST_UART_DEV_NAME); if (!hci_uart_dev) { @@ -713,6 +715,9 @@ void gpio_callback(struct device *port, sys_reboot(SYS_REBOOT_COLD); } +#define BOOTLOADER_REQ_GPIO_PIN 9 +#define BOOTLOADER_STATUS_GPIO_PIN 9 + void gpio_init() { int ret; gpio_dev = device_get_binding("GPIO_0"); @@ -739,7 +744,7 @@ void gpio_init() { void main(void) { - SYS_LOG_DBG("Start"); + LOG_DBG("Start"); gpio_init(); // Adds controller output to host output queue bt_enable_raw(&h5.host_queue); @@ -771,7 +776,7 @@ void main(void) h5.tx_win = conf_rsp[2] & 0x7; h5.tx_seq = 0; h5.tx_ack = 0; - SYS_LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win); + LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win); } } else if (h5.link_state == ACTIVE) { if (packet_is_config(buf)) { @@ -788,7 +793,7 @@ void main(void) //hexdump("From CTRL To HOST => ", buf->data, buf->len); h5_send(buf->data, HCI_EVENT_PKT, buf->len); } else { - SYS_LOG_DBG("WTF is this (%u): ", bt_buf_get_type(buf)); + LOG_DBG("WTF is this (%u): ", bt_buf_get_type(buf)); hexdump("CTRL WTF: ", buf->data, buf->len); } }