SPI revised

This commit is contained in:
Tristan Krause 2019-04-03 13:30:57 +02:00
parent 5671bd9a78
commit 2298c2a783
8 changed files with 541 additions and 509 deletions

View file

@ -1,18 +1,7 @@
#ifndef SPI_H
#define SPI_H
#include <util/delay.h>
#include <avr/io.h>
#include <stdint.h>
#define SLSL PB4
#define MOSI PB5
#define MISO PB6
#define SCLK PB7
#define DMUX1 PD2
#define DMUX2 PD3
#define DMUX3 PD4
enum SPIADR {
AA0 = 0,
@ -27,22 +16,68 @@ enum SPIADR {
class SPI
{
public:
SPI(void);
/**
* Initialisiert die SPI Register
*/
void init(void) const volatile;
void handleTransfer() volatile;
void transfer(uint8_t adr) volatile;
/**
* Behandlungsroutine für SPI_STC interrupt
*/
void handleTransfer(void) volatile;
/**
* Fügt ein Byte dem SPI Puffer an
*/
void addByte(uint8_t b) volatile;
/**
* 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
*/
void wait(void) volatile;
/**
* SPI Puffer Größe
* Maximale Anzahl an Bytes, die auf einmal an ein Gerät
* gesendet & empfangen werden können
*/
constexpr static uint8_t BUFFER_SIZE = 8;
/**
* SPI Puffer
* Vor Zugriff unbedingt wait() aufrufen
* Es sollte nur lesend zugegriffen werden, für schreiben siehe addByte()
*/
volatile uint8_t buffer[BUFFER_SIZE];
private:
void setAdr(uint8_t) const volatile;
/**
* Setzt die CS Leitung für den gewünschten Slave
*/
void setAdr(SPIADR adr) const volatile;
// Variablen für die Pufferverwaltung
volatile uint8_t index = 0;
volatile uint8_t length = 0;
volatile bool active = false;
// 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;
};
#endif // SPI_H