19
19
20
20
#include " SPI_registers.h"
21
21
22
- // SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
23
- // usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
22
+ /* SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(),
23
+ * usingInterrupt(), and SPISetting(clock, bitOrder, dataMode) */
24
24
#define SPI_HAS_TRANSACTION 1
25
25
26
- // SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method
26
+ /* SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method */
27
27
#define SPI_HAS_NOTUSINGINTERRUPT 1
28
28
29
- // SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version.
30
- // This way when there is a bug fix you can check this define to alert users
31
- // of your code if it uses better version of this library.
32
- // This also implies everything that SPI_HAS_TRANSACTION as documented above is
33
- // available too.
29
+ /* SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version.
30
+ * This way when there is a bug fix you can check this define to alert users
31
+ * of your code if it uses better version of this library.
32
+ * This also implies everything that SPI_HAS_TRANSACTION as documented above is
33
+ * available too. */
34
34
#define SPI_ATOMIC_VERSION 1
35
35
36
- // Uncomment this line to add detection of mismatched begin/end transactions.
37
- // A mismatch occurs if other libraries fail to use SPI.endTransaction() for
38
- // each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn
39
- // on if any mismatch is ever detected.
36
+ /* Uncomment this line to add detection of mismatched begin/end transactions.
37
+ * A mismatch occurs if other libraries fail to use SPI.endTransaction() for
38
+ * each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn
39
+ * on if any mismatch is ever detected. */
40
+
40
41
// #define SPI_TRANSACTION_MISMATCH_LED 5
41
42
42
43
#ifndef LSBFIRST
51
52
52
53
#define SPIDEV_0 0
53
54
#define SPIDEV_1 1
54
- /* For Arduino Uno compatibility, divider values are doubled to provide equivalent clock rates
55
- * e.g. SPI_CLOCK_DIV4 will produce a 4MHz clock
56
- * The Intel Curie has a 32MHz base clock and a min divider of 2, so max SPI clock is 16MHz
57
- */
55
+
56
+ /* For Arduino Uno compatibility, divider values are doubled to provide
57
+ * equivalent clock rates e.g. SPI_CLOCK_DIV4 will produce a 4MHz clock
58
+ * The Intel Curie has a 32MHz base clock and a min divider of 2, so max
59
+ * SPI clock is 16MHz */
58
60
#define SPI_CLOCK_DIV4 8
59
61
#define SPI_CLOCK_DIV16 32
60
62
#define SPI_CLOCK_DIV64 128
69
71
#define SPI_MODE3 0x03
70
72
71
73
#define NUM_SPIDEVS 2
74
+
72
75
class SPISettings {
73
76
public:
74
77
SPISettings (uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
75
78
/* Set frame size, bus mode and transfer mode */
76
- ctrl0 = (SPI_8_BIT << SPI_FSIZE_SHIFT) | ((dataMode << SPI_MODE_SHIFT) & SPI_MODE_MASK);
79
+ ctrl0 = (SPI_8_BIT << SPI_FSIZE_SHIFT)
80
+ | ((dataMode << SPI_MODE_SHIFT) & SPI_MODE_MASK);
77
81
/* Set SPI Clock Divider */
78
82
baudr = (SPI_BASE_CLOCK / clock ) & SPI_CLOCK_MASK;
79
- /* Only MSBFIRST supported in hardware, need to swizzle the bits if LSBFIRST selected */
83
+ /* Only MSBFIRST supported in hardware, need to swizzle the bits if
84
+ * LSBFIRST selected */
80
85
lsbFirst = (bitOrder == LSBFIRST) ? true : false ;
81
86
}
82
87
SPISettings () {
@@ -99,26 +104,26 @@ class SPIClass {
99
104
ss_gpio = spidevs[dev][3 ];
100
105
}
101
106
102
- // Initialize the SPI library
107
+ /* Initialize the SPI library */
103
108
void begin ();
104
109
105
- // If SPI is used from within an interrupt, this function registers
106
- // that interrupt with the SPI library, so beginTransaction() can
107
- // prevent conflicts. The input interruptNumber is the number used
108
- // with attachInterrupt. If SPI is used from a different interrupt
109
- // (eg, a timer), interruptNumber should be 255.
110
+ /* If SPI is used from within an interrupt, this function registers
111
+ * that interrupt with the SPI library, so beginTransaction() can
112
+ * prevent conflicts. The input interruptNumber is the number used
113
+ * with attachInterrupt. If SPI is used from a different interrupt
114
+ * (eg, a timer), interruptNumber should be 255. */
110
115
void usingInterrupt (uint8_t interruptNumber);
111
- // And this does the opposite.
116
+ /* And this does the opposite. */
112
117
void notUsingInterrupt (uint8_t interruptNumber);
113
- // Note: the usingInterrupt and notUsingInterrupt functions should
114
- // not to be called from ISR context or inside a transaction.
115
- // For details see:
116
- // https://github.com/arduino/Arduino/pull/2381
117
- // https://github.com/arduino/Arduino/pull/2449
118
-
119
- // Before using SPI.transfer() or asserting chip select pins,
120
- // this function is used to gain exclusive access to the SPI bus
121
- // and configure the correct settings.
118
+ /* Note: the usingInterrupt and notUsingInterrupt functions should
119
+ * not to be called from ISR context or inside a transaction.
120
+ * For details see:
121
+ * https://github.com/arduino/Arduino/pull/2381
122
+ * https://github.com/arduino/Arduino/pull/2449 */
123
+
124
+ /* Before using SPI.transfer() or asserting chip select pins,
125
+ * this function is used to gain exclusive access to the SPI bus
126
+ * and configure the correct settings. */
122
127
inline void beginTransaction (SPISettings settings) {
123
128
if (interruptMode > 0 ) {
124
129
if (interruptMode < 8 ) {
@@ -152,7 +157,7 @@ class SPIClass {
152
157
SPI_M_REG_VAL (spi_addr, SPIEN) |= SPI_ENABLE;
153
158
}
154
159
155
- // Write to the SPI bus (MOSI pin) and also receive (MISO pin)
160
+ /* Write to the SPI bus (MOSI pin) and also receive (MISO pin) */
156
161
inline uint8_t transfer (uint8_t data) {
157
162
setFrameSize (SPI_8_BIT);
158
163
if (lsbFirst)
@@ -217,8 +222,8 @@ class SPIClass {
217
222
}
218
223
}
219
224
220
- // After performing a group of transfers and releasing the chip select
221
- // signal, this function allows others to access the SPI bus
225
+ /* After performing a group of transfers and releasing the chip select
226
+ * signal, this function allows others to access the SPI bus */
222
227
inline void endTransaction (void ) {
223
228
#ifdef SPI_TRANSACTION_MISMATCH_LED
224
229
if (!inTransactionFlag) {
@@ -231,31 +236,35 @@ class SPIClass {
231
236
if (interruptMode > 0 ) {
232
237
if (interruptMode < 8 ) {
233
238
if (interruptMode & 1 )
234
- CLEAR_ARC_MASK (SS_GPIO_8B0_BASE_ADDR + SS_GPIO_INTMASK, interruptMask[0 ]);
239
+ CLEAR_ARC_MASK (SS_GPIO_8B0_BASE_ADDR + SS_GPIO_INTMASK,
240
+ interruptMask[0 ]);
235
241
if (interruptMode & 2 )
236
- CLEAR_ARC_MASK (SS_GPIO_8B1_BASE_ADDR + SS_GPIO_INTMASK, interruptMask[1 ]);
242
+ CLEAR_ARC_MASK (SS_GPIO_8B1_BASE_ADDR + SS_GPIO_INTMASK,
243
+ interruptMask[1 ]);
237
244
if (interruptMode & 4 )
238
- CLEAR_MMIO_MASK (SOC_GPIO_BASE_ADDR + SOC_GPIO_INTMASK, interruptMask[2 ]);
245
+ CLEAR_MMIO_MASK (SOC_GPIO_BASE_ADDR + SOC_GPIO_INTMASK,
246
+ interruptMask[2 ]);
239
247
} else {
240
248
interrupts ();
241
249
}
242
250
}
243
251
}
244
252
245
- // Disable the SPI bus
253
+ /* Disable the SPI bus */
246
254
void end ();
247
255
248
- // This function is deprecated. New applications should use
249
- // beginTransaction() to configure SPI settings.
256
+ /* This function is deprecated. New applications should use
257
+ * beginTransaction() to configure SPI settings. */
250
258
inline void setBitOrder (uint8_t bitOrder) {
251
- /* Only MSBFIRST supported in hardware, need to swizzle the bits if LSBFIRST selected */
259
+ /* Only MSBFIRST supported in hardware, need to swizzle the bits if
260
+ * LSBFIRST selected */
252
261
lsbFirst = (bitOrder == LSBFIRST) ? true : false ;
253
262
}
254
- // This function is deprecated. New applications should use
255
- // beginTransaction() to configure SPI settings.
263
+ /* This function is deprecated. New applications should use
264
+ * beginTransaction() to configure SPI settings. */
256
265
void setDataMode (uint8_t dataMode);
257
- // This function is deprecated. New applications should use
258
- // beginTransaction() to configure SPI settings.
266
+ /* This function is deprecated. New applications should use
267
+ * beginTransaction() to configure SPI settings. */
259
268
void setClockDivider (uint8_t clockDiv);
260
269
261
270
private:
@@ -264,8 +273,8 @@ class SPIClass {
264
273
uint32_t enable_val;
265
274
uint32_t disable_val;
266
275
uint32_t initialized;
267
- uint32_t interruptMode; // 0=none, 1-7=mask, 8=global
268
- uint32_t interruptMask[3 ]; // which interrupts to mask
276
+ uint32_t interruptMode; /* 0=none, 1-7=mask, 8=global */
277
+ uint32_t interruptMask[3 ]; /* which interrupts to mask */
269
278
#ifdef SPI_TRANSACTION_MISMATCH_LED
270
279
uint32_t inTransactionFlag;
271
280
#endif
0 commit comments