From 8dc88064840148a5445d39abbc17934e23aae051 Mon Sep 17 00:00:00 2001 From: Jordan Schaenzle Date: Mon, 8 Oct 2018 14:41:13 -0400 Subject: [PATCH 1/2] SPI Updates - Updated Adafruit_SSD1306 to optionally use SPI1. - Pass chip select pin to spi::begin so that the driver no longer sets the pin mode for the default pins which may not be used. --- Adafruit_SSD1306.cpp | 16 ++++++++++------ Adafruit_SSD1306.h | 9 ++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Adafruit_SSD1306.cpp b/Adafruit_SSD1306.cpp index a108e1f..fe30e88 100644 --- a/Adafruit_SSD1306.cpp +++ b/Adafruit_SSD1306.cpp @@ -128,14 +128,16 @@ Adafruit_SSD1306::Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RS sclk = SCLK; sid = SID; hwSPI = false; + spiClass = NULL; } // constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset -Adafruit_SSD1306::Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) { +Adafruit_SSD1306::Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS, bool USE_SPI1) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) { dc = DC; rst = RST; cs = CS; hwSPI = true; + spiClass = USE_SPI1 ? &SPI1 : &SPI; } // initializer for I2C - we only indicate the reset pin! @@ -143,6 +145,7 @@ Adafruit_SSD1306::Adafruit_SSD1306(int8_t reset) : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) { sclk = dc = cs = sid = -1; rst = reset; + spiClass = NULL; } @@ -161,10 +164,11 @@ void Adafruit_SSD1306::begin(uint8_t vccstate, uint8_t i2caddr) { } if (hwSPI){ digitalWrite(cs, HIGH); - SPI.setBitOrder(MSBFIRST); - SPI.setClockDivider(SPI_CLOCK_DIV8); // 72MHz / 8 = 9Mhz - SPI.setDataMode(0); - SPI.begin(); + spiClass->setBitOrder(MSBFIRST); + spiClass->setClockDivider(SPI_CLOCK_DIV8); // 72MHz / 8 = 9Mhz + spiClass->setDataMode(0); + spiClass->begin(cs); // Passing the chip-select here just sets the pin mode. + // The peripheral won't automatically assert/deassert the pin during operation. } } else @@ -450,7 +454,7 @@ void Adafruit_SSD1306::clearDisplay(void) { inline void Adafruit_SSD1306::fastSPIwrite(uint8_t d) { if(hwSPI) { - (void)SPI.transfer(d); + (void)spiClass->transfer(d); } else { shiftOut(sid, sclk, MSBFIRST, d); // SSD1306 specs show MSB out first } diff --git a/Adafruit_SSD1306.h b/Adafruit_SSD1306.h index 76f13ea..5771986 100644 --- a/Adafruit_SSD1306.h +++ b/Adafruit_SSD1306.h @@ -111,8 +111,14 @@ All text above, and the splash screen must be included in any redistribution class Adafruit_SSD1306 : public Adafruit_GFX { public: + + enum SpiPeripheral { + PERIPH_SPI, + PERIPH_SPI1, + }; + Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS); - Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS); + Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS, bool USE_SPI1 = false); Adafruit_SSD1306(int8_t RST); void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = SSD1306_I2C_ADDRESS); @@ -139,6 +145,7 @@ class Adafruit_SSD1306 : public Adafruit_GFX { private: int8_t _i2caddr, _vccstate, sid, sclk, dc, rst, cs; + SPIClass * spiClass; void fastSPIwrite(uint8_t c); boolean hwSPI; From d54f594281059b452c8385bb2ed03d82cfe88711 Mon Sep 17 00:00:00 2001 From: Jordan Schaenzle Date: Mon, 8 Oct 2018 20:58:45 -0400 Subject: [PATCH 2/2] Removed enum that ended up being unneeded --- Adafruit_SSD1306.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Adafruit_SSD1306.h b/Adafruit_SSD1306.h index 5771986..63d4e1a 100644 --- a/Adafruit_SSD1306.h +++ b/Adafruit_SSD1306.h @@ -112,11 +112,6 @@ All text above, and the splash screen must be included in any redistribution class Adafruit_SSD1306 : public Adafruit_GFX { public: - enum SpiPeripheral { - PERIPH_SPI, - PERIPH_SPI1, - }; - Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS); Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS, bool USE_SPI1 = false); Adafruit_SSD1306(int8_t RST);