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" #include "uart.h"
#define uart0 ((struct uart*)0xc010)
int main() { int main() {
while (1) { while (1) {
char c = uart_read(); char c = uart_read(uart0);
uart_write(c); uart_write(uart0, c);
} }
// never returns // never returns

View File

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

View File

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

View File

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