summaryrefslogtreecommitdiff
path: root/include/uart.h
blob: 76f4b341ea3b72a9e9416110a9fc11ece70ed2c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once

#define UART_BASE 0x10000
#define UART_DATA *(volatile char *)(UART_BASE + 0)
#define UART_STATUS *(volatile char *)(UART_BASE + 1)
#define UART_TX_READY 0b01
#define UART_RX_READY 0b10

static inline int uart_rx_ready() { return (UART_STATUS & UART_RX_READY) != 0; }
static inline int uart_tx_ready() { return (UART_STATUS & UART_TX_READY) != 0; }

static inline void uart_putc(char c) {
  while (!uart_tx_ready())
    ;
  UART_DATA = c;
}

static inline void uart_puts(const char *s) {
  while (*s) {
    uart_putc(*s++);
  }
}

static inline char uart_getc_nonblocking() { return UART_DATA; }

static inline char uart_getc() {
  while (!uart_rx_ready())
    ;
  return UART_DATA;
}