Astra MCU SDK Peripheral Driver Library
 
Loading...
Searching...
No Matches

Topics

 Enumerated Types
 Enumerated types for UART driver.
 
 Data Structures
 Data structures for UART driver.
 
 Functions
 

Detailed Description

Driver API for Synaptics UART peripheral.

The UART (Universal Asynchronous Receiver/Transmitter) protocol is a serial communication protocol that allows full-duplex data exchange.

Features:

Note
UART APIs are divided into two categories:
Warning
Mixing high-level and low-level UART APIs in the same application is not supported. Each operates with different assumptions about interrupt handling, register state, and buffering. Using both on the same UART instance can lead to data loss, missed interrupts, and corrupted driver state. Choose one model per UART instance based on your use case.

Configuration Considerations

To initialize and use the UART driver:

PINMUX Configuration for UART

void sys_uart_config_mux(void)
{
#if CONFIG_MODULE_UART_0_ENABLED
/* configure UART0 pins */
HW_REG_FIELD_WRITE(GLOBAL_BASE_ADDRESS, GLOBAL_PERIF_UART_PORT_CTRL, UART0_RX_SEL, 0);
pinmux_set_pin_func(SPI_SLV_MISO, SPI_SLV_MISO__UART0_TX);
pinmux_set_pin_func(SPI_SLV_MOSI, SPI_SLV_MOSI__UART0_RX);
#endif
#if CONFIG_MODULE_UART_1_ENABLED
/* configure UART1 pins */
pinmux_set_pin_func(SPI_MSTR_MOSI, SPI_MSTR_MOSI__UART1_TX);
pinmux_set_pin_func(SPI_MSTR_MISO, SPI_MSTR_MISO__UART1_RX);
#endif
#if CONFIG_MODULE_UART_LP_ENABLED
/* configure LP_UART pins, use SPI_SLV_CLK/CS) as LP_UART_TX/RX */
pinmux_set_pin_func(SPI_SLV_CLK, SPI_SLV_CLK__UART_LP_TX);
pinmux_set_pin_func(SPI_SLV_CS, SPI_SLV_CS__UART_LP_RX);
#endif
}

General Functions.

uart_init

Initializes the UART hardware and internal driver structures for the specified UART instance.

if (uart_init(id) != UART_OK)
{
return UART_ERROR;
}

uart_deinit

Deinitializes the UART hardware and releases any resources held by the driver for the specified UART instance.

if (uart_deinit(id) != UART_OK)
{
return UART_ERROR;
}

uart_set_config

Sets the UART hardware configuration for the specified UART instance, including data bits, stop bits, parity, flow control, and baud rate. Programs the UART registers accordingly and returns the actual baud rate achieved.

uart_config_t config = {
.baud_rate = 230400,
.flow_control = 0,
.stop_bits = UART_STOP_BITS_ONE,
.parity = UART_PARITY_NONE,
.char_len = UART_CHAR_LEN_EIGHT
};
/* configure to UART mode: 8 bits, no parity, 1 stop bit, no flow control */
if (uart_set_configuration(id, &config, &achieved_baudrate) != UART_OK)
{
return UART_ERROR;
}

High-level Functions.

uart_put_non_blocking

Initiates a non-blocking transmit operation over UART for the specified instance. Data is written to the UART transmit FIFO and the function returns immediately. Completion is handled via callback.

/* Flag to indicate whether to wait until the TX FIFO is fully drained */
bool wait_for_tx_fifo_drain = true;
/* Transmit buffer containing the string to be sent over UART */
char uart_tx_buf[] = "UART_PUT_NON_BLOCKING_TESTING";
/* Length of data to transmit, including the null terminator */
uint32_t tx_size = sizeof(uart_tx_buf);
/* UART transmit configuration structure for non-blocking transfer */
uart_put_config_t put_config = {
.put_buff = uart_tx_buf, /* Pointer to transmit buffer */
.num = tx_size, /* Number of bytes to transmit */
.callback = fp_event_cb, /* Callback function on completion */
.user_data = NULL, /* Optional user data for callback */
.wait_for_tx_fifo_drain = wait_for_tx_fifo_drain /* Whether to wait for TX FIFO to fully drain */
};
/* Send buffer to UART TX FIFO using non-blocking API */
if ((rc = uart_put_non_blocking(id, &put_config)) != UART_OK)
{
/* Return error code if transmission request failed */
return rc;
}

uart_get_non_blocking

Initiates a non-blocking receive operation over UART for the specified instance. Attempts to read data from the UART receive FIFO into the provided buffer and returns immediately with the number of bytes received so far.

/* Variable to store the number of characters already available in RX FIFO */
uint32_t received_chars = 0;
/* Receive buffer size */
#define UART_RX_SIZE 10
/* Receive buffer to hold incoming UART data */
char uart_rx_buf[UART_RX_SIZE];
/* Number of bytes to receive */
uint32_t rx_size = UART_RX_SIZE;
/* UART receive configuration structure for non-blocking reception */
uart_get_config_t get_config = {
.get_buff = uart_rx_buf, /* Pointer to receive buffer */
.num = rx_size, /* Number of bytes to receive */
.callback = fp_event_cb, /* Callback function on data reception */
.user_data = NULL /* Optional user data for callback */
};
/* Initiate UART receive using non-blocking API */
if ((rc = uart_get_non_blocking(id, &get_config, &received_chars)) != UART_OK)
{
/* Return error code if receive request failed */
return rc;
}

Low-level Functions.

uart_put_blocking

Performs a blocking transmit operation over UART for the specified instance. Sends data from the provided buffer and blocks until all bytes are transmitted or the timeout occurs.

/* Flag to indicate whether to wait until the TX FIFO is fully drained */
bool wait_for_tx_fifo_drain = true;
/* Transmit buffer containing the string to be sent over UART */
char uart_tx_buf[] = "UART_PUT_BLOCKING_TESTING";
/* Length of data to transmit, including the null terminator */
uint32_t tx_size = sizeof(uart_tx_buf);
/* Timeout duration in milliseconds for the blocking transmit operation */
uint32_t timeout = 1000;
/* Transmit the data using a blocking UART API */
if ((rc = uart_put_blocking(id, uart_tx_buf, tx_size, timeout, wait_for_tx_fifo_drain)) != UART_OK)
{
/* Return the error code if transmission failed */
return rc;
}

uart_get_blocking

Performs a blocking receive operation over UART for the specified instance. Waits for the specified number of bytes to be received or until the timeout expires.

/* Receive buffer size */
#define UART_RX_SIZE 10
/* Receive buffer to hold incoming UART data */
char uart_rx_buf[UART_RX_SIZE];
/* Timeout duration in milliseconds for blocking receive */
uint32_t timeout = 1000;
/* Number of bytes to receive */
uint32_t rx_size = UART_RX_SIZE;
/* Receive data using blocking UART API */
if ((rc = uart_get_blocking(id, uart_rx_buf, rx_size, timeout)) != UART_OK)
{
/* Return error code if receive failed */
return rc;
}