summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJF <jf@codingfield.com>2020-01-22 19:45:53 +0100
committerJF <jf@codingfield.com>2020-01-22 19:45:53 +0100
commitaa3e5c0c6f6f7bb9df02ae5d333dc6c4a6d2e744 (patch)
tree58bc64ef06dd6f0199aff19321713cff4b43dd0f /src
parentb4bd41cd562f89bcd320ac0985e9b33c766babe1 (diff)
[WIP] Use IRQ in SPI driver to improve performances
Diffstat (limited to 'src')
-rw-r--r--src/DisplayApp/Screens/Clock.cpp8
-rw-r--r--src/DisplayApp/Screens/Message.cpp2
-rw-r--r--src/drivers/SpiMaster.cpp108
-rw-r--r--src/drivers/SpiMaster.h12
-rw-r--r--src/main.cpp14
5 files changed, 90 insertions, 54 deletions
diff --git a/src/DisplayApp/Screens/Clock.cpp b/src/DisplayApp/Screens/Clock.cpp
index 153f4f2e..155cb581 100644
--- a/src/DisplayApp/Screens/Clock.cpp
+++ b/src/DisplayApp/Screens/Clock.cpp
@@ -9,10 +9,10 @@ using namespace Pinetime::Applications::Screens;
void Clock::Refresh(bool fullRefresh) {
if(fullRefresh) {
gfx.FillRectangle(0,0,240,240,0x0000);
- currentChar[0] = 0;
- currentChar[1] = 0;
- currentChar[2] = 0;
- currentChar[3] = 0;
+ currentChar[0] = 1;
+ currentChar[1] = 2;
+ currentChar[2] = 3;
+ currentChar[3] = 4;
auto dummy = currentDateTime.Get();
}
diff --git a/src/DisplayApp/Screens/Message.cpp b/src/DisplayApp/Screens/Message.cpp
index 2ade4349..121e34b9 100644
--- a/src/DisplayApp/Screens/Message.cpp
+++ b/src/DisplayApp/Screens/Message.cpp
@@ -9,6 +9,6 @@ using namespace Pinetime::Applications::Screens;
void Message::Refresh(bool fullRefresh) {
if(fullRefresh) {
gfx.FillRectangle(0,0,240,240,0xffff);
- gfx.DrawString(120, 10, 0x0000, "COUCOU", &smallFont, false);
+ gfx.DrawString(120, 10, 0x5555, "COUCOU", &smallFont, false);
}
}
diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp
index e8130c76..f7e64db6 100644
--- a/src/drivers/SpiMaster.cpp
+++ b/src/drivers/SpiMaster.cpp
@@ -4,8 +4,10 @@
#include <algorithm>
using namespace Pinetime::Drivers;
+SpiMaster* spiInstance;
SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters &params) :
spi{spi}, params{params} {
+ spiInstance = this;
}
bool SpiMaster::Init() {
@@ -57,50 +59,18 @@ bool SpiMaster::Init() {
NRF_SPIM0->EVENTS_ENDRX = 0;
NRF_SPIM0->EVENTS_ENDTX = 0;
NRF_SPIM0->EVENTS_END = 0;
- NRF_SPI0->EVENTS_READY = 0;
- NRF_SPI0->ENABLE = (SPI_ENABLE_ENABLE_Enabled << SPI_ENABLE_ENABLE_Pos);
- NRF_SPIM0->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos);
-
- 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_SPIM0->INTENSET = ((unsigned)1 << (unsigned)6);
+ NRF_SPIM0->INTENSET = ((unsigned)1 << (unsigned)1);
- 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);
+ 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;
}
+
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.
NRF_GPIOTE->CONFIG[gpiote_channel] = (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) |
@@ -113,41 +83,85 @@ void SpiMaster::setup_workaround_for_ftpan_58(NRF_SPIM_Type *spim, uint32_t ppi_
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) {
if(data == nullptr) return false;
+ while(busy) {
+ asm("nop");
+ }
+
if(size == 1) {
setup_workaround_for_ftpan_58(NRF_SPIM0, 0,0);
+ NRF_SPIM0->INTENCLR = (1<<6);
+ NRF_SPIM0->INTENCLR = (1<<1);
+
} else {
NRF_GPIOTE->CONFIG[0] = 0;
NRF_PPI->CH[0].EEP = 0;
NRF_PPI->CH[0].TEP = 0;
NRF_PPI->CHENSET = 0;
+ NRF_SPIM0->INTENSET = (1<<6);
+ NRF_SPIM0->INTENSET = (1<<1);
}
nrf_gpio_pin_clear(pinCsn);
- auto spim = reinterpret_cast<NRF_SPIM_Type *>(spiBaseAddress);
- while(size > 0) {
- auto currentSize = std::min((size_t)255, size);
- size -= currentSize;
+ bufferAddr = (uint32_t)data;
+ bufferSize = size;
+ busy = true;
+
+// while(size > 0) {
+ auto currentSize = std::min((size_t)255, bufferSize);
NRF_SPIM0->TXD.PTR = (uint32_t) data;
NRF_SPIM0->TXD.MAXCNT = currentSize;
NRF_SPIM0->TXD.LIST = 0;
+ bufferSize -= currentSize;
+ bufferAddr += currentSize;
+
NRF_SPIM0->RXD.PTR = (uint32_t) 0;
NRF_SPIM0->RXD.MAXCNT = 0;
NRF_SPIM0->RXD.LIST = 0;
- NRF_SPIM0->EVENTS_END = 0;
+ if(size != 1) {
+ NRF_SPIM0->EVENTS_END = 0;
+ }
NRF_SPIM0->TASKS_START = 1;
+ if(size == 1) {
+ while (NRF_SPIM0->EVENTS_END == 0);
+ busy = false;
+ nrf_gpio_pin_set(pinCsn);
+ }
- while (NRF_SPIM0->EVENTS_END == 0);
- }
-
- nrf_gpio_pin_set(pinCsn);
return true;
}
diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h
index 5376ac8e..dff63e94 100644
--- a/src/drivers/SpiMaster.h
+++ b/src/drivers/SpiMaster.h
@@ -2,7 +2,7 @@
#include <cstdint>
#include <cstddef>
#include <array>
-
+#include <atomic>
namespace Pinetime {
namespace Drivers {
class SpiMaster {
@@ -24,7 +24,7 @@ namespace Pinetime {
SpiMaster(const SpiModule spi, const Parameters& params);
bool Init();
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 Sleep();
@@ -33,12 +33,20 @@ namespace Pinetime {
bool GetStatusEnd();
bool GetStatusStarted();
+ void irq();
+
private:
NRF_SPIM_Type * spiBaseAddress;
uint8_t pinCsn;
SpiMaster::SpiModule spi;
SpiMaster::Parameters params;
+
+ std::atomic<bool> busy {false};
+
+ uint32_t bufferAddr = 0;
+
+ size_t bufferSize = 0;
};
}
}
diff --git a/src/main.cpp b/src/main.cpp
index 062a36e4..14be5ca2 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -158,6 +158,20 @@ void OnNewTime(current_time_char_t* currentTime) {
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) {
displayApp.reset(new Pinetime::Applications::DisplayApp(batteryController, bleController, dateTimeController));
logger.Init();