improved uart character printing and poweroff/restart
This commit is contained in:
parent
10851eeb64
commit
8cd08165e0
6 changed files with 66 additions and 7 deletions
27
src/drivers/uart.c
Normal file
27
src/drivers/uart.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <drivers/uart.h>
|
||||
|
||||
void uart_put(size_t base_addr, uint8_t data) {
|
||||
*(volatile uint8_t *)base_addr = data;
|
||||
}
|
||||
|
||||
int kputchar(int ch) {
|
||||
uart_put(UART_ADDRESS, ch);
|
||||
return ch;
|
||||
}
|
||||
|
||||
void kprint(const char *str) {
|
||||
while (*str) {
|
||||
kputchar(*str++);
|
||||
}
|
||||
}
|
||||
|
||||
void knewline(void) {
|
||||
kputchar('\n');
|
||||
}
|
||||
|
||||
void kputs(const char *str) {
|
||||
kprint(str);
|
||||
knewline();
|
||||
}
|
||||
9
src/drivers/uart.h
Normal file
9
src/drivers/uart.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef UART_H
|
||||
#define UART_H
|
||||
|
||||
#define UART_ADDRESS 0x10000000
|
||||
void uart_put(size_t base_addr, uint8_t data);
|
||||
int kputchar(int ch);
|
||||
void kputs(const char *str);
|
||||
void kprint(const char *str);
|
||||
#endif
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <drivers/uart.h>
|
||||
#include <syscon/syscon.h>
|
||||
|
||||
void kmain() {
|
||||
char *uart = (char *)0x10000000;
|
||||
char *msg = "Hello, OS World!\n";
|
||||
|
||||
for (int i = 0; msg[i] != '\0'; i++) {
|
||||
*uart = msg[i]; // Write each character to the UART
|
||||
}
|
||||
kprint("Hello, OS World!\n");
|
||||
poweroff();
|
||||
}
|
||||
|
|
|
|||
14
src/syscon/syscon.c
Normal file
14
src/syscon/syscon.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "syscon.h"
|
||||
#include "drivers/uart.h"
|
||||
|
||||
void poweroff(void) {
|
||||
kputs("Poweroff requested");
|
||||
*(uint32_t *)SYSCON_ADDR = 0x5555;
|
||||
}
|
||||
|
||||
void reboot(void) {
|
||||
kputs("Reboot requested");
|
||||
*(uint32_t *)SYSCON_ADDR = 0x7777;
|
||||
}
|
||||
11
src/syscon/syscon.h
Normal file
11
src/syscon/syscon.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef SYSCON_H
|
||||
#define SYSCON_H
|
||||
|
||||
// "test" syscon-compatible device is at memory-mapped address 0x100000
|
||||
// according to our device tree
|
||||
#define SYSCON_ADDR 0x100000
|
||||
|
||||
void poweroff(void);
|
||||
void reboot(void);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue