diff options
Diffstat (limited to 'src/spi.c')
-rw-r--r-- | src/spi.c | 49 |
1 files changed, 41 insertions, 8 deletions
@@ -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); } } |