@@ -94,32 +94,45 @@ bool SpiMaster::Init() {
94
94
return true ;
95
95
}
96
96
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
+
106
114
spiBaseAddress->EVENTS_END = 0 ;
107
115
108
116
// 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 ;
112
121
}
113
122
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
+ }
119
129
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 ;
123
136
}
124
137
125
138
void SpiMaster::OnEndEvent () {
@@ -136,17 +149,11 @@ void SpiMaster::OnEndEvent() {
136
149
137
150
spiBaseAddress->TASKS_START = 1 ;
138
151
} else {
139
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
140
- if (taskToNotify != nullptr ) {
141
- vTaskNotifyGiveFromISR (taskToNotify, &xHigherPriorityTaskWoken);
142
- portYIELD_FROM_ISR (xHigherPriorityTaskWoken);
143
- }
144
-
145
152
nrf_gpio_pin_set (this ->pinCsn );
146
153
currentBufferAddr = 0 ;
147
- BaseType_t xHigherPriorityTaskWoken2 = pdFALSE;
148
- xSemaphoreGiveFromISR (mutex, &xHigherPriorityTaskWoken2 );
149
- portYIELD_FROM_ISR (xHigherPriorityTaskWoken | xHigherPriorityTaskWoken2 );
154
+ BaseType_t xHigherPriorityTaskWoken = pdFALSE;
155
+ xSemaphoreGiveFromISR (mutex, &xHigherPriorityTaskWoken );
156
+ portYIELD_FROM_ISR (xHigherPriorityTaskWoken);
150
157
}
151
158
}
152
159
@@ -173,21 +180,23 @@ void SpiMaster::PrepareRx(const uint32_t bufferAddress, const size_t size) {
173
180
spiBaseAddress->EVENTS_END = 0 ;
174
181
}
175
182
176
- bool SpiMaster::Write (uint8_t pinCsn, const uint8_t * data, size_t size) {
183
+ bool SpiMaster::Write (uint8_t pinCsn, const uint8_t * data, size_t size, const std::function< void ()>& preTransactionHook ) {
177
184
if (data == nullptr )
178
185
return false ;
179
186
auto ok = xSemaphoreTake (mutex, portMAX_DELAY);
180
187
ASSERT (ok == true );
181
- taskToNotify = xTaskGetCurrentTaskHandle ();
182
188
183
189
this ->pinCsn = pinCsn;
184
190
185
191
if (size == 1 ) {
186
- SetupWorkaroundForFtpan58 (spiBaseAddress, 0 , 0 );
192
+ SetupWorkaroundForErratum58 ( );
187
193
} else {
188
- DisableWorkaroundForFtpan58 (spiBaseAddress, 0 , 0 );
194
+ DisableWorkaroundForErratum58 ( );
189
195
}
190
196
197
+ if (preTransactionHook != nullptr ) {
198
+ preTransactionHook ();
199
+ }
191
200
nrf_gpio_pin_clear (this ->pinCsn );
192
201
193
202
currentBufferAddr = (uint32_t ) data;
@@ -205,7 +214,7 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) {
205
214
nrf_gpio_pin_set (this ->pinCsn );
206
215
currentBufferAddr = 0 ;
207
216
208
- DisableWorkaroundForFtpan58 (spiBaseAddress, 0 , 0 );
217
+ DisableWorkaroundForErratum58 ( );
209
218
210
219
xSemaphoreGive (mutex);
211
220
}
@@ -216,10 +225,8 @@ bool SpiMaster::Write(uint8_t pinCsn, const uint8_t* data, size_t size) {
216
225
bool SpiMaster::Read (uint8_t pinCsn, uint8_t * cmd, size_t cmdSize, uint8_t * data, size_t dataSize) {
217
226
xSemaphoreTake (mutex, portMAX_DELAY);
218
227
219
- taskToNotify = nullptr ;
220
-
221
228
this ->pinCsn = pinCsn;
222
- DisableWorkaroundForFtpan58 (spiBaseAddress, 0 , 0 );
229
+ DisableWorkaroundForErratum58 ( );
223
230
spiBaseAddress->INTENCLR = (1 << 6 );
224
231
spiBaseAddress->INTENCLR = (1 << 1 );
225
232
spiBaseAddress->INTENCLR = (1 << 19 );
@@ -265,10 +272,8 @@ void SpiMaster::Wakeup() {
265
272
bool SpiMaster::WriteCmdAndBuffer (uint8_t pinCsn, const uint8_t * cmd, size_t cmdSize, const uint8_t * data, size_t dataSize) {
266
273
xSemaphoreTake (mutex, portMAX_DELAY);
267
274
268
- taskToNotify = nullptr ;
269
-
270
275
this ->pinCsn = pinCsn;
271
- DisableWorkaroundForFtpan58 (spiBaseAddress, 0 , 0 );
276
+ DisableWorkaroundForErratum58 ( );
272
277
spiBaseAddress->INTENCLR = (1 << 6 );
273
278
spiBaseAddress->INTENCLR = (1 << 1 );
274
279
spiBaseAddress->INTENCLR = (1 << 19 );
0 commit comments