From ede9bee7f22fd5d0e1bacb7689f1cac23992b70b Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Fri, 15 Nov 2024 02:48:27 -0700 Subject: UART is working. --- src/blinky.c | 85 ------------------------------------------- src/init.c | 12 ++++--- src/isr.s | 5 --- src/main.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 95 deletions(-) delete mode 100644 src/blinky.c create mode 100644 src/main.c (limited to 'src') diff --git a/src/blinky.c b/src/blinky.c deleted file mode 100644 index 355b479..0000000 --- a/src/blinky.c +++ /dev/null @@ -1,85 +0,0 @@ -#include - -#include "isr_vector.h" -#include "ch573/gpio.h" - -#define GPIO_PORT_A ch573_gpio__gpio_port_a -#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF - -/* - * Function which delays for a bit. - */ -void delay(void); - -void main(void); - -uint32_t collatz(uint32_t n) -{ - uint32_t c = 0; - - while (n > 1) { - if (n % 2 == 0) { - n /= 2; - } else { - n = n * 3 + 1; - } - c++; - } - - 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; - -// 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; - -/* Main routine. This is called on_reset once everything else has been set up. - */ -void main(void) -{ - GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8); - GPIO_PORT.pd_drv.set(GPIO_PORT_A, PD_DRV_OPEN_DRAIN, 8); - - for (;;) { - if (deadbeef == 0xdeadbeef) { - GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); - } - delay(); - delay(); - delay(); - GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); - delay(); - } -} - -IRQ(systick) { - collatz(5); -} - -IRQ(exc) {} - -IRQ(nmi) {} diff --git a/src/init.c b/src/init.c index 30e2c6d..5753a13 100644 --- a/src/init.c +++ b/src/init.c @@ -5,7 +5,7 @@ void on_reset(void); -void __attribute__((weak, interrupt, __section__(".isr_vector"))) +void __attribute__((weak, interrupt, __section__(".isr_vector.routines"))) default_irq_handler(void) { return; @@ -13,7 +13,7 @@ default_irq_handler(void) #define WEAK_IRQ(irq) \ void __attribute__(( \ - weak, alias("default_irq_handler"), __section__(".isr_vector"))) \ + weak, alias("default_irq_handler"), __section__(".isr_vector.routines"))) \ irq(void) WEAK_IRQ(irq_on_reset); @@ -60,7 +60,9 @@ extern uint32_t BSS_STOP; static inline void set_mtvec(void* vector_table) { - asm volatile("csrw mtvec, %0" : : "r"(vector_table)); + uint32_t mtvec = (uint32_t) vector_table; + mtvec |= 1; // Set interrupt table mode to "VECTORED" + asm volatile("csrw mtvec, %0" : : "r"(mtvec)); } /* @@ -99,7 +101,7 @@ extern void main(void); /* Start function. Responsible for initializing the system and jumping to the * main function. */ -static void start(void) +static __attribute((__section__(".sinit.1"))) void start(void) { /* Initialize the data segments. */ init_data_segments(); @@ -113,7 +115,7 @@ static void start(void) * The reset callback.This has to be a naked function because the stack pointer * may not be initialized!!. */ -__attribute((naked, __section__(".isr_routines.on_reset"))) void on_reset(void) +__attribute((naked, __section__(".sinit"))) void _begin(void) { // Set up the stack pointer to point to the end of SRAM. asm volatile( diff --git a/src/isr.s b/src/isr.s index 065450d..04d66d1 100644 --- a/src/isr.s +++ b/src/isr.s @@ -1,8 +1,3 @@ - -.section .sinit -_start: -j on_reset /* first instruction. Should jump directly to on_reset */ - .section .isr_vector .global isr_vector isr_vector: diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..3066123 --- /dev/null +++ b/src/main.c @@ -0,0 +1,116 @@ +#include + +#include "ch573/gpio.h" +#include "ch573/uart.h" +#include "ch573/pwr.h" + +#include "isr_vector.h" + +#define GPIO_PORT_A ch573_gpio__gpio_port_a +#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF + +#define UART1 ch573_uart__uart1 +#define UART CH573_UART__UART_T_INTF + +#define PWR1 ch573_pwr__pwr_mgmt +#define PWR CH573_PWR__PWR_MGMT_T_INTF + +/* + * Function which delays for a bit. + */ +void delay(void); + +void main(void); + +uint32_t collatz(uint32_t n) +{ + uint32_t c = 0; + + while (n > 1) { + if (n % 2 == 0) { + n /= 2; + } else { + n = n * 3 + 1; + } + c++; + } + + 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; + +// 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"; + +#define BAUD_RATE 115200 + +/* Main routine. This is called on_reset once everything else has been set up. + */ +void 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); + + 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; + UART.dl.set(UART1, dl); + + // PWR.slp_clk_off_0.uart1.set(PWR1, CLK_SOURCE_ENABLED); + + const char* ptr = hello_world; + + while (1) { + if (*ptr == 0) { + ptr = hello_world; + } + + while (!UART.lsr.thr_empty.get(UART1)); + UART.thr.set(UART1, *(ptr++)); + } +} + +IRQ(systick) +{ + collatz(5); +} + +IRQ(exc) +{ +} + +IRQ(nmi) +{ +} -- cgit