SPI transaction hooks

This commit is contained in:
mark9064 2024-02-11 00:44:06 +00:00 committed by JF
parent 6b5235c301
commit 079e676baf
9 changed files with 52 additions and 29 deletions

View File

@ -9,8 +9,8 @@ Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster {spiMaster}, pinCsn {
nrf_gpio_pin_set(pinCsn); nrf_gpio_pin_set(pinCsn);
} }
bool Spi::Write(const uint8_t* data, size_t size) { bool Spi::Write(const uint8_t* data, size_t size, void (*TransactionHook)(bool)) {
return spiMaster.Write(pinCsn, data, size); return spiMaster.Write(pinCsn, data, size, TransactionHook);
} }
bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) {

View File

@ -14,7 +14,7 @@ namespace Pinetime {
Spi& operator=(Spi&&) = delete; Spi& operator=(Spi&&) = delete;
bool Init(); 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 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); bool WriteCmdAndBuffer(const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize);
void Sleep(); void Sleep();

View File

@ -143,6 +143,9 @@ void SpiMaster::OnEndEvent() {
} }
nrf_gpio_pin_set(this->pinCsn); nrf_gpio_pin_set(this->pinCsn);
if (this->TransactionHook != nullptr) {
this->TransactionHook(false);
}
currentBufferAddr = 0; currentBufferAddr = 0;
BaseType_t xHigherPriorityTaskWoken2 = pdFALSE; BaseType_t xHigherPriorityTaskWoken2 = pdFALSE;
xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken2); xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken2);
@ -173,13 +176,14 @@ void SpiMaster::PrepareRx(const uint32_t bufferAddress, const size_t size) {
spiBaseAddress->EVENTS_END = 0; 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) if (data == nullptr)
return false; return false;
auto ok = xSemaphoreTake(mutex, portMAX_DELAY); auto ok = xSemaphoreTake(mutex, portMAX_DELAY);
ASSERT(ok == true); ASSERT(ok == true);
taskToNotify = xTaskGetCurrentTaskHandle(); taskToNotify = xTaskGetCurrentTaskHandle();
this->TransactionHook = TransactionHook;
this->pinCsn = pinCsn; this->pinCsn = pinCsn;
if (size == 1) { if (size == 1) {
@ -188,6 +192,9 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) {
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
} }
if (this->TransactionHook != nullptr) {
this->TransactionHook(true);
}
nrf_gpio_pin_clear(this->pinCsn); nrf_gpio_pin_clear(this->pinCsn);
currentBufferAddr = (uint32_t) data; 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) while (spiBaseAddress->EVENTS_END == 0)
; ;
nrf_gpio_pin_set(this->pinCsn); nrf_gpio_pin_set(this->pinCsn);
if (this->TransactionHook != nullptr) {
this->TransactionHook(false);
}
currentBufferAddr = 0; currentBufferAddr = 0;
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 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); xSemaphoreTake(mutex, portMAX_DELAY);
taskToNotify = nullptr; taskToNotify = nullptr;
this->TransactionHook = nullptr;
this->pinCsn = pinCsn; this->pinCsn = pinCsn;
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
spiBaseAddress->INTENCLR = (1 << 6); spiBaseAddress->INTENCLR = (1 << 6);
@ -267,6 +277,8 @@ bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmd
taskToNotify = nullptr; taskToNotify = nullptr;
this->TransactionHook = nullptr;
this->pinCsn = pinCsn; this->pinCsn = pinCsn;
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0); DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
spiBaseAddress->INTENCLR = (1 << 6); spiBaseAddress->INTENCLR = (1 << 6);

View File

@ -31,7 +31,7 @@ namespace Pinetime {
SpiMaster& operator=(SpiMaster&&) = delete; SpiMaster& operator=(SpiMaster&&) = delete;
bool Init(); 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 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); 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; NRF_SPIM_Type* spiBaseAddress;
uint8_t pinCsn; uint8_t pinCsn;
void (*TransactionHook)(bool);
SpiMaster::SpiModule spi; SpiMaster::SpiModule spi;
SpiMaster::Parameters params; SpiMaster::Parameters params;

View File

