Skip to content

Commit 5d764fc

Browse files
committed
Hardware SPI BREAKING: unswap mode 2 and 3 for portability
use SPI.restoreEsp8266LegacyMode2And3() for former behaviour
1 parent ca79f2c commit 5d764fc

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

libraries/SPI/SPI.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef union {
4242
SPIClass::SPIClass() {
4343
useHwCs = false;
4444
pinSet = SPI_PINS_HSPI;
45+
legacyMode2and3 = false;
4546
}
4647

4748
bool SPIClass::pins(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
@@ -161,6 +162,13 @@ void SPIClass::setDataMode(uint8_t dataMode) {
161162
bool CPOL = (dataMode & 0x10); ///< CPOL (Clock Polarity)
162163
bool CPHA = (dataMode & 0x01); ///< CPHA (Clock Phase)
163164

165+
if(!legacyMode2and3) {
166+
// https://github.com/esp8266/Arduino/issues/2416
167+
// https://github.com/esp8266/Arduino/pull/2418
168+
if(CPOL) // Ensure same behavior as
169+
CPHA ^= 1; // SAM, AVR and Intel Boards
170+
}
171+
164172
if(CPHA) {
165173
SPI1U |= (SPIUSME);
166174
} else {

libraries/SPI/SPI.h

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class SPIClass {
5858
void end();
5959
void setHwCs(bool use);
6060
void setBitOrder(uint8_t bitOrder);
61+
void restoreEsp8266LegacyMode2And3(); // must be called *before* setDataMode()
6162
void setDataMode(uint8_t dataMode);
6263
void setFrequency(uint32_t freq);
6364
void setClockDivider(uint32_t clockDiv);
@@ -77,6 +78,7 @@ class SPIClass {
7778
private:
7879
bool useHwCs;
7980
uint8_t pinSet;
81+
bool legacyMode2and3;
8082
void writeBytes_(const uint8_t * data, uint8_t size);
8183
void transferBytes_(const uint8_t * out, uint8_t * in, uint8_t size);
8284
void transferBytesAligned_(const uint8_t * out, uint8_t * in, uint8_t size);

0 commit comments

Comments
 (0)