uart: upgrade C api

This commit is contained in:
Paul Mathieu 2021-03-13 15:49:28 -08:00
parent 5d5eeb45cc
commit 75b5488b8d
4 changed files with 27 additions and 20 deletions

View File

@ -1,9 +1,11 @@
#include "uart.h"
#define uart0 ((struct uart*)0xc010)
int main() {
while (1) {
char c = uart_read();
uart_write(c);
char c = uart_read(uart0);
uart_write(uart0, c);
}
// never returns

View File

@ -1,13 +1,15 @@
#include "led.h"
#include "uart.h"
#define uart0 ((struct uart*)0xc010)
int main() {
int c;
int led = 0;
while (1) {
led_write(1 << led);
c = uart_read();
c = uart_read(uart0);
if (c == 'a') {
led++;

View File

@ -1,23 +1,23 @@
#include "uart.h"
int uart_rx_available() {
return (*UART_STATUS & UART_STATUS_RXNE_msk) != 0;
int uart_rx_available(struct uart* uart) {
return uart->flags & UART_STATUS_RXNE_msk != 0;
}
int uart_tx_available() {
return (*UART_STATUS & UART_STATUS_TXE_msk) != 0;
int uart_tx_available(struct uart* uart) {
return uart->flags & UART_STATUS_TXE_msk != 0;
}
void uart_write(const char c) {
while (!uart_tx_available()) {
void uart_write(struct uart* uart, const char c) {
while (!uart_tx_available(uart)) {
}
*UART_DATA = c;
uart->data = c;
}
char uart_read() {
while (!uart_rx_available()) {
char uart_read(struct uart* uart) {
while (!uart_rx_available(uart)) {
}
return *UART_DATA;
return uart->data;
}

View File

@ -1,13 +1,16 @@
#define UART_BASE 0xc010
#define UART_DATA ((volatile char*) (UART_BASE + 0x00))
#define UART_STATUS ((volatile char*) (UART_BASE + 0x02))
#pragma once
#define UART_STATUS_RXNE_pos 1
#define UART_STATUS_RXNE_msk (1 << UART_STATUS_RXNE_pos)
#define UART_STATUS_TXE_pos 0
#define UART_STATUS_TXE_msk (1 << UART_STATUS_TXE_pos)
int uart_rx_available();
int uart_tx_available();
void uart_write(const char c);
char uart_read();
struct uart {
volatile uint8_t data;
volatile uint16_t flags;
};
int uart_rx_available(struct uart* uart);
int uart_tx_available(struct uart* uart);
void uart_write(struct uart* uart, const char c);
char uart_read(struct uart* uart);