From 079e676baf70a943d31317afde47b759ca69ca2d Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Sun, 11 Feb 2024 00:44:06 +0000 Subject: [PATCH] SPI transaction hooks --- src/drivers/Spi.cpp | 4 ++-- src/drivers/Spi.h | 2 +- src/drivers/SpiMaster.cpp | 16 ++++++++++++-- src/drivers/SpiMaster.h | 3 ++- src/drivers/SpiNorFlash.cpp | 2 +- src/drivers/St7789.cpp | 42 +++++++++++++++++++++++-------------- src/drivers/St7789.h | 8 +++---- src/main.cpp | 2 +- src/recoveryLoader.cpp | 2 +- 9 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/drivers/Spi.cpp b/src/drivers/Spi.cpp index c85b90c1..e0b716fa 100644 --- a/src/drivers/Spi.cpp +++ b/src/drivers/Spi.cpp @@ -9,8 +9,8 @@ Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster {spiMaster}, pinCsn { nrf_gpio_pin_set(pinCsn); } -bool Spi::Write(const uint8_t* data, size_t size) { - return spiMaster.Write(pinCsn, data, size); +bool Spi::Write(const uint8_t* data, size_t size, void (*TransactionHook)(bool)) { + return spiMaster.Write(pinCsn, data, size, TransactionHook); } bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 9b6a30f4..55eef05c 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -14,7 +14,7 @@ namespace Pinetime { Spi& operator=(Spi&&) = delete; bool Init(); - bool Write(const uint8_t* data, size_t size); + bool Write(const uint8_t* data, size_t size, void (*TransactionHook)(bool)); bool Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize); bool WriteCmdAndBuffer(const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize); void Sleep(); diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 3446d639..4c2bc940 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -143,6 +143,9 @@ void SpiMaster::OnEndEvent() { } nrf_gpio_pin_set(this->pinCsn); + if (this->TransactionHook != nullptr) { + this->TransactionHook(false); + } currentBufferAddr = 0; BaseType_t xHigherPriorityTaskWoken2 = pdFALSE; xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken2); @@ -173,13 +176,14 @@ void SpiMaster::PrepareRx(const uint32_t bufferAddress, const size_t size) { spiBaseAddress->EVENTS_END = 0; } -bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { +bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, void (*TransactionHook)(bool)) { if (data == nullptr) return false; auto ok = xSemaphoreTake(mutex, portMAX_DELAY); ASSERT(ok == true); taskToNotify = xTaskGetCurrentTaskHandle(); + this->TransactionHook = TransactionHook; this->pinCsn = pinCsn; if (size == 1) { @@ -188,6 +192,9 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); } + if (this->TransactionHook != nullptr) { + this->TransactionHook(true); + } nrf_gpio_pin_clear(this->pinCsn); currentBufferAddr = (uint32_t) data; @@ -203,6 +210,9 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { while (spiBaseAddress->EVENTS_END == 0) ; nrf_gpio_pin_set(this->pinCsn); + if (this->TransactionHook != nullptr) { + this->TransactionHook(false); + } currentBufferAddr = 0; DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); @@ -217,7 +227,7 @@ bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data xSemaphoreTake(mutex, portMAX_DELAY); taskToNotify = nullptr; - + this->TransactionHook = nullptr; this->pinCsn = pinCsn; DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); spiBaseAddress->INTENCLR = (1 << 6); @@ -267,6 +277,8 @@ bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmd taskToNotify = nullptr; + this->TransactionHook = nullptr; + this->pinCsn = pinCsn; DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); spiBaseAddress->INTENCLR = (1 << 6); diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 8b698c57..9014061e 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -31,7 +31,7 @@ namespace Pinetime { SpiMaster& operator=(SpiMaster&&) = delete; bool Init(); - bool Write(uint8_t pinCsn, const uint8_t* data, size_t size); + bool Write(uint8_t pinCsn, const uint8_t* data, size_t size, void (*TransactionHook)(bool)); bool Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize); bool WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize); @@ -50,6 +50,7 @@ namespace Pinetime { NRF_SPIM_Type* spiBaseAddress; uint8_t pinCsn; + void (*TransactionHook)(bool); SpiMaster::SpiModule spi; SpiMaster::Parameters params; diff --git a/src/drivers/SpiNorFlash.cpp b/src/drivers/SpiNorFlash.cpp index 28f82fe6..56a8aabd 100644 --- a/src/drivers/SpiNorFlash.cpp +++ b/src/drivers/SpiNorFlash.cpp @@ -22,7 +22,7 @@ void SpiNorFlash::Uninit() { void SpiNorFlash::Sleep() { auto cmd = static_cast(Commands::DeepPowerDown); - spi.Write(&cmd, sizeof(uint8_t)); + spi.Write(&cmd, sizeof(uint8_t), nullptr); NRF_LOG_INFO("[SpiNorFlash] Sleep") } diff --git a/src/drivers/St7789.cpp b/src/drivers/St7789.cpp index e583aac8..6824acd8 100644 --- a/src/drivers/St7789.cpp +++ b/src/drivers/St7789.cpp @@ -3,16 +3,17 @@ #include #include #include "drivers/Spi.h" +#include "drivers/PinMap.h" using namespace Pinetime::Drivers; -St7789::St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset) : spi {spi}, pinDataCommand {pinDataCommand}, pinReset {pinReset} { +St7789::St7789(Spi& spi) : spi {spi} { } void St7789::Init() { - nrf_gpio_cfg_output(pinDataCommand); - nrf_gpio_cfg_output(pinReset); - nrf_gpio_pin_set(pinReset); + nrf_gpio_cfg_output(PinMap::LcdDataCommand); + nrf_gpio_cfg_output(PinMap::LcdReset); + nrf_gpio_pin_set(PinMap::LcdReset); HardwareReset(); SoftwareReset(); SleepOut(); @@ -29,18 +30,28 @@ void St7789::Init() { DisplayOn(); } +void St7789::EnableDataMode(bool isStart) { + if (isStart) { + nrf_gpio_pin_set(PinMap::LcdDataCommand); + } +} + +void St7789::EnableCommandMode(bool isStart) { + if (isStart) { + nrf_gpio_pin_clear(PinMap::LcdDataCommand); + } +} + void St7789::WriteCommand(uint8_t cmd) { - nrf_gpio_pin_clear(pinDataCommand); - WriteSpi(&cmd, 1); + WriteSpi(&cmd, 1, EnableCommandMode); } void St7789::WriteData(uint8_t data) { - nrf_gpio_pin_set(pinDataCommand); - WriteSpi(&data, 1); + WriteSpi(&data, 1, EnableDataMode); } -void St7789::WriteSpi(const uint8_t* data, size_t size) { - spi.Write(data, size); +void St7789::WriteSpi(const uint8_t* data, size_t size, void (*TransactionHook)(bool)) { + spi.Write(data, size, TransactionHook); } void St7789::SoftwareReset() { @@ -152,24 +163,23 @@ void St7789::Uninit() { void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* data, size_t size) { SetAddrWindow(x, y, x + width - 1, y + height - 1); - nrf_gpio_pin_set(pinDataCommand); - WriteSpi(data, size); + WriteSpi(data, size, EnableDataMode); } void St7789::HardwareReset() { - nrf_gpio_pin_clear(pinReset); + nrf_gpio_pin_clear(PinMap::LcdReset); nrf_delay_ms(10); - nrf_gpio_pin_set(pinReset); + nrf_gpio_pin_set(PinMap::LcdReset); } void St7789::Sleep() { SleepIn(); - nrf_gpio_cfg_default(pinDataCommand); + nrf_gpio_cfg_default(PinMap::LcdDataCommand); NRF_LOG_INFO("[LCD] Sleep"); } void St7789::Wakeup() { - nrf_gpio_cfg_output(pinDataCommand); + nrf_gpio_cfg_output(PinMap::LcdDataCommand); SleepOut(); VerticalScrollStartAddress(verticalScrollingStartAddress); DisplayOn(); diff --git a/src/drivers/St7789.h b/src/drivers/St7789.h index b00bee03..185c44a0 100644 --- a/src/drivers/St7789.h +++ b/src/drivers/St7789.h @@ -8,7 +8,7 @@ namespace Pinetime { class St7789 { public: - explicit St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset); + explicit St7789(Spi& spi); St7789(const St7789&) = delete; St7789& operator=(const St7789&) = delete; St7789(St7789&&) = delete; @@ -26,8 +26,6 @@ namespace Pinetime { private: Spi& spi; - uint8_t pinDataCommand; - uint8_t pinReset; uint8_t verticalScrollingStartAddress = 0; void HardwareReset(); @@ -45,7 +43,9 @@ namespace Pinetime { void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void SetVdv(); void WriteCommand(uint8_t cmd); - void WriteSpi(const uint8_t* data, size_t size); + void WriteSpi(const uint8_t* data, size_t size, void (*TransactionHook)(bool)); + static void EnableDataMode(bool isStart); + static void EnableCommandMode(bool isStart); enum class Commands : uint8_t { SoftwareReset = 0x01, diff --git a/src/main.cpp b/src/main.cpp index ee6a6d3d..723c2e63 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,7 @@ Pinetime::Drivers::SpiMaster spi {Pinetime::Drivers::SpiMaster::SpiModule::SPI0, Pinetime::PinMap::SpiMiso}}; Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; -Pinetime::Drivers::St7789 lcd {lcdSpi, Pinetime::PinMap::LcdDataCommand, Pinetime::PinMap::LcdReset}; +Pinetime::Drivers::St7789 lcd {lcdSpi}; Pinetime::Drivers::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; diff --git a/src/recoveryLoader.cpp b/src/recoveryLoader.cpp index a0b4d784..56cb965f 100644 --- a/src/recoveryLoader.cpp +++ b/src/recoveryLoader.cpp @@ -45,7 +45,7 @@ Pinetime::Drivers::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; -Pinetime::Drivers::St7789 lcd {lcdSpi, Pinetime::PinMap::LcdDataCommand, Pinetime::PinMap::LcdReset}; +Pinetime::Drivers::St7789 lcd {lcdSpi}; Pinetime::Controllers::BrightnessController brightnessController;