aboutsummaryrefslogtreecommitdiff
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
parent74ea2acc63e7716f1e6bd24ee8a0cac3c0dbb819 (diff)
downloadch573-281672d418bdc093716b069198c8852ad87eabb2.tar.gz
ch573-281672d418bdc093716b069198c8852ad87eabb2.tar.bz2
ch573-281672d418bdc093716b069198c8852ad87eabb2.zip
Got the DMA to work! (That was surprisingly easy).
-rw-r--r--include/spi.h6
-rw-r--r--src/main.c18
-rw-r--r--src/spi.c49
3 files changed, 64 insertions, 9 deletions
diff --git a/include/spi.h b/include/spi.h
index 65e3a9d..a3ed89b 100644
--- a/include/spi.h
+++ b/include/spi.h
@@ -1,5 +1,11 @@
#pragma once
+#include <stdlib.h>
+
void enable_spi(void);
void run_spi(void);
+
+void dma_transfer(void* output_buffer, size_t len);
+
+void wait_for_dma();
diff --git a/src/main.c b/src/main.c
index 8bf8e15..5f883f9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,6 +1,7 @@
#include <stdint.h>
#include <stdio.h>
+#include "string.h"
#include "ch573/gpio.h"
#include "ch573/pwr.h"
#include "ch573/uart.h"
@@ -127,9 +128,24 @@ int main(void)
{
printf("Running SPI.\n");
+ set_system_clock_60Mhz();
+
enable_spi();
- run_spi();
+ char buf[1024];
+ memset(buf, 0xf0, sizeof(buf));
+ for (int i = 0; i < 50; ++ i) {
+ buf[i] = 0xaa;
+ buf[sizeof(buf) - i] = 0xaa;
+ }
+
+ while (1) {
+ printf("Xfer.\n");
+ dma_transfer(buf, sizeof(buf));
+ wait_for_dma();
+ }
+
+ // run_spi();
// GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8);
// GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 8);
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);
}
}