Pico-Arduino
HardwareSPI.h
1 /*
2  Copyright (c) 2018 Arduino LLC. All right reserved.
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  See the GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #pragma once
20 
21 #include "Common.h"
22 #include <inttypes.h>
23 #include "Stream.h"
24 
25 #define SPI_HAS_TRANSACTION
26 
27 namespace arduino {
28 
29 typedef enum {
30  SPI_MODE0 = 0,
31  SPI_MODE1 = 1,
32  SPI_MODE2 = 2,
33  SPI_MODE3 = 3,
34 } SPIMode;
35 
36 
37 class SPISettings {
38  public:
39  SPISettings(uint32_t clock, BitOrder bitOrder, SPIMode dataMode) {
40  if (__builtin_constant_p(clock)) {
41  init_AlwaysInline(clock, bitOrder, dataMode);
42  } else {
43  init_MightInline(clock, bitOrder, dataMode);
44  }
45  }
46 
47  SPISettings(uint32_t clock, BitOrder bitOrder, int dataMode) {
48  if (__builtin_constant_p(clock)) {
49  init_AlwaysInline(clock, bitOrder, (SPIMode)dataMode);
50  } else {
51  init_MightInline(clock, bitOrder, (SPIMode)dataMode);
52  }
53  }
54 
55  // Default speed set to 4MHz, SPI mode set to MODE 0 and Bit order set to MSB first.
56  SPISettings() { init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); }
57 
58  bool operator==(const SPISettings& rhs) const
59  {
60  if ((this->clockFreq == rhs.clockFreq) &&
61  (this->bitOrder == rhs.bitOrder) &&
62  (this->dataMode == rhs.dataMode)) {
63  return true;
64  }
65  return false;
66  }
67 
68  bool operator!=(const SPISettings& rhs) const
69  {
70  return !(*this == rhs);
71  }
72 
73  uint32_t getClockFreq() const {
74  return clockFreq;
75  }
76  SPIMode getDataMode() const {
77  return dataMode;
78  }
79  BitOrder getBitOrder() const {
80  return (bitOrder);
81  }
82 
83  private:
84  void init_MightInline(uint32_t clock, BitOrder bitOrder, SPIMode dataMode) {
85  init_AlwaysInline(clock, bitOrder, dataMode);
86  }
87 
88  // Core developer MUST use an helper function in beginTransaction() to use this data
89  void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, SPIMode dataMode) __attribute__((__always_inline__)) {
90  this->clockFreq = clock;
91  this->dataMode = dataMode;
92  this->bitOrder = bitOrder;
93  }
94 
95  uint32_t clockFreq;
96  SPIMode dataMode;
97  BitOrder bitOrder;
98 
99  friend class HardwareSPI;
100 };
101 
102 const SPISettings DEFAULT_SPI_SETTINGS = SPISettings();
103 
105 {
106  public:
107  virtual ~HardwareSPI() { }
108 
109  virtual uint8_t transfer(uint8_t data) = 0;
110  virtual uint16_t transfer16(uint16_t data) = 0;
111  virtual void transfer(void *buf, size_t count) = 0;
112 
113  // Transaction Functions
114  virtual void usingInterrupt(int interruptNumber) = 0;
115  virtual void notUsingInterrupt(int interruptNumber) = 0;
116  virtual void beginTransaction(SPISettings settings) = 0;
117  virtual void endTransaction(void) = 0;
118 
119  // SPI Configuration methods
120  virtual void attachInterrupt() = 0;
121  virtual void detachInterrupt() = 0;
122 
123  virtual void begin() = 0;
124  virtual void end() = 0;
125 };
126 
127 // Alias SPIClass to HardwareSPI since it's already the defacto standard for SPI classe name
128 typedef HardwareSPI SPIClass;
129 
130 }
Definition: HardwareSPI.h:105
Definition: HardwareSPI.h:37