AMP skin adc.read() from ISR?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
JudiciaryPag
Posts: 6
Joined: Wed Jul 13, 2016 9:27 pm

AMP skin adc.read() from ISR?

Post by JudiciaryPag » Wed Jul 13, 2016 9:34 pm

I can read the AMP skin microphone using a timed read and I can seem to read values individually using adc.read() from the prompt. But, I need to read at an interrupt-driven rate (6kHz). I've tried everything but seem to get weird (max) values doing adc.read() from an ISR.

Can you do an adc.read() from an ISR?

I've check timer values etc. etc., nothing clashes but all I seem to be able to read from X22 in my ISR are values stuck pretty much as 4096.

JudiciaryPag
Posts: 6
Joined: Wed Jul 13, 2016 9:27 pm

Re: AMP skin adc.read() from ISR?

Post by JudiciaryPag » Sun Apr 09, 2017 5:40 pm

Wow - can'y anybody answer this? Can you do an adc.read() from within an ISR? I'll update to latest drivers but if you can't access the h/w from an ISR, mmmm.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: AMP skin adc.read() from ISR?

Post by Roberthh » Mon Apr 10, 2017 7:50 am

I see no reason why you cannot. You have to tell the ISR where adc is. So once you have the adc to, let's say, you have to declare that symbol global in the ISR to make it visible, or cache it, like:

Code: Select all

from pyb import ADC
from array import array
_BUFFERSIZE = const(128)
adc = ADC(pin)
data = array("H", [0] * _BUFFERSIZE)
index = 0

def adc_isr(x, adc = adc.read, data=data):
    global index
    data[index] = adc()
    index = (index + 1 ) % _BUFFERSIZE
See also a related discussion here: viewtopic.php?f=6&t=3038#p18058
@pythoncoder found, that adc.read() takes 94 µs. That does not leave much headroom, when you want to sample at 6 kHz = 167µs period.

JudiciaryPag
Posts: 6
Joined: Wed Jul 13, 2016 9:27 pm

Re: AMP skin adc.read() from ISR?

Post by JudiciaryPag » Sat Apr 15, 2017 3:08 pm

Thanks. It's working now, even though I'm seeing a significant DC offset using the AMP Audi skin - but that's a separate issue! Thanks.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: AMP skin adc.read() from ISR?

Post by Roberthh » Sat Apr 15, 2017 3:25 pm

You need an DC offset to sample an AC signal, since the built-in ADC just receives positive values. The optimal DC offset in readings would be about half of the ADC range, for a 12 bit ADC that would be 2047.

JudiciaryPag
Posts: 6
Joined: Wed Jul 13, 2016 9:27 pm

Re: AMP skin adc.read() from ISR?

Post by JudiciaryPag » Sat Apr 15, 2017 10:31 pm

Thanks, yes, understand that as they're unsigned 12-but values (0 - 4095). I'm seeing a significant offset with the AMP Audio skin, with a somewhat noisy silence hovering anywhere between 2,400 to 3,000. I can compensate for this in post-processing but it would be nice to be able trim it out.

Post Reply