From 2690c274af72cfbac88a8c83fce311665a917a93 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sun, 17 Oct 2021 21:33:01 +0000 Subject: [PATCH] Workaround for SPI bus being asleep. This needs to get cherrypicked to another PR as SPI Sleep needs to use a semaphore or something --- src/drivers/SpiMaster.cpp | 16 +++++++++++++--- src/drivers/SpiMaster.h | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 747dbc84..4492e899 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -10,7 +10,7 @@ SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters } bool SpiMaster::Init() { - if(mutex == nullptr) { + if (mutex == nullptr) { mutex = xSemaphoreCreateBinary(); ASSERT(mutex != nullptr); } @@ -179,6 +179,10 @@ void SpiMaster::PrepareRx(const volatile uint32_t cmdAddress, bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { if (data == nullptr) return false; + + if (!active) { + Wakeup(); + } auto ok = xSemaphoreTake(mutex, portMAX_DELAY); ASSERT(ok == true); taskToNotify = xTaskGetCurrentTaskHandle(); @@ -215,7 +219,9 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) { bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) { xSemaphoreTake(mutex, portMAX_DELAY); - + if (!active) { + Wakeup(); + } taskToNotify = nullptr; this->pinCsn = pinCsn; @@ -253,12 +259,16 @@ void SpiMaster::Sleep() { nrf_gpio_cfg_default(params.pinSCK); nrf_gpio_cfg_default(params.pinMOSI); nrf_gpio_cfg_default(params.pinMISO); - + active = false; NRF_LOG_INFO("[SPIMASTER] sleep") } void SpiMaster::Wakeup() { + if (active) { + return; + } Init(); + active = true; NRF_LOG_INFO("[SPIMASTER] Wakeup"); } diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 5ea624f2..f5d2b125 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -60,6 +60,8 @@ namespace Pinetime { volatile size_t currentBufferSize = 0; volatile TaskHandle_t taskToNotify; SemaphoreHandle_t mutex = nullptr; + + bool active; }; } }