Skip to content

Commit 331b2f0

Browse files
committed
Clean up the SPI files
Be consistent about commenting style, and stay below 80 chars where appropriate.
1 parent 2bb742a commit 331b2f0

File tree

3 files changed

+106
-90
lines changed

3 files changed

+106
-90
lines changed

libraries/SPI/src/SPI.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
* You should have received a copy of the GNU Lesser General Public
1515
* License along with this library; if not, write to the Free Software
16-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1717
*
1818
*/
1919
#include "SPI.h"
@@ -48,7 +48,8 @@ void SPIClass::setDataMode(uint8_t dataMode)
4848

4949
void SPIClass::begin()
5050
{
51-
uint32_t flags = interrupt_lock(); // Protect from a scheduler and prevent transactionBegin
51+
/* Protect from a scheduler and prevent transactionBegin*/
52+
uint32_t flags = interrupt_lock();
5253
if (!initialized) {
5354
interruptMode = 0;
5455
interruptMask[0] = 0;
@@ -60,13 +61,14 @@ void SPIClass::begin()
6061
lsbFirst = false;
6162
frameSize = SPI_8_BIT;
6263

63-
// Set SS to high so a connected chip will be "deselected" by default
64-
// TODO - confirm that data register is updated even if pin is set as input
64+
/* Set SS to high so a connected chip will be "deselected" by default.
65+
* TODO - confirm that data register is updated even if pin is set as
66+
* input. */
6567
digitalWrite(ss_gpio, HIGH);
6668

67-
// When the SS pin is set as OUTPUT, it can be used as
68-
// a general purpose output port (it doesn't influence
69-
// SPI operations).
69+
/* When the SS pin is set as OUTPUT, it can be used as
70+
* a general purpose output port (it doesn't influence
71+
* SPI operations). */
7072
pinMode(ss_gpio, OUTPUT);
7173

7274
/* disable controller */
@@ -81,8 +83,9 @@ void SPIClass::begin()
8183
(frameSize << SPI_FSIZE_SHIFT) | (SPI_MODE0 << SPI_MODE_SHIFT);
8284

8385
/* Disable interrupts */
84-
/* Enable at least one slave device (mandatory, though SS signals are unused) */
8586
SPI_M_REG_VAL(spi_addr, IMR) = SPI_DISABLE_INT;
87+
/* Enable at least one slave device (mandatory, though
88+
* SS signals are unused) */
8689
SPI_M_REG_VAL(spi_addr, SER) = 0x1;
8790
/* Enable controller */
8891
SPI_M_REG_VAL(spi_addr, SPIEN) |= SPI_ENABLE;
@@ -96,16 +99,17 @@ void SPIClass::begin()
9699
g_APinDescription[SCK].ulPinMode = SPI_MUX_MODE;
97100

98101
}
99-
initialized++; // reference count
102+
initialized++; /* reference count */
100103
interrupt_unlock(flags);
101104
}
102105

