diff --git a/prj.conf b/prj.conf index e3278a0ec43deb1b598f2539e3bdee3eedb3712d..16e350265893480f04c919494320341195f997fa 100644 --- a/prj.conf +++ b/prj.conf @@ -1,11 +1,10 @@ # UART CONFIG_SERIAL=y -CONFIG_CONSOLE=y -CONFIG_UART_CONSOLE=y CONFIG_UART_INTERRUPT_DRIVEN=y # Logging CONFIG_LOG=y +CONFIG_LOG_OUTPUT=y CONFIG_NET_LOG=y CONFIG_BEAGLEPLAY_GREYBUS_LOG_LEVEL_DBG=y CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=y @@ -64,3 +63,6 @@ CONFIG_NET_CONTEXT_NET_PKT_POOL=y CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=4 CONFIG_NET_MAX_CONTEXTS=10 + +# MISC +CONFIG_RING_BUFFER=y diff --git a/src/hdlc.c b/src/hdlc.c index a67a4c863463549a807fae4c55c483b7c016eddf..20cf4c49f2821638805a1e6cf7e1e9c877f434f3 100644 --- a/src/hdlc.c +++ b/src/hdlc.c @@ -18,7 +18,7 @@ static void hdlc_tx_handler(struct k_work *); static void hdlc_rx_handler(struct k_work *); -LOG_MODULE_DECLARE(experiment, 4); +LOG_MODULE_DECLARE(cc1352_greybus, CONFIG_BEAGLEPLAY_GREYBUS_LOG_LEVEL); K_FIFO_DEFINE(hdlc_tx_queue); K_WORK_DEFINE(hdlc_tx_work, hdlc_tx_handler); @@ -72,7 +72,7 @@ static void hdlc_process_greybus_frame(struct hdlc_driver *drv) { // Do something with hdlc information. Starts at hdlc->rx_buffer[3] // Can be variable length char temp[20]; - size_t len = drv->rx_buffer_len - 2; + size_t len = drv->rx_buffer_len - 4; memcpy(temp, &drv->rx_buffer[2], len); temp[len] = '\0'; LOG_DBG("Got a Greybus Frame: %s", temp); @@ -87,7 +87,7 @@ static void hdlc_process_frame(struct hdlc_driver *drv) { if ((ctrl & 1) == 0) { drv->rx_send_seq = (ctrl >> 5) & 0x07; - } else if (address == ADDRESS_GREYBUS && drv->rx_buffer_len > 9) { + } else if (address == ADDRESS_GREYBUS) { hdlc_process_greybus_frame(drv); } else if (address == ADDRESS_DBG) { LOG_WRN("Ignore DBG Frame"); @@ -167,8 +167,8 @@ static void hdlc_rx_handler(struct k_work *work) { int hdlc_block_submit(uint8_t *buffer, size_t buffer_len, uint8_t address, uint8_t control) { - struct hdlc_block *block = - k_malloc(sizeof(struct hdlc_block) + sizeof(uint8_t) * buffer_len); + size_t block_size = sizeof(struct hdlc_block) + sizeof(uint8_t) * buffer_len; + struct hdlc_block *block = k_malloc(block_size); if (block == NULL) { return -1; @@ -182,7 +182,7 @@ int hdlc_block_submit(uint8_t *buffer, size_t buffer_len, uint8_t address, k_fifo_put(&hdlc_tx_queue, block); k_work_submit(&hdlc_tx_work); - return 0; + return block_size; } int hdlc_init() { diff --git a/src/main.c b/src/main.c index 3a212af9606e303a847714d1a8f0fd597d26993f..4fbaa6ae89cf680be3801953ee20edadb9327327 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ */ #include "control.h" +#include "hdlc.h" #include "node_handler.h" #include "node_table.h" #include "operations.h" @@ -29,10 +30,6 @@ LOG_MODULE_REGISTER(cc1352_greybus, CONFIG_BEAGLEPLAY_GREYBUS_LOG_LEVEL); -static const struct device *const uart_dev = DEVICE_DT_GET(UART_DEVICE_NODE); - -K_MSGQ_DEFINE(uart_msgq, sizeof(char), 10, 4); - static void node_discovery_entry(void *, void *, void *); static void node_reader_entry(void *, void *, void *); static void node_writer_entry(void *, void *, void *); @@ -61,7 +58,7 @@ static void node_reader_entry(void *p1, void *p2, void *p3) { goto sleep_label; } - LOG_DBG("Polling %u sockets", len); + // LOG_DBG("Polling %u sockets", len); for (i = 0; i < len; ++i) { fds[i].events = ZSOCK_POLLIN; } @@ -109,7 +106,7 @@ static void node_writer_entry(void *p1, void *p2, void *p3) { goto sleep_label; } - LOG_DBG("Polling %u sockets", len); + // LOG_DBG("Polling %u sockets", len); for (i = 0; i < len; ++i) { fds[i].events = ZSOCK_POLLOUT; } @@ -121,7 +118,7 @@ static void node_writer_entry(void *p1, void *p2, void *p3) { /// Send all pending requests ret = gb_operation_send_request_all(fds, len); - LOG_DBG("Written %d operations", ret); + // LOG_DBG("Written %d operations", ret); sleep_label: // Not sure why this is needed. @@ -155,7 +152,7 @@ static void node_discovery_entry(void *p1, void *p2, void *p3) { LOG_WRN("Failed to get greybus nodes"); continue; } - LOG_INF("Discoverd %u nodes", ret); + // LOG_INF("Discoverd %u nodes", ret); for (size_t i = 0; i < ret; ++i) { if (!node_table_is_active_by_addr(&node_array[i])) { @@ -169,50 +166,26 @@ static void node_discovery_entry(void *p1, void *p2, void *p3) { } // Put the thread to sleep for an interval - LOG_DBG("Going to sleep"); + // LOG_DBG("Going to sleep"); k_msleep(NODE_DISCOVERY_INTERVAL); } } static void serial_callback(const struct device *dev, void *user_data) { - char c; - - if (!uart_irq_update(uart_dev)) { - return; - } - - if (!uart_irq_rx_ready(uart_dev)) { - return; - } - - while (uart_fifo_read(uart_dev, &c, 1) == 1) { - if (c == '1' || c == '2') { - k_msgq_put(&uart_msgq, &c, K_NO_WAIT); - } else { - LOG_DBG("Invalid Input: %u", c); - } - } -} - -static void print_uart(char *buf) { - int msg_len = strlen(buf); - - for (int i = 0; i < msg_len; i++) { - uart_poll_out(uart_dev, buf[i]); - } + hdlc_rx_submit(); } void main(void) { LOG_INF("Starting BeaglePlay Greybus"); int ret; - char tx; - if (!device_is_ready(uart_dev)) { LOG_ERR("UART device not found!"); return; } + hdlc_init(); + ret = uart_irq_callback_user_data_set(uart_dev, serial_callback, NULL); if (ret < 0) { if (ret == -ENOTSUP) { @@ -227,7 +200,5 @@ void main(void) { uart_irq_rx_enable(uart_dev); - while (k_msgq_get(&uart_msgq, &tx, K_FOREVER) == 0) { - LOG_DBG("Pressed: %c", tx); - } + k_sleep(K_FOREVER); }