DMA completion callback

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
MattMatic
Posts: 13
Joined: Wed Apr 22, 2015 1:44 pm

DMA completion callback

Post by MattMatic » Wed Apr 22, 2015 1:52 pm

Finally took the plunge for the MicroPython... have a neat project in mind for it ;-)

Overall the pyboard has been a pleasant experience so far :D

I've encountered a couple of niggles regarding DMA. Firstly, the WS2812 library would be nicer if the SPI could be kicked off and forgotten about. Of course, there would need to be either a callback or a status request on the DMA transfer progress. (Have a minor mod to the WS2812 library too... must get into GitHub sometime)

Secondly, the WAV file handling wasn't obvious that it was RAM based only. Sigh :(
So I knocked up a double-buffered approach where Timer4 switches the DAC DMA transfer over on a timer. At the moment I have 200ms intervals, with 200ms worth of samples being buffered in each of the two chunk areas. Works ok and happily plays a 4 minute Neil Cowley Trio track ("His Nibs") at 32kHz :D

But I would like to be able to adjust the DMA frequency while playing back, and unfortunately this isn't that easy to tie a timer callback with a DAC/DMA transfer that could have a variable time. It would MUCH nicer if I could get a callback for DMA end, then I can clock out at any frequency and not worry (and not tie up a timer too!!)

(As an aside, I found it a little tricky linking the timer callback with my "LongWave" object instance. Gave up and used a global just to get going. But that's just my Python inexperience)

I saw that the ExtInt module supports 16-22 integer sources, but there's no documentation about what they relate to, or whether there's any way of getting DMA completion.

Any info??

(Apologies if I've not spotted it - new to Python, but with 30 years of C/C++/ASM embedded experience with various chips)

Thank you!
Matt

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: DMA completion callback

Post by pythoncoder » Thu Apr 23, 2015 6:07 am

linking the timer callback with my "LongWave" object instance
Without seeing your code it's hard to comment but, in an object method or constructor, you can assign another method to a callback. The callback function then has access to the specific instance via self (a reference to the instance is passed as a hidden argument). So, taking an example from some code which assigns a callback to a hardware interrupt, the class constructor has

Code: Select all

        self.x_interrupt = pyb.ExtInt(pin_x, pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_NONE, self.x_callback)
and the callback method declaration is:

Code: Select all

    def x_callback(self, line):
	# code
As if by magic the correct instance is accessed by the hardware interrupt ;)
Peter Hinch
Index to my micropython libraries.

MattMatic
Posts: 13
Joined: Wed Apr 22, 2015 1:44 pm

Re: DMA completion callback

Post by MattMatic » Thu Apr 23, 2015 11:07 am

Aha! Fabulous!
I had to read the reply a few times... but now I get it :)

I had declared the callback as just a general (global) function in the "longwave.py" file, rather than as a class method of the LongWave class.
Having made the callback a class method it all fell into place ;)

I'll try and get the LongWave file put on GitHub later. I want to add support to immediately switch to playing another a file - it's primarily for sound effects firing (like a "sound board").

Would still very much like a DMA completion on the DAC's write_timed method. Then I can tie in sample rate adjustment to the accelerometer :P
I presume this requires a µPy update... am trying to get up to speed for compilation of the µPy project before tackling that. Any pointers would be great though!

Matt

Post Reply