103106
void SPIClass::end() {
104-
uint32_t flags = interrupt_lock(); // Protect from a scheduler and prevent transactionBegin
105-
// Decrease the reference counter
107+
/* Protect from a scheduler and prevent transactionBegin */
108+
uint32_t flags = interrupt_lock();
109+
/* Decrease the reference counter */
106110
if (initialized)
107111
initialized--;
108-
// If there are no more references disable SPI
112+
/* If there are no more references disable SPI */
109113
if (!initialized) {
110114
SPI_M_REG_VAL(spi_addr, SPIEN) &= SPI_DISABLE;
111115
MMIO_REG_VAL(PERIPH_CLK_GATE_CTRL) &= disable_val;
@@ -142,7 +146,7 @@ void SPIClass::usingInterrupt(uint8_t interruptNumber) {
142146
}
143147

144148
void SPIClass::notUsingInterrupt(uint8_t interruptNumber) {
145-
// Once in mode 8 we can't go back to 0 without a proper reference count
149+
/* Once in mode 8 we can't go back to 0 without a proper reference count */
146150
if (interruptMode == 8)
147151
return;
148152

libraries/SPI/src/SPI.h

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,25 @@
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
@@ -51,10 +52,11 @@
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
@@ -69,14 +71,17 @@
6971
#define SPI_MODE3 0x03
7072

7173
#define NUM_SPIDEVS 2
74+
7275
class SPISettings {
7376
public:
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

261270
private:
@@ -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

libraries/SPI/src/SPI_registers.h

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
* You should have received a copy of the GNU Lesser General Public
1515
* License along with this library; if not, write to the Free Software
16-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1717
*
1818
*/
1919
#ifndef _SPI_REGISTERS_H_
@@ -25,44 +25,47 @@
2525
#define SPI_M_REG_VAL(base, reg) MMIO_REG_VAL_FROM_BASE(base, (reg))
2626

2727
/* SoC SPI device register offsets */
28-
#define CTRL0 (0x00) /* SoC SPI Control Register 1 */
29-
#define SPIEN (0x08) /* SoC SPI Enable Register */
30-
#define SER (0x10) /* SoC SPI Slave Enable Register */
31-
#define BAUDR (0x14) /* SoC SPI Baud Rate Select */
32-
#define TXFL (0x20) /* SoC SPI Transmit FIFO Level Register */
33-
#define RXFL (0x24) /* SoC SPI Receive FIFO Level Register */
34-
#define SR (0x28) /* SoC SPI Status Register */
35-
#define IMR (0x2C) /* SoC SPI Interrupt Mask Register */
36-
#define DR (0x60) /* SoC SPI Data Register */
28+
#define CTRL0 (0x00) /* SoC SPI Control */
29+
#define SPIEN (0x08) /* SoC SPI Enable */
30+
#define SER (0x10) /* SoC SPI Slave Enable */
31+
#define BAUDR (0x14) /* SoC SPI Baud Rate Select */
32+
#define TXFL (0x20) /* SoC SPI Transmit FIFO Level */
33+
#define RXFL (0x24) /* SoC SPI Receive FIFO Level */
34+
#define SR (0x28) /* SoC SPI Status Register */
35+
#define IMR (0x2C) /* SoC SPI Interrupt Mask */
36+
#define DR (0x60) /* SoC SPI Data */
3737

3838
/* SPI specific macros */
39-
#define SPI_DISABLE_INT (0x0) /* Disable SoC SPI Interrupts */
40-
#define SPI_ENABLE (0x1) /* Enable SoC SPI Device */
41-
#define SPI_DISABLE (0x0) /* Disable SoC SPI Device */
42-
#define SPI_STATUS_BUSY (0x1) /* Busy status */
39+
#define SPI_DISABLE_INT (0x0) /* Disable SoC SPI Interrupts */
40+
#define SPI_ENABLE (0x1) /* Enable SoC SPI Device */
41+
#define SPI_DISABLE (0x0) /* Disable SoC SPI Device */
42+
#define SPI_STATUS_BUSY (0x1) /* Busy status */
4343

44-
#define SPI_8_BIT (7) /* 8-bit frame size */
45-
#define SPI_16_BIT (15) /* 16-bit frame size */
46-
#define SPI_24_BIT (23) /* 24-bit frame size */
47-
#define SPI_32_BIT (31) /* 32-bit frame size */
44+
#define SPI_8_BIT (7) /* 8-bit frame size */
45+
#define SPI_16_BIT (15) /* 16-bit frame size */
46+
#define SPI_24_BIT (23) /* 24-bit frame size */
47+
#define SPI_32_BIT (31) /* 32-bit frame size */
4848

49-
#define SPI_MODE_MASK (0xC0) /* CPOL = bit 7, CPHA = bit 6 on CTRL0 */
50-
#define SPI_MODE_SHIFT (6)
51-
#define SPI_FSIZE_MASK (0x1F0000) /* Valid frame sizes: 1- 32 bits */
52-
#define SPI_FSIZE_SHIFT (16)
53-
#define SPI_CLOCK_MASK (0xFFFE) /* Clock divider: any even value in the ragnge 2-65534 */
49+
#define SPI_MODE_MASK (0xC0) /* CPOL=bit 7, CPHA=bit 6 on CTRL0 */
50+
#define SPI_MODE_SHIFT (6)
51+
#define SPI_FSIZE_MASK (0x1F0000) /* Valid frame sizes: 1-32 bits */
52+
#define SPI_FSIZE_SHIFT (16)
53+
#define SPI_CLOCK_MASK (0xFFFE) /* Clock divider: any even value
54+
* in the ragnge 2-65534 */
5455

55-
#define SPI_FIFO_DEPTH (8UL)
56+
#define SPI_FIFO_DEPTH (8UL)
5657

5758
#define ENABLE_SPI_MASTER_0 (0x1 << 14)
5859
#define DISABLE_SPI_MASTER_0 (~ENABLE_SPI_MASTER_0)
5960
#define ENABLE_SPI_MASTER_1 (0x1 << 15)
6061
#define DISABLE_SPI_MASTER_1 (~ENABLE_SPI_MASTER_1)
6162

62-
#define SPI_BASE_CLOCK (CLOCK_SPEED*1000*1000) /* CLOCK_SPEED in MHz */
63+
/* CLOCK_SPEED in MHz */
64+
#define SPI_BASE_CLOCK (CLOCK_SPEED*1000*1000)
6365

6466
/* Utility function to reverse the bit order of a 32-bit word */
65-
static inline uint32_t arc32_bit_reverse(register uint32_t src, register uint32_t count)
67+
static inline uint32_t arc32_bit_reverse(register uint32_t src,
68+
register uint32_t count)
6669
{
6770
register uint32_t dst = 0;
6871
/* Copy the specified number of least-significant bits from src to dst,
@@ -85,4 +88,4 @@ static inline uint32_t arc32_bit_reverse(register uint32_t src, register uint32_
8588
#define SPI_REVERSE_24(b) arc32_bit_reverse((b), 24)
8689
#define SPI_REVERSE_32(b) arc32_bit_reverse((b), 32)
8790

88-
#endif // _SPI_REGISTERS_H_
91+
#endif /* _SPI_REGISTERS_H_ */

0 commit comments

Comments
 (0)