49
49
#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR
50
50
#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR
51
51
52
+ // Flags for the state of SPI, used as needed.
53
+ // Normally inTransaction is not used.
54
+ typedef struct SPIflags {
55
+ bool padding : 1 ;
56
+ bool inTransaction : 1 ;
57
+ uint8_t interruptMode : 6 ; // 0=none, 1=mask, 2=global (more can be added)
58
+ } __attribute__((packed)) SPIflags_t;
59
+
52
60
// define SPI_AVR_EIMSK for AVR boards with external interrupt pins
53
61
#if defined(EIMSK)
54
62
#define SPI_AVR_EIMSK EIMSK
@@ -158,9 +166,9 @@ class SPIClass {
158
166
// this function is used to gain exclusive access to the SPI bus
159
167
// and configure the correct settings.
160
168
inline static void beginTransaction (SPISettings settings) {
161
- if (interruptMode > 0 ) {
169
+ if (modeFlags. interruptMode > 0 ) {
162
170
#ifdef SPI_AVR_EIMSK
163
- if (interruptMode == 1 ) {
171
+ if (modeFlags. interruptMode == 1 ) {
164
172
interruptSave = SPI_AVR_EIMSK;
165
173
SPI_AVR_EIMSK &= ~interruptMask;
166
174
} else
@@ -171,11 +179,11 @@ class SPIClass {
171
179
}
172
180
}
173
181
#ifdef SPI_TRANSACTION_MISMATCH_LED
174
- if (inTransactionFlag ) {
182
+ if (modeFlags. inTransaction ) {
175
183
pinMode (SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
176
184
digitalWrite (SPI_TRANSACTION_MISMATCH_LED, HIGH);
177
185
}
178
- inTransactionFlag = 1 ;
186
+ modeFlags. inTransaction = true ;
179
187
#endif
180
188
SPCR = settings.spcr ;
181
189
SPSR = settings.spsr ;
@@ -230,15 +238,15 @@ class SPIClass {
230
238
// signal, this function allows others to access the SPI bus
231
239
inline static void endTransaction (void ) {
232
240
#ifdef SPI_TRANSACTION_MISMATCH_LED
233
- if (!inTransactionFlag ) {
241
+ if (!modeFlags. inTransaction ) {
234
242
pinMode (SPI_TRANSACTION_MISMATCH_LED, OUTPUT);
235
243
digitalWrite (SPI_TRANSACTION_MISMATCH_LED, HIGH);
236
244
}
237
- inTransactionFlag = 0 ;
245
+ modeFlags. inTransaction = false ;
238
246
#endif
239
- if (interruptMode > 0 ) {
247
+ if (modeFlags. interruptMode > 0 ) {
240
248
#ifdef SPI_AVR_EIMSK
241
- if (interruptMode == 1 ) {
249
+ if (modeFlags. interruptMode == 1 ) {
242
250
SPI_AVR_EIMSK = interruptSave;
243
251
} else
244
252
#endif
@@ -275,12 +283,9 @@ class SPIClass {
275
283
inline static void detachInterrupt () { SPCR &= ~_BV (SPIE); }
276
284
277
285
private:
278
- static uint8_t interruptMode ; // 0=none, 1=mask, 2=global
286
+ static SPIflags_t modeFlags ; // Flags for the state and mode of SPI
279
287
static uint8_t interruptMask; // which interrupts to mask
280
288
static uint8_t interruptSave; // temp storage, to restore state
281
- #ifdef SPI_TRANSACTION_MISMATCH_LED
282
- static uint8_t inTransactionFlag;
283
- #endif
284
289
};
285
290
286
291
extern SPIClass SPI;
0 commit comments