Arduino-SPI
Serial Peripheral Interface (SPI) library for Arduino
SPI.h
Go to the documentation of this file.
1 
19 #ifndef SPI_H
20 #define SPI_H
21 
22 #include "GPIO.h"
23 
27 class SPI {
28 public:
30  static const uint32_t MAX_FREQ = F_CPU / 2;
31 
35  SPI() : m_busy(false) {}
36 
43  virtual uint8_t prescale(uint32_t frequency)
44  {
45  return (F_CPU / frequency);
46  }
47 
55  virtual void acquire(uint8_t mode, uint8_t bitorder, uint8_t prescale)
56  {
57  (void) mode;
58  (void) bitorder;
59  (void) prescale;
60  lock();
61  }
62 
67  virtual void release()
68  {
69  unlock();
70  }
71 
78  virtual uint8_t transfer(uint8_t value) = 0;
79 
84  SPI& operator>>(uint8_t& value)
85  __attribute__((always_inline))
86  {
87  value = transfer(0);
88  return (*this);
89  }
90 
95  SPI& operator<<(uint8_t value)
96  __attribute__((always_inline))
97  {
98  transfer(value);
99  return (*this);
100  }
101 
110  virtual void transfer(void* dest, const void* src, size_t count)
111  {
112  if (count == 0 || dest == NULL || src == NULL) return;
113  const uint8_t* sp = (const uint8_t*) src;
114  uint8_t* dp = (uint8_t*) dest;
115  do *dp++ = transfer(*sp++); while (--count);
116  }
117 
124  virtual void read(void* buf, size_t count)
125  {
126  if (count == 0 || buf == NULL) return;
127  uint8_t* bp = (uint8_t*) buf;
128  do *bp++ = transfer(0); while (--count);
129  }
130 
137  virtual void write(const void* buf, size_t count)
138  {
139  if (count == 0 || buf == NULL) return;
140  uint8_t* bp = (uint8_t*) buf;
141  do transfer(*bp++); while (--count);
142  }
143 
151  template<uint8_t MODE, uint8_t BITORDER, uint32_t FREQ, BOARD::pin_t SS_PIN>
152  class Device {
153  public:
160  Device(SPI& spi, bool ss = HIGH) :
161  m_spi(spi),
162  PRESCALE(spi.prescale(FREQ))
163  {
164  m_ss.output();
165  m_ss = ss;
166  }
167 
171  void acquire()
172  __attribute__((always_inline))
173  {
174  m_spi.acquire(MODE, BITORDER, PRESCALE);
175  m_ss.toggle();
176  }
177 
181  void release()
182  __attribute__((always_inline))
183  {
184  m_ss.toggle();
185  m_spi.release();
186  }
187 
193  uint8_t transfer(uint8_t value)
194  __attribute__((always_inline))
195  {
196  return (m_spi.transfer(value));
197  }
198 
206  void transfer(void* dest, const void* src, size_t count)
207  __attribute__((always_inline))
208  {
209  m_spi.transfer(dest, src, count);
210  }
211 
217  void read(void* buf, size_t count)
218  __attribute__((always_inline))
219  {
220  m_spi.read(buf, count);
221  }
222 
228  void write(const void* buf, size_t count)
229  __attribute__((always_inline))
230  {
231  m_spi.write(buf, count);
232  }
233 
234  protected:
237 
239  GPIO<SS_PIN> m_ss;
240 
242  const uint8_t PRESCALE;
243  };
244 
245 protected:
247  volatile bool m_busy;
248 
252  void lock()
253  __attribute__((always_inline))
254  {
255  while (m_busy) yield();
256  m_busy = true;
257  }
258 
262  void unlock()
263  __attribute__((always_inline))
264  {
265  m_busy = false;
266  }
267 };
268 #endif
virtual void read(void *buf, size_t count)
Definition: SPI.h:124
const uint8_t PRESCALE
Definition: SPI.h:242
Definition: SPI.h:27
void read(void *buf, size_t count)
Definition: SPI.h:217
virtual uint8_t prescale(uint32_t frequency)
Definition: SPI.h:43
virtual uint8_t transfer(uint8_t value)=0
SPI()
Definition: SPI.h:35
Device(SPI &spi, bool ss=HIGH)
Definition: SPI.h:160
static const uint32_t MAX_FREQ
Definition: SPI.h:30
virtual void acquire(uint8_t mode, uint8_t bitorder, uint8_t prescale)
Definition: SPI.h:55
void write(const void *buf, size_t count)
Definition: SPI.h:228
virtual void transfer(void *dest, const void *src, size_t count)
Definition: SPI.h:110
GPIO< BOARD::D10 > ss
Definition: Benchmark.ino:26
void lock()
Definition: SPI.h:252
volatile bool m_busy
Definition: SPI.h:247
void unlock()
Definition: SPI.h:262
void transfer(void *dest, const void *src, size_t count)
Definition: SPI.h:206
SPI & operator>>(uint8_t &value)
Definition: SPI.h:84
Hardware::SPI spi
Definition: Benchmark.ino:27
SPI & m_spi
Definition: SPI.h:236
SPI & operator<<(uint8_t value)
Definition: SPI.h:95
virtual void write(const void *buf, size_t count)
Definition: SPI.h:137
#define BITORDER
Definition: Benchmark.ino:8
GPIO< SS_PIN > m_ss
Definition: SPI.h:239
uint8_t transfer(uint8_t value)
Definition: SPI.h:193
void acquire()
Definition: SPI.h:171
void release()
Definition: SPI.h:181
virtual void release()
Definition: SPI.h:67