1919
2020#include " SPI_registers.h"
2121
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) */
2424#define SPI_HAS_TRANSACTION 1
2525
26- // SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method
26+ /* SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method */
2727#define SPI_HAS_NOTUSINGINTERRUPT 1
2828
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. */
3434#define SPI_ATOMIC_VERSION 1
3535
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+
4041// #define SPI_TRANSACTION_MISMATCH_LED 5
4142
4243#ifndef LSBFIRST
5152
5253#define SPIDEV_0 0
5354#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 */
5860#define SPI_CLOCK_DIV4 8
5961#define SPI_CLOCK_DIV16 32
6062#define SPI_CLOCK_DIV64 128
6971#define SPI_MODE3 0x03
7072
7173#define NUM_SPIDEVS 2
74+
7275class SPISettings {
7376public:
7477 SPISettings (uint32_t clock, uint8_t bitOrder, uint8_t dataMode) {
7578 /* 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);
7781 /* Set SPI Clock Divider */
7882 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 */
8085 lsbFirst = (bitOrder == LSBFIRST) ? true : false ;
8186 }
8287 SPISettings () {
@@ -99,26 +104,26 @@ class SPIClass {
99104 ss_gpio = spidevs[dev][3 ];
100105 }
101106
102- // Initialize the SPI library
107+ /* Initialize the SPI library */
103108 void begin ();
104109
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. */
110115 void usingInterrupt (uint8_t interruptNumber);
111- // And this does the opposite.
116+ /* And this does the opposite. */
112117 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. */
122127 inline void beginTransaction (SPISettings settings) {
123128 if (interruptMode > 0 ) {
124129 if (interruptMode < 8 ) {
@@ -152,7 +157,7 @@ class SPIClass {
152157 SPI_M_REG_VAL (spi_addr, SPIEN) |= SPI_ENABLE;
153158 }
154159
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) */
156161 inline uint8_t transfer (uint8_t data) {
157162 setFrameSize (SPI_8_BIT);
158163 if (lsbFirst)
@@ -217,8 +222,8 @@ class SPIClass {
217222 }
218223 }
219224
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 */
222227 inline void endTransaction (void ) {
223228#ifdef SPI_TRANSACTION_MISMATCH_LED
224229 if (!inTransactionFlag) {
@@ -231,31 +236,35 @@ class SPIClass {
231236 if (interruptMode > 0 ) {
232237 if (interruptMode < 8 ) {
233238 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 ]);
235241 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 ]);
237244 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 ]);
239247 } else {
240248 interrupts ();
241249 }
242250 }
243251 }
244252
245- // Disable the SPI bus
253+ /* Disable the SPI bus */
246254 void end ();
247255
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. */
250258 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 */
252261 lsbFirst = (bitOrder == LSBFIRST) ? true : false ;
253262 }
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. */
256265 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. */
259268 void setClockDivider (uint8_t clockDiv);
260269
261270private:
@@ -264,8 +273,8 @@ class SPIClass {
264273 uint32_t enable_val;
265274 uint32_t disable_val;
266275 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 */
269278 #ifdef SPI_TRANSACTION_MISMATCH_LED
270279 uint32_t inTransactionFlag;
271280 #endif
0 commit comments