Skip to content

Echo application with UART module

UART tutorial will guide you through initializing and working with the UART module inside your MCU using mikroSDK 2.0.

This example is a simple UART echo example, reading received bytes from RX and sending them back via TX.

To start, in the include section we will include the driver for UART called drv_uart.h:

#include "drv_uart.h

Next part involves defining the necessary driver context as well as the configuration structure and buffers. uart_rx_buffer and uart_tx_buffer are buffers used internally by the driver, therefore, they must be defined. buffer is our buffer that we will use to store data and size is a variable that will specify how many bytes were successfully read.

uart_t        uart;
uart_config_t uart_cfg;

static uint8_t uart_rx_buffer[256];
static uint8_t uart_tx_buffer[256];

uint8_t buffer[16];
uint8_t size;

Next we move onto the application_init() function, where we specify the UART pins and reference the buffers we created. The driver will choose the appropriate UART module based on the pins we specified:

uart_cfg.rx_pin = PC7;
uart_cfg.tx_pin = PC6;
uart.uart_tx_buffer = uart_tx_buffer;
uart.uart_rx_buffer = uart_rx_buffer;
uart_cfg.tx_ring_size = sizeof( uart_tx_buffer );
uart_cfg.rx_ring_size = sizeof( uart_rx_buffer );

uart_open( &uart, &uart_cfg );

uart_set_baud() function is used to specify the desired baud rate.

uart_set_baud( &uart, 115200 );

We can use uart_print() or uart_println() function to send some text via UART and test whether it was successfully initialized.

uart_println(&uart, "Starting echo:");

Since UART is now ready we can move onto the application_task() function, which will read a received byte and immediately send it back. We achieve that by writing the following code:

// Check how many bytes are read and send the receive data back via UART.
size = uart_read(&uart, buffer, sizeof(buffer));
if (size)
{
    uart_write(&uart, buffer, size);
}

Where size is the variable we created earlier for checking how many bytes were read via uart_read() function and stored in the buffer. We use uart_write() to send the same data back over UART.

The entire code can be seen below:

#include "drv_uart.h"
#include "board.h"

uart_t        uart;
uart_config_t uart_cfg;

static uint8_t buffer[16];
static uint8_t size;

static uint8_t uart_rx_buffer[256];
static uint8_t uart_tx_buffer[256];

void application_init ( void )
{
    uart_configure_default( &uart_cfg );

    uart_cfg.rx_pin = PC7;
    uart_cfg.tx_pin = PC6;
    uart_cfg.tx_ring_size = sizeof( uart_tx_buffer );
    uart_cfg.rx_ring_size = sizeof( uart_rx_buffer );

    uart_open( &uart, &uart_cfg );

    uart_set_baud( &uart, 115200 );

    uart_println( &uart, "Starting echo:" );
}

void application_task ( void )
{
    size = uart_read( &uart, buffer, sizeof( buffer ) );

    if ( size )
    {
        uart_write( &uart, buffer, size );
    }
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}