From 4c9558852a982e25ff6c5cb7b7febe6abdb3a77f Mon Sep 17 00:00:00 2001 From: Nero <41307858+nero@users.noreply.github.com> Date: Wed, 13 Nov 2019 22:40:02 +0000 Subject: [PATCH] Add SPI driver --- drivers/spi.c | 23 +++++++++++++++++++++++ drivers/spi.h | 4 ++++ 2 files changed, 27 insertions(+) create mode 100644 drivers/spi.c create mode 100644 drivers/spi.h diff --git a/drivers/spi.c b/drivers/spi.c new file mode 100644 index 0000000..bb59a03 --- /dev/null +++ b/drivers/spi.c @@ -0,0 +1,23 @@ +#include "spi.h" +#include + +uint8_t spi_init(uint8_t spcr) { + PORTB |= (1 << 5); // set SCK hi + DDRB |= (1 << 5); // set SCK as output + DDRB &= ~(1 << 4); // set MISO as input + DDRB |= (1 << 3); // set MOSI as output + DDRB |= (1 << 2); // SS must be output for Master mode to work + + SPCR = spcr | SPE; + return(SPSR); +} + +uint8_t spi_transfer(uint8_t in) { + uint8_t v; + SPDR = in; + // wait for transfer to complete + while(! (SPSR & (1< + +uint8_t spi_init(uint8_t spcr); +uint8_t spi_transfer(uint8_t in);