Improve SPI driver (use a mutex to prevent race conditions).
This commit is contained in:
parent
9753967d8a
commit
79131f4fe3
|
@ -9,7 +9,8 @@ using namespace Pinetime::Drivers;
|
||||||
|
|
||||||
SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters ¶ms) :
|
SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters ¶ms) :
|
||||||
spi{spi}, params{params} {
|
spi{spi}, params{params} {
|
||||||
|
mutex = xSemaphoreCreateBinary();
|
||||||
|
ASSERT(mutex != NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SpiMaster::Init() {
|
bool SpiMaster::Init() {
|
||||||
|
@ -68,6 +69,8 @@ bool SpiMaster::Init() {
|
||||||
|
|
||||||
NRFX_IRQ_PRIORITY_SET(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn,2);
|
NRFX_IRQ_PRIORITY_SET(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn,2);
|
||||||
NRFX_IRQ_ENABLE(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
|
NRFX_IRQ_ENABLE(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
|
||||||
|
|
||||||
|
xSemaphoreGive(mutex);
|
||||||
return true;
|
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].EEP = (uint32_t) &NRF_GPIOTE->EVENTS_IN[gpiote_channel];
|
||||||
NRF_PPI->CH[ppi_channel].TEP = (uint32_t) &spim->TASKS_STOP;
|
NRF_PPI->CH[ppi_channel].TEP = (uint32_t) &spim->TASKS_STOP;
|
||||||
NRF_PPI->CHENSET = 1U << ppi_channel;
|
NRF_PPI->CHENSET = 1U << ppi_channel;
|
||||||
|
spiBaseAddress->EVENTS_END = 0;
|
||||||
|
|
||||||
// Disable IRQ
|
// Disable IRQ
|
||||||
spim->INTENCLR = (1<<6);
|
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].EEP = 0;
|
||||||
NRF_PPI->CH[ppi_channel].TEP = 0;
|
NRF_PPI->CH[ppi_channel].TEP = 0;
|
||||||
NRF_PPI->CHENSET = ppi_channel;
|
NRF_PPI->CHENSET = ppi_channel;
|
||||||
|
spiBaseAddress->EVENTS_END = 0;
|
||||||
spim->INTENSET = (1<<6);
|
spim->INTENSET = (1<<6);
|
||||||
spim->INTENSET = (1<<1);
|
spim->INTENSET = (1<<1);
|
||||||
spim->INTENSET = (1<<19);
|
spim->INTENSET = (1<<19);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpiMaster::OnEndEvent() {
|
void SpiMaster::OnEndEvent() {
|
||||||
if(!busy) return;
|
if(currentBufferAddr == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
auto s = currentBufferSize;
|
auto s = currentBufferSize;
|
||||||
if(s > 0) {
|
if(s > 0) {
|
||||||
|
@ -113,21 +120,21 @@ void SpiMaster::OnEndEvent() {
|
||||||
} else {
|
} else {
|
||||||
uint8_t* buffer = nullptr;
|
uint8_t* buffer = nullptr;
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
busy = false;
|
if(taskToNotify != nullptr) {
|
||||||
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
|
vTaskNotifyGiveFromISR(taskToNotify, &xHigherPriorityTaskWoken);
|
||||||
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||||
|
}
|
||||||
|
|
||||||
|
nrf_gpio_pin_set(this->pinCsn);
|
||||||
if(taskToNotify != nullptr) {
|
currentBufferAddr = 0;
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
vTaskNotifyGiveFromISR(taskToNotify, &xHigherPriorityTaskWoken);
|
xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken);
|
||||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||||
}
|
|
||||||
|
|
||||||
nrf_gpio_pin_set(pinCsn);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpiMaster::OnStartedEvent() {
|
void SpiMaster::OnStartedEvent() {
|
||||||
if(!busy) return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpiMaster::PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size) {
|
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) {
|
bool SpiMaster::Write(const uint8_t *data, size_t size) {
|
||||||
if(data == nullptr) return false;
|
if(data == nullptr) return false;
|
||||||
|
auto ok = xSemaphoreTake(mutex, portMAX_DELAY);
|
||||||
|
ASSERT(ok == true);
|
||||||
taskToNotify = xTaskGetCurrentTaskHandle();
|
taskToNotify = xTaskGetCurrentTaskHandle();
|
||||||
while(busy) {
|
|
||||||
asm("nop");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(size == 1) {
|
if(size == 1) {
|
||||||
SetupWorkaroundForFtpan58(spiBaseAddress, 0,0);
|
SetupWorkaroundForFtpan58(spiBaseAddress, 0,0);
|
||||||
|
@ -157,7 +163,6 @@ bool SpiMaster::Write(const uint8_t *data, size_t size) {
|
||||||
|
|
||||||
currentBufferAddr = (uint32_t)data;
|
currentBufferAddr = (uint32_t)data;
|
||||||
currentBufferSize = size;
|
currentBufferSize = size;
|
||||||
busy = true;
|
|
||||||
|
|
||||||
auto currentSize = std::min((size_t)255, (size_t)currentBufferSize);
|
auto currentSize = std::min((size_t)255, (size_t)currentBufferSize);
|
||||||
PrepareTx(currentBufferAddr, currentSize);
|
PrepareTx(currentBufferAddr, currentSize);
|
||||||
|
@ -167,7 +172,7 @@ bool SpiMaster::Write(const uint8_t *data, size_t size) {
|
||||||
|
|
||||||
if(size == 1) {
|
if(size == 1) {
|
||||||
while (spiBaseAddress->EVENTS_END == 0);
|
while (spiBaseAddress->EVENTS_END == 0);
|
||||||
busy = false;
|
xSemaphoreGive(mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
#include <task.h>
|
#include <task.h>
|
||||||
|
|
||||||
#include "BufferProvider.h"
|
#include "BufferProvider.h"
|
||||||
|
#include <semphr.h>
|
||||||
|
|
||||||
namespace Pinetime {
|
namespace Pinetime {
|
||||||
namespace Drivers {
|
namespace Drivers {
|
||||||
class SpiMaster {
|
class SpiMaster {
|
||||||
|
@ -51,10 +53,10 @@ namespace Pinetime {
|
||||||
SpiMaster::SpiModule spi;
|
SpiMaster::SpiModule spi;
|
||||||
SpiMaster::Parameters params;
|
SpiMaster::Parameters params;
|
||||||
|
|
||||||
volatile bool busy = false;
|
|
||||||
volatile uint32_t currentBufferAddr = 0;
|
volatile uint32_t currentBufferAddr = 0;
|
||||||
volatile size_t currentBufferSize = 0;
|
volatile size_t currentBufferSize = 0;
|
||||||
volatile TaskHandle_t taskToNotify;
|
volatile TaskHandle_t taskToNotify;
|
||||||
|
SemaphoreHandle_t mutex;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue