eBoard ๐Ÿ‰  โ‘ โ‘งโ‘จ
Written for SIA 2017/2018
eagle_SPI.h
Go to the documentation of this file.
1 #ifndef EAGLE_EBOARD_HELPLIB_SPI
2 #define EAGLE_EBOARD_HELPLIB_SPI
3 
4  #include <stdio.h>
6  #define SPI_CLOCK_DIV4 0x00
7  #define SPI_CLOCK_DIV16 0x01
9  #define SPI_CLOCK_DIV64 0x02
11  #define SPI_CLOCK_DIV128 0x03
13  #define SPI_CLOCK_DIV2 0x04
15  #define SPI_CLOCK_DIV8 0x05
17  #define SPI_CLOCK_DIV32 0x06
19 
21  #define SPI_MODE0 0x00
22  #define SPI_MODE1 0x04
24  #define SPI_MODE2 0x08
26  #define SPI_MODE3 0x0C
28 
30  #define SPI_MODE_MASK 0x0C
31  #define SPI_CLOCK_MASK 0x03
33  #define SPI_2XCLOCK_MASK 0x01
35  namespace eagle_impl {
49  struct SPIClass {
55  inline static byte transfer(byte _data);
59  inline static void attachInterrupt(void);
63  inline static void detachInterrupt(void); // Default
67  static void begin(void); // Default
71  inline static void end(void);
76  inline static void setBitOrder(uint8_t bitOrder);
81  inline static void setDataMode(uint8_t mode);
86  inline static void setClockDivider(uint8_t rate);
87  };
88  }
90  byte SPIClass::transfer(byte _data) {
91  SPDR = _data;
92  while (!(SPSR & _BV(SPIF)));
93  return SPDR;
94  }
95 
96  void SPIClass::attachInterrupt() { SPCR |= _BV(SPIE);}
97 
98  void SPIClass::detachInterrupt() { SPCR &= ~_BV(SPIE);}
99 
100  void SPIClass::begin() {
101  digitalWrite(SS, HIGH);
102  pinMode(SS, OUTPUT); //doesn't block common use as_ OUTPUT!
103  SPCR |= _BV(MSTR);
104  SPCR |= _BV(SPE);
105  pinMode(SCK, OUTPUT);
106  pinMode(MOSI, OUTPUT);
107  }
108 
109  void SPIClass::end() {SPCR &= ~_BV(SPE);}
110 
111  void SPIClass::setBitOrder(uint8_t bitOrder) {
112  if(bitOrder == LSBFIRST) SPCR |= _BV(DORD);
113  else SPCR &= ~(_BV(DORD));
114  }
115 
116  void SPIClass::setDataMode(uint8_t mode) { SPCR = (SPCR & ~SPI_MODE_MASK) | mode; }
117 
118  void SPIClass::setClockDivider(uint8_t rate) {
119  SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
120  SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
121  }
122  SPIClass SPI;
123 
125 #endif
static void detachInterrupt(void)
disables the interrupt feature
[SPI] This is used to avoid path resolving issues and defines the common known Arduino SPI interface ...
Definition: eagle_SPI.h:49
#define SPI_CLOCK_MASK
Definition: eagle_SPI.h:32
static void end(void)
this will end the SPI connection
this namespace contains all the Don&#39;t use manually classes ;)
static void attachInterrupt(void)
enables the interrupt feature
static void begin(void)
this will setup everything for SPI connection
static void setDataMode(uint8_t mode)
this will set the Data transfer mode
static void setClockDivider(uint8_t rate)
this will change the clock devider the
#define SPI_MODE_MASK
Definition: eagle_SPI.h:30
static byte transfer(byte _data)
this will send a single bite via the SPI connection
static void setBitOrder(uint8_t bitOrder)
this will set the BitOrder
#define SPI_2XCLOCK_MASK
Definition: eagle_SPI.h:34