ADC as DMA with TIM2 - What am I doing wrong?

Discuss how to use the features of Atollic TrueSTUDIO, including the editor, assembler, C/C++ compiler, linker, debugger, static code analysis and team collaboration tools.

Moderators: Markus Girdland, Mattias Norlander

nicdiamor
Posts: 1
Joined: Mon Feb 11, 2019 9:26 pm

ADC as DMA with TIM2 - What am I doing wrong?

Postby nicdiamor » Thu Feb 14, 2019 3:28 pm

Hello everyone, newbie here seeking for help!

I've acquired a NUCLEO-F411RE and I'm doing some test with it, but something doesn't seem to be working properly with the ADC conversions. I'll explain my math and process:

- I've set the initial configuration using CubeMX:

NUCLEO-F411RE chosen

Pinouts cleared.

Clock Config -> HSI, Main PLL *N=352 /P=4, SYSCLK=88MHz

RCC -> HSE BYPASS clock source.

SYS -> Debug Serial Wire.

ADC channel 0 selected -> PCLK/4, 12bits, No continuous conversion, DMA Continuos request enabled, EOC Flag single conversion, 1 conversion, Timer 2 Trigger Out on rising edge, 15 cycles sampling time.
If I understood it right, a conversion should take 27 cycles; with the chosen ADCCLK this translates to 1,28ns approx.

TIM2 -> Clock source internal clock, prescaler = 0, Counter= 1000, Trigger Event - Update Event
With this I wanted to have 44KHz sampling rate on the ADC, as the TIM2 is connected to the APB1 Peripheral Clocks (44MHz). It should call the ADC each 23ns approx.

DMA -> DMA2 Stream 0 Request by ADC, Circular mode, Word.

NVIC -> Enable ADC global interrupt.

- Then the export to TrueSTUDIO. Here are the user code on the main.c file:

/* USER CODE BEGIN 0 */
//Buffer ADC
uint32_t adcVal[2048];
//Conversion counter
uint16_t counter=0;
/* USER CODE END 0 */

/* USER CODE BEGIN 2 */
//Start the timer 2
HAL_TIM_Base_Start(&htim2);
//Start the ADC to DMA conversion
HAL_ADC_Start_DMA(&hadc1, adcVal, 2048);
/* USER CODE END 2 */

/* USER CODE BEGIN 4 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
//Buffer index
if (counter == 2047)
counter=0;
else
counter++;

/* Prevent unused argument(s) compilation warning */
UNUSED(hadc);
}
/* USER CODE END 4 */

- Everything compiles fine, but then I watch the counter variable on STMStudio and the conversions are running incredibly slow... AFAIK the buffer should be full of data in about 43ms, but it is taking about 44 seg! I thought I was missing a factor of 1000 somewhere, but if I change the TIM2 counter to 1, the buffer fills in about 6s.

Are my math wrong and the ADC is working as intended, or am I missing something?

ggutierrez
Posts: 1
Joined: Wed Feb 20, 2019 10:08 pm

Re: ADC as DMA with TIM2 - What am I doing wrong?

Postby ggutierrez » Wed Feb 20, 2019 10:12 pm

change TIM2 prescaler=1


Return to “Atollic TrueSTUDIO tool discussions”

Who is online

Users browsing this forum: No registered users and 2 guests