Coder Social home page Coder Social logo

Comments (8)

jmdodd95682 avatar jmdodd95682 commented on May 28, 2024

I'm starting to understand what the issue really is. First there is some misunderstanding on my part on how this library works. adc.read() is returning a buffer. The intent, I guess, is to copy the contents of this buffer ASAP and release the buffer (adc.release()). I was able to hack a sketch which did this. I was able to acheive >96KHz sampling rate.

I now see that there are really two issues which make it tricky to use this to sample audio input real-time.

  1. The adc.read() is a blocking operation. This means if the next buffer is not ready, you stall waiting for the interrupt. So you must first check adc.available() or you can incur a penalty.

  2. The adc.read() seems to not be able to do less than 8 samples per buffer. A value for num_samples of less than 8 seems to take just as long as num_samples=8. This may be a limitation of the hardware.

The documentation does not really explain this. Perhaps better documentation is all that is required.

Anyway, I now understand the library much better.

Thanks again for creating this library.

from arduino_advancedanalog.

aentinger avatar aentinger commented on May 28, 2024

Hi @iabdalkader ☕ 👋

Can you perhaps take a look at this? 🙇

from arduino_advancedanalog.

iabdalkader avatar iabdalkader commented on May 28, 2024

The adc.read() seems to not be able to do less than 8 samples per buffer. A value for num_samples of less than 8 seems to take just as long as num_samples=8. This

I'm not sure what you're trying to do but I don't think the time you're measuring is relevant, regardless of the buffer size there will always be some overhead from the dequeue calls etc.. so read() will take some fixes time + total time to convert N samples (if there are no buffers in the queue). You can try to stop the ADC and measure how long it takes to read a buffer, but I think what really matters is that samples are 1/Fs apart regardless of the number of samples or number of buffers. As for audio sampling, the ADC is currently configured to run at 64 MHz, the total conversion time is (Sample Time + Resolution/2 + 0.5) which is 17 cycles (or 0.265625 uS) so it should be possible in theory to sample up to ~3.6 Msps.

from arduino_advancedanalog.

jmdodd95682 avatar jmdodd95682 commented on May 28, 2024

I'll try to capture a waveform from the logic analyzer. Here's what I see. When I put the num_samples to 1 (and the queue_depth is 8) and the sample rate to 32KHz, and I try to run adc.read() as without checking adc.available(), the waveform shows one adc.read() can complete every 250us...that about 4KH sample rate. The adc.read() blocks waiting for the DMA interrupt, so I'm guessing what is happening is that while num_samples is 1, the DMA engine always fetches at least 8 (which is 16*8=128-bits of data).

Like I said in the subsequent reply, I can get it to sample at even 96KHz, but I have to set the num_samples to 8 and check adc.available(). I guess what I learned is never use adc.read() blindly, always use adv.available() to check.

Thanks again for looking into this.

from arduino_advancedanalog.

iabdalkader avatar iabdalkader commented on May 28, 2024

so I'm guessing what is happening is that while num_samples is 1, the DMA engine always fetches at least 8 (which is 16*8=128-bits of data).

Ah I see what you mean now, yes you're right that's due to the DMA FIFO threshold, the DMA does not transfer samples immediately but it stores them in a FIFO. The FIFO level can be configured, from none (direct mode) to FULL, so it's probably something we can improve, but I'm wondering if we should just return an error on very small buffers and document this, as it's not very efficient to trigger a memory transfer on every sample.

from arduino_advancedanalog.

jmdodd95682 avatar jmdodd95682 commented on May 28, 2024

I agree. I don't think this is a bug, so much as documentation and possibly an error message to the user. I was used to the standard Arduino analogRead function which returns a single sample each time you run it. So, I was expecting that doing
adc.read() for a single sample would be a normal operating mode. Now that I see how it really works I'm fine with 8 being the minimum for full sample rate. Doing a single data sample is very inefficient, and I'm starting to like the DMA model.

from arduino_advancedanalog.

jmdodd95682 avatar jmdodd95682 commented on May 28, 2024

BTW. Just to close. I was able to get the ADC working using this library for my oscilloscope sketch. Works great.

from arduino_advancedanalog.

aentinger avatar aentinger commented on May 28, 2024

Terrific! Glad to hear you got your sketch working. Will be closing this issue.

from arduino_advancedanalog.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.