[WIP] Use IRQ in SPI driver to improve performances

This commit is contained in:
JF 2020-01-22 19:45:53 +01:00
parent b4bd41cd56
commit aa3e5c0c6f
5 changed files with 90 additions and 54 deletions

View File

@ -9,10 +9,10 @@ using namespace Pinetime::Applications::Screens;
void Clock::Refresh(bool fullRefresh) { void Clock::Refresh(bool fullRefresh) {
if(fullRefresh) { if(fullRefresh) {
gfx.FillRectangle(0,0,240,240,0x0000); gfx.FillRectangle(0,0,240,240,0x0000);
currentChar[0] = 0; currentChar[0] = 1;
currentChar[1] = 0; currentChar[1] = 2;
currentChar[2] = 0; currentChar[2] = 3;
currentChar[3] = 0; currentChar[3] = 4;
auto dummy = currentDateTime.Get(); auto dummy = currentDateTime.Get();
} }

View File

@ -9,6 +9,6 @@ using namespace Pinetime::Applications::Screens;
void Message::Refresh(bool fullRefresh) { void Message::Refresh(bool fullRefresh) {
if(fullRefresh) { if(fullRefresh) {
gfx.FillRectangle(0,0,240,240,0xffff); gfx.FillRectangle(0,0,240,240,0xffff);
gfx.DrawString(120, 10, 0x0000, "COUCOU", &smallFont, false); gfx.DrawString(120, 10, 0x5555, "COUCOU", &smallFont, false);
} }
} }

View File

@ -4,8 +4,10 @@
#include <algorithm> #include <algorithm>
using namespace Pinetime::Drivers; using namespace Pinetime::Drivers;
SpiMaster* spiInstance;
SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters &params) : SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters &params) :
spi{spi}, params{params} { spi{spi}, params{params} {
spiInstance = this;
} }
bool SpiMaster::Init() { bool SpiMaster::Init() {
@ -57,49 +59,17 @@ bool SpiMaster::Init() {
NRF_SPIM0->EVENTS_ENDRX = 0; NRF_SPIM0->EVENTS_ENDRX = 0;
NRF_SPIM0->EVENTS_ENDTX = 0; NRF_SPIM0->EVENTS_ENDTX = 0;
NRF_SPIM0->EVENTS_END = 0; NRF_SPIM0->EVENTS_END = 0;
NRF_SPI0->EVENTS_READY = 0;
NRF_SPI0->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos); NRF_SPIM0->INTENSET = ((unsigned)1 << (unsigned)6);
NRF_SPIM0->INTENSET = ((unsigned)1 << (unsigned)1);
NRF_SPIM0->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos); NRF_SPIM0->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos);
NRFX_IRQ_PRIORITY_SET(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn,6);
NRFX_IRQ_ENABLE(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
return true; return true;
} }
bool SpiMaster::WriteFast(const uint8_t *data, size_t size) {
auto spi = reinterpret_cast<NRF_SPI_Type*>(spiBaseAddress);
volatile uint32_t dummyread;
if(data == nullptr) return false;
/* enable slave (slave select active low) */
nrf_gpio_pin_clear(pinCsn);
NRF_SPI0->EVENTS_READY = 0;
NRF_SPI0->TXD = (uint32_t)*data++;
while(--size)
{
NRF_SPI0->TXD = (uint32_t)*data++;
/* Wait for the transaction complete or timeout (about 10ms - 20 ms) */
while (NRF_SPI0->EVENTS_READY == 0);
/* clear the event to be ready to receive next messages */
NRF_SPI0->EVENTS_READY = 0;
dummyread = NRF_SPI0->RXD;
}
/* Wait for the transaction complete or timeout (about 10ms - 20 ms) */
while (NRF_SPI0->EVENTS_READY == 0);
dummyread = NRF_SPI0->RXD;
/* disable slave (slave select active low) */
nrf_gpio_pin_set(pinCsn);
return true;
}
void SpiMaster::setup_workaround_for_ftpan_58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel) { void SpiMaster::setup_workaround_for_ftpan_58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel) {
// Create an event when SCK toggles. // Create an event when SCK toggles.
@ -113,41 +83,85 @@ void SpiMaster::setup_workaround_for_ftpan_58(NRF_SPIM_Type *spim, uint32_t ppi_
NRF_PPI->CHENSET = 1U << ppi_channel; NRF_PPI->CHENSET = 1U << ppi_channel;
} }
void SpiMaster::irq() {
if(busy) {
if(bufferSize > 0) {
auto currentSize = std::min((size_t)255, bufferSize);
NRF_SPIM0->TXD.PTR = (uint32_t) bufferAddr;
NRF_SPIM0->TXD.MAXCNT = currentSize;
NRF_SPIM0->TXD.LIST = 0;
bufferAddr += currentSize;
bufferSize -= currentSize;
NRF_SPIM0->RXD.PTR = (uint32_t) 0;
NRF_SPIM0->RXD.MAXCNT = 0;
NRF_SPIM0->RXD.LIST = 0;
NRF_SPIM0->TASKS_START = 1;
return;
}else {
nrf_gpio_pin_set(pinCsn);
busy = false;
}
}
}
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;
while(busy) {
asm("nop");
}
if(size == 1) { if(size == 1) {
setup_workaround_for_ftpan_58(NRF_SPIM0, 0,0); setup_workaround_for_ftpan_58(NRF_SPIM0, 0,0);
NRF_SPIM0->INTENCLR = (1<<6);
NRF_SPIM0->INTENCLR = (1<<1);
} else { } else {
NRF_GPIOTE->CONFIG[0] = 0; NRF_GPIOTE->CONFIG[0] = 0;
NRF_PPI->CH[0].EEP = 0; NRF_PPI->CH[0].EEP = 0;
NRF_PPI->CH[0].TEP = 0; NRF_PPI->CH[0].TEP = 0;
NRF_PPI->CHENSET = 0; NRF_PPI->CHENSET = 0;
NRF_SPIM0->INTENSET = (1<<6);
NRF_SPIM0->INTENSET = (1<<1);
} }
nrf_gpio_pin_clear(pinCsn); nrf_gpio_pin_clear(pinCsn);
auto spim = reinterpret_cast<NRF_SPIM_Type *>(spiBaseAddress);
while(size > 0) { bufferAddr = (uint32_t)data;
auto currentSize = std::min((size_t)255, size); bufferSize = size;
size -= currentSize; busy = true;
// while(size > 0) {
auto currentSize = std::min((size_t)255, bufferSize);
NRF_SPIM0->TXD.PTR = (uint32_t) data; NRF_SPIM0->TXD.PTR = (uint32_t) data;
NRF_SPIM0->TXD.MAXCNT = currentSize; NRF_SPIM0->TXD.MAXCNT = currentSize;
NRF_SPIM0->TXD.LIST = 0; NRF_SPIM0->TXD.LIST = 0;
bufferSize -= currentSize;
bufferAddr += currentSize;
NRF_SPIM0->RXD.PTR = (uint32_t) 0; NRF_SPIM0->RXD.PTR = (uint32_t) 0;
NRF_SPIM0->RXD.MAXCNT = 0; NRF_SPIM0->RXD.MAXCNT = 0;
NRF_SPIM0->RXD.LIST = 0; NRF_SPIM0->RXD.LIST = 0;
if(size != 1) {
NRF_SPIM0->EVENTS_END = 0; NRF_SPIM0->EVENTS_END = 0;
}
NRF_SPIM0->TASKS_START = 1; NRF_SPIM0->TASKS_START = 1;
if(size == 1) {
while (NRF_SPIM0->EVENTS_END == 0); while (NRF_SPIM0->EVENTS_END == 0);
busy = false;
nrf_gpio_pin_set(pinCsn);
} }
nrf_gpio_pin_set(pinCsn);
return true; return true;
} }

