diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/spi.c | 66 | ||||
-rw-r--r-- | src/spi_usart.c | 14 | ||||
-rw-r--r-- | src/spi_usi.c | 54 | ||||
-rw-r--r-- | src/usb_cdc.c | 4 |
4 files changed, 36 insertions, 102 deletions
@@ -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); } diff --git a/src/spi_usart.c b/src/spi_usart.c deleted file mode 100644 index 2e5d612..0000000 --- a/src/spi_usart.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "../spi_usart.h" -#include "../mcu/mcu_def.h" - -inline void ioe_spi_usart_init(void) { -} - -inline int8_t ioe_spi_usart_transfer(int8_t d) { -} - -inline int ioe_spi_usart_bussy(void) { -} - -inline void ioe_spi_usart_join(void) { -} diff --git a/src/spi_usi.c b/src/spi_usi.c deleted file mode 100644 index 8baa8e2..0000000 --- a/src/spi_usi.c +++ /dev/null @@ -1,54 +0,0 @@ -#include "../spi_usi.h" -#include "../mcu/mcu_def.h" - -#ifdef IOE_SPI_USI_MASTER - -// TODO counter settings with interups -inline void ioe_spi_usi_init(void) { - // Set USCK and DO as output - DDR_USI |= _BV(DD_USCK) | _BV(DD_DO); - // Set DI pull up resistor - PORT_USI |= _BV(PORT_DI); - - USICR |= _BV(USIWM0) | _BV(USICS1) | _BV(USICLK); -} - -inline int8_t ioe_spi_usi_transfer(int8_t d) { - USISR |= _BV(USIOIF); - USIDR = d; - do { - USICR |= _BV(USITC); - } while (!(USISR & _BV(USIOIF))); - return USIDR; -} - -#else /* IOE_SPI_USI_MASTER */ - -inline void ioe_spi_usi_init(void) { - // Set DO as output - DDR_USI |= _BV(DD_DO); - // Set USCK and DI pull up resistor - PORT_USI |= _BV(PORT_USCK) | _BV(PORT_DI); - - USICR |= _BV(USIWM0) | _BV(USICS1) | _BV(USIOIE); -} - -inline void ioe_spi_usi_expose(int8_t data) { - USIDR = data; -} - -inline uint8_t ioe_spi_usi_busy(void) { - return USISR & 0x0F; -} - -inline void ioe_spi_usi_join(void) { - while (ioe_spi_usi_busy()) { - } -} - -SIGNAL(USI_OVF_vect) { - ioe_spi_usi_retrieve(USIDR); - USISR |= _BV(USIOIF); -} - -#endif /* IOE_SPI_USI_MASTER */ diff --git a/src/usb_cdc.c b/src/usb_cdc.c deleted file mode 100644 index 2a1ba3a..0000000 --- a/src/usb_cdc.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "../usb_cdc.h" - -void ioe_usb_cdc_init(void) { -} |