diff options
Diffstat (limited to 'lib/spi_sw.c')
| -rw-r--r-- | lib/spi_sw.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/spi_sw.c b/lib/spi_sw.c new file mode 100644 index 0000000..1b5656d --- /dev/null +++ b/lib/spi_sw.c @@ -0,0 +1,32 @@ +#include "spi_sw.h" +#include <libopencm3/stm32/gpio.h> +#include <stdint.h> + +SPI_IMPL SpiInit(uint16_t MOSI_pin, uint16_t CLK_pin) { + SPI_IMPL outp = { + .MOSI_pin = MOSI_pin, + .CLK_pin = CLK_pin, + .delay = (uint16_t) 1000, + }; + return outp; +} + +void SpiSend(SPI_IMPL *spi, uint8_t playload) { + if (TRAILING_EDGE) { + gpio_clear(GPIOA, spi->CLK_pin); + } else { + gpio_set(GPIOA, spi->CLK_pin); + } + + for (int i = 0; i < 8; i++) { + gpio_toggle(GPIOA, spi->CLK_pin); + if ((playload >> i) & 1) { + gpio_clear(GPIOA, spi->MOSI_pin); + } else { + gpio_set(GPIOA, spi->MOSI_pin); + } + delayCycles(spi->delay); + gpio_toggle(GPIOA, spi->CLK_pin); + delayCycles(spi->delay); + } +} |
