aboutsummaryrefslogtreecommitdiff
path: root/src/spi.c
blob: d19b2bbe4de7129ae2eedf86f3e407c5e545de59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "spi.h"

#include <stdio.h>

#include "ch573/gpio.h"
#include "ch573/spi.h"
#include "gpio.h"
#include "sysled.h"

#define MAX_SPI_FIFO 8

#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_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_configure_pin(PIN_PB15, PIN_CFG_INPUT_PULL_DOWN);

  gpio_configure_pin(PIN_PA14, PIN_CFG_OUTPUT_20mA);
  gpio_configure_pin(PIN_PA12, PIN_CFG_OUTPUT_20mA);

  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.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);
  SPI.ctrl_mod.mst_sck_mod.set(SPI0, 0);
}

void dma_transfer(void* output_buffer, size_t 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));

  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) {
    set_sysled(1);
  }
  set_sysled(0);
}