Refactoring

This commit is contained in:
Daniil Gentili 2022-06-20 18:06:49 +02:00
parent 6536272d32
commit 478cddd82e
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 113 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -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());
}
}*/
}

75
src/mini_uart0.c Normal file
View File

@ -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]);
}
}