From 2dc2fc0dfd324320c1d4b35d68a19a491e462afd Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Mon, 22 Apr 2024 19:02:13 -0500 Subject: [PATCH] Add configurable SPI device to arduino_default BUS_SPI --- .../v1/platforms/arduino_default/Bus_SPI.cpp | 37 +++++++++++-------- .../v1/platforms/arduino_default/Bus_SPI.hpp | 4 +- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/lgfx/v1/platforms/arduino_default/Bus_SPI.cpp b/src/lgfx/v1/platforms/arduino_default/Bus_SPI.cpp index eb39fd30..4035ae0a 100644 --- a/src/lgfx/v1/platforms/arduino_default/Bus_SPI.cpp +++ b/src/lgfx/v1/platforms/arduino_default/Bus_SPI.cpp @@ -50,14 +50,19 @@ namespace lgfx { dc_h(); pinMode(_cfg.pin_dc, pin_mode_t::output); - //SPI.pins(_cfg.pin_sclk, _cfg.pin_miso, _cfg.pin_mosi, -1); - SPI.begin(); + //PrivateSPI->pins(_cfg.pin_sclk, _cfg.pin_miso, _cfg.pin_mosi, -1); + PrivateSPI->begin(); return true; } void Bus_SPI::release(void) { - SPI.end(); + PrivateSPI->end(); + } + + void Bus_SPI::spi_device(HardwareSPI *newSPI) + { + PrivateSPI = newSPI; } void Bus_SPI::beginTransaction(void) @@ -65,26 +70,26 @@ namespace lgfx dc_h(); //SPISettings setting(_cfg.freq_write, BitOrder::MSBFIRST, _cfg.spi_mode, true); SPISettings setting(_cfg.freq_write, MSBFIRST, _cfg.spi_mode); - SPI.beginTransaction(setting); + PrivateSPI->beginTransaction(setting); } void Bus_SPI::endTransaction(void) { - SPI.endTransaction(); + PrivateSPI->endTransaction(); dc_h(); } void Bus_SPI::beginRead(void) { - SPI.endTransaction(); + PrivateSPI->endTransaction(); //SPISettings setting(_cfg.freq_read, BitOrder::MSBFIRST, _cfg.spi_mode, false); SPISettings setting(_cfg.freq_read, MSBFIRST, _cfg.spi_mode); - SPI.beginTransaction(setting); + PrivateSPI->beginTransaction(setting); } void Bus_SPI::endRead(void) { - SPI.endTransaction(); + PrivateSPI->endTransaction(); beginTransaction(); } @@ -100,14 +105,14 @@ namespace lgfx bool Bus_SPI::writeCommand(uint32_t data, uint_fast8_t bit_length) { dc_l(); - SPI.transfer((uint8_t*)&data, bit_length >> 3); + PrivateSPI->transfer((uint8_t*)&data, bit_length >> 3); dc_h(); return true; } void Bus_SPI::writeData(uint32_t data, uint_fast8_t bit_length) { - SPI.transfer((uint8_t*)&data, bit_length >> 3); + PrivateSPI->transfer((uint8_t*)&data, bit_length >> 3); } void Bus_SPI::writeDataRepeat(uint32_t data, uint_fast8_t bit_length, uint32_t length) @@ -116,7 +121,7 @@ namespace lgfx auto bytes = bit_length >> 3; do { - SPI.send(reinterpret_cast(&data), bytes); + PrivateSPI->send(reinterpret_cast(&data), bytes); } while (--length); /*/ const uint8_t dst_bytes = bit_length >> 3; @@ -137,7 +142,7 @@ namespace lgfx fillpos += fillpos; } - SPI.transfer(buf, len * dst_bytes); + PrivateSPI->transfer(buf, len * dst_bytes); } while (length -= len); //*/ } @@ -153,7 +158,7 @@ namespace lgfx if (limit <= 32) limit <<= 1; auto buf = _flip_buffer.getBuffer(len * dst_bytes); param->fp_copy(buf, 0, len, param); - SPI.transfer(buf, len * dst_bytes); + PrivateSPI->transfer(buf, len * dst_bytes); } while (length -= len); } @@ -161,7 +166,7 @@ namespace lgfx { if (dc) dc_h(); else dc_l(); - SPI.transfer(const_cast(data), length); + PrivateSPI->transfer(const_cast(data), length); if (!dc) dc_h(); } @@ -173,7 +178,7 @@ namespace lgfx int idx = 0; do { - res |= SPI.transfer(0) << idx; + res |= PrivateSPI->transfer(0) << idx; idx += 8; } while (--bit_length); return res; @@ -183,7 +188,7 @@ namespace lgfx { do { - dst[0] = SPI.transfer(0); + dst[0] = PrivateSPI->transfer(0); ++dst; } while (--length); return true; diff --git a/src/lgfx/v1/platforms/arduino_default/Bus_SPI.hpp b/src/lgfx/v1/platforms/arduino_default/Bus_SPI.hpp index f9e4f027..40de089d 100644 --- a/src/lgfx/v1/platforms/arduino_default/Bus_SPI.hpp +++ b/src/lgfx/v1/platforms/arduino_default/Bus_SPI.hpp @@ -52,6 +52,7 @@ namespace lgfx bool init(void) override; void release(void) override; + void spi_device(HardwareSPI *newSPI); void beginTransaction(void) override; void endTransaction(void) override; @@ -76,7 +77,7 @@ namespace lgfx bool readBytes(uint8_t* dst, uint32_t length, bool use_dma) override; void readPixels(void* dst, pixelcopy_t* param, uint32_t length) override; - private: + protected: __attribute__ ((always_inline)) inline void dc_h(void) { gpio_hi(_cfg.pin_dc); @@ -85,6 +86,7 @@ namespace lgfx gpio_lo(_cfg.pin_dc); } + HardwareSPI *PrivateSPI = &SPI; config_t _cfg; FlipBuffer _flip_buffer; bool _need_wait;