diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 41 | ||||
-rw-r--r-- | src/spi.c | 62 |
2 files changed, 86 insertions, 17 deletions
@@ -5,6 +5,7 @@ #include "ch573/pwr.h" #include "ch573/uart.h" #include "clock.h" +#include "spi.h" #include "system.h" #define GPIO_PORT_A ch573_gpio__gpio_port_a @@ -124,28 +125,34 @@ static void fast_delay() */ int main(void) { - GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8); - GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 8); - GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + printf("Running SPI.\n"); - GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 11); - GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 11); + enable_spi(); - GPIO_PORT.dir.set(GPIO_PORT_A, DIR_IN, 10); - GPIO_PORT.pd_drv.set(GPIO_PORT_A, PD_DRV_OPEN_DRAIN, 11); + run_spi(); - set_system_clock_6Mhz(); - uint32_t reg = (*(uint32_t*)0x40001008); - reg &= ~0x1f; - reg |= 10; - enter_safe_mode(); - (*(uint32_t*)0x40001008) = reg; + // GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8); + // GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 8); + // GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + // GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 11); + // GPIO_PORT.pd_drv.set(GPIO_PORT_A, 0, 11); - for (int i = 0;; ++i) { - GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); - GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); - } + // GPIO_PORT.dir.set(GPIO_PORT_A, DIR_IN, 10); + // GPIO_PORT.pd_drv.set(GPIO_PORT_A, PD_DRV_OPEN_DRAIN, 11); + + // set_system_clock_6Mhz(); + // uint32_t reg = (*(uint32_t*)0x40001008); + // reg &= ~0x1f; + // reg |= 10; + // enter_safe_mode(); + // (*(uint32_t*)0x40001008) = reg; + + + // for (int i = 0;; ++i) { + // GPIO_PORT.out.set(GPIO_PORT_A, ON, 8); + // GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8); + // } // clock_cfg_t cfg; // for (int i = 0;; ++i) { diff --git a/src/spi.c b/src/spi.c new file mode 100644 index 0000000..3105f1b --- /dev/null +++ b/src/spi.c @@ -0,0 +1,62 @@ +#include "spi.h" + +#include <stdio.h> + +#include "ch573/gpio.h" +#include "ch573/spi.h" + +#define SPI CH573_SPI__SPI_T_INTF +#define SPI0 ch573_spi__spi0 + +#define GPIO_PORT_A ch573_gpio__gpio_port_a +#define GPIO_PORT_B ch573_gpio__gpio_port_b +#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF + +#define GPIO_I CH573_GPIO__GPIO_T_INTF +#define GPIO ch573_gpio__gpio + +void enable_spi(void) +{ + GPIO_I.pin_alternate.pin_spi0.set(GPIO, OFF); + + 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_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_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); + + SPI.clock_div.set(SPI0, 16); + 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, 0x7); // Set mosi and sck + SPI.ctrl_cfg.auto_if.set(SPI0, 1); + SPI.ctrl_cfg.dma_enable.set(SPI0, OFF); +} + +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); + } +} |