Skip to content

Commit

Permalink
Hardware SPI BREAKING: unswap mode 2 and 3 for portability
Browse files Browse the repository at this point in the history
use SPI.restoreEsp8266LegacyMode2And3() for former behaviour
  • Loading branch information
d-a-v committed Apr 4, 2019
1 parent ca79f2c commit 5d764fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libraries/SPI/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef union {
SPIClass::SPIClass() {
useHwCs = false;
pinSet = SPI_PINS_HSPI;
legacyMode2and3 = false;
}

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

if(!legacyMode2and3) {
// https://github.com/esp8266/Arduino/issues/2416
// https://github.com/esp8266/Arduino/pull/2418
if(CPOL) // Ensure same behavior as
CPHA ^= 1; // SAM, AVR and Intel Boards
}

if(CPHA) {
SPI1U |= (SPIUSME);
} else {
Expand Down
2 changes: 2 additions & 0 deletions libraries/SPI/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class SPIClass {
void end();
void setHwCs(bool use);
void setBitOrder(uint8_t bitOrder);
void restoreEsp8266LegacyMode2And3(); // must be called *before* setDataMode()
void setDataMode(uint8_t dataMode);
void setFrequency(uint32_t freq);
void setClockDivider(uint32_t clockDiv);
Expand All @@ -77,6 +78,7 @@ class SPIClass {
private:
bool useHwCs;
uint8_t pinSet;
bool legacyMode2and3;
void writeBytes_(const uint8_t * data, uint8_t size);
void transferBytes_(const uint8_t * out, uint8_t * in, uint8_t size);
void transferBytesAligned_(const uint8_t * out, uint8_t * in, uint8_t size);
Expand Down

0 comments on commit 5d764fc

Please sign in to comment.