I am using UART to try to send and receive data from the MCU on my nRF9160 board. I noticed in the tutorial on DevAcademy that using the asynchrounous mode, we're able to control the amount of data received before an interrupt is triggered. Right now, the interrupt is triggered whenever I type a single character, but I was wondering how we can make the MCU receive more data before the callback is called. Below is my codes:
#include <zephyr.h>
#include <drivers/gpio.h>
#include <sys/printk.h>
/* include the header file of the UART driver */
#include <drivers/uart.h>
/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000
// define the size of the receive buffer and receive timeout
#define RECEIVE_BUFF_SIZE 10
#define RECEIVE_TIMEOUT 100
// get the device pointer of the UART hardware
const struct device *uart = DEVICE_DT_GET(DT_NODELABEL(uart0));
// define the transmission buffer to send over UART
static uint8_t tx_buf[] = "Type anything\r\n";
// define the receive buffer, an array of strings
static uint8_t rx_buf[RECEIVE_BUFF_SIZE];
// define the callback function for UART
static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data)
{
switch (evt->type)
{
case UART_RX_RDY:
printk("the received data is: %c, and the length of the data is %d\r\n", evt->data.rx.buf[evt->data.rx.offset], evt->data.rx.len);
break;
case UART_RX_DISABLED:
// when the receive buffer is full, reenable receiving
uart_rx_enable(dev, rx_buf, sizeof rx_buf, RECEIVE_TIMEOUT);
break;
default:
break;
}
}
int main(void)
{
int ret;
// verify that the UART device is ready
if (!device_is_ready(uart))
{
printk("UART device not ready\r\n");
return 1;
}
// register the UART callback function
ret = uart_callback_set(uart, uart_cb, NULL);
if (ret)
{
return 1;
}
// send the data over UART by calling uart_tx()
ret = uart_tx(uart, tx_buf, sizeof(tx_buf), SYS_FOREVER_US);
if (ret)
{
return 1;
}
// start receiving by calling uart_rx_enable() and pass it the address of the receive buffer
ret = uart_rx_enable(uart, rx_buf, sizeof rx_buf, RECEIVE_TIMEOUT);
if (ret)
{
return 1;
}
while (1)
{
k_msleep(SLEEP_TIME_MS);
}
}