Electronic – Processing ADC conversion result: DMA vs Processor Registers

adcdata acquisitiondmaregister

I have a periodic ADC conversion (every 50 micro-seconds) and I use the result to do some further calculations and update the PWM load registers. The ADC is triggered by the up-down counter of hardware PWM and has to be done exactly at that time. I am using a Teensy 3.2 (ARM M4) and although it's a beast, I have to be as fast as possible with the ADC to be able to squeeze in the rest of the calculations before the next duty cycle starts.

The general idea to operate faster seems like using DMA which transfers the conversion results directly to system memory without involving CPU, which looks like

ADC_result -> DMA -> SRAM,

But after that I have to fetch the result back from the SRAM to process it, which looks like:

ADC_result -> DMA -> SRAM -> CPU -> finally getting processed.

Of course, not involving the DMA is way slower:

ADC_result -> CPU -> SRAM -> CPU -> finally getting processed.

My question is, since I have to put the conversion result into CPU to be processed anyhow, can't I just store it directly in processor registers without involving DMA or SRAM at all and process is right away?

ADC_result -> CPU -> finally getting processed.

There are a couple of points I would like to make to further clarify the nature of my issue and test any gaps in my understanding.

First, from what I have understood, DMA is mainly used when there is real logging to be done and we would to use the CPU in parallel, especially if the data providing peripheral is considerably faster/slower than CPU. This is not my case, since all the following calculation are dependent on the ADC conversion result and I have very little data being sent every 50 micro-seconds rather than huge chunks.

Second, there is no guarantee that I will be able to use processor registers freely, since they are subjugated to compiler optimization and might be totally ignored (using the register keyword in C).

I would love to hear any feedback on the matter, especially if my understanding is somewhat flawed or plain wrong.

Best Answer

If you just write a function that reads the ADC result into a variable, does some math on it, and then writes the result to PWM, and if you use a decent compiler with optimization turned up, then the critical data will probably end up in a register whether you want it to or not. That's really not that much different from setting up DMA just so that you can fetch one memory location and do the same thing.

Where I would use DMA for this sort of thing is if I were going to do something like fetch multiple ADC samples per cycle of the controller (which I often do) -- the DMA saves a lot vs. the overhead of an ISR to collect the data from an ADC.