restrcuct
This commit is contained in:
64
include/io.h
Normal file
64
include/io.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef UART_IO_H
|
||||
#define UART_IO_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define UART0_BASE 0x3F201000
|
||||
#define UART0_DR (*((volatile uint32_t *)(UART0_BASE + 0x00)))
|
||||
#define UART0_FR (*((volatile uint32_t *)(UART0_BASE + 0x18)))
|
||||
|
||||
typedef struct UART_IO {
|
||||
void (*putc)(char c);
|
||||
void (*puts)(const char* str);
|
||||
char (*getc)(void);
|
||||
void (*print_int)(unsigned int value);
|
||||
} UART_IO;
|
||||
|
||||
// Function implementations
|
||||
|
||||
static void uart_putc(char c)
|
||||
{
|
||||
while (UART0_FR & (1 << 5)) { } // Wait while TXFF is set
|
||||
UART0_DR = c;
|
||||
}
|
||||
|
||||
static void uart_puts(const char* str)
|
||||
{
|
||||
while (*str)
|
||||
uart_putc(*str++);
|
||||
}
|
||||
|
||||
static char uart_getc(void)
|
||||
{
|
||||
while (UART0_FR & (1 << 4)) { } // Wait while RXFE is set
|
||||
return (char)(UART0_DR & 0xFF);
|
||||
}
|
||||
|
||||
static void uart_print_int(unsigned int value)
|
||||
{
|
||||
char buffer[10];
|
||||
int i = 0;
|
||||
|
||||
if (value == 0) {
|
||||
uart_putc('0');
|
||||
return;
|
||||
}
|
||||
|
||||
while (value > 0 && i < 10) {
|
||||
buffer[i++] = (value % 10) + '0';
|
||||
value /= 10;
|
||||
}
|
||||
|
||||
while (i--)
|
||||
uart_putc(buffer[i]);
|
||||
}
|
||||
|
||||
// UART_IO instance with function pointers assigned
|
||||
static const UART_IO uart_io = {
|
||||
.putc = uart_putc,
|
||||
.puts = uart_puts,
|
||||
.getc = uart_getc,
|
||||
.print_int = uart_print_int,
|
||||
};
|
||||
|
||||
#endif // UART_IO_H
|
||||
Reference in New Issue
Block a user