0

Metro M4 high speed ADC (using DMA)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Metro M4 high speed ADC (using DMA)

by BryonMiller on Wed Jun 13, 2018 2:37 pm

Hi Everyone,

I've been having fun with my Metro M4 Express. One of the things I wanted to play around with getting analog samples at very high rates. I developed the code to enable a DMA channel and connect it to one of the analog inputs (A0..A5). I thought I'd share it in case anyone might find it useful.

See the attached zip with a directory containing adcdma.ino and a utility file called adcdmautil.ino. I'd be curious to hear back from anyone who might try this out.

Bryon
Attachments
adcdma.zip
(4.74 KiB) Downloaded 37 times

BryonMiller
 
Posts: 119
Joined: Fri Mar 04, 2016 10:34 am

Re: Metro M4 high speed ADC (using DMA)

by adafruit_support_mike on Thu Jun 14, 2018 3:41 am

Interesting.. thanks for posting the code!

adafruit_support_mike
 
Posts: 53024
Joined: Thu Feb 11, 2010 2:51 pm

Re: Metro M4 high speed ADC (using DMA)

by jrmclaugh on Wed Aug 01, 2018 11:41 pm

Hi BryonMiller!

I've gotten your sample running and am now trying to adapt it to my app.

I can't seem to find how to set/calculate the sample rate and whenever I try to do any actual processing on the data it locks up the Metro. Presumably it's too much work in the interrupt, but I'm not clear how to reduce frequency of that work to a manageable level. I'm investigating now, but any tips?

The useful registers seem to be the GCLK->PCHCTRL[ADC1_GCLK_ID], CTRLA PRESCALER bit, and the SAMPCTRL.

jrmclaugh
 
Posts: 4
Joined: Fri Jul 13, 2018 12:32 am

Re: Metro M4 high speed ADC (using DMA)

by BryonMiller on Thu Aug 02, 2018 10:21 am

Hello jrmclaugh,

I can't seem to find how to set/calculate the sample rate

You're right I did not provide a way to slow it down. I'd have to rescan the SAMD51 datasheet and recreate my thought process when I came up with this and see if I could post an example of slower sampling.

Rather than that let me tell you what I did. Full disclosure - I have not actually used the 1MSPS data but I did something very similar on a M0 machine. I made an oscilloscope with a feather M0. In this case the maximum sampling rate I could muster was 0.5M SPS. Many times, when using the 'scope, you don't need full speed, you might only need a subset of the samples. It all depended on the "sweep speed" desired. But almost always I would have a trigger condition I wanted satisfied. So I would scan each full-speed buffer looking for the trigger. Once the trigger occurred I would take points out of the full-speed buffer spaced at a sample-spacing corresponding to the desired sweep speed and place them into a sweep buffer. All calculations requiring anything but +/- math or greater/less comparisons (especially any floating point calculations) were performed in the main task, not the interrupt routine.

I also measured and saved the microseconds at the start of the interrupt routine and also at the end. With this data I could get a measurement of how busy the machine was. As you might imagine, the busiest time was when searching the entire buffer looking for the trigger. The actual act of taking a point out of the full-speed-buffer and saving it in the sweep buffer was relatively fast. While looking for the trigger condition I could easily have the M0 more than half consumed in the interrupt routine. The M4 is faster than the M0 so even with twice as much data per second I think moving my 'scope to a M4 would go smoothly. (I'm waiting for the M4 Feather to come into stock to find out.)

So for your case try to pull the data out of the high-speed buffer at whatever spacing corresponds to your needs. But no matter what, you are going to need enough of a machine to process the data and make results available at whatever speed you decide.

Post back here with your results!
Bryon

BryonMiller
 
Posts: 119
Joined: Fri Mar 04, 2016 10:34 am

Re: Metro M4 high speed ADC (using DMA)

by DanaK6JQ on Thu Aug 09, 2018 4:02 pm

BryonMiller wrote:Hello jrmclaugh,

I can't seem to find how to set/calculate the sample rate

You're right I did not provide a way to slow it down. I'd have to rescan the SAMD51 datasheet and recreate my thought process when I came up with this and see if I could post an example of slower sampling.

Rather than that let me tell you what I did. Full disclosure - I have not actually used the 1MSPS data but I did something very similar on a M0 machine.


I've done something similar, though without DMA, on the Metro M4 Express. I haplessly figured I would use Atmel START (Atmel Studio 7, Atmel-ICE) to port a DSP modem project I already have running on STM32F4xx parts to the SAMD51; the hardware averaging in the ADC is appealing.

Because I want a precise sampling rate for DSP, I configured a DPLL and Generic Clock to produce 9600 events/second, and configured that even to trigger conversion on the ADC. That's working as expected; note that you don't configure the ADC for free-running, and you don't need to start conversion, the event does it for you.

DanaK6JQ
 
Posts: 18
Joined: Thu Jun 15, 2017 2:38 pm

Re: Metro M4 high speed ADC (using DMA)

by Pumpa0815 on Thu Aug 23, 2018 9:45 am

Hi Bryon,

thanks for this post. It's really helpful. I got it running on my metro m4 express.

I would like to continuously acquire data and send it to my computer to write it to a file. I am quite new to low-level programming and couldn't figure it out myself. Therefore, I would highly appreciate your advise!

Thanks!!

Pumpa0815
 
Posts: 3
Joined: Thu Aug 23, 2018 7:01 am

Re: Metro M4 high speed ADC (using DMA)

by BryonMiller on Thu Aug 23, 2018 3:21 pm

Hi Pumpa0815.

I found you had another post where you were trying to read analog values and write them to Serial at 11kHz. Assuming that is what you're trying to do you could :

In the interrupt routine go through the array that was last filled and take every 91th point and write that value to a FIFO buffer. In your main routine watch for the FIFO buffer to show data is available. When it is, write all of the newly available data to the Serial port. Then you can do the bulk of further processing in your host computer.

Enhancement might be to do some filtering in the interrupt routine. I think you'll really want to minimize what you do here but some averaging of the 91 samples leading up to "this" point may or may not be helpful.

But I think your biggest problem might be trying to cram that much data through the serial port. Let's say your getting 3 digit ADC samples. That means you'll be writing 5 characters to Serial (3 characters for the digits plus a CR and LF). 5 characters at 11khz is over 500,000 baud. I'm not saying this is impossible but it isn't obvious to me that it will work.

You could just try that part of it by making a simple M4 test program that tries to push that much static data and keeps track of whether it overflows or not.

Good luck,
Bryon

BryonMiller
 
Posts: 119
Joined: Fri Mar 04, 2016 10:34 am

Please be positive and constructive with your questions and comments.