aboutsummaryrefslogtreecommitdiff
path: root/src/spi.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-11-27 12:51:24 -0700
committerJosh Rahm <joshuarahm@gmail.com>2024-11-27 13:04:00 -0700
commitfcb936b4ea13841e8c4f87648182b20a194413f2 (patch)
tree11ba2cf223e2efe5c5f2b4c4f1192560631ddf89 /src/spi.c
parent22c5b3e1dc4e3cf7de3f73ebbf5b59542f207f4b (diff)
downloadch573-fcb936b4ea13841e8c4f87648182b20a194413f2.tar.gz
ch573-fcb936b4ea13841e8c4f87648182b20a194413f2.tar.bz2
ch573-fcb936b4ea13841e8c4f87648182b20a194413f2.zip
Some SPI is working, a bit. Still no clock, but I'm about to give up on that.
Diffstat (limited to 'src/spi.c')
-rw-r--r--src/spi.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/spi.c b/src/spi.c
new file mode 100644
index 0000000..3105f1b
--- /dev/null
+++ b/src/spi.c
@@ -0,0 +1,62 @@
+#include "spi.h"
+
+#include <stdio.h>
+
+#include "ch573/gpio.h"
+#include "ch573/spi.h"
+
+#define SPI CH573_SPI__SPI_T_INTF
+#define SPI0 ch573_spi__spi0
+
+#define GPIO_PORT_A ch573_gpio__gpio_port_a
+#define GPIO_PORT_B ch573_gpio__gpio_port_b
+#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF
+
+#define GPIO_I CH573_GPIO__GPIO_T_INTF
+#define GPIO ch573_gpio__gpio
+
+void enable_spi(void)
+{
+ GPIO_I.pin_alternate.pin_spi0.set(GPIO, OFF);
+
+ GPIO_PORT.out.set(GPIO_PORT_A, ON, 12);
+ GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 12);
+ GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 14);
+ GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 12);
+ GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 14);
+
+ GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 12);
+ GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 14);
+ GPIO_PORT.pd_drv.set(GPIO_PORT_B, 0, 12);
+ GPIO_PORT.pd_drv.set(GPIO_PORT_B, 0, 14);
+ GPIO_PORT.out.set(GPIO_PORT_B, OFF, 14);
+ GPIO_PORT.out.set(GPIO_PORT_B, ON, 12);
+
+ GPIO_PORT.dir.set(GPIO_PORT_B, DIR_IN, 13);
+ GPIO_PORT.dir.set(GPIO_PORT_B, DIR_IN, 15);
+ GPIO_PORT.dir.set(GPIO_PORT_A, DIR_IN, 13);
+ GPIO_PORT.dir.set(GPIO_PORT_A, DIR_IN, 15);
+
+ SPI.clock_div.set(SPI0, 16);
+ SPI.ctrl_mod.all_clear.set(SPI0, 1);
+ // SPI.ctrl_mod.set(SPI0, 0xe0); // Set mosi and sck
+
+ SPI.ctrl_mod.all_clear.set(SPI0, 0);
+ SPI.ctrl_mod.pin_enable.set(SPI0, 0x7); // Set mosi and sck
+ SPI.ctrl_cfg.auto_if.set(SPI0, 1);
+ SPI.ctrl_cfg.dma_enable.set(SPI0, OFF);
+}
+
+void run_spi(void)
+{
+ GPIO_PORT.out.set(GPIO_PORT_A, ON, 12);
+ while (1) {
+ GPIO_PORT_A->clr |= 1 << 12;
+ SPI.ctrl_mod.fifo_dir.set(SPI0, FIFO_DIR_OUTPUT);
+ // R8_SPI0_CTRL_MOD &= ~RB_SPI_FIFO_DIR;
+ SPI.data_buf.set(SPI0, 0xaa);
+ // R8_SPI0_BUFFER = 0xaa;
+ while (!SPI.int_flag.free.get(SPI0));
+ GPIO_PORT.out.set(GPIO_PORT_A, ON, 12);
+ }
+}