UART stops working after a few calls

While running UART rx and tx calls the uart_callback_set I have set during initialization will stop running. This prevents me from sending or receiving any new UART information. At no point in the code do I make attempts to change the UART configuration.

Here is the initialization code I am using.


static int uart_init(void)
{
int err;
int pos;
struct uart_data_t *rx;
struct uart_data_t *tx;

uart = device_get_binding(CONFIG_BT_NUS_UART_DEV);
if (!uart) {
return -ENXIO;
}

if (IS_ENABLED(CONFIG_USB)) {
err = usb_enable(NULL);
if (err) {
LOG_ERR("Failed to enable USB");
return err;
}
}

rx = k_malloc(sizeof(*rx));
if (rx) {
rx->len = 0;
} else {
return -ENOMEM;
}

k_work_init_delayable(&uart_work, uart_work_handler);


if (IS_ENABLED(CONFIG_BT_NUS_UART_ASYNC_ADAPTER) && !uart_test_async_api(uart)) {
/* Implement API adapter */
uart_async_adapter_init(async_adapter, uart);
uart = async_adapter;
}

err = uart_callback_set(uart, uart_cb, NULL);
if (err) {
LOG_ERR("Cannot initialize UART callback");
return err;
}

if (IS_ENABLED(CONFIG_UART_LINE_CTRL)) {
LOG_INF("Wait for DTR");
while (true) {
uint32_t dtr = 0;

uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &dtr);
if (dtr) {
break;
}
/* Give CPU resources to low priority threads. */
k_sleep(K_MSEC(100));
}
LOG_INF("DTR set");
err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DCD, 1);
if (err) {
LOG_WRN("Failed to set DCD, ret code %d", err);
}
err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DSR, 1);
if (err) {
LOG_WRN("Failed to set DSR, ret code %d", err);
}
}

tx = k_malloc(sizeof(*tx));

if (tx) {
pos = snprintf(tx->data, sizeof(tx->data),
"Starting Nordic UART service example\r\n");

if ((pos < 0) || (pos >= sizeof(tx->data))) {
k_free(tx);
LOG_ERR("snprintf returned %d", pos);
return -ENOMEM;
}

tx->len = pos;
} else {
return -ENOMEM;
}

err = uart_tx(uart, tx->data, tx->len, SYS_FOREVER_MS);
if (err) {
LOG_ERR("Cannot display welcome message (err: %d)", err);
return err;
}

return uart_rx_enable(uart, rx->data, sizeof(rx->data), 50);
}

Related