add support for selective SPI device!

This commit is contained in:
ladyada 2018-11-03 17:39:10 -04:00
parent 3a14516a76
commit f5e164945b
4 changed files with 37 additions and 18 deletions

View File

@ -126,10 +126,29 @@ Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h,
/**************************************************************************/ /**************************************************************************/
Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h,
int8_t cs, int8_t dc, int8_t rst) int8_t cs, int8_t dc, int8_t rst)
: Adafruit_SPITFT(w, h, &SPI, cs, dc, rst)
{
// We just call the hardware SPI instantiator with the default SPI device (&SPI)
}
/**************************************************************************/
/*!
@brief Instantiate Adafruit SPI display driver with hardware SPI
@param w Display width in pixels
@param h Display height in pixels
@param spiClass A pointer to an SPI hardware interface, e.g. &SPI1
@param cs Chip select pin #
@param dc Data/Command pin #
@param rst Reset pin # (optional, pass -1 if unused)
*/
/**************************************************************************/
Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, SPIClass *spiClass,
int8_t cs, int8_t dc, int8_t rst)
: Adafruit_GFX(w, h) { : Adafruit_GFX(w, h) {
_cs = cs; _cs = cs;
_dc = dc; _dc = dc;
_rst = rst; _rst = rst;
_spi = spiClass;
_sclk = -1; _sclk = -1;
_mosi = -1; _mosi = -1;
_miso = -1; _miso = -1;

View File

@ -37,6 +37,7 @@ class Adafruit_SPITFT : public Adafruit_GFX {
public: public:
Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK, int8_t _RST = -1, int8_t _MISO = -1); Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK, int8_t _RST = -1, int8_t _MISO = -1);
Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _RST = -1); Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _RST = -1);
Adafruit_SPITFT(uint16_t w, uint16_t h, SPIClass *spiClass, int8_t _CS, int8_t _DC, int8_t _RST = -1);
virtual void begin(uint32_t freq) = 0; ///< Virtual begin() function to set SPI frequency, must be overridden in subclass. @param freq Maximum SPI hardware clock speed virtual void begin(uint32_t freq) = 0; ///< Virtual begin() function to set SPI frequency, must be overridden in subclass. @param freq Maximum SPI hardware clock speed
@ -87,6 +88,7 @@ class Adafruit_SPITFT : public Adafruit_GFX {
uint16_t color565(uint8_t r, uint8_t g, uint8_t b); uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
protected: protected:
SPIClass *_spi; ///< The SPI device we want to use (set in constructor)
uint32_t _freq; ///< SPI clock frequency (for hardware SPI) uint32_t _freq; ///< SPI clock frequency (for hardware SPI)
#if defined (__AVR__) || defined(TEENSYDUINO) || defined (ESP8266) || defined (ESP32) #if defined (__AVR__) || defined(TEENSYDUINO) || defined (ESP8266) || defined (ESP32)
int8_t _cs, _dc, _rst, _sclk, _mosi, _miso; int8_t _cs, _dc, _rst, _sclk, _mosi, _miso;

View File

@ -46,27 +46,25 @@
* Hardware SPI Macros * Hardware SPI Macros
* */ * */
#define SPI_OBJECT SPI
#if defined (__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1) #if defined (__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(SPI_CLOCK_DIV2); #define HSPI_SET_CLOCK() _spi->setClockDivider(SPI_CLOCK_DIV2);
#elif defined (__arm__) #elif defined (__arm__)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(11); #define HSPI_SET_CLOCK() _spi->setClockDivider(11);
#elif defined(ESP8266) || defined(ESP32) #elif defined(ESP8266) || defined(ESP32)
#define HSPI_SET_CLOCK() SPI_OBJECT.setFrequency(_freq); #define HSPI_SET_CLOCK() _spi->setFrequency(_freq);
#elif defined(RASPI) #elif defined(RASPI)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq); #define HSPI_SET_CLOCK() _spi->setClock(_freq);
#elif defined(ARDUINO_ARCH_STM32F1) #elif defined(ARDUINO_ARCH_STM32F1)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq); #define HSPI_SET_CLOCK() _spi->setClock(_freq);
#else #else
#define HSPI_SET_CLOCK() #define HSPI_SET_CLOCK()
#endif #endif
#ifdef SPI_HAS_TRANSACTION #ifdef SPI_HAS_TRANSACTION
#define HSPI_BEGIN_TRANSACTION() SPI_OBJECT.beginTransaction(SPISettings(_freq, MSBFIRST, SPI_MODE0)) #define HSPI_BEGIN_TRANSACTION() _spi->beginTransaction(SPISettings(_freq, MSBFIRST, SPI_MODE0))
#define HSPI_END_TRANSACTION() SPI_OBJECT.endTransaction() #define HSPI_END_TRANSACTION() _spi->endTransaction()
#else #else
#define HSPI_BEGIN_TRANSACTION() HSPI_SET_CLOCK(); SPI_OBJECT.setBitOrder(MSBFIRST); SPI_OBJECT.setDataMode(SPI_MODE0) #define HSPI_BEGIN_TRANSACTION() HSPI_SET_CLOCK(); _spi->setBitOrder(MSBFIRST); _spi->setDataMode(SPI_MODE0)
#define HSPI_END_TRANSACTION() #define HSPI_END_TRANSACTION()
#endif #endif
@ -75,13 +73,13 @@
#endif #endif
#if defined(ESP8266) || defined(ESP32) #if defined(ESP8266) || defined(ESP32)
// Optimized SPI (ESP8266 and ESP32) // Optimized SPI (ESP8266 and ESP32)
#define HSPI_READ() SPI_OBJECT.transfer(0) #define HSPI_READ() _spi->transfer(0)
#define HSPI_WRITE(b) SPI_OBJECT.write(b) #define HSPI_WRITE(b) _spi->write(b)
#define HSPI_WRITE16(s) SPI_OBJECT.write16(s) #define HSPI_WRITE16(s) _spi->write16(s)
#define HSPI_WRITE32(l) SPI_OBJECT.write32(l) #define HSPI_WRITE32(l) _spi->write32(l)
#ifdef SPI_HAS_WRITE_PIXELS #ifdef SPI_HAS_WRITE_PIXELS
#define SPI_MAX_PIXELS_AT_ONCE 32 #define SPI_MAX_PIXELS_AT_ONCE 32
#define HSPI_WRITE_PIXELS(c,l) SPI_OBJECT.writePixels(c,l) #define HSPI_WRITE_PIXELS(c,l) _spi->writePixels(c,l)
#else #else
#define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<((l)/2); i++){ SPI_WRITE16(((uint16_t*)(c))[i]); } #define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<((l)/2); i++){ SPI_WRITE16(((uint16_t*)(c))[i]); }
#endif #endif
@ -100,7 +98,7 @@ static inline uint8_t _avr_spi_read(void) {
#define HSPI_WRITE(b) {SPDR = (b); while(!(SPSR & _BV(SPIF)));} #define HSPI_WRITE(b) {SPDR = (b); while(!(SPSR & _BV(SPIF)));}
#define HSPI_READ() _avr_spi_read() #define HSPI_READ() _avr_spi_read()
#else #else
#define HSPI_WRITE(b) SPI_OBJECT.transfer((uint8_t)(b)) #define HSPI_WRITE(b) _spi->transfer((uint8_t)(b))
#define HSPI_READ() HSPI_WRITE(0) #define HSPI_READ() HSPI_WRITE(0)
#endif #endif
#define HSPI_WRITE16(s) HSPI_WRITE((s) >> 8); HSPI_WRITE(s) #define HSPI_WRITE16(s) HSPI_WRITE((s) >> 8); HSPI_WRITE(s)
@ -108,7 +106,7 @@ static inline uint8_t _avr_spi_read(void) {
#define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<(l); i+=2){ HSPI_WRITE(((uint8_t*)(c))[i+1]); HSPI_WRITE(((uint8_t*)(c))[i]); } #define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<(l); i+=2){ HSPI_WRITE(((uint8_t*)(c))[i+1]); HSPI_WRITE(((uint8_t*)(c))[i]); }
#endif #endif
#define SPI_BEGIN() if(_sclk < 0){SPI_OBJECT.begin();} #define SPI_BEGIN() if(_sclk < 0){_spi->begin();}
#define SPI_BEGIN_TRANSACTION() if(_sclk < 0){HSPI_BEGIN_TRANSACTION();} #define SPI_BEGIN_TRANSACTION() if(_sclk < 0){HSPI_BEGIN_TRANSACTION();}
#define SPI_END_TRANSACTION() if(_sclk < 0){HSPI_END_TRANSACTION();} #define SPI_END_TRANSACTION() if(_sclk < 0){HSPI_END_TRANSACTION();}
#define SPI_WRITE16(s) if(_sclk < 0){HSPI_WRITE16(s);}else{SSPI_WRITE16(s);} #define SPI_WRITE16(s) if(_sclk < 0){HSPI_WRITE16(s);}else{SSPI_WRITE16(s);}

View File

@ -1,5 +1,5 @@
name=Adafruit GFX Library name=Adafruit GFX Library
version=1.2.9 version=1.3.0
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from. sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from.