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);
}
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) {

View file

@ -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();

View file

@ -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);

View file

@ -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;

View file

@ -22,7 +22,7 @@ void SpiNorFlash::Uninit() {
void SpiNorFlash::Sleep() {
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")
}

View file

@ -3,16 +3,17 @@
#include <libraries/delay/nrf_delay.h>
#include <nrfx_log.h>
#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();

View file

@ -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,

View file

@ -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};

View file

@ -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;