Looking for working DMA ADC tutorial

Post here first, or if you can't find a relevant section!
bobemoe
Posts: 10
Joined: Sun May 30, 2021 11:43 am

Looking for working DMA ADC tutorial

Post by bobemoe »

I’m trying to get DMA ADC working to sample a small audio clip for DSP/FFT analysis.

I’ve read loads of tutorials, and tried loads of sample code but I can’t get anything to compile.

I’m fairly good with arduino, but new to STM32. I’ve never done DMA before on either.

I’m using STM32F411CE with ArduinoIDE and https://github.com/stm32duino/BoardManagerFiles

A lot of the example I’ve found require libmaple, which I considered switching to, but after some reading seems it is deprecated and maybe not the best idea to use it for a new project?

I’ve tried generating the code using STM32CubeMX but the files it generates seem to clash with the internals of stm32duino core. One example I think was redeclaration errors for things like SystemClock_Config.

I would post some code but I’ve tried so many things that I’ve kind of lost track of what code gave which errors, but what I can say is that I found nothing that worked. So I think its time to start again from the beginning.

My aim is to use one ADC pin to fill a continuous DMA buffer ( float32_t[4096] ) at a sample rate suitable for FFT.

I have the FFT code working sampling with analogRead() but the docs say it’s best with DMA.

I will also need to use some other analog pins for some other reading, but they can be done with normal analogRead no problem. I did see some people struggling to get both DMA and analogRead to work together, so that may be a requirement consideration too.

Any thoughts? Why am I finding this so difficult? What is the correct way to go about this? A link to a nice tutorial that actually works on stm32duino core would be brilliant!

Thanks :)
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Looking for working DMA ADC tutorial

Post by fpiSTM »

Here you will find an example of STM32 cube example converted to Arduino sketch:

viewtopic.php?f=41&t=110
bobemoe
Posts: 10
Joined: Sun May 30, 2021 11:43 am

Re: Looking for working DMA ADC tutorial

Post by bobemoe »

Hi, thanks for quick reply.

I cant get that one to compile either:

Code: Select all

/home/bob/Arduino/DMA/DMA.ino: In function 'void HAL_ADC_MspInit(ADC_HandleTypeDef*)':
DMA:64:40: error: 'RCC_PERIPHCLK_ADC' was not declared in this scope; did you mean 'RCC_PERIPHCLK_RTC'?
   64 |   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
If i comment out the 3 clock pre scaler lines, i then get this next error:

Code: Select all

/home/bob/Arduino/DMA/DMA.ino: In function 'void HAL_ADC_MspInit(ADC_HandleTypeDef*)':
DMA:80:24: error: 'DMA1_Channel1' was not declared in this scope
   80 |   DmaHandle.Instance = DMA1_Channel1;
Do I need to be adjusting this code for my specific chip? Looks like some includes or constants are missing?
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Looking for working DMA ADC tutorial

Post by fpiSTM »

Yes you have to adapt. The linked code are for F1.
bobemoe
Posts: 10
Joined: Sun May 30, 2021 11:43 am

Re: Looking for working DMA ADC tutorial

Post by bobemoe »

Ok, so guess that's what I need the tutorial or help with.

It looks like maybe I need to use some different constants to specify the DMA and ADC channels. If so how do I find these constants?

First stop is the STM32F411CE data sheet. It shows channel names the various things are in, but I have no idea which of these values correspond with the constants in the core? And even so, which would be valid to pass to which function.

Next stop is STM32CubeMX! Lots of tutorials on how to set up the ADC DMA and generate code, but it doesn't directly run as it seems to define things that are already defined in the stm32duino core. Do I need a different core to run this? Or a different IDE? I don't really like ArduinoIDE as an editor so I'd consider a different toolchain if that helps.

Maybe I should be studying the stm32duino core code? Are the constants in there? I wouldn't know which to choose if I found them but there could be extensive comments explaining things?

Or should I really grok channels, interrupts, HAL, and timers? I'm surprised there isn't a library that abstracts all this low level stuff. This looks great but I don't think it works for STM32. https://github.com/TMRh20/AutoAnalogAudio

I've read lots of examples and every time it seems to be different! From configuring with shift registers (way over my head) to using libmaple, STMCube, user libraries or HAL functions with and possibly without the files from STMCube code generator.

These documents, datasheets and sources are huge! Gunna take some time to read it all! Where are my efforts best directed?

