aboutsummaryrefslogtreecommitdiff
path: root/src/spi.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2025-09-30 13:06:16 -0600
committerJosh Rahm <joshuarahm@gmail.com>2025-09-30 13:06:16 -0600
commit290d784950b6248782b049cd9831bd6e034fd538 (patch)
treec231c02e92ed83a02b234a85b3c07bcc5e44b168 /src/spi.c
parentf89f75b5616de99865448f41b068a2783cd3648e (diff)
downloadch573-290d784950b6248782b049cd9831bd6e034fd538.tar.gz
ch573-290d784950b6248782b049cd9831bd6e034fd538.tar.bz2
ch573-290d784950b6248782b049cd9831bd6e034fd538.zip
Add a bunch. Neglected doing commits.HEADmain
Add many different patterns, and organize it. Add a voltage discovery subsystem to allow the ch573 to detect if it's on 12v or 5v lights.
Diffstat (limited to 'src/spi.c')
-rw-r--r--src/spi.c61
1 files changed, 10 insertions, 51 deletions
diff --git a/src/spi.c b/src/spi.c
index a416014..d19b2bb 100644
--- a/src/spi.c
+++ b/src/spi.c
@@ -4,6 +4,7 @@
#include "ch573/gpio.h"
#include "ch573/spi.h"
+#include "gpio.h"
#include "sysled.h"
#define MAX_SPI_FIFO 8
@@ -17,57 +18,29 @@
void enable_spi(void)
{
- GPIO_I.pin_alternate.pin_spi0.set(GPIO, ON);
+ gpio_enable_alternate_function(AF_SPI, 1);
+ gpio_configure_pin(PIN_PB14, PIN_CFG_OUTPUT_20mA);
+ gpio_configure_pin(PIN_PB12, PIN_CFG_OUTPUT_20mA);
- 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_configure_pin(PIN_PB15, PIN_CFG_INPUT_PULL_DOWN);
- 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_configure_pin(PIN_PA14, PIN_CFG_OUTPUT_20mA);
+ gpio_configure_pin(PIN_PA12, PIN_CFG_OUTPUT_20mA);
- GPIO_PORT.pu.set(GPIO_PORT_B, ENABLED, 14);
-
- 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);
+ gpio_configure_pin(PIN_PA15, PIN_CFG_INPUT_PULL_DOWN);
SPI.clock_div.set(SPI0, 25);
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, 0x2); // Set mosi and sck
+ 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);
+ SPI.ctrl_mod.mst_sck_mod.set(SPI0, 0);
}
-// 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_cfg.dma_enable.set(SPI0, OFF);
-
- // printf("Start DMA: %p, %zu\n", output_buffer, len);
-
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));
@@ -85,17 +58,3 @@ void wait_for_dma()
}
set_sysled(0);
}
-
-void run_spi(void)
-{
- GPIO_PORT.out.set(GPIO_PORT_A, ON, 12);
- while (1) {
- 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);
- }
-}