15 #include "uart_regs.h"
17 #define UART_INTR_STATE_MASK 0xffffffffu
21 static bool uart_tx_full(
const dif_uart_t *uart) {
22 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_STATUS_REG_OFFSET);
26 static bool uart_tx_idle(
const dif_uart_t *uart) {
27 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_STATUS_REG_OFFSET);
31 static bool uart_rx_empty(
const dif_uart_t *uart) {
32 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_STATUS_REG_OFFSET);
36 static uint8_t uart_rx_fifo_read(
const dif_uart_t *uart) {
37 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_RDATA_REG_OFFSET);
42 static void uart_tx_fifo_write(
const dif_uart_t *uart, uint8_t
byte) {
44 mmio_region_write32(uart->
base_addr, UART_WDATA_REG_OFFSET, reg);
47 static void uart_reset(
const dif_uart_t *uart) {
48 mmio_region_write32(uart->
base_addr, UART_CTRL_REG_OFFSET, 0u);
54 mmio_region_write32(uart->
base_addr, UART_FIFO_CTRL_REG_OFFSET, reg);
56 mmio_region_write32(uart->
base_addr, UART_OVRD_REG_OFFSET, 0u);
57 mmio_region_write32(uart->
base_addr, UART_TIMEOUT_CTRL_REG_OFFSET, 0u);
58 mmio_region_write32(uart->
base_addr, UART_INTR_ENABLE_REG_OFFSET, 0u);
59 mmio_region_write32(uart->
base_addr, UART_INTR_STATE_REG_OFFSET,
60 UART_INTR_STATE_MASK);
66 static size_t uart_bytes_send(
const dif_uart_t *uart,
const uint8_t *data,
67 size_t bytes_requested) {
68 size_t bytes_written = 0;
69 while ((bytes_written < bytes_requested) && !uart_tx_full(uart)) {
70 uart_tx_fifo_write(uart, data[bytes_written]);
80 static size_t uart_bytes_receive(
const dif_uart_t *uart,
size_t bytes_requested,
82 size_t bytes_read = 0;
83 while ((bytes_read < bytes_requested) && !uart_rx_empty(uart)) {
84 data[bytes_read] = uart_rx_fifo_read(uart);
102 uint32_t nco_width = 0;
104 for (
int i = 0; i < 32; i++) {
105 nco_width += (UART_CTRL_NCO_MASK >> i) & 1;
108 static_assert((UART_CTRL_NCO_MASK >> 28) == 0,
109 "NCO bit width exceeds 28 bits.");
121 uint32_t nco_masked = nco & UART_CTRL_NCO_MASK;
124 if (nco != nco_masked) {
130 if ((rxblvl & UART_CTRL_RXBLVL_MASK) != rxblvl) {
153 mmio_region_write32(uart->
base_addr, UART_CTRL_REG_OFFSET, reg);
156 mmio_region_write32(uart->
base_addr, UART_INTR_ENABLE_REG_OFFSET, 0u);
167 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_CTRL_REG_OFFSET);
169 mmio_region_write32(uart->
base_addr, UART_CTRL_REG_OFFSET, reg);
185 value = UART_FIFO_CTRL_RXILVL_VALUE_RXLVL1;
188 value = UART_FIFO_CTRL_RXILVL_VALUE_RXLVL2;
191 value = UART_FIFO_CTRL_RXILVL_VALUE_RXLVL4;
194 value = UART_FIFO_CTRL_RXILVL_VALUE_RXLVL8;
197 value = UART_FIFO_CTRL_RXILVL_VALUE_RXLVL16;
200 value = UART_FIFO_CTRL_RXILVL_VALUE_RXLVL32;
203 value = UART_FIFO_CTRL_RXILVL_VALUE_RXLVL62;
210 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_FIFO_CTRL_REG_OFFSET);
212 mmio_region_write32(uart->
base_addr, UART_FIFO_CTRL_REG_OFFSET, reg);
228 value = UART_FIFO_CTRL_TXILVL_VALUE_TXLVL1;
231 value = UART_FIFO_CTRL_TXILVL_VALUE_TXLVL2;
234 value = UART_FIFO_CTRL_TXILVL_VALUE_TXLVL4;
237 value = UART_FIFO_CTRL_TXILVL_VALUE_TXLVL8;
240 value = UART_FIFO_CTRL_TXILVL_VALUE_TXLVL16;
248 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_FIFO_CTRL_REG_OFFSET);
250 mmio_region_write32(uart->
base_addr, UART_FIFO_CTRL_REG_OFFSET, reg);
262 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_CTRL_REG_OFFSET);
283 mmio_region_write32(uart->
base_addr, UART_CTRL_REG_OFFSET, reg);
289 size_t bytes_requested,
290 size_t *bytes_written) {
291 if (uart == NULL || data == NULL) {
296 size_t res = uart_bytes_send(uart, data, bytes_requested);
297 if (bytes_written != NULL) {
298 *bytes_written = res;
305 size_t bytes_requested, uint8_t *data,
306 size_t *bytes_read) {
307 if (uart == NULL || data == NULL) {
312 size_t res = uart_bytes_receive(uart, bytes_requested, data);
313 if (bytes_read != NULL) {
326 while (uart_tx_full(uart)) {
329 (void)uart_bytes_send(uart, &
byte, 1);
333 while (!uart_tx_idle(uart)) {
341 if (uart == NULL ||
byte == NULL) {
346 while (uart_rx_empty(uart)) {
349 (void)uart_bytes_receive(uart, 1,
byte);
356 if (uart == NULL || num_bytes == NULL) {
362 mmio_region_read32(uart->
base_addr, UART_FIFO_STATUS_REG_OFFSET);
370 if (uart == NULL || num_bytes == NULL) {
376 mmio_region_read32(uart->
base_addr, UART_FIFO_STATUS_REG_OFFSET);
377 uint32_t fill_bytes =
390 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_FIFO_CTRL_REG_OFFSET);
407 mmio_region_write32(uart->
base_addr, UART_FIFO_CTRL_REG_OFFSET, reg);
419 uint32_t index = loopback ? UART_CTRL_LLPBK_BIT : UART_CTRL_SLPBK_BIT;
420 uint32_t reg = mmio_region_read32(uart->
base_addr, UART_CTRL_REG_OFFSET);
422 mmio_region_write32(uart->
base_addr, UART_CTRL_REG_OFFSET, reg);
428 uint32_t duration_ticks) {
430 (duration_ticks & ~(uint32_t)UART_TIMEOUT_CTRL_VAL_MASK) != 0) {
437 mmio_region_write32(uart->
base_addr, UART_TIMEOUT_CTRL_REG_OFFSET, reg);
449 mmio_region_write32(uart->
base_addr, UART_TIMEOUT_CTRL_REG_OFFSET, reg);
456 uint32_t *duration_ticks) {
457 if (uart == NULL ||
status == NULL) {
462 mmio_region_read32(uart->
base_addr, UART_TIMEOUT_CTRL_REG_OFFSET);
467 if (duration_ticks != NULL) {