@@ -261,7 +261,6 @@ void SPIClass::transfer(void* txbuf, void* rxbuf, size_t count)
261261 }
262262}
263263
264-
265264// Pointer to SPIClass object, one per DMA channel.
266265static SPIClass *spiPtr[DMAC_CH_NUM] = { 0 }; // Legit inits list to NULL
267266
@@ -279,8 +278,12 @@ void SPIClass::dmaCallback_read(Adafruit_ZeroDMA *dma)
279278 // read and write dmas are both done
280279 if (spiPtr[channel]->dma_read_done && spiPtr[channel]->dma_write_done )
281280 {
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+ }
284287 }
285288}
286289
@@ -298,15 +301,41 @@ void SPIClass::dmaCallback_write(Adafruit_ZeroDMA *dma)
298301 // read and write dmas are both done
299302 if (spiPtr[channel]->dma_read_done && spiPtr[channel]->dma_write_done )
300303 {
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 ();
303323 }
304324}
305325
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+
306335// dma transfer function for spi
307336// this function does not block, and dma will transfer in the background
308337// 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 ) )
310339{
311340 // save this function to call when the dma is done
312341 userDmaCallback = functionToCallWhenComplete;
0 commit comments