Author Topic: "Best" way to trigger fast ADC sample rate with freeRTOS???.......  (Read 2133 times)

0 Members and 1 Guest are viewing this topic.

Online SmokeyTopic starter

  • Super Contributor
  • ***
  • Posts: 2597
  • Country: us
  • Not An Expert
I have a project that requires the use of FreeRTOS because of a network stack.  This happens to be running on an ESP32, but I think this is essentially the same question for any project running freeRTOS.

Problem:
I need to take ADC samples from a single channel at about 5kHz (200us), but the FreeRTOS system tick is 1ms.  That rules out using FreeRTOS tasks/timers/etc that rely on the tick.  Making the tick faster starves the system of cycles since it runs so often.

Given that I can't just go bare-metal for everything and have to keep the FreeRTOS system happy and not break anything RTOS related, what is the "best" way to trigger the ADC and store those samples at a rate faster than the System Tick?
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14482
  • Country: fr
 
The following users thanked this post: harerod, 5U4GB

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #2 on: March 07, 2024, 06:31:04 am »
+1 for interrupts, and read that chapter page. or if DMA is available ADC + DMA and then interrupt once you have enough samples in the DMA buffer
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14482
  • Country: fr
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #3 on: March 07, 2024, 06:36:21 am »
+1 for DMA as well, unless you need to react to each sample in some specific way.
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1641
  • Country: nl
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #4 on: March 07, 2024, 08:16:17 am »
I think still the answer is bare metal.

The tick period of 1ms is already quite fast (some projects use 10ms or are tickless), and should be allocated for the relatively slow response of RTOS tasks.

5kHz sampling is then too fast to do in software and carry all of this RTOS overhead. Unless the ADC needs to be preemptive its better to put it in a small IRQ handler and call it a day.
RTOS is great if you need preemption, but for 5kHz (200us) you would need to very quickly handle that preemption (and subsequent priority inversion) to adhere to this real-time requirement.

If those ADC samples @5kHz need to go to ADC, consider buffering a couple to cut down the invocation rate of the RTOS task. This is also a common technique applied with DMA, where you can specify how many transfers till a half/full complete IRQ occurs.
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26907
  • Country: nl
    • NCT Developments
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #5 on: March 07, 2024, 04:16:16 pm »
DMA is certainly the way to go in this specific case. Normally a microcontroller shouldn't have a problem with keeping up with a 5kHz interrupt at all but I have the feeling the Espressif software is blocking interrupts for prolonged periods of time. Recently I investigated why the UART on an ESP32 loses characters at 115k2 baud. After digging deep into the UART driver (including stripping it to a bare minimum) it turns out that the UART overflows due to missing the interrupts even though the UART has a 128byte FIFO.

Edit: the UART problem turned out to be caused by the UART ISR handler not being placed into IRAM (this is a configuration option).
« Last Edit: March 23, 2024, 09:17:31 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline MarginallyStable

  • Regular Contributor
  • *
  • Posts: 66
  • Country: us
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #6 on: March 07, 2024, 04:53:07 pm »
Can run RTOS on one core and a baremetal task on the other.
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19517
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #7 on: March 07, 2024, 05:11:13 pm »
...
Problem:
I need to take ADC samples from a single channel at about 5kHz (200us), ...

Can I suggest you evaluate the jitter in the 200µs that your application can tolerate. That may determine what is and isn't possible.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Online PlainName

  • Super Contributor
  • ***
  • Posts: 6847
  • Country: va
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #8 on: March 07, 2024, 05:22:12 pm »
Quote
I need to take ADC samples from a single channel at about 5kHz (200us), but the FreeRTOS system tick is 1ms.

Isn't FreeRTOS tick on the ESP32 10ms? The resolution is 1ms but actual task switch is 10ms I'm sure. Not that it makes much of a difference in this case :)

Quote
I think still the answer is bare metal.

It's perfectly possible to use FreeRTOS as a subservient process. You can use it in cooperative mode if necessary, or you could drive the tick from the ADC interrupt (allowing the ADC to pre-empt FreeRTOS.

In this case, apparently FreeRTOS is desired because of the services it provides, so going baremetal would require much more work than just bringing the ADC interrupt forward a bit.
 

Offline martinribelotta

  • Regular Contributor
  • *
  • Posts: 56
  • Country: ar
  • A camel is a horse designed by a committee
    • Martin Ribelotta design services
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #9 on: March 07, 2024, 05:27:27 pm »
Here is the pertinent example using ESP-IDF:

https://github.com/espressif/esp-idf/blob/v5.1.1/examples/peripherals/adc/continuous_read/main/continuous_read_main.c

This use ADC in continuous mode (ADC-DMA) and is suitable to change the sampling rate and take the result per frame (configurable amount of samples)

In my experience, the sample-to-sample mode (in any platforms) is only suitable when the processing latency from input to output will be only one sample... This situation is extremelly rare and normally is desirable to process frame by frame in an application-dependent size buffer
 

Offline TomS_

  • Frequent Contributor
  • **
  • Posts: 834
  • Country: gb
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #10 on: March 10, 2024, 12:31:26 pm »
Timing doesn't have to come exclusively from FreeRTOS. You can use interrupts at a priority above the FreeRTOS kernel (and any priorities that FreeRTOS needs to be able to mask) and still have those interrupts fire even if FreeRTOS masks out the lower priorities.

This allows you to e.g. run a timer whos ISR takes an ADC reading. But you can't interface with FreeRTOS from those higher priorities to put those readings into a queue for example, so you have to get a bit more clever with how you take and store the readings to make them available to a task running under FreeRTOS.
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 3700
  • Country: gb
  • Doing electronics since the 1960s...
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #11 on: March 12, 2024, 04:48:00 pm »
Timer -> ADC -> DMA -> RAM

The DMA writes into a circular buffer and this either just wraps and overwrites old stuff, or reaching the transfer count triggers an ISR which resets the DMA address or whatever.

ISRs run very fast on these chips. I have ISRs running is 3-4us on a 168MHz 32F417.

And using the timer to trigger the ADC will give you minimal jitter.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #12 on: March 17, 2024, 02:22:20 pm »
just put some sound codec chip in your circuit, something like TLV320AIC3204.
You can find a lot of examples and drivers for these codecs.
It helps you to capture signal with higher sample rate and low sample jitter.

There is no need RTOS for such simple task.

PS: by the way, 5 kHz sample rate for ADC is a very slow low speed ADC. High speed ADC working at 100-200 MHz sample rate or higher.
« Last Edit: March 17, 2024, 02:29:34 pm by radiolistener »
 

Online PlainName

  • Super Contributor
  • ***
  • Posts: 6847
  • Country: va
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #13 on: March 17, 2024, 02:41:02 pm »
Quote
There is no need RTOS for such simple task.

I think you've just responded to the title and neglected to read this bit in the actual requirements of the first post:

Quote
use of FreeRTOS because of a network stack
 
The following users thanked this post: tooki

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #14 on: March 18, 2024, 01:58:18 pm »
Quote
There is no need RTOS for such simple task.

I think you've just responded to the title and neglected to read this bit in the actual requirements of the first post:

Quote
use of FreeRTOS because of a network stack

I responded to original post. What I wanted to say is that RTOS is not required for ADC or audio codec like TLV320. If you're needs to use RTOS for other requirements, that's your choice, but you can read stream data from audio codec independently if you use RTOS or just implementing bare metal firmware with no RTOS.
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11561
  • Country: ch
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #15 on: March 22, 2024, 08:54:50 am »
I responded to original post. What I wanted to say is that RTOS is not required for ADC or audio codec like TLV320. If you're needs to use RTOS for other requirements, that's your choice, but you can read stream data from audio codec independently if you use RTOS or just implementing bare metal firmware with no RTOS.
But everyone, including the OP, clearly knows an RTOS isn’t needed for the ADC. You aren’t providing some keen insight, you’re just adding noise.
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #16 on: March 25, 2024, 01:29:21 pm »
I just proposed the good and cheap way to get low jitter ADC measurements.
It allows to decrease ADC phase noise and get higher speed stream from ADC with less CPU load.

And it provide you much less ADC phase noise and more easy solution than using internal ADC with DMA.

