Cortex M0, STM32, how to store samples every time they change in ADC and averaging them from DMA

Thread Starter

ashokraj

Joined Feb 1, 2018
89
Development kit:- STM32F0 discovery
debugger : St linkv2.
IDE: keil
Sensor:http://www.pcb.com/products.aspx?m=601a02
I would like to transfer the data coming to ADC to DMA and average the data collected in 15 seconds.
I could able to read the values from ADC BUT here are my problems:

1.I am using industrial acceleromoeter while reading adc values i could able to see them when i change the position of accelerometer it does appear in the watch windows of keil but when i connect it to the motor ( to measure the vibrations) I am always getting an error that cannot able to acccess the target !!! so I have decided to store the values read by adc to DMA and calculate the average. i assumed keil cannot displays the values which are very fast and changing.

I have seen many tutorials but i could not able to find a way to send the data collected from adc to dma ? how can i achieve that ???? all of the tutorials are using external rc osillator where as i dont prefer to use external oscillator. won't dma work without external osicllator ?

how can i store the values of adc everytime they change? what are the functions, key words i need to use where can i look for them ?
 

danadak

Joined Mar 10, 2018
4,057
DMA in conventional thought and most implementations is the HW capability to
perform operations via HW that transfer peripheral to memory, the inverse, memory to
memory, and peripheral to peripheral. This includes I/O, COMM.....

It is phenomenally useful not only for speed but to sustain many concurrent operations
in modern processors. There are varients on the actual implementation of DMA
from one architecture to another. An early adopter of standalone DMA engines
(state machines driving the DMA operation) was Hitachi in the V Series.
Unfortunately before their time, engineering community was reticent to use.

Some processors have ADC averaging built into HW, like PSOC. I think Renesas also
implements HW averaging.

Regards, Dana.
 
Last edited:

danadak

Joined Mar 10, 2018
4,057
As far as using the generic ST part, is there chained DMA such that sample
gets sent to memory, and chained operation takes it and puts it into array for
further operation ?

The DMA must have capability of auto incrementing its write address ? I know
it has address computation. And interrupting when N transfers done ? I do not
know the architecture, why I am asking the questions. In this case no chained
DMA needed.

Regards, Dana.
 

MrChips

Joined Oct 2, 2009
21,366
DMA is not for a beginner and I will tell you why.

Firstly, ADC does not send data when a change is detected. You need to understand how to use an ADC.
1) You can ask for an ADC reading on demand.
2) You can ask for an ADC reading at fixed intervals.

Hence the first thing you need to establish is, how often should you ask for a reading.
To answer this, you need to know something about the sampling theorem, also known as the Nyquist theorem.

The sampling theorem states that you must sample at least twice the highest frequency contained in the signal.
Let us take an example. Suppose you want to digitize the human ECG (electrocardiogram), or the electrical signal from the human heart.
Let us take as an example a heart beat of 60 bpm, that is 60 beats per minute, or 1 beat per second.
Does that mean that you need to take a minimum of two samples per second as stated by the sampling theorem? Answer is NO.
The heart beat might be 1 beat per second. But each beat is a short spike, or an impulse function which contains very high frequencies. You need to perform a preliminary test in order to determine the highest frequency component in the signal. Then your sampling rate needs to be double that.

In our heart beat example, we may have to sample at 1000 samples per second in order to faithfully record the ECG waveform.
Hence we must have some knowledge of the frequency content of what we are trying to measure.

Ok, suppose that we want to take 1000 samples per second. The next step is to ensure that the ADC and the recording hardware and software is capable of delivering 1000 samples per second.

Before getting into DMA, you need to get some experience with:

1) Sampling on demand.

We can set up an ADC, hardware interface, and software, to acquire random samples on demand as long as our sampling rate satisfies the sampling theorem. With the data saved in a memory buffer (data array) we can analyze the data and extract any desired information such as changes, average, peak value, etc.

In order to extract frequency information, we need to:

2) Sample at a periodic interval.
In order to do this, we need to establish a time-base that can provide us with a fixed delay between samples. We can do this with a software timing loop but this is not reliable and consistent. A better solution is to use an interval timer to trigger the ADC. In order to record the data, we can use:

a) polling
b) interrupts

In either case, we now have to incorporate a timer into our solution. This is not a problem and is a workable solution when the sampling rate is low, for example, 1ksps.

What if you want to sample at 1Msps?
Now you are pushing the limits of the MCU system, even for MCU that are running with 16MHz clocks or higher. This is where DMA becomes important. How does DMA work? DMA gives the processor the ability to trigger the ADC and transfer the data to memory directly without software intervention. If you think about this, this becomes a very complex operation.

