b15f/firmware/spi.h

86 lines
1.8 KiB
C
Raw Normal View History

2019-03-25 12:00:22 +00:00
#ifndef SPI_H
#define SPI_H
2019-03-22 14:51:52 +00:00
2019-03-26 07:44:30 +00:00
#include <avr/io.h>
2019-03-25 12:00:22 +00:00
enum SPIADR {
2019-04-03 09:38:15 +00:00
AA0 = 0,
AA1 = 1,
BEBA0 = 2,
BEBA1 = 3,
EXT = 4,
2019-03-25 14:02:24 +00:00
SWITCH = 5,
2019-04-03 09:38:15 +00:00
NONE = 7,
2019-03-25 12:00:22 +00:00
};
class SPI
{
public:
2019-04-03 11:30:57 +00:00
/**
* Initialisiert die SPI Register
*/
2019-04-03 06:40:14 +00:00
void init(void) const volatile;
2019-04-03 11:30:57 +00:00
/**
* Behandlungsroutine für SPI_STC interrupt
*/
void handleTransfer(void) volatile;
/**
* Fügt ein Byte dem SPI Puffer an
*/
2019-04-03 09:38:15 +00:00
void addByte(uint8_t b) volatile;
2019-04-03 11:30:57 +00:00
/**
* Startet die Übertragung SPI Puffers
* Die Werte im Puffer werden mit den empfangen Werten überschrieben
* \param adr Adressierung des Empfängers (Slave Device)
*/
void transfer(SPIADR adr) volatile;
/**
* Blockiert, bis laufende SPI Vorgänge beendet sind
* Ist SPI gerade inaktiv, wird die Methode sofort beendet
*/
2019-04-03 11:35:12 +00:00
void wait(void) const volatile;
2019-04-03 09:38:15 +00:00
2019-04-03 11:30:57 +00:00
/**
* SPI Puffer Größe
* Maximale Anzahl an Bytes, die auf einmal an ein Gerät
* gesendet & empfangen werden können
*/
2019-04-03 09:38:15 +00:00
constexpr static uint8_t BUFFER_SIZE = 8;
2019-04-03 11:30:57 +00:00
/**
* SPI Puffer
* Vor Zugriff unbedingt wait() aufrufen
* Es sollte nur lesend zugegriffen werden, für schreiben siehe addByte()
*/
2019-04-03 09:38:15 +00:00
volatile uint8_t buffer[BUFFER_SIZE];
2019-03-22 14:51:52 +00:00
2019-04-03 09:38:15 +00:00
private:
2019-04-03 11:30:57 +00:00
/**
* Setzt die CS Leitung für den gewünschten Slave
*/
void setAdr(SPIADR adr) const volatile;
2019-04-03 09:38:15 +00:00
2019-04-03 11:30:57 +00:00
// Variablen für die Pufferverwaltung
2019-04-03 09:38:15 +00:00
volatile uint8_t index = 0;
volatile uint8_t length = 0;
2019-04-03 12:51:33 +00:00
// semaphore
2019-04-03 09:38:15 +00:00
volatile bool active = false;
2019-04-03 11:30:57 +00:00
// Constants
constexpr static uint8_t SLSL = PB4;
constexpr static uint8_t MOSI = PB5;
constexpr static uint8_t MISO = PB6;
constexpr static uint8_t SCLK = PB7;
constexpr static uint8_t DMUX1 = PD2;
constexpr static uint8_t DMUX2 = PD3;
constexpr static uint8_t DMUX3 = PD4;
2019-03-22 14:51:52 +00:00
};
2019-03-25 12:00:22 +00:00
#endif // SPI_H