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 
12 //=====================================================================================================================================================
13 // Macro Definitions
14 //=====================================================================================================================================================
15 
16  #include <stdio.h>
18  #define SPI_CLOCK_DIV4 0x00
19  #define SPI_CLOCK_DIV16 0x01
21  #define SPI_CLOCK_DIV64 0x02
23  #define SPI_CLOCK_DIV128 0x03
25  #define SPI_CLOCK_DIV2 0x04
27  #define SPI_CLOCK_DIV8 0x05
29  #define SPI_CLOCK_DIV32 0x06
31 
33  #define SPI_MODE0 0x00
34  #define SPI_MODE1 0x04
36  #define SPI_MODE2 0x08
38  #define SPI_MODE3 0x0C
40 
42  #define SPI_MODE_MASK 0x0C
43  #define SPI_CLOCK_MASK 0x03
45  #define SPI_2XCLOCK_MASK 0x01
47 
48  namespace eagle_impl {
49 
50 //=====================================================================================================================================================
51 // SPI
52 //=====================================================================================================================================================
53 
54  //-------------------------------------------------------------------------------------------------------------------------------------------------
55  // class
56  //-------------------------------------------------------------------------------------------------------------------------------------------------
57 
71  struct SPIClass {
77  inline static byte transfer(byte data);
81  inline static void attachInterrupt(void);
85  inline static void detachInterrupt(void); // Default
89  static void begin(void); // Default
93  inline static void end(void);
98  inline static void setBitOrder(uint8_t bitOrder);
103  inline static void setDataMode(uint8_t mode);
108  inline static void setClockDivider(uint8_t rate);
109  };
110  }
112 
113  //-------------------------------------------------------------------------------------------------------------------------------------------------
114  // definitions
115  //-------------------------------------------------------------------------------------------------------------------------------------------------
116 
117  byte SPIClass::transfer(byte data) {
118  SPDR = data;
119  while (!(SPSR & _BV(SPIF)));
120  return SPDR;
121  }
122 
123  void SPIClass::attachInterrupt() { SPCR |= _BV(SPIE);}
124 
125  void SPIClass::detachInterrupt() { SPCR &= ~_BV(SPIE);}
126 
127 
128 
129  void SPIClass::end() { SPCR &= ~_BV(SPE); }
130 
131  void SPIClass::setBitOrder(uint8_t bitOrder) {
132  if(bitOrder == LSBFIRST) SPCR |= _BV(DORD);
133  else SPCR &= ~(_BV(DORD));
134  }
135 
136  void SPIClass::setDataMode(uint8_t mode) { SPCR = (SPCR & ~SPI_MODE_MASK) | mode; }
137 
138  void SPIClass::setClockDivider(uint8_t rate) {
139  SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
140  SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
141  }
142  SPIClass SPI;
143  void SPIClass::begin() {
144  digitalWrite(SS, HIGH);
145  pinMode(SS, OUTPUT); //doesn't block common use as_ OUTPUT!
146  SPCR |= _BV(MSTR);
147  SPCR |= _BV(SPE);
148  pinMode(SCK, OUTPUT);
149  pinMode(MOSI, OUTPUT);
150  }
151 
153 #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:71
#define SPI_CLOCK_MASK
Definition: eagle_SPI.h:44
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 byte transfer(byte data)
this will send a single bite via the SPI connection
static void setClockDivider(uint8_t rate)
this will change the clock devider the
#define SPI_MODE_MASK
Definition: eagle_SPI.h:42
static void setBitOrder(uint8_t bitOrder)
this will set the BitOrder
#define SPI_2XCLOCK_MASK
Definition: eagle_SPI.h:46