aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-11-15 02:48:27 -0700
committerJosh Rahm <joshuarahm@gmail.com>2024-11-15 02:48:27 -0700
commitede9bee7f22fd5d0e1bacb7689f1cac23992b70b (patch)
tree904d8d80063aa415b9e407f6ec73aa874483ebcd /src/main.c
parent63054d2fdf9a0ae3e9dcdd0d20eb8714671a010b (diff)
downloadch573-ede9bee7f22fd5d0e1bacb7689f1cac23992b70b.tar.gz
ch573-ede9bee7f22fd5d0e1bacb7689f1cac23992b70b.tar.bz2
ch573-ede9bee7f22fd5d0e1bacb7689f1cac23992b70b.zip
UART is working.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c116
1 files changed, 116 insertions, 0 deletions
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 <stdint.h>
+
+#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)
+{
+}