From bab75a6068ab0a64fe22395ad11efafccbf0d842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sun, 11 Oct 2015 13:06:28 +0200 Subject: Implement SPI and remove files --- src/spi.c | 66 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 30 deletions(-) (limited to 'src/spi.c') diff --git a/src/spi.c b/src/spi.c index 9562e6a..262f402 100644 --- a/src/spi.c +++ b/src/spi.c @@ -1,48 +1,54 @@ #include "../spi.h" -#include "mcu/mcu_def.h" -inline void ioe_spi_join(void) { - // TODO +volatile int8_t _spi_busy; + +inline void spi_init(enum spiMode mode) { + _spi_busy = 0; + if (mode == SPI_MODE_MASTER) { + // Set MOSI and SCK output + DDR_SPI |= _BV(DD_MOSI) | _BV(DD_SCLK); + // Set MISO pull up resistor + PORT_SPI |= _BV(PORT_MISO); + // Enable SPI master, set clock rate fck/16 and enable SPI interrupt + SPCR = _BV(SPE) | _BV(SPIE) | _BV(MSTR) | _BV(SPR0); + } else { + // Set MISO as output + DDR_SPI |= _BV(DD_MISO); + // Set SCLK and MOSI pull up resistor + PORT_SPI |= _BV(PORT_SCLK) | _BV(PORT_MOSI); + // Enable SPI and interrupt + SPCR = _BV(SPE) | _BV(SPIE); + } } -inline int ioe_spi_bussy(void) { - // TODO +inline int8_t spi_busy(void) { + return _spi_busy; } -#ifdef IOE_SPI_MASTER -inline void ioe_spi_init(void) { - // Set MOSI and SCK output - DDR_SPI |= _BV(DD_MOSI) | _BV(DD_SCLK); - // Set MISO pull up resistor - PORT_SPI |= _BV(PORT_MISO); - // Enable SPI interrupt - SPCR |= _BV(SPIE); - // Enable SPI master and set clock rate fck/16 - SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR0); +inline void spi_join(void) { + while (spi_busy()); } -inline void ioe_spi_transfer(int8_t data) { - SPDR = data; +inline uint8_t spi_send(uint8_t data) { + spi_transfer(data); + while (spi_busy()); + return SPDR; } -#else /* IOE_SPI_MASTER */ -inline void ioe_spi_init(void) { - // Set MISO as output - DDR_SPI = _BV(DD_MISO); - // Set SCLK and MOSI pull up resistor - PORT_SPI |= _BV(PORT_SCLK) | _BV(PORT_MOSI); - // Enable SPI interrupt - SPCR |= _BV(SPIE); - // Enable SPI - SPCR = _BV(SPE); +inline void spi_transfer(uint8_t data) { + _spi_busy = 1; + SPDR = data; } -inline void ioe_spi_expose(int8_t data) { +inline void spi_expose(uint8_t data) { SPDR = data; } -#endif /* IOE_SPI_MASTER */ +////// Interrupts //////////////////////////////// +void (*spi_receive)(uint8_t data) = 0; SIGNAL(SPI_STC_vect) { - ioe_spi_retrieve(SPDR); + _spi_busy = 0; + if (spi_receive) + spi_receive(SPDR); } -- cgit v1.2.3