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 --- spi.h | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 13 deletions(-) (limited to 'spi.h') diff --git a/spi.h b/spi.h index 86149f7..e1a1a5c 100644 --- a/spi.h +++ b/spi.h @@ -1,23 +1,67 @@ #include +#include #include +#include "mcu/mcu_def.h" +#include "buffers.h" + #ifndef _IOE_SPI_H_ #define _IOE_SPI_H_ -inline void ioe_spi_init(void); -inline int ioe_spi_bussy(void); -inline void ioe_spi_join(void); -#ifdef IOE_SPI_MASTER -inline int8_t ioe_spi_transfer(int8_t data); -#else /* IOE_SPI_MASTER */ -inline void ioe_spi_expose(int8_t data); -#endif /* IOE_SPI_MASTER */ +#ifndef MCUSUPPORT_SPI +#error "No SPI interface is known on your mcu." +#endif -// Following function must be user defined -inline void ioe_spi_retrieve(int8_t); +enum spiMode { + SPI_MODE_MASTER, + SPI_MODE_SLAVE +}; -#if !(__MCU_ATmega328p__ || __MCU_ATmega32U4p__) -#error "SPI is not probably supported by your hardware." -#endif + +/* + * Initialize SPI + * + * Parameters: + * mode - Specify mode of SPI interface + * + * NOTE: Global interrupt must be enabled for right function of SPI. + * { SREG |= _BV(7) } + */ +void spi_init(enum spiMode mode); +/* + * Returns NULL when device is not busy. + * When device is busy return values in non-zero. + */ +int8_t spi_busy(void); +/* + * Blocks processor until device is not busy. + */ +void spi_join(void); +/* + * Swap bytes with slave over SPI. + * This function blocks execution until device isn't busy (transfer completed). + * WARNING: Invoke this only when interface is initialized in MASTER mode. + */ +uint8_t spi_send(uint8_t data); +/* + * Transfer byte to slave over SPI. + * This function isn't blocking execution until transfer is complete. + * Always call spi_join before this function when called outside of spi_receive(). + * WARNING: Invoke this only when interface is initialized in MASTER mode. + */ +void spi_transfer(uint8_t data); +/* + * Expose data for next master request. + * Please don't use this when device is busy. + * Best place to call this is spi_receive(). + * WARNING: Invoke this only when interface is initialized in SLAVE mode. + */ +void spi_expose(uint8_t data); + +/* + * This function is called every time transfer is finished. + * And until return from this function interrupts are disabled. + */ +extern void (*spi_receive)(uint8_t data); #endif /* _IOE_SPI_H_ */ -- cgit v1.2.3