diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 177 |
1 files changed, 155 insertions, 22 deletions
@@ -1,9 +1,10 @@ #include <stdint.h> +#include <stdio.h> +#include <string.h> #include "ch573/gpio.h" -#include "ch573/uart.h" #include "ch573/pwr.h" - +#include "ch573/uart.h" #include "isr_vector.h" #define GPIO_PORT_A ch573_gpio__gpio_port_a @@ -15,12 +16,49 @@ #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; + +int my_puts(const char* str); + +// FILE* const stdout = &_uart1_FILE; + /* * Function which delays for a bit. */ void delay(void); -void main(void); +int main(void); uint32_t collatz(uint32_t n) { @@ -59,47 +97,57 @@ void delay(void) volatile uint32_t deadbeef = 0xdeadbeef; -// Memory-mapped address of the data values in flash. -extern uint32_t DATA_VALUES_IN_FLASH; - -// Where the data is located in sram. -extern uint32_t DATA_SEGMENT_START; -extern uint32_t DATA_SEGMENT_STOP; - #define gpio_usart1_tx_pin 9 #define gpio_usart1_rx_pin 8 -const char* hello_world = "Hello, World!\r\n"; +const char* hello_world_static = "Hello, World!\r\n"; #define BAUD_RATE 115200 +int test(char ch, ...); + /* Main routine. This is called on_reset once everything else has been set up. */ -void main(void) +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); - uint32_t dl = (10 * 6400000 / 8 / BAUD_RATE + 5) / 10; + volatile uint32_t dl = (10 * 6400000 / 8 / BAUD_RATE + 5) / 10; UART.dl.set(UART1, dl); - - // PWR.slp_clk_off_0.uart1.set(PWR1, CLK_SOURCE_ENABLED); - const char* ptr = hello_world; + char buf[32] = { 0 }; + volatile uint32_t i = 0xdeadbeef; - while (1) { - if (*ptr == 0) { - ptr = hello_world; - } + stack_dump(&i); + + print_hex(i); + + stdout->put('a', NULL); + stdout->put('\n', NULL); + fputs("bulitin fputs\n", stdout); + my_puts("my_puts\n"); + puts("bulitin puts\n"); - while (!UART.lsr.thr_empty.get(UART1)); - UART.thr.set(UART1, *(ptr++)); + 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); + + for (;;); + return 0; } IRQ(systick) @@ -107,10 +155,95 @@ 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; + } +} + +void stack_dump(uint32_t* sp) +{ + fputs("\n\nstack_dump:\n\n", &_uart1_FILE); + while ((uint32_t) sp < 0x20008000) { + fputs(" 0x", &_uart1_FILE); + print_hex((uint32_t)sp); + fputs(" -> 0x", &_uart1_FILE); + print_hex(*sp); + fputs("\n", &_uart1_FILE); + sp += 1; + } +} + IRQ(exc) { + uint32_t mcause, mepc, mtval, *sp; + asm volatile("csrr %0, mcause" : "=r"(mcause)); + asm volatile("csrr %0, mepc" : "=r"(mepc)); + asm volatile("csrr %0, mtval" : "=r"(mtval)); + + fputs("NMI Detected:\n", &_uart1_FILE); + fputs(" mcause: 0b", &_uart1_FILE); + print_binary(mcause); + fputs("\n mepc: 0b", &_uart1_FILE); + print_binary(mepc); + fputs("\n 0x", &_uart1_FILE); + print_hex(mepc); + fputs("\n mtval: 0b", &_uart1_FILE); + print_binary(mtval); + fputs("\n deadbeef: 0b", &_uart1_FILE); + print_binary(0xdeadbeef); + fputs("\n", &_uart1_FILE); + + stack_dump(__builtin_return_address(0)); + + while (1) { + GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); + delay(); + delay(); + delay(); + delay(); + delay(); + delay(); + delay(); + delay(); + GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + delay(); + delay(); + delay(); + } } IRQ(nmi) { + while (1) { + GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); + delay(); + GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + delay(); + } } |