@ -22,7 +22,7 @@ void SpiNorFlash::Uninit() {
void SpiNorFlash::Sleep() { void SpiNorFlash::Sleep() {
auto cmd = static_cast<uint8_t>(Commands::DeepPowerDown); auto cmd = static_cast<uint8_t>(Commands::DeepPowerDown);
spi.Write(&cmd, sizeof(uint8_t)); spi.Write(&cmd, sizeof(uint8_t), nullptr);
NRF_LOG_INFO("[SpiNorFlash] Sleep") NRF_LOG_INFO("[SpiNorFlash] Sleep")
} }

View File

@ -3,16 +3,17 @@
#include <libraries/delay/nrf_delay.h> #include <libraries/delay/nrf_delay.h>
#include <nrfx_log.h> #include <nrfx_log.h>
#include "drivers/Spi.h" #include "drivers/Spi.h"
#include "drivers/PinMap.h"
using namespace Pinetime::Drivers; 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() { void St7789::Init() {
nrf_gpio_cfg_output(pinDataCommand); nrf_gpio_cfg_output(PinMap::LcdDataCommand);
nrf_gpio_cfg_output(pinReset); nrf_gpio_cfg_output(PinMap::LcdReset);
nrf_gpio_pin_set(pinReset); nrf_gpio_pin_set(PinMap::LcdReset);
HardwareReset(); HardwareReset();
SoftwareReset(); SoftwareReset();
SleepOut(); SleepOut();
@ -29,18 +30,28 @@ void St7789::Init() {
DisplayOn(); 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) { void St7789::WriteCommand(uint8_t cmd) {
nrf_gpio_pin_clear(pinDataCommand); WriteSpi(&cmd, 1, EnableCommandMode);
WriteSpi(&cmd, 1);
} }
void St7789::WriteData(uint8_t data) { void St7789::WriteData(uint8_t data) {
nrf_gpio_pin_set(pinDataCommand); WriteSpi(&data, 1, EnableDataMode);
WriteSpi(&data, 1);
} }
void St7789::WriteSpi(const uint8_t* data, size_t size) { void St7789::WriteSpi(const uint8_t* data, size_t size, void (*TransactionHook)(bool)) {
spi.Write(data, size); spi.Write(data, size, TransactionHook);
} }
void St7789::SoftwareReset() { 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) { 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); SetAddrWindow(x, y, x + width - 1, y + height - 1);
nrf_gpio_pin_set(pinDataCommand); WriteSpi(data, size, EnableDataMode);
WriteSpi(data, size);
} }
void St7789::HardwareReset() { void St7789::HardwareReset() {
nrf_gpio_pin_clear(pinReset); nrf_gpio_pin_clear(PinMap::LcdReset);
nrf_delay_ms(10); nrf_delay_ms(10);
nrf_gpio_pin_set(pinReset); nrf_gpio_pin_set(PinMap::LcdReset);
} }
void St7789::Sleep() { void St7789::Sleep() {
SleepIn(); SleepIn();
nrf_gpio_cfg_default(pinDataCommand); nrf_gpio_cfg_default(PinMap::LcdDataCommand);
NRF_LOG_INFO("[LCD] Sleep"); NRF_LOG_INFO("[LCD] Sleep");
} }
void St7789::Wakeup() { void St7789::Wakeup() {
nrf_gpio_cfg_output(pinDataCommand); nrf_gpio_cfg_output(PinMap::LcdDataCommand);
SleepOut(); SleepOut();
VerticalScrollStartAddress(verticalScrollingStartAddress); VerticalScrollStartAddress(verticalScrollingStartAddress);
DisplayOn(); DisplayOn();

View File

@ -8,7 +8,7 @@ namespace Pinetime {
class St7789 { class St7789 {
public: public:
explicit St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset); explicit St7789(Spi& spi);
St7789(const St7789&) = delete; St7789(const St7789&) = delete;
St7789& operator=(const St7789&) = delete; St7789& operator=(const St7789&) = delete;
St7789(St7789&&) = delete; St7789(St7789&&) = delete;
@ -26,8 +26,6 @@ namespace Pinetime {
private: private:
Spi& spi; Spi& spi;
uint8_t pinDataCommand;
uint8_t pinReset;
uint8_t verticalScrollingStartAddress = 0; uint8_t verticalScrollingStartAddress = 0;
void HardwareReset(); void HardwareReset();
@ -45,7 +43,9 @@ namespace Pinetime {
void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); void SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
void SetVdv(); void SetVdv();
void WriteCommand(uint8_t cmd); 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 { enum class Commands : uint8_t {
SoftwareReset = 0x01, SoftwareReset = 0x01,

View File

@ -68,7 +68,7 @@ Pinetime::Drivers::SpiMaster spi {Pinetime::Drivers::SpiMaster::SpiModule::SPI0,
Pinetime::PinMap::SpiMiso}}; Pinetime::PinMap::SpiMiso}};
Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; 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::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn};
Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};

View File

@ -45,7 +45,7 @@ Pinetime::Drivers::Spi flashSpi {spi, Pinetime::PinMap::SpiFlashCsn};
Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};
Pinetime::Drivers::Spi lcdSpi {spi, Pinetime::PinMap::SpiLcdCsn}; 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; Pinetime::Controllers::BrightnessController brightnessController;