aboutsummaryrefslogtreecommitdiff
path: root/src/spi.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-11-27 17:56:47 -0700
committerJosh Rahm <joshuarahm@gmail.com>2024-11-27 17:56:47 -0700
commit281672d418bdc093716b069198c8852ad87eabb2 (patch)
tree37edd78f8395390d7fdbe342a604d11060bda1f7 /src/spi.c
parent74ea2acc63e7716f1e6bd24ee8a0cac3c0dbb819 (diff)
downloadch573-281672d418bdc093716b069198c8852ad87eabb2.tar.gz
ch573-281672d418bdc093716b069198c8852ad87eabb2.tar.bz2
ch573-281672d418bdc093716b069198c8852ad87eabb2.zip
Got the DMA to work! (That was surprisingly easy).
Diffstat (limited to 'src/spi.c')
-rw-r--r--src/spi.c49
1 files changed, 41 insertions, 8 deletions
diff --git a/src/spi.c b/src/spi.c
index 3105f1b..6d9fb2a 100644
--- a/src/spi.c
+++ b/src/spi.c
@@ -5,6 +5,8 @@
#include "ch573/gpio.h"
#include "ch573/spi.h"
+#define MAX_SPI_FIFO 8
+
#define SPI CH573_SPI__SPI_T_INTF
#define SPI0 ch573_spi__spi0
@@ -37,7 +39,7 @@ void enable_spi(void)
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.clock_div.set(SPI0, 25);
SPI.ctrl_mod.all_clear.set(SPI0, 1);
// SPI.ctrl_mod.set(SPI0, 0xe0); // Set mosi and sck
@@ -47,16 +49,47 @@ void enable_spi(void)
SPI.ctrl_cfg.dma_enable.set(SPI0, OFF);
}
+// void write_color(uint8_t r, uint8_t g, uint8_t b)
+// {
+//
+//
+// while (!SPI.int_flag.if_fifo_hf.get(SPI0));
+// SPI.fifo.set(SPI0, r);
+// SPI.fifo.set(SPI0, g);
+// SPI.fifo.set(SPI0, b);
+// }
+
+void dma_transfer(void* output_buffer, size_t len)
+{
+ // Clear everything.
+ SPI.ctrl_mod.all_clear.set(SPI0, 1);
+ SPI.ctrl_mod.all_clear.set(SPI0, 0);
+
+ SPI.ctrl_mod.fifo_dir.set(SPI0, FIFO_DIR_OUTPUT);
+ SPI.dma_beg.set(SPI0, (uint32_t)output_buffer);
+ SPI.dma_end.set(SPI0, (uint32_t)(output_buffer + len));
+
+ SPI.total_count.set(SPI0, len);
+
+ // Start DMA.
+ SPI.ctrl_cfg.dma_enable.set(SPI0, ON);
+}
+
+void wait_for_dma()
+{
+ while (SPI.total_count.get(SPI0) > 0);
+}
+
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);
+ SPI.total_count.set(SPI0, 1024);
+ // GPIO_PORT_A->clr |= 1 << 12;
+
+ while (SPI.fifo_count.get(SPI0) == 8);
+
+ SPI.fifo.set(SPI0, 0xaa);
+ SPI.total_count.set(SPI0, 1024);
}
}