View File

@ -2,7 +2,7 @@
#include <cstdint> #include <cstdint>
#include <cstddef> #include <cstddef>
#include <array> #include <array>
#include <atomic>
namespace Pinetime { namespace Pinetime {
namespace Drivers { namespace Drivers {
class SpiMaster { class SpiMaster {
@ -24,7 +24,7 @@ namespace Pinetime {
SpiMaster(const SpiModule spi, const Parameters& params); SpiMaster(const SpiModule spi, const Parameters& params);
bool Init(); bool Init();
bool Write(const uint8_t* data, size_t size); bool Write(const uint8_t* data, size_t size);
bool WriteFast(const uint8_t* data, size_t size); bool WriteRepeat(const uint8_t *data, size_t size, int repeat);
void setup_workaround_for_ftpan_58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel); void setup_workaround_for_ftpan_58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel);
void Sleep(); void Sleep();
@ -33,12 +33,20 @@ namespace Pinetime {
bool GetStatusEnd(); bool GetStatusEnd();
bool GetStatusStarted(); bool GetStatusStarted();
void irq();
private: private:
NRF_SPIM_Type * spiBaseAddress; NRF_SPIM_Type * spiBaseAddress;
uint8_t pinCsn; uint8_t pinCsn;
SpiMaster::SpiModule spi; SpiMaster::SpiModule spi;
SpiMaster::Parameters params; SpiMaster::Parameters params;
std::atomic<bool> busy {false};
uint32_t bufferAddr = 0;
size_t bufferSize = 0;
}; };
} }
} }

View File

@ -158,6 +158,20 @@ void OnNewTime(current_time_char_t* currentTime) {
dayOfWeek, hour, minute, second, nrf_rtc_counter_get(portNRF_RTC_REG)); dayOfWeek, hour, minute, second, nrf_rtc_counter_get(portNRF_RTC_REG));
} }
extern Pinetime::Drivers::SpiMaster* spiInstance;
void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) {
if(((NRF_SPIM0->INTENSET & (1<<6)) != 0) && NRF_SPIM0->EVENTS_END == 1) {
NRF_SPIM0->EVENTS_END = 0;
spiInstance->irq();
}
if(((NRF_SPIM0->INTENSET & (1<<1)) != 0) && NRF_SPIM0->EVENTS_STOPPED == 1) {
NRF_SPIM0->EVENTS_STOPPED = 0;
}
return;
}
int main(void) { int main(void) {
displayApp.reset(new Pinetime::Applications::DisplayApp(batteryController, bleController, dateTimeController)); displayApp.reset(new Pinetime::Applications::DisplayApp(batteryController, bleController, dateTimeController));
logger.Init(); logger.Init();