From 281672d418bdc093716b069198c8852ad87eabb2 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Wed, 27 Nov 2024 17:56:47 -0700 Subject: Got the DMA to work! (That was surprisingly easy). --- src/spi.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'src/spi.c') 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); } } -- cgit