aboutsummaryrefslogtreecommitdiff
path: root/spi.h
diff options
context:
space:
mode:
authorKarel Kočí <cynerd@email.cz>2015-10-11 13:06:28 +0200
committerKarel Kočí <cynerd@email.cz>2015-10-11 13:06:28 +0200
commitbab75a6068ab0a64fe22395ad11efafccbf0d842 (patch)
tree59344457d25c2e72904acac20118eb16633908fe /spi.h
parent1b0b3cb1b61759be5c2e100d08c84e5eec924a58 (diff)
downloadavr-ioe-bab75a6068ab0a64fe22395ad11efafccbf0d842.tar.gz
avr-ioe-bab75a6068ab0a64fe22395ad11efafccbf0d842.tar.bz2
avr-ioe-bab75a6068ab0a64fe22395ad11efafccbf0d842.zip
Implement SPI and remove files
Diffstat (limited to 'spi.h')
-rw-r--r--spi.h70
1 files changed, 57 insertions, 13 deletions
diff --git a/spi.h b/spi.h
index 86149f7..e1a1a5c 100644
--- a/spi.h
+++ b/spi.h
@@ -1,23 +1,67 @@
#include <avr/io.h>
+#include <avr/interrupt.h>
#include <stdint.h>
+#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_ */