diff options
author | JF <jf@codingfield.com> | 2020-05-17 10:29:13 +0200 |
---|---|---|
committer | Gitea <gitea@fake.local> | 2020-05-17 10:29:13 +0200 |
commit | 8a94750e30399bfb204cbec59a769d9d1b6b5baa (patch) | |
tree | 8a1a58beae54e238d28aff116c900f3b428b7db4 /src/drivers | |
parent | 86d5732b960fbe7f81ed711b2de7e6b79293c96a (diff) | |
parent | be1ad9b07083e656a649d223750ff4b14b781b7b (diff) |
Merge branch 'develop' of JF/PineTime into master
Diffstat (limited to 'src/drivers')
-rw-r--r-- | src/drivers/SpiMaster.cpp | 37 | ||||
-rw-r--r-- | src/drivers/SpiMaster.h | 4 | ||||
-rw-r--r-- | src/drivers/Watchdog.cpp | 20 |
3 files changed, 34 insertions, 27 deletions
diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 71986054..4b3dd677 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -9,7 +9,8 @@ using namespace Pinetime::Drivers; SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters ¶ms) : spi{spi}, params{params} { - + mutex = xSemaphoreCreateBinary(); + ASSERT(mutex != NULL); } bool SpiMaster::Init() { @@ -68,6 +69,8 @@ bool SpiMaster::Init() { NRFX_IRQ_PRIORITY_SET(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn,2); NRFX_IRQ_ENABLE(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn); + + xSemaphoreGive(mutex); return true; } @@ -82,6 +85,7 @@ void SpiMaster::SetupWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_chan NRF_PPI->CH[ppi_channel].EEP = (uint32_t) &NRF_GPIOTE->EVENTS_IN[gpiote_channel]; NRF_PPI->CH[ppi_channel].TEP = (uint32_t) &spim->TASKS_STOP; NRF_PPI->CHENSET = 1U << ppi_channel; + spiBaseAddress->EVENTS_END = 0; // Disable IRQ spim->INTENCLR = (1<<6); @@ -94,13 +98,16 @@ void SpiMaster::DisableWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_ch NRF_PPI->CH[ppi_channel].EEP = 0; NRF_PPI->CH[ppi_channel].TEP = 0; NRF_PPI->CHENSET = ppi_channel; + spiBaseAddress->EVENTS_END = 0; spim->INTENSET = (1<<6); spim->INTENSET = (1<<1); spim->INTENSET = (1<<19); } void SpiMaster::OnEndEvent() { - if(!busy) return; + if(currentBufferAddr == 0) { + return; + } auto s = currentBufferSize; if(s > 0) { @@ -113,21 +120,21 @@ void SpiMaster::OnEndEvent() { } else { uint8_t* buffer = nullptr; size_t size = 0; - busy = false; - - - if(taskToNotify != nullptr) { + if(taskToNotify != nullptr) { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + vTaskNotifyGiveFromISR(taskToNotify, &xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + + nrf_gpio_pin_set(this->pinCsn); + currentBufferAddr = 0; BaseType_t xHigherPriorityTaskWoken = pdFALSE; - vTaskNotifyGiveFromISR(taskToNotify, &xHigherPriorityTaskWoken); + xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - } - - nrf_gpio_pin_set(pinCsn); } } void SpiMaster::OnStartedEvent() { - if(!busy) return; } void SpiMaster::PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size) { @@ -142,10 +149,9 @@ void SpiMaster::PrepareTx(const volatile uint32_t bufferAddress, const volatile bool SpiMaster::Write(const uint8_t *data, size_t size) { if(data == nullptr) return false; + auto ok = xSemaphoreTake(mutex, portMAX_DELAY); + ASSERT(ok == true); taskToNotify = xTaskGetCurrentTaskHandle(); - while(busy) { - asm("nop"); - } if(size == 1) { SetupWorkaroundForFtpan58(spiBaseAddress, 0,0); @@ -157,7 +163,6 @@ bool SpiMaster::Write(const uint8_t *data, size_t size) { currentBufferAddr = (uint32_t)data; currentBufferSize = size; - busy = true; auto currentSize = std::min((size_t)255, (size_t)currentBufferSize); PrepareTx(currentBufferAddr, currentSize); @@ -167,7 +172,7 @@ bool SpiMaster::Write(const uint8_t *data, size_t size) { if(size == 1) { while (spiBaseAddress->EVENTS_END == 0); - busy = false; + xSemaphoreGive(mutex); } return true; diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 362f480c..8a633b7f 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -7,6 +7,8 @@ #include <task.h> #include "BufferProvider.h" +#include <semphr.h> + namespace Pinetime { namespace Drivers { class SpiMaster { @@ -51,10 +53,10 @@ namespace Pinetime { SpiMaster::SpiModule spi; SpiMaster::Parameters params; - volatile bool busy = false; volatile uint32_t currentBufferAddr = 0; volatile size_t currentBufferSize = 0; volatile TaskHandle_t taskToNotify; + SemaphoreHandle_t mutex; }; } } diff --git a/src/drivers/Watchdog.cpp b/src/drivers/Watchdog.cpp index 55b6de73..850fd2f1 100644 --- a/src/drivers/Watchdog.cpp +++ b/src/drivers/Watchdog.cpp @@ -33,16 +33,16 @@ void Watchdog::Kick() { Watchdog::ResetReasons Watchdog::ActualResetReason() const { uint32_t resetReason; - sd_power_reset_reason_get(&resetReason); - sd_power_reset_reason_clr(0xFFFFFFFF); - if(resetReason & 0x01u) return ResetReasons::ResetPin; - if((resetReason >> 1u) & 0x01u) return ResetReasons::Watchdog; - if((resetReason >> 2u) & 0x01u) return ResetReasons::SoftReset; - if((resetReason >> 3u) & 0x01u) return ResetReasons::CpuLockup; - if((resetReason >> 16u) & 0x01u) return ResetReasons::SystemOff; - if((resetReason >> 17u) & 0x01u) return ResetReasons::LpComp; - if((resetReason >> 18u) & 0x01u) return ResetReasons::DebugInterface; - if((resetReason >> 19u) & 0x01u) return ResetReasons::NFC; +// sd_power_reset_reason_get(&resetReason); +// sd_power_reset_reason_clr(0xFFFFFFFF); +// if(resetReason & 0x01u) return ResetReasons::ResetPin; +// if((resetReason >> 1u) & 0x01u) return ResetReasons::Watchdog; +// if((resetReason >> 2u) & 0x01u) return ResetReasons::SoftReset; +// if((resetReason >> 3u) & 0x01u) return ResetReasons::CpuLockup; +// if((resetReason >> 16u) & 0x01u) return ResetReasons::SystemOff; +// if((resetReason >> 17u) & 0x01u) return ResetReasons::LpComp; +// if((resetReason >> 18u) & 0x01u) return ResetReasons::DebugInterface; +// if((resetReason >> 19u) & 0x01u) return ResetReasons::NFC; return ResetReasons::HardReset; } |