Skip to content

SPI DMA complete callback #225

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
Timvrakas opened this issue Apr 28, 2020 · 7 comments · May be fixed by #299
Open

SPI DMA complete callback #225

Timvrakas opened this issue Apr 28, 2020 · 7 comments · May be fixed by #299
Assignees

Comments

@Timvrakas
Copy link

Timvrakas commented Apr 28, 2020

I'd like to use DMA SPI transfers asynchronously. I think I can add a callback to SPIClass that would be called when the transfer is complete. It would involve a function pointer in SPIClass, a setter, and a line added to SPIClass::dmaCallback() that evaluated the callback for validity, and if valid calls it. Should I pursue this and submit a PR?

Tim

@PaintYourDragon
Copy link

SPI.transfer(txbuf, rxbuf, count, block=false) provides an asynchronous write. Could then use the existing SPI.waitForTransfer() immediately before starting a new transfer or other transaction — this will only block if the current transfer is still in progress. If you need non-blocking, rather than a nested callback (which is probably just going to set a variable that you poll — and make sure it’s volatile), could be easier to add a “getter function” that returns the state of the SPI.dma_busy member (which will likely compile down to a direct access)…or, quick and dirty…could move that member to the “public” section…I know it’s bad form but is anyone really looking?

@Timvrakas
Copy link
Author

My goal was to set a FreeRTOS Semaphore on completion. Polling the volatile flag could work, but its not very elegant. Perhaps I would be better off just forking for my specific use case.

@PaintYourDragon
Copy link

Would that be OK? It’s a pretty esoteric case and the first time it’s come up. I’d look at a PR if it’s something pretty minimal change though.

@BriscoeTech
Copy link

Hello Everyone,

I actually implemented this exact feature for the same reason @Timvrakas mentioned. The current implementation is technically only asynchronous for the transfer, and poling for completion sub optimal, especially if you are going to the trouble of doing a dma to save processor time to begin with. There is alot of performance that can be gained calling a function callback on dma completion as iv seen on my project with an oscilloscope.

I will post this pr soon, and it also fixes #230 as well.

@BriscoeTech
Copy link

Ok pull request posted :-)

@PaintYourDragon
Copy link

Sorry, found this issue again during a github sweep. I think the transfer bugs (along with large DMA transfers) were both addressed in the 1.6.2 release a while back. If there’s a PR with just the callback additions, I could merge that.

@BriscoeTech
Copy link

I am looking at the code again and a lot has changed in the last year, im looking at what would need to be changed. I had made some architecture choices to deal with #230 and assumed that dma read only were not possible at that time.

I posted a follow up question for you in #230

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants