@@ -261,7 +261,6 @@ void SPIClass::transfer(void* txbuf, void* rxbuf, size_t count)
261
261
}
262
262
}
263
263
264
-
265
264
// Pointer to SPIClass object, one per DMA channel.
266
265
static SPIClass *spiPtr[DMAC_CH_NUM] = { 0 }; // Legit inits list to NULL
267
266
@@ -279,8 +278,12 @@ void SPIClass::dmaCallback_read(Adafruit_ZeroDMA *dma)
279
278
// read and write dmas are both done
280
279
if (spiPtr[channel]->dma_read_done && spiPtr[channel]->dma_write_done )
281
280
{
282
- // call the callback function the user specified
283
- spiPtr[channel]->userDmaCallback ();
281
+ // is a user specified callback to call on completion
282
+ if (spiPtr[channel]->userDmaCallback != NULL )
283
+ {
284
+ // call the callback function the user specified
285
+ spiPtr[channel]->userDmaCallback ();
286
+ }
284
287
}
285
288
}
286
289
@@ -298,15 +301,41 @@ void SPIClass::dmaCallback_write(Adafruit_ZeroDMA *dma)
298
301
// read and write dmas are both done
299
302
if (spiPtr[channel]->dma_read_done && spiPtr[channel]->dma_write_done )
300
303
{
301
- // call the callback function the user specified
302
- spiPtr[channel]->userDmaCallback ();
304
+ // is a user specified callback to call on completion
305
+ if (spiPtr[channel]->userDmaCallback != NULL )
306
+ {
307
+ // call the callback function the user specified
308
+ spiPtr[channel]->userDmaCallback ();
309
+ }
310
+ }
311
+ }
312
+
313
+ // dma transfer function for spi with pole for completion
314
+ void SPIClass::transfer (const void * txbuf, void * rxbuf, uint32_t count, bool block)
315
+ {
316
+ // start the dma transfer, but do not specify a user callback function, will pole for completion instead
317
+ transfer (txbuf, rxbuf, count, NULL );
318
+
319
+ // if this function should automatically wait for completion, otherwise user must do manually
320
+ if (block)
321
+ {
322
+ waitForTransfer ();
303
323
}
304
324
}
305
325
326
+ // Waits for a prior in-background DMA transfer to complete.
327
+ void SPIClass::waitForTransfer (void )
328
+ {
329
+ while ( !dma_read_done || !dma_write_done )
330
+ {
331
+ // do nothing, wait for transfer completion
332
+ }
333
+ }
334
+
306
335
// dma transfer function for spi
307
336
// this function does not block, and dma will transfer in the background
308
337
// the callback parameter should be passed in by the user, it is called when the dma is done
309
- void SPIClass::transfer (void * txbuf, void * rxbuf, size_t count, void (*functionToCallWhenComplete)(void ) )
338
+ void SPIClass::transfer (void * txbuf, void * rxbuf, uint32_t count, void (*functionToCallWhenComplete)(void ) )
310
339
{
311
340
// save this function to call when the dma is done
312
341
userDmaCallback = functionToCallWhenComplete;
0 commit comments