Skip to content

Commit 4948bf5

Browse files
committed
[avr] Added SPI.notUsingInterrupt() (Andrew Kroll)
1 parent 6cfc5c2 commit 4948bf5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

libraries/SPI/SPI.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,45 @@ void SPIClass::usingInterrupt(uint8_t interruptNumber)
151151
interrupts();
152152
}
153153

154+
void SPIClass::notUsingInterrupt(uint8_t interruptNumber)
155+
{
156+
// Once in mode 2 we can't go back to 0 without a proper reference count
157+
if (interruptMode == 2)
158+
return;
159+
uint8_t mask = 0;
160+
uint8_t sreg = SREG;
161+
noInterrupts(); // Protect from a scheduler and prevent transactionBegin
162+
switch (interruptNumber) {
163+
#ifdef SPI_INT0_MASK
164+
case 0: mask = SPI_INT0_MASK; break;
165+
#endif
166+
#ifdef SPI_INT1_MASK
167+
case 1: mask = SPI_INT1_MASK; break;
168+
#endif
169+
#ifdef SPI_INT2_MASK
170+
case 2: mask = SPI_INT2_MASK; break;
171+
#endif
172+
#ifdef SPI_INT3_MASK
173+
case 3: mask = SPI_INT3_MASK; break;
174+
#endif
175+
#ifdef SPI_INT4_MASK
176+
case 4: mask = SPI_INT4_MASK; break;
177+
#endif
178+
#ifdef SPI_INT5_MASK
179+
case 5: mask = SPI_INT5_MASK; break;
180+
#endif
181+
#ifdef SPI_INT6_MASK
182+
case 6: mask = SPI_INT6_MASK; break;
183+
#endif
184+
#ifdef SPI_INT7_MASK
185+
case 7: mask = SPI_INT7_MASK; break;
186+
#endif
187+
default:
188+
break;
189+
// this case can't be reached
190+
}
191+
interruptMask &= ~mask;
192+
if (!interruptMask)
193+
interruptMode = 0;
194+
SREG = sreg;
195+
}

libraries/SPI/SPI.h

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
// usingInterrupt(), and SPISetting(clock, bitOrder, dataMode)
2121
#define SPI_HAS_TRANSACTION 1
2222

23+
// SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method
24+
#define SPI_HAS_NOTUSINGINTERRUPT 1
25+
2326
// Uncomment this line to add detection of mismatched begin/end transactions.
2427
// A mismatch occurs if other libraries fail to use SPI.endTransaction() for
2528
// each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn
@@ -154,6 +157,13 @@ class SPIClass {
154157
// with attachInterrupt. If SPI is used from a different interrupt
155158
// (eg, a timer), interruptNumber should be 255.
156159
static void usingInterrupt(uint8_t interruptNumber);
160+
// And this does the opposite.
161+
static void notUsingInterrupt(uint8_t interruptNumber);
162+
// Note: the usingInterrupt and notUsingInterrupt functions should
163+
// not to be called from ISR context or inside a transaction.
164+
// For details see:
165+
// https://github.com/arduino/Arduino/pull/2381
166+
// https://github.com/arduino/Arduino/pull/2449
157167

158168
// Before using SPI.transfer() or asserting chip select pins,
159169
// this function is used to gain exclusive access to the SPI bus

0 commit comments

Comments
 (0)