From 478cddd82e0c0c36e008dcf999c819d1295aa2bb Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Mon, 20 Jun 2022 18:06:49 +0200 Subject: [PATCH] Refactoring --- Makefile | 1 + include/reg.h | 5 ++++ src/boot.S | 2 +- src/kernel.c | 33 +++++++++++++++++++-- src/mini_uart0.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/mini_uart0.c diff --git a/Makefile b/Makefile index 8686cc3..8d89a01 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ all: kernel8.img install: all udisksctl mount -b /dev/mmcblk0p1 cp kernel8.img /run/media/daniil/*/ + cp /run/media/daniil/*/blobcode.bin /run/media/daniil/*/bootcode.bin umount /run/media/daniil/* sync diff --git a/include/reg.h b/include/reg.h index 5025c27..2ef58da 100644 --- a/include/reg.h +++ b/include/reg.h @@ -20,3 +20,8 @@ #define UART_BASE (0x7E201000-BUS_OFFSET) #define ST_CLO (0x7e003004 - BUS_OFFSET) + +#define ARM_BASE (0x7E00B000 - BUS_OFFSET) + +// Basic configuration +#define ARM_ID (ARM_BASE+0x44c) \ No newline at end of file diff --git a/src/boot.S b/src/boot.S index 1969449..56f4da7 100644 --- a/src/boot.S +++ b/src/boot.S @@ -16,5 +16,5 @@ _start: mov sp, #LOW_MEMORY // Initialize the kernel stack pointer to +4mb, growing downwards bl kernel_main // kernel_main() -halt: +halt: b halt \ No newline at end of file diff --git a/src/kernel.c b/src/kernel.c index a71d125..3e1c9a4 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -2,12 +2,41 @@ #include "reg.h" #include "utils.h" +#define ARM_IDVAL 0x364D5241 + +void uart_hex(int i) { + uart0_send_string("0x"); + for (int x = 0; x < 8; x++) { + int c = 48 + ((i & 0xF0000000) >> 28); + if (c == 48+10) { + uart0_send('A'); + } else if (c == 48+11) { + uart0_send('B'); + } else if (c == 48+12) { + uart0_send('C'); + } else if (c == 48+13) { + uart0_send('D'); + } else if (c == 48+14) { + uart0_send('E'); + } else if (c == 48+15) { + uart0_send('F'); + } else { + uart0_send(c); + } + i <<= 4; + } + uart0_send_string("\r\n"); +} void kernel_main(void) { + /*while(get32(ARM_ID) != ARM_IDVAL); + delay(500); + uart0_init(); + delay(1000);*/ uart0_send_string("Hello, world!\r\n"); - while (1) { + /*while (1) { uart0_send(uart0_recv()); - } + }*/ } diff --git a/src/mini_uart0.c b/src/mini_uart0.c new file mode 100644 index 0000000..68266c3 --- /dev/null +++ b/src/mini_uart0.c @@ -0,0 +1,75 @@ +#include "mini_uart.h" +#include "reg.h" + +#define UART_DR (UART_BASE+0x0) +#define UART_FR (UART_BASE+0x18) + +#define UART_IBRD (UART_BASE+0x24) +#define UART_FBRD (UART_BASE+0x28) +#define UART_LCRH (UART_BASE+0x2C) +#define UART_CR (UART_BASE+0x30) +#define UART_IMSC (UART_BASE+0x38) +#define UART_ICR (UART_BASE+0x44) + +// UART clock baud rate divisor register +// Why is this undocumented?? +#define CM_UARTDIV (0x7e1010f4-BUS_OFFSET) + +// UART control register +// Why is this undocumented?? +#define CM_UARTCTL 0x7e1010f0 + + +// cucumber moment +#define CM_PASSWORD 0x5a000000 +#define CM_SRC_OSC 1 + +#define CM_UARTCTL_FRAC_SET 0x00000200 +#define CM_UARTCTL_ENAB_SET 0x00000010 + +void uart0_init(void) +{ + unsigned int selector; + + selector = get32(GPFSEL1); + selector &= ~(7<<12); // clean gpio14 + selector |= 4<<12; // set alt0 for gpio14 + selector &= ~(7<<15); // clean gpio15 + selector |= 4<<15; // set alt0 for gpio 15 + put32(GPFSEL1,selector); + + put32(GPPUD,0); + delay(150); + put32(GPPUDCLK0,(1<<14)|(1<<15)); + delay(150); + put32(GPPUDCLK0,0); + + put32(UART_CR, 0); + put32(UART_IMSC, 0); + + //put32(CM_UARTDIV, CM_PASSWORD | 0x6666); + //put32(CM_UARTDIV, CM_PASSWORD | CM_SRC_OSC | CM_UARTCTL_FRAC_SET | CM_UARTCTL_ENAB_SET); + + put32(UART_IBRD, 26); + put32(UART_FBRD, 3); + put32(UART_LCRH, (3 << 5) | (1 << 4)); + put32(UART_CR, 0x301); +} + +void uart0_send(char c) +{ + while(get32(UART_FR) & (1<<5)); + put32(UART_DR, c); +} +char uart0_recv() +{ + while(get32(UART_FR) & (1<<4)); + return put32(UART_DR) & 0xFF; +} + +void uart0_send_string(const char *str) +{ + for (int i = 0; str[i] != '\0'; i++) { + uart0_send(str[i]); + } +} \ No newline at end of file