Skip to content

Commit

Permalink
move arduino_default to use HardwareSPI constructor(int)
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-bennett committed Apr 30, 2024
1 parent 78b058e commit dc46e05
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
38 changes: 17 additions & 21 deletions src/lgfx/v1/platforms/arduino_default/Bus_SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,46 +50,42 @@ namespace lgfx
{
dc_h();
pinMode(_cfg.pin_dc, pin_mode_t::output);
//PrivateSPI->pins(_cfg.pin_sclk, _cfg.pin_miso, _cfg.pin_mosi, -1);
PrivateSPI->begin();
spi = new HardwareSPI(_cfg.spi_host);
spi->begin();
return true;
}

void Bus_SPI::release(void)
{
PrivateSPI->end();
spi->end();
}

void Bus_SPI::spi_device(HardwareSPI *newSPI)
{
PrivateSPI = newSPI;
}

void Bus_SPI::beginTransaction(void)
{
dc_h();
//SPISettings setting(_cfg.freq_write, BitOrder::MSBFIRST, _cfg.spi_mode, true);
SPISettings setting(_cfg.freq_write, MSBFIRST, _cfg.spi_mode);
PrivateSPI->beginTransaction(setting);
spi->beginTransaction(setting);
}

void Bus_SPI::endTransaction(void)
{
PrivateSPI->endTransaction();
spi->endTransaction();
dc_h();
}

void Bus_SPI::beginRead(void)
{
PrivateSPI->endTransaction();
spi->endTransaction();
//SPISettings setting(_cfg.freq_read, BitOrder::MSBFIRST, _cfg.spi_mode, false);
SPISettings setting(_cfg.freq_read, MSBFIRST, _cfg.spi_mode);
PrivateSPI->beginTransaction(setting);
spi->beginTransaction(setting);
}

void Bus_SPI::endRead(void)
{
PrivateSPI->endTransaction();
spi->endTransaction();
beginTransaction();
}

Expand All @@ -105,14 +101,14 @@ namespace lgfx
bool Bus_SPI::writeCommand(uint32_t data, uint_fast8_t bit_length)
{
dc_l();
PrivateSPI->transfer((uint8_t*)&data, bit_length >> 3);
spi->transfer((uint8_t*)&data, bit_length >> 3);
dc_h();
return true;
}

void Bus_SPI::writeData(uint32_t data, uint_fast8_t bit_length)
{
PrivateSPI->transfer((uint8_t*)&data, bit_length >> 3);
spi->transfer((uint8_t*)&data, bit_length >> 3);
}

void Bus_SPI::writeDataRepeat(uint32_t data, uint_fast8_t bit_length, uint32_t length)
Expand All @@ -121,7 +117,7 @@ namespace lgfx
auto bytes = bit_length >> 3;
do
{
PrivateSPI->send(reinterpret_cast<uint8_t*>(&data), bytes);
spi->send(reinterpret_cast<uint8_t*>(&data), bytes);
} while (--length);
/*/
const uint8_t dst_bytes = bit_length >> 3;
Expand All @@ -142,7 +138,7 @@ namespace lgfx
fillpos += fillpos;
}

PrivateSPI->transfer(buf, len * dst_bytes);
spi->transfer(buf, len * dst_bytes);
} while (length -= len);
//*/
}
Expand All @@ -158,15 +154,15 @@ namespace lgfx
if (limit <= 32) limit <<= 1;
auto buf = _flip_buffer.getBuffer(len * dst_bytes);
param->fp_copy(buf, 0, len, param);
PrivateSPI->transfer(buf, len * dst_bytes);
spi->transfer(buf, len * dst_bytes);
} while (length -= len);
}

void Bus_SPI::writeBytes(const uint8_t* data, uint32_t length, bool dc, bool use_dma)
{
if (dc) dc_h();
else dc_l();
PrivateSPI->transfer(const_cast<uint8_t*>(data), length);
spi->transfer(const_cast<uint8_t*>(data), length);
if (!dc) dc_h();
}

Expand All @@ -178,7 +174,7 @@ namespace lgfx
int idx = 0;
do
{
res |= PrivateSPI->transfer(0) << idx;
res |= spi->transfer(0) << idx;
idx += 8;
} while (--bit_length);
return res;
Expand All @@ -188,7 +184,7 @@ namespace lgfx
{
do
{
dst[0] = PrivateSPI->transfer(0);
dst[0] = spi->transfer(0);
++dst;
} while (--length);
return true;
Expand All @@ -214,4 +210,4 @@ namespace lgfx
}
}

#endif
#endif
5 changes: 3 additions & 2 deletions src/lgfx/v1/platforms/arduino_default/Bus_SPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace lgfx
int16_t pin_mosi = -1;
int16_t pin_dc = -1;
uint8_t spi_mode = 0;
int8_t spi_host = 0;

};

const config_t& config(void) const { return _cfg; }
Expand All @@ -52,7 +54,6 @@ namespace lgfx

bool init(void) override;
void release(void) override;
void spi_device(HardwareSPI *newSPI);

void beginTransaction(void) override;
void endTransaction(void) override;
Expand Down Expand Up @@ -86,7 +87,7 @@ namespace lgfx
gpio_lo(_cfg.pin_dc);
}

HardwareSPI *PrivateSPI = &SPI;
HardwareSPI *spi;
config_t _cfg;
FlipBuffer _flip_buffer;
bool _need_wait;
Expand Down
13 changes: 9 additions & 4 deletions src/lgfx/v1/platforms/arduino_default/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ namespace lgfx
/// unimplemented.
namespace spi
{
cpp::result<void, error_t> init(int spi_host, int spi_sclk, int spi_miso, int spi_mosi) { cpp::result<void, error_t> res = {}; return res; }
HardwareSPI *spi;
cpp::result<void, error_t> init(int spi_host, int spi_sclk, int spi_miso, int spi_mosi) {
cpp::result<void, error_t> res = {};
spi = new HardwareSPI(spi_host);
spi->begin();
return res; }
void release(int spi_host) {}
void beginTransaction(int spi_host, uint32_t freq, int spi_mode) {
SPISettings setting(freq, MSBFIRST, SPI_MODE0);
SPI.beginTransaction(setting);
spi->beginTransaction(setting);
}
void endTransaction(int spi_host) {SPI.endTransaction();}
void endTransaction(int spi_host) {spi->endTransaction();}
void writeBytes(int spi_host, const uint8_t* data, size_t length) {}
void readBytes(int spi_host, uint8_t* data, size_t length) {SPI.transfer(data, length);} }
void readBytes(int spi_host, uint8_t* data, size_t length) {spi->transfer(data, length);} }

//----------------------------------------------------------------------------

Expand Down

0 comments on commit dc46e05

Please sign in to comment.