Skip to main content

UART API

The ALP SDK UART driver provides a CMSIS-Driver compatible interface for asynchronous serial communication across all E1M modules.

#include "alp_uart.h"

Initialization

#include "alp_uart.h"

/* Get the UART driver instance for channel 0 */
ALP_UART_Driver_t *uart = ALP_UART_GetDriver(0);

/* Initialize and power on */
uart->Initialize(uart_event_callback);
uart->PowerControl(ALP_POWER_FULL);

/* Configure: 115200 baud, 8N1 */
uart->Control(ALP_UART_MODE_ASYNCHRONOUS |
ALP_UART_DATA_BITS_8 |
ALP_UART_PARITY_NONE |
ALP_UART_STOP_BITS_1,
115200);

/* Enable TX and RX */
uart->Control(ALP_UART_CONTROL_TX, 1);
uart->Control(ALP_UART_CONTROL_RX, 1);

Sending Data

const char *msg = "Hello from ALP SDK\r\n";

uart->Send((const uint8_t *)msg, strlen(msg));
while (uart->GetStatus().tx_busy);

Receiving Data

uint8_t rx_buf[64];

uart->Receive(rx_buf, sizeof(rx_buf));
while (uart->GetStatus().rx_busy);

uint32_t count = uart->GetRxCount();
/* rx_buf contains 'count' bytes of received data */

Event Callback

void uart_event_callback(uint32_t event)
{
if (event & ALP_UART_EVENT_RECEIVE_COMPLETE) {
/* All requested bytes received */
}
if (event & ALP_UART_EVENT_SEND_COMPLETE) {
/* Transmit buffer fully sent */
}
if (event & ALP_UART_EVENT_RX_OVERFLOW) {
/* Receive buffer overrun */
}
if (event & ALP_UART_EVENT_RX_FRAMING_ERROR) {
/* Framing error detected */
}
}

Printf Redirect

A common pattern is to redirect printf() output to UART for debug logging:

#include <stdio.h>
#include "alp_uart.h"

static ALP_UART_Driver_t *debug_uart;

/* Retarget _write for newlib */
int _write(int fd, const char *buf, int len)
{
(void)fd;
debug_uart->Send((const uint8_t *)buf, len);
while (debug_uart->GetStatus().tx_busy);
return len;
}

void debug_uart_init(void)
{
debug_uart = ALP_UART_GetDriver(0);
debug_uart->Initialize(NULL);
debug_uart->PowerControl(ALP_POWER_FULL);
debug_uart->Control(ALP_UART_MODE_ASYNCHRONOUS |
ALP_UART_DATA_BITS_8 |
ALP_UART_PARITY_NONE |
ALP_UART_STOP_BITS_1,
115200);
debug_uart->Control(ALP_UART_CONTROL_TX, 1);
}

API Reference

Functions

FunctionDescription
ALP_UART_GetDriver()Get driver instance for a UART channel
Initialize()Initialize the UART driver with callback
Uninitialize()De-initialize and release resources
PowerControl()Set power state
Control()Configure baud rate, data format, flow control
Send()Transmit data
Receive()Start receiving data into a buffer
GetTxCount()Get number of bytes transmitted
GetRxCount()Get number of bytes received
GetStatus()Get current driver status
Abort()Abort send or receive operation

Events

EventDescription
ALP_UART_EVENT_SEND_COMPLETEAll data transmitted
ALP_UART_EVENT_RECEIVE_COMPLETERequested number of bytes received
ALP_UART_EVENT_RX_TIMEOUTReceive timeout (inter-byte gap)
ALP_UART_EVENT_RX_OVERFLOWHardware receive buffer overrun
ALP_UART_EVENT_RX_FRAMING_ERRORStop bit not detected
ALP_UART_EVENT_RX_PARITY_ERRORParity mismatch

Supported Baud Rates

Common baud rates: 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600.