Where in memory is the data stored?
How many points are recorded?
What happens when the memory is full?

All of these questions and scenarios have to be handled by the DMA system. Plus there are additional features that can be advantageous for your specific application.

Do you want to stop when the given number of samples are recorded?
Or do you want to continuously sample into a circular buffer, repeating from the start when we get to the end of the buffer?
Or do you want a dual circular buffer where the ADC fills one buffer while you process the other buffer?

How do you get started programming for DMA?
You need to initialize a periodic timer which will trigger the ADC.
You need to initialize the ADC and DMA with the address of the ADC, the address of the memory buffer, the number of data to be saved, and all the desired options as stated above.

If you are not quite ready for DMA you need to step back and do something simple such as request ADC data on demand by polling the ADC when it has completed a single conversion.
 

danadak

Joined Mar 10, 2018
4,057
DMA setup certainly not trivial, but modern tools with DMA wizard make life a lot easier.
That of course does not make setting up advanced operations like chaining trivial
but nor is it rocket science.

Here is a couple of ap notes that cover some basics and advanced topics, like timing. Particular
to a specific processor, but instructive never-the-less.

http://www.cypress.com/documentation/application-notes/an61102-psoc-3-and-psoc-5lp-adc-data-buffering-using-dma

http://www.cypress.com/documentation/application-notes/an52705-psoc-3-and-psoc-5lp-getting-started-dma

http://www.cypress.com/file/46156/download


Regards, Dana.
 

Thread Starter

ashokraj

Joined Feb 1, 2018
89
Thanks for the links and a lot of information Dana! I am new to ARM and also new to STM32 microcontroller. Unfortunately this is the first time I have heard of cypress and also PSOC, I will keep this in consideration for future. I have some small basic questions i.e., does cypress use the IDE of keil, is it use ARM architecture?
 

danadak

Joined Mar 10, 2018
4,057
Cypress -

PSOC 4 family ARM M0

PSOC 5LP family ARM M3

PSOC 6 family M0+ and M4, dual core

Attached a list of the components/facilities/HW inside PSOC Chip. Note extensive analog
facility and advanced like digital filter, LUTs, custom component creation, standard logic,
routable with drag and drop gates, flops......

IDE and Compiler free, PSOC Creator -

http://www.cypress.com/products/psoc-creator-integrated-design-environment-ide

Regards, Dana.
 

Attachments

MrChips

Joined Oct 2, 2009
21,366
You need to understand what is DMA and when, where, and why you would use it.
DMA transfers data from ADC to memory without software intervention. The only reason for using DMA is for speed.
If you do not need super fast sampling rates then you don't need it.
 

danadak

Joined Mar 10, 2018
4,057
Another reason for using DMA is to enable concurrent background processes
to run.

Example -





This offloads processor to perform other tasks while implementing an involved
process for signal conditioning. Just one example. Other examples could
be handling COMM streams, tasks that need to be done but do not need to
consume CPU MIPS. The ever present tradeoff, HW vs SW. There are processor
families out there that allow both as a flexible capability. Above example
happens to be Cypress PSOC. All done onchip both analog and digital.

Regards, Dana.
 
Last edited:

MrChips

Joined Oct 2, 2009
21,366
Concurrency and speed/throughput go hand-in-hand.

The reason you want to use DMA is so that you can off-load data handling to HW and free up the processor to do SW processing. If speed were not an issue, you wouldn't need DMA. Interrupts may be able to get the job done.

As an example, I have a STM32F407 with a 42Msps ADC acquiring data continuously into a circular buffer using DMA. SW performs FFT. The graphical result is pipelined out to VGA monitor, again using DMA, no external chips required. PS/2 mouse attached. Serial comm to USB is done with MATLAB using interrupts.
 

danadak

Joined Mar 10, 2018
4,057
If speed were not an issue, you wouldn't need DMA. Interrupts may be able to get the job done.
Agree most of the time, but not as an absolute only qualifier for using DMA.

I think of HW as more fault tolerant than code base execution.

And then there are sampling applications, trying to achieve more determinacy, less jitter.
Not perfect depending on how arbitration done, and if separate busses for highways not present.
But then one could argue that's a speed issue, even if only sampling once an eon.

Always tradeoffs, always tradeoffs.

Regards, Dana.
 

MrChips

Joined Oct 2, 2009
21,366
Minimum jitter is achieved not with interrupts but by using a stable HW sampling clock. Interrupts are invoked only to save the ADC data once it has been digitized (i.e. at end of conversion).
 
Top