#include <libopencm3/nrf/uart.h>
#include <libopencm3/nrf/gpio.h>
void uart_enable(uint32_t uart)
{
UART_ENABLE(uart) = UART_ENABLE_ENABLED;
}
void uart_disable(uint32_t uart)
{
UART_ENABLE(uart) = UART_ENABLE_DISABLED;
}
void uart_configure(uint32_t uart,
uint32_t tx_pin, uint32_t rx_pin, uint32_t rts_pin, uint32_t cts_pin,
enum uart_baud br, bool enable_parity)
{
uart_set_pins(uart, rx_pin, tx_pin, cts_pin, rts_pin);
uint32_t reg_config = enable_parity ? UART_CONFIG_PARITY : 0;
if (rts_pin <= UART_MAX_PIN || cts_pin <= UART_MAX_PIN) {
reg_config |= UART_CONFIG_HWFC;
}
UART_CONFIG(uart) = reg_config;
uart_set_baudrate(uart, br);
}
void uart_set_pins(uint32_t uart, uint32_t rx, uint32_t tx, uint32_t cts, uint32_t rts)
{
if (rx != GPIO_UNCONNECTED) {
UART_PSELRXD(uart) = __GPIO2PIN(rx);
} else {
UART_PSELRXD(uart) = rx;
}
if (tx != GPIO_UNCONNECTED) {
UART_PSELTXD(uart) = __GPIO2PIN(tx);
} else {
UART_PSELTXD(uart) = tx;
}
if (cts != GPIO_UNCONNECTED) {
UART_PSELCTS(uart) = __GPIO2PIN(cts);
} else {
UART_PSELCTS(uart) = cts;
}
if (rts != GPIO_UNCONNECTED) {
UART_PSELRTS(uart) = __GPIO2PIN(rts);
} else {
UART_PSELRTS(uart) = rts;
}
}
#undef _LOG2
void uart_set_baudrate(uint32_t uart, enum uart_baud br)
{
UART_BAUDRATE(uart) = br;
}
void uart_set_parity(uint32_t uart, int parity)
{
UART_CONFIG(uart) |= parity ? UART_CONFIG_PARITY : 0;
}
void uart_set_flow_control(uint32_t uart, int flow)
{
UART_CONFIG(uart) |= flow ? UART_CONFIG_HWFC : 0;
}
void uart_start_tx(uint32_t uart)
{
PERIPH_TRIGGER_TASK(UART_TASK_STARTTX((uart)));
}
void uart_send(uint32_t uart, uint16_t byte)
{
UART_TXD((uart)) = byte;
}
void uart_stop_tx(uint32_t uart)
{
PERIPH_TRIGGER_TASK(UART_TASK_STOPTX((uart)));
}
void uart_start_rx(uint32_t uart)
{
PERIPH_TRIGGER_TASK(UART_TASK_STARTRX((uart)));
}
uint16_t uart_recv(uint32_t uart)
{
return UART_RXD(uart);
}
void uart_stop_rx(uint32_t uart)
{
PERIPH_TRIGGER_TASK(UART_TASK_STOPRX((uart)));
}