Hello,
NCS1.9.1, VSCode, nRF52840,
from link UART Driver – Nordic Developer Academy (nordicsemi.com)
uart can't to receive data continuously,
receiving evt->data.rx.offset will Increasing,
when rx_buf[128] is exhausted, will got evt
UART_RX_BUF_RELEASED
UART_RX_DISABLED
it will lost data,even call uart_rx_enable(dev, rx_buf, sizeof(rx_buf), 1000); again
is it a bug or missing some step?
#include <zephyr.h> #include <device.h> #include <drivers/gpio.h> #include <sys/util.h> #include <sys/printk.h> #include <inttypes.h> #include "uart.h" #include "stdlib.h" #include <drivers/uart.h> const struct device *uart=NULL; static uint8_t rx_buf[128] = {0}; //A buffer to store incoming UART data static uint8_t tx_buf[] = {"uart started\n\r"}; static uint8_t tem_show_buf[128] = {0}; const struct uart_config uart_cfg = { .baudrate = 115200, .parity = UART_CFG_PARITY_NONE, .stop_bits = UART_CFG_STOP_BITS_1, .data_bits = UART_CFG_DATA_BITS_8, .flow_ctrl = UART_CFG_FLOW_CTRL_NONE }; static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data) { switch (evt->type) { case UART_TX_DONE: printk("Tx sent %d bytesn\n", evt->data.tx.len); break; case UART_TX_ABORTED: printk("Tx aborted\n"); break; case UART_RX_RDY: { int l=evt->data.rx.len; memcpy(tem_show_buf,evt->data.rx.buf+evt->data.rx.offset,l);tem_show_buf[l]=0; printk("Received data:[%d]%s len:%d bytes\n", evt->data.rx.offset,tem_show_buf,l); }break; case UART_RX_BUF_REQUEST: // uint8_t *buf; // err = k_mem_slab_alloc(&uart_slab, (void **)&buf, K_NO_WAIT); // __ASSERT(err == 0, "Failed to allocate slab"); // err = uart_rx_buf_rsp(uart, buf, BUF_SIZE); // __ASSERT(err == 0, "Failed to provide new buffer"); printk("UART_RX_BUF_REQUEST\n"); break; case UART_RX_BUF_RELEASED: // k_mem_slab_free(&uart_slab, (void **)&evt->data.rx_buf.buf); printk("UART_RX_BUF_RELEASED\n"); break; case UART_RX_DISABLED: uart_rx_enable(dev, rx_buf, sizeof(rx_buf), 1000); printk("UART_RX_DISABLED\n"); break; case UART_RX_STOPPED: printk("UART_RX_STOPPED\n"); break; default: break; } } int uart_init() { uart= device_get_binding(DT_LABEL(DT_NODELABEL(uart0))); if (uart == NULL) { printk("Could not find %s!\n\r", DT_LABEL(DT_NODELABEL(uart0))); return; } int err = uart_configure(uart, &uart_cfg); if (err == -ENOSYS) { return -ENOSYS; } err = uart_callback_set(uart, uart_cb, NULL); if (err) { return err; } uart_rx_enable(uart ,rx_buf,sizeof rx_buf,1000); return 0; }
Best regards