UART

../_images/uart.svg

Examples

#include "halmcu/system.h"
#include "halmcu/irq.h"
#include "halmcu/hal/uart.h"
#include "halmcu/hal/gpio.h"
#include "halmcu/hal/clk.h"

//#define POLLING

static uart_handle_t uart0_handle;

static void uart_rx_handler(uint32_t flags)
{
	(void)flags;
	uint8_t c;
	uart_read(PERIPH_UART0, &c, 1);
	uart_write(PERIPH_UART0, "Received!\r\n", 11);
}

static void system_clock_init(void)
{
	clk_enable_source(CLK_HSI);
	clk_set_pll_frequency(CLK_PLL, CLK_HSI, 16000000);
	clk_start_pll();
	clk_set_source(CLK_PLL);
	while (!clk_is_pll_locked()) ;
}

static void uart_gpio_init(void)
{
	struct gpio_cfg cfg = {
		.mode = GPIO_MODE_PUSHPULL,
		.altfunc = true,
		.altfunc_number = 1,
	};
	gpio_open(PERIPH_GPIOC, 9, &cfg); // TX

	cfg.mode = GPIO_MODE_INPUT_PULLUP;
	gpio_open(PERIPH_GPIOC, 8, &cfg); // RX
}

int main(void)
{
	system_clock_init();

	uart_gpio_init();
	uart_init(PERIPH_UART0, &(struct uart_cfg) {
			.wordsize = UART_WORDSIZE_8,
			.stopbit = UART_STOPBIT_1,
			.parity = UART_PARITY_NONE,
			.baudrate = 115200,
#if !defined(POLLING)
			.rx_interrupt = true,
#endif
			},
			&uart0_handle);
	uart_register_rx_handler(&uart0_handle, uart_rx_handler);

	uart_write(PERIPH_UART0, "Hello, World!\r\n", 15);

	while (1) {
		uint8_t ch;
		size_t received = uart_read(PERIPH_UART0, &ch, sizeof(ch));
		if (received > 0) {
			uart_write(PERIPH_UART0, &ch, sizeof(ch));
		}
	}

	return 0;
}

void ISR_UART0(void)
{
	uart_default_isr(PERIPH_UART0, &uart0_handle);
}

HAL

Typedefs

typedef void (*uart_irq_callback_t)(uint32_t flags)

UART handler type

Functions

bool uart_init(periph_t uart, const struct uart_cfg *cfg, uart_handle_t *handle)

Initialize UART port with given configuration.

Parameters
  • uart[in] a peripheral enumerated in periph_t

  • cfg[in] configuration

  • handle[in] handle of uart port

Returns

true on success

void uart_deinit(periph_t uart)

Deinitialize UART port.

Parameters

uart[in] a peripheral enumerated in periph_t

size_t uart_read(periph_t uart, void *buf, size_t bufsize)

Read bytes from UART port.

Parameters
  • uart[in] a peripheral enumerated in periph_t

  • buf[out] receive buffer address

  • bufsize[in] buffer sizie

size_t uart_write(periph_t uart, const void *data, size_t datasize)

Write data to UART port.

Parameters
  • uart[in] a peripheral enumerated in periph_t

  • data[in] data buffer address

  • datasize[in] data size to send

int uart_read_byte(periph_t port)

Read a byte from UART.

Parameters

port[in] a peripheral enumerated in periph_t

Returns

received byte

int uart_read_byte_nonblock(periph_t port)

Read a byte from UART.

Note

This function is non-blocking.

Parameters

port[in] a peripheral enumerated in periph_t

Returns

received bytes on success

Returns

-1 – when no received data

void uart_write_byte(periph_t port, uint8_t val)

Write a byte to UART.

This function will block until the byte gets written into the hold register.

Parameters
  • port[in] a peripheral enumerated in periph_t

  • val[in] value to write

void uart_register_rx_handler(uart_handle_t *handle, uart_irq_callback_t handler)

Register rx interrupt handler.

Parameters
  • handle[in] handle of uart port

  • handler[in] rx interrupt handler

void uart_register_tx_handler(uart_handle_t *handle, uart_irq_callback_t handler)

Register tx ready interrupt handler.

Parameters
  • handle[in] handle of uart port

  • handler[in] tx ready interrupt handler

void uart_register_error_handler(uart_handle_t *handle, uart_irq_callback_t handler)

Register error interrupt handler.

Parameters
  • handle – handle of uart port

  • handler – error interrupt handler

void uart_default_isr(periph_t uart, const uart_handle_t *handle)

The default UART interrupt handler.

Parameters
  • uart[in] a peripheral enumerated in periph_t

  • handle – handle of uart port

struct uart_cfg
#include <uart.h>

UART configuration

Public Members

uart_wordsize_t wordsize
uart_stopbit_t stopbit
uart_parity_t parity
unsigned int baudrate
bool rx_interrupt
bool tx_interrupt
union uart_handle_t
#include <uart.h>

UART handle type

Public Members

char _size[sizeof(struct uart_cfg) + 12]
long _align

LL

Enums

enum uart_parity_t

UART parity enumeration

Values:

enumerator UART_PARITY_NONE
enumerator UART_PARITY_ODD
enumerator UART_PARITY_EVEN
enum uart_stopbit_t

UART stopbit enumeration

Values:

enumerator UART_STOPBIT_1
enumerator UART_STOPBIT_1_5
enumerator UART_STOPBIT_2
enum uart_wordsize_t

UART wordsize enumeration

Values:

enumerator UART_WORDSIZE_8
enumerator UART_WORDSIZE_9
enumerator UART_WORDSIZE_7
enumerator UART_WORDSIZE_6
enumerator UART_WORDSIZE_5
enum uart_irq_t

UART irq enumeration

Values:

enumerator UART_IRQ_NONE
enumerator UART_IRQ_RX
enumerator UART_IRQ_TX_READY
enumerator UART_IRQ_MASK
enum uart_event_t

UART event enumeration

Values:

enumerator UART_EVENT_BIT
enumerator UART_EVENT_RX
enumerator UART_EVENT_TX_READY
enumerator UART_EVENT_ERROR
enumerator UART_EVENT_MASK

Functions

void uart_reset(periph_t port)

Reset UART interface.

This function makes the given UART the reset default state.

Parameters

port[in] a peripheral enumerated in periph_t

bool uart_has_rx(periph_t port)
bool uart_is_tx_ready(periph_t port)
int uart_get_rxd(periph_t port)
void uart_set_txd(periph_t port, uint32_t value)
void uart_enable_irq(periph_t port, uart_irq_t irqs)

Enable UART interrupts.

Parameters
void uart_disable_irq(periph_t port, uart_irq_t irqs)

Disable UART interrupts.

Parameters
void uart_start(periph_t port)
void uart_stop(periph_t port)
void uart_set_baudrate(periph_t port, uint32_t baudrate, uint32_t pclk)

Set UART baudrate.

Parameters
  • port[in] a peripheral enumerated in periph_t

  • baudrate[in] baudrate

  • pclk[in] pclk

uart_event_t uart_get_event(periph_t port)

Read UART event flag.

Parameters

port[in] a peripheral enumerated in periph_t

Returns

event uart_event_t

void uart_clear_event(periph_t port, uart_event_t events)

Clear UART event flag.

Parameters
void uart_set_parity(periph_t port, uart_parity_t parity)

Set UART parity.

Parameters
void uart_set_stopbits(periph_t port, uart_stopbit_t stopbit)

Set UART stopbits.

Parameters
void uart_set_wordsize(periph_t port, uart_wordsize_t wordsize)

Set UART data bit length.

Parameters