Byggern
spi.h
1 #pragma once
2 
3 #include "../stream/stream.h"
4 #include <avr/io.h>
5 #include <avr/interrupt.h>
9 namespace SPI {
10 
14  struct PIN{
15 
19  volatile uint8_t *port;
23  volatile uint8_t *ddr;
27  uint8_t pin;
28 
35  PIN(volatile uint8_t *port, volatile uint8_t *ddr, uint8_t pin): port(port), ddr(ddr), pin(pin){}
36 
42  PIN(): port(nullptr), ddr(nullptr), pin(0){}
43 
47  PIN operator=(PIN *original_pin){
48  PIN newPin(original_pin->port, original_pin->ddr, original_pin->pin);
49  return newPin;
50  }
51  };
52 
53  ISR(SPI_STC_vect);
60  class SPI: public Stream {
61 
62  private:
66  SPI();
67 
71  void InitializeTransmission();
72 
76  volatile bool ongoing_transmission = false;
77 
81  uint8_t throw_away_data_count = 0;
82 
86  friend void SPI_STC_vect();
87 
88 
89  public:
94  static SPI& GetInstance() {
95  static SPI instance;
96  return instance;
97  }
98 
102  SPI(const SPI&) = delete;
103 
107  void operator=(const SPI&) = delete;
108 
115  void SetDevice(PIN pin);
116 
124  void Initialize(PIN *pins, uint8_t number_of_pins, bool clock_polarity_falling, bool clock_phase_trailing);
125 
131  void WriteByte(uint8_t byte, bool wait);
132 
138  void WriteByteAndThrowAwayData(uint8_t byte, bool wait);
139 
144  };
145 }
This class is an interface for implementing a duplex FIFO stream.
Definition: stream.h:19
PIN current_pin
Definition: spi.h:143
static SPI & GetInstance()
Definition: spi.h:94
A struct which holds the information about a pin. Used in order to pass pin information.
Definition: spi.h:14
PIN(volatile uint8_t *port, volatile uint8_t *ddr, uint8_t pin)
Definition: spi.h:35
void SPI_STC_vect()
Definition: spi.cpp:6
PIN operator=(PIN *original_pin)
Definition: spi.h:47
A namespace containing everything related to the SPI driver.
Definition: spi.cpp:4
PIN()
Definition: spi.h:42
volatile uint8_t * ddr
Definition: spi.h:23
volatile uint8_t * port
Definition: spi.h:19
uint8_t pin
Definition: spi.h:27