I can't believe there are no working examples for STM32F411! It's very rare I cant find someone who's already done something and posted about it! What am I missing?

Is the board the processor is on (WeAct BlackPill v3) also defining pins and channels that need to be configured?

Cheers!
User avatar
MasterT
Posts: 33
Joined: Mon Apr 20, 2020 12:02 am

Re: Looking for working DMA ADC tutorial

Post by MasterT »

This is my way, how to configure some peripheral that I can't get a library :

1. Run CubeMX - generate a code. Very likely cube would skip some parts, for example start-pwm() for timers channels. So, next step to
check what is missing.
2. Get pack of examples bind to Cube MX, for specific mcu series - F4, I see pack includes nucleo-F411 & F411-discovery.
Study. If no examples adc-dma in f411 folder, look in the others, f446, likely it would be the same or very close.
3. HAL driver pack IS a big library for everything, all you need to know is the name of right functions() that does what you need.
Dig into arduino installation folders on your comp, somewhere in stm32/ subdirectory you will find STM32F4xx_HAL_Driver,
and stm32f4xx_hal_adc.h + stm32f4xx_hal_adc.c, it lists all possibilities to tweak adc.
4. Big chances, that nothing works, and you have no idea what is wrong. You need a FRAME-WORK for debugging.
4.1 serial monitor is the best, study my example written couple years ago for F446. There are a-r-w commands, where
"a" stands for register address, "r"-read, "w"-write. "a4000042c" - to read TIM-2 reg 2C. Programming Reference Manual would explains all details for each bit or bit-fields.
4.2 use digital pins to indicate status of software, interrupts, timing performance. Get a scope, even a cheap one provides great advantage for debugging.

ADC-DMA code was written for nucleo-F446, at that time I didn't know that adc compilation can be blocked, all I knew is that arduino
code run post-init and ruins all my effort to call HAL_ driver directly, So I overwrite adc_init and dma code, to find out why it hangs up .
Attachments
adc_1b.zip
ADC-DMA F446re
(7.99 KiB) Downloaded 407 times
JimEli
Posts: 20
Joined: Sat Apr 17, 2021 11:33 pm

Re: Looking for working DMA ADC tutorial

Post by JimEli »

maybe this will help: STM32 DMA ADC P2M Demo
bobemoe
Posts: 10
Joined: Sun May 30, 2021 11:43 am

Re: Looking for working DMA ADC tutorial

Post by bobemoe »

JimEli wrote: Mon May 31, 2021 1:39 am maybe this will help: STM32 DMA ADC P2M Demo
Thanks :) Closest so far! It compiles and I thought it was working as it prints 24.5degC. But it seems that its just returning that value no matter what temp the chip is.

If I change the channel from ADC_CHANNEL_TEMPSENSOR to ADC_CHANNEL_4 then the buffer doesn't seem to be getting updated anymore, apart from the first element which has a value floating around 0-7 no matter what I put on the analog pins, the rest of the elements are 0.

Looks like I'm still missing something.
MasterT wrote: Sun May 30, 2021 9:50 pm This is my way....
Thanks for this, it compiled (after I removed one line RCC_OscInitStruct.PLL.PLLR not sure what this was :/) it's hanging for me too, doesn't seem to initialize the serial so I'm not able to open serial monitor to debug or send commands.

Lots of info in you provided though so I will go through it more thoroughly.

Two steps forward, one step back. Getting there slowly.
User avatar
MasterT
Posts: 33
Joined: Mon Apr 20, 2020 12:02 am

Re: Looking for working DMA ADC tutorial

Post by MasterT »

I commented out
// SystemClock_Config();
bit it's called anyway, so remove function completely out of sketch.
JimEli
Posts: 20
Joined: Sat Apr 17, 2021 11:33 pm

Re: Looking for working DMA ADC tutorial

Post by JimEli »

I would suggest using STM32CubeMX to generate your base code. From the MX generated files you will need to combine the required routines from the main, it and msp files. Disable the SystemClock_Config and HAL_Init functions (performed by the STM32 core). Distribute the initiation code to the setup function and use loop to run your main code. Disable the conflicting STM32 core use of the HAL modules via the instructions here. If this doesn’t make sense, I would suggest using the STMCubeMx and STMCubeIDE to generate native code. Either way, you will need to become familiar with the mcu reference manual.
Post Reply

Return to “General discussion”