There are possible other solutions but they are not so simple and cheap, because it requires more expensive components, such as FPGA. Which are more complicated and will be overkill for the topic starter needs.

While internal ADC phase noise is limited with internal timers jitter, which is not good and you will get higher phase noise. With high clock jitter, you will get more noisy ADC output.

Since topic starter needs ADC with about 5 kHz sample rate, using audio codec will be a nice solution. Because it already has all things to capture ADC data with low clock jitter and with high dynamic range and stream that data to CPU with sample rate up to 384 kHz.

By using external audio codec, you can clock it from ultra low phase noise clock source and get ready to use sample stream... without having to worry about samples being taken with configured sample rate and with minimum clock jitter.


The topic starter asked for the best way to trigger ADC. And trigger it from dedicated ultra low phase noise clock source is the best way to get maximum ADC performance and minimize phase and amplitude noise on ADC output.

Any clock source from microcontroller is not good for ADC due to a very high clock jitter. Just open microcontroller datasheet and check it's jitter on GPIO, it is very high. FPGA has better jitter performance than microcontrollers, but still very bad for ADC clock. This is why using dedicated ultra low phase noise clock source is the best way to trigger ADC in order to get the best ADC performance.
« Last Edit: March 25, 2024, 02:14:00 pm by radiolistener »
 

Online SmokeyTopic starter

  • Super Contributor
  • ***
  • Posts: 2597
  • Country: us
  • Not An Expert
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #17 on: March 25, 2024, 06:38:57 pm »
ha!  you guys leave radiolistener alone!  He's just trying to be helpful!  Let me just go add an FPGA and external audio codec to my design...
Something like this should be good right?
https://www.digikey.com/en/products/detail/amd/XC5VFX100T-2FF1738C/3981496

Seriously though, nothing I'm doing for this application needs anywhere near that level of precision from either the ADC resolution or phase noise perspective (the ESP32 should be a giveaway). 

The "right" answer, as you guys pointed out, is interrupts/dma and then signaling the RTOS task to use the ADC data and being careful not to take too many cycles from the RTOS.  That is so far working and not interfering with the radio aspects of the RTOS that I don't really have control over.
 
The following users thanked this post: hans, tooki

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #18 on: March 25, 2024, 06:57:12 pm »
ha!  you guys leave radiolistener alone!  He's just trying to be helpful!  Let me just go add an FPGA and external audio codec to my design...
Something like this should be good right?
https://www.digikey.com/en/products/detail/amd/XC5VFX100T-2FF1738C/3981496

if you're want to work with a high speed ADC for a phased array radar it may be the good choice  :D

For 5 kHz sample rate and single ADC it will be overkill

Seriously though, nothing I'm doing for this application needs anywhere near that level of precision from either the ADC resolution or phase noise perspective (the ESP32 should be a giveaway). 

but you're asked for the best way to trigger ADC...   :)

Feeding ADC with a clock from microcontroller timer is definitely not the best way for jitter / phase noise performance.
« Last Edit: March 25, 2024, 07:07:11 pm by radiolistener »
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11561
  • Country: ch
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #19 on: March 25, 2024, 07:15:55 pm »
ha!  you guys leave radiolistener alone!  He's just trying to be helpful!  Let me just go add an FPGA and external audio codec to my design...
Something like this should be good right?
https://www.digikey.com/en/products/detail/amd/XC5VFX100T-2FF1738C/3981496

if you're want to work with a high speed ADC for a phased array radar it may be the good choice  :D

For 5 kHz sample rate and single ADC it will be overkill
You don’t understand sarcasm, do you? He was making fun of your suggestion, which was excessive and ignored OP’s stated requirements.
 

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3385
  • Country: ua
Re: "Best" way to trigger fast ADC sample rate with freeRTOS???.......
« Reply #20 on: March 25, 2024, 07:29:57 pm »
You don’t understand sarcasm, do you?

I well understand his sarcasm. But some peoples just don't understand and don't realize that ADC clock jitter can affect ADC output results and dynamic range. And I'm not sure if these who laugh understand it.  :)

I personally feel there is something wrong with the design if it needs to read a 5kHz ADC clocked from software generated clock and using interrupts.

« Last Edit: March 25, 2024, 07:33:33 pm by radiolistener »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf