Skip to content

Commit

Permalink
Add UART module
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjian85 committed Mar 28, 2024
1 parent c38b73d commit f194002
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#define CONFIG_H_

#define KSTACK_BASE 0x88000000
#define UART0_BASE 0x10000000

#endif // CONFIG_H_
6 changes: 6 additions & 0 deletions include/uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef UART_H_
#define UART_H_

void uart_putc(char c);

#endif // UART_H_
5 changes: 2 additions & 3 deletions src/kmain.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <attribs.h>
#include <types.h>
#include <uart.h>

noreturn void kmain(void) {
volatile u8 *uart = (volatile u8 *) 0x10000000;
const char *s = "Hello, world!\n";

for (int i = 0; s[i] != '\0'; i++)
*uart = s[i];
uart_putc(s[i]);

while (true)
;
Expand Down
27 changes: 27 additions & 0 deletions src/uart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <attribs.h>
#include <config.h>
#include <types.h>

#define UART_RBR 0
#define UART_THR 0
#define UART_DLL 0
#define UART_IER 1
#define UART_DLM 1
#define UART_IIR 2
#define UART_FCR 2
#define UART_LCR 3
#define UART_MCR 4
#define UART_LSR 5
#define UART_MSR 6
#define UART_SCR 7

#define UART_LSR_THRE 0x20

#define uart_read_reg(reg) *((volatile u8 *) UART0_BASE + reg)
#define uart_write_reg(reg, val) *((volatile u8 *) UART0_BASE + reg) = val

void uart_putc(char c) {
while (!(uart_read_reg(UART_LSR) & UART_LSR_THRE))
;
uart_write_reg(UART_THR, c);
}

0 comments on commit f194002

Please sign in to comment.