Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI Updates #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Adafruit_SSD1306.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,24 @@ 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!
Adafruit_SSD1306::Adafruit_SSD1306(int8_t reset) :
Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT) {
sclk = dc = cs = sid = -1;
rst = reset;
spiClass = NULL;
}


Expand All @@ -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.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By not passing the chip select pin here, we were mistakenly allowing the SPI driver to set up the default chip select pin, A2, as an output in addition to the pin that was passed in by the user.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
else
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion Adafruit_SSD1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down