Skip to content

Commit 0dcfb2e

Browse files
mark9064JF002
authored andcommitted
Fix erratum 58 workaround
1 parent 06c6935 commit 0dcfb2e

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

src/drivers/SpiMaster.cpp

+38-25
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,45 @@ bool SpiMaster::Init() {
9494
return true;
9595
}
9696

97-
void SpiMaster::SetupWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel) {
98-
// Create an event when SCK toggles.
99-
NRF_GPIOTE->CONFIG[gpiote_channel] = (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) | (spim->PSEL.SCK << GPIOTE_CONFIG_PSEL_Pos) |
100-
(GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos);
101-
102-
// Stop the spim instance when SCK toggles.
103-
NRF_PPI->CH[ppi_channel].EEP = (uint32_t) &NRF_GPIOTE->EVENTS_IN[gpiote_channel];
104-
NRF_PPI->CH[ppi_channel].TEP = (uint32_t) &spim->TASKS_STOP;
105-
NRF_PPI->CHENSET = 1U << ppi_channel;
97+
void SpiMaster::SetupWorkaroundForErratum58() {
98+
nrfx_gpiote_pin_t pin = spiBaseAddress->PSEL.SCK;
99+
nrfx_gpiote_in_config_t gpioteCfg = {.sense = NRF_GPIOTE_POLARITY_TOGGLE,
100+
.pull = NRF_GPIO_PIN_NOPULL,
101+
.is_watcher = false,
102+
.hi_accuracy = true,
103+
.skip_gpio_setup = true};
104+
if (!workaroundActive) {
105+
// Create an event when SCK toggles.
106+
APP_ERROR_CHECK(nrfx_gpiote_in_init(pin, &gpioteCfg, NULL));
107+
nrfx_gpiote_in_event_enable(pin, false);
108+
109+
// Stop the spim instance when SCK toggles.
110+
nrf_ppi_channel_endpoint_setup(workaroundPpi, nrfx_gpiote_in_event_addr_get(pin), spiBaseAddress->TASKS_STOP);
111+
nrf_ppi_channel_enable(workaroundPpi);
112+
}
113+
106114
spiBaseAddress->EVENTS_END = 0;
107115

108116
// Disable IRQ
109-
spim->INTENCLR = (1 << 6);
110-
spim->INTENCLR = (1 << 1);
111-
spim->INTENCLR = (1 << 19);
117+
spiBaseAddress->INTENCLR = (1 << 6);
118+
spiBaseAddress->INTENCLR = (1 << 1);
119+
spiBaseAddress->INTENCLR = (1 << 19);
120+
workaroundActive = true;
112121
}
113122

114-
void SpiMaster::DisableWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel) {
115-
NRF_GPIOTE->CONFIG[gpiote_channel] = 0;
116-
NRF_PPI->CH[ppi_channel].EEP = 0;
117-
NRF_PPI->CH[ppi_channel].TEP = 0;
118-
NRF_PPI->CHENSET = ppi_channel;
123+
void SpiMaster::DisableWorkaroundForErratum58() {
124+
nrfx_gpiote_pin_t pin = spiBaseAddress->PSEL.SCK;
125+
if (workaroundActive) {
126+
nrfx_gpiote_in_uninit(pin);
127+
nrf_ppi_channel_disable(workaroundPpi);
128+
}
119129
spiBaseAddress->EVENTS_END = 0;
120-
spim->INTENSET = (1 << 6);
121-
spim->INTENSET = (1 << 1);
122-
spim->INTENSET = (1 << 19);
130+
131+
// Enable IRQ
132+
spiBaseAddress->INTENSET = (1 << 6);
133+
spiBaseAddress->INTENSET = (1 << 1);
134+
spiBaseAddress->INTENSET = (1 << 19);
135+
workaroundActive = false;
123136
}
124137

125138
void SpiMaster::OnEndEvent() {
@@ -176,9 +189,9 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, const st
176189
this->pinCsn = pinCsn;
177190

178191
if (size == 1) {
179-
SetupWorkaroundForFtpan58(spiBaseAddress, 0, 0);
192+
SetupWorkaroundForErratum58();
180193
} else {
181-
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
194+
DisableWorkaroundForErratum58();
182195
}
183196

184197
if (preTransactionHook != nullptr) {
@@ -201,7 +214,7 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size, const st
201214
nrf_gpio_pin_set(this->pinCsn);
202215
currentBufferAddr = 0;
203216

204-
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
217+
DisableWorkaroundForErratum58();
205218

206219
xSemaphoreGive(mutex);
207220
}
@@ -213,7 +226,7 @@ bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data
213226
xSemaphoreTake(mutex, portMAX_DELAY);
214227

215228
this->pinCsn = pinCsn;
216-
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
229+
DisableWorkaroundForErratum58();
217230
spiBaseAddress->INTENCLR = (1 << 6);
218231
spiBaseAddress->INTENCLR = (1 << 1);
219232
spiBaseAddress->INTENCLR = (1 << 19);
@@ -260,7 +273,7 @@ bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmd
260273
xSemaphoreTake(mutex, portMAX_DELAY);
261274

262275
this->pinCsn = pinCsn;
263-
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
276+
DisableWorkaroundForErratum58();
264277
spiBaseAddress->INTENCLR = (1 << 6);
265278
spiBaseAddress->INTENCLR = (1 << 1);
266279
spiBaseAddress->INTENCLR = (1 << 19);

src/drivers/SpiMaster.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <FreeRTOS.h>
77
#include <semphr.h>
88
#include <task.h>
9+
#include "nrfx_gpiote.h"
10+
#include "nrf_ppi.h"
911

1012
namespace Pinetime {
1113
namespace Drivers {
@@ -44,8 +46,8 @@ namespace Pinetime {
4446
void Wakeup();
4547

4648
private:
47-
void SetupWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel);
48-
void DisableWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel);
49+
void SetupWorkaroundForErratum58();
50+
void DisableWorkaroundForErratum58();
4951
void PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size);
5052
void PrepareRx(const volatile uint32_t bufferAddress, const volatile size_t size);
5153

@@ -58,6 +60,8 @@ namespace Pinetime {
5860
volatile uint32_t currentBufferAddr = 0;
5961
volatile size_t currentBufferSize = 0;
6062
SemaphoreHandle_t mutex = nullptr;
63+
static constexpr nrf_ppi_channel_t workaroundPpi = NRF_PPI_CHANNEL0;
64+
bool workaroundActive = false;
6165
};
6266
}
6367
}

0 commit comments

Comments
 (0)