#include "uart.h"

int uart_rx_available(struct uart* uart) {
  return uart->flags & UART_STATUS_RXNE_msk != 0;
}

int uart_tx_available(struct uart* uart) {
  return uart->flags & UART_STATUS_TXE_msk != 0;
}

void uart_write(struct uart* uart, const char c) {
  while (!uart_tx_available(uart)) {
  }

  uart->data = c;
}

char uart_read(struct uart* uart) {
  while (!uart_rx_available(uart)) {
  }

  return uart->data;
}

void uart_writen(struct uart* uart, const char* data, int len) {
  for (int i = 0; i < len; ++i) {
    uart_write(uart, data[i]);
  }
}
