diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/init.c | 17 | ||||
-rw-r--r-- | src/io.c | 64 | ||||
-rw-r--r-- | src/main.c | 154 |
3 files changed, 79 insertions, 156 deletions
@@ -1,6 +1,7 @@ #include <stddef.h> #include <stdint.h> +#include "io.h" #include "isr_vector.h" void on_reset(void); @@ -11,9 +12,11 @@ default_irq_handler(void) return; } -#define WEAK_IRQ(irq) \ - void __attribute__(( \ - weak, alias("default_irq_handler"), __section__(".isr_vector.routines"))) \ +#define WEAK_IRQ(irq) \ + void __attribute__(( \ + weak, \ + alias("default_irq_handler"), \ + __section__(".isr_vector.routines"))) \ irq(void) WEAK_IRQ(irq_on_reset); @@ -60,8 +63,8 @@ extern uint32_t BSS_STOP; static inline void set_mtvec(void* vector_table) { - uint32_t mtvec = (uint32_t) vector_table; - mtvec |= 1; // Set interrupt table mode to "VECTORED" + uint32_t mtvec = (uint32_t)vector_table; + mtvec |= 1; // Set interrupt table mode to "VECTORED" asm volatile("csrw mtvec, %0" : : "r"(mtvec)); } @@ -107,6 +110,10 @@ static __attribute((__section__(".sinit.1"))) void start(void) init_data_segments(); /* Set the mtvec to the isr_vector. */ set_mtvec(&isr_vector); + + /* Initialize stdout. */ + init_uart1_for_stdout(); + /* Jump to main */ main(); } diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..0da33d1 --- /dev/null +++ b/src/io.c @@ -0,0 +1,64 @@ +#include <stdint.h> +#include <stdio.h> + +#include "ch573/gpio.h" +#include "ch573/uart.h" + +#include "io.h" + +#ifndef DEBUG_UART +#define DEBUG_UART UART1 +#endif + +#define UART1 ch573_uart__uart1 +#define UART CH573_UART__UART_T_INTF + +#define GPIO_PORT_A ch573_gpio__gpio_port_a +#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF + +#define gpio_usart1_tx_pin 9 + +static int uart1_FILE_put(char ch, FILE* unused) +{ + if (ch == '\n') { + uart1_FILE_put('\r', unused); + } + + while (!UART.lsr.thr_empty.get(UART1)); + UART.thr.set(UART1, ch); + + return 1; +} + +static int uart1_FILE_get(FILE* f) +{ + return -1; +} + +static int uart1_FILE_flush(FILE* f) +{ + return 0; +} + +FILE _uart1_FILE = (FILE){ + .put = uart1_FILE_put, + .get = uart1_FILE_get, + .flush = uart1_FILE_flush, + .flags = __SWR, +}; + +FILE* const stdout = &_uart1_FILE; + +void init_uart1_for_stdout(void) +{ + GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, gpio_usart1_tx_pin); + GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, gpio_usart1_tx_pin); + + UART.div.set(UART1, 1); + UART.fcr.set(UART1, 0x07); + UART.ier.txd_en.set(UART1, ON); + UART.lcr.word_sz.set(UART1, WORD_SZ_8_BITS); + + volatile uint32_t dl = (10 * 6400000 / 8 / BAUD_RATE + 5) / 10; + UART.dl.set(UART1, dl); +} @@ -1,12 +1,9 @@ #include <stdint.h> #include <stdio.h> -#include <string.h> #include "ch573/gpio.h" #include "ch573/pwr.h" #include "ch573/uart.h" -#include "isr_vector.h" -#include "panic.h" #define GPIO_PORT_A ch573_gpio__gpio_port_a #define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF @@ -17,48 +14,6 @@ #define PWR1 ch573_pwr__pwr_mgmt #define PWR CH573_PWR__PWR_MGMT_T_INTF -void stack_dump(uint32_t*); -void print_hex(uint32_t x); - -int uart1_FILE_put(char ch, FILE* f) -{ - if (ch == '\n') { - uart1_FILE_put('\r', f); - } - - while (!UART.lsr.thr_empty.get(UART1)); - UART.thr.set(UART1, ch); - return 1; -} - -static int uart1_FILE_get(FILE* f) -{ - return -1; -} - -static int uart1_FILE_flush(FILE* f) -{ - return 0; -} - -FILE _uart1_FILE = (FILE){ - .put = uart1_FILE_put, - .get = uart1_FILE_get, - .flush = uart1_FILE_flush, - .flags = __SWR, -}; - -FILE* const stdout = &_uart1_FILE; - -// FILE* const stdout = &_uart1_FILE; - -/* - * Function which delays for a bit. - */ -void delay(void); - -int main(void); - uint32_t collatz(uint32_t n) { uint32_t c = 0; @@ -75,118 +30,15 @@ uint32_t collatz(uint32_t n) return c; } -void blink_n(int n) -{ - uint32_t bit = 1 << 8; - while (n > 0) { - GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); - delay(); - GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); - delay(); - --n; - } -} - -void delay(void) -{ - for (volatile uint32_t i = 0; i < 10000; ++i) { - asm volatile(""); - } -} - -volatile uint32_t deadbeef = 0xdeadbeef; - -#define gpio_usart1_tx_pin 9 -#define gpio_usart1_rx_pin 8 - -const char* hello_world_static = "Hello, World!\r\n"; - -#define BAUD_RATE 115200 - -int test(char ch, ...); - -volatile int zero = 0; - -/* Main routine. This is called on_reset once everything else has been set up. +/* + * Main routine. This is called on_reset once everything else has been set up. */ int main(void) { - GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, gpio_usart1_tx_pin); - GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, gpio_usart1_tx_pin); - GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8); GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 8); - UART.div.set(UART1, 1); - UART.fcr.set(UART1, 0x07); - UART.ier.txd_en.set(UART1, ON); - UART.lcr.word_sz.set(UART1, WORD_SZ_8_BITS); - - volatile uint32_t dl = (10 * 6400000 / 8 / BAUD_RATE + 5) / 10; - UART.dl.set(UART1, dl); - - char buf[32] = {0}; - volatile uint32_t i = 0xdeadbeef; - - asm volatile("ebreak"); - - - panic(0xdeadbeef); - - stack_dump(&i); - - print_hex(i); - - stdout->put('a', NULL); - stdout->put('\n', NULL); - fputs("bulitin fputs\n", stdout); - puts("bulitin puts\n"); - - while (1) { - fprintf(stdout, "Hello! %% %s\n", "Josh"); - fputs(buf, &_uart1_FILE); - delay(); - } - - // printf("Hello: %s\n", hello_world_static); - // printf("Here's deadbeef: %08x\n", deadbeef); - + printf("Hello, %s!\n", "World"); for (;;); return 0; } - -IRQ(systick) -{ - collatz(5); -} - -#define putch(c) uart1_FILE_put(c, NULL) -void print_binary(uint32_t x) -{ - int i = 0; - while (i < 32) { - int msb = (x & 0x80000000) >> 31; - x <<= 1; - if (msb) { - uart1_FILE_put('1', NULL); - } else { - uart1_FILE_put('0', NULL); - } - i++; - } -} - -void print_hex(uint32_t x) -{ - int i = 0; - while (i < 8) { - int ms = (x & 0xf0000000) >> 28; - x <<= 4; - if (ms < 10) { - putch('0' + ms); - } else { - putch('a' + (ms - 10)); - } - ++i; - } -} |