Showing results for 
Search instead for 
Did you mean: 

STM32F334R8 reading ADC (single channel timer triggered) with 2us intervals

Associate II

Hi, I'm trying to read ADC data with 2us periods using ADC interrupts on Nucleo F334R8. I'm triggering ADC with TRGO event of Timer 2. I'm toggling a pin whenever callback function is called to debug using an oscilloscope. However, I cannot observe 2us on the scope. Pin cannot be toggled below 10us. I tried DMA method and got the same result. What could I be doing wrong here?

(I'm a beginner on STM32 and using CubeMx.)


Which STM32?

How is the ADC set, exactly, together with clocks? If the sampling+conversion time of ADC is longer than 2us (plus whatever time the software takes, and in case of Cube/HAL it may be quite a lot), then you won't be able to achieve the 2us period.


Thanks for the reply Jan.

ADC clock is 64Mhz through PLL and I chose sampling time 1.5 cycles. According to manual (stm32f334xx), 14 clocks cycle are needed for conversion which translates to about 220ns.

I also thought it might be a software related issue. What can I do to increase software speed?

Associate III

Check if the maximum frequency that can be set to the system clock is 64MHz. I guess you can set it more than that. Also check if you are timer is called at 2us only.

Senior III

I prefer to trigger ADC by HRTIMER. Can you also try?

Also pls provide a screenshot of CubeMX and source code.

Also, check how fast you can toggle the pin without any code.

You can try how fast you can toggle by adding these code snippets in while(1).

You have guidance on how fast the GPIO can toggle here.

Associate II


Thanks for the replies. I attached the source code. I'm going to try to to toggle the GPIO the way you suggested.

Senior III

For GPIO toggling speed, you can create a project that toggles the IO, and check how fast it can toggle in the while(1) loop.

For ADC, you may need to add


before using the ADC.

Associate II

I tried to toggle an IO pin in while loop and saw that it can be toggled at over 1Mhz frequency. I think my problem is with duration of the interrupt function. I looked inside the HAL_ADC_IRQHandler function, there is a lot of code in there. How can I simplify it to increase speed?

First, review/debug-step the HAL code and find the relevant pieces for your use case. Then, add your code to the original IRQ Handler in *_it.c file. Add your code in the first user code section and end it with a return. Leave the call to the HAL handler intact. It will never be called anymore, because you return early, but it will always be regenerated by the tools.

Edit: you might wrap the IRQ handler in

#pragma GCC push_options
#pragma GCC optimize ("O3")
#pragma GCC pop_options

to gain more speed.



Senior III

You did not start the timer, did you?