Skip to content

Commit

Permalink
Workaround for SPI bus being asleep.
Browse files Browse the repository at this point in the history
This needs to get cherrypicked to another PR as SPI Sleep needs to use a semaphore or something
  • Loading branch information
geekbozu committed Oct 17, 2021
1 parent 4eeb5ab commit 1edeb5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/drivers/SpiMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 2 additions & 0 deletions src/drivers/SpiMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace Pinetime {
volatile size_t currentBufferSize = 0;
volatile TaskHandle_t taskToNotify;
SemaphoreHandle_t mutex = nullptr;

bool active;
};
}
}

0 comments on commit 1edeb5c

Please sign in to comment.