cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get minimum nanosecond delay with a Nucleo F446RE using a Cortex M4 180 Mhz?

YSall.1
Senior

Hi everyone,

I am using a nucleo F446RE using a Cortex M4 180 Mhz, and I have a problem when I try to get nano second delays.

void delay (uint16_t delay)
 
{
 
	__HAL_TIM_SET_COUNTER(&htim1,0);
 
	while(__HAL_TIM_GET_COUNTER(&htim1)<delay);
 
 
 
}

In the main, while(1):

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
 
	 delay(1);

I used this function to set the delay according to the clock. I set up the APB2 clock corresponding to the timer 1 at 100 MHz, logically the function delay(1) would get me a delay of 10 ns, but the problem is that I only have a minimum value of 530 ns delay.

What would be a better method to get a minimum delay without using assembly language?

26 REPLIES 26
For minimum PWM
htim1.Init.Period = 2;
...
sConfigOC.Pulse = 1;
Read and learn about TIM PWM...

It wold be helpful to know the "bigger picture".

Are you trying to generate one single pulse (or a fixed number of pulses) on that one pin as fast and accurately as possible, during which the stm32 won't be required to do anything else?

Or is this just a proof-of-concept but you actually want to generate an arbitrary pattern onto multiple pins?

stm32 are complex microcontrollers with lots of optimisations to make processing = calculations very fast at amazingly low power-consumption, but that can mean the GPIO pins are "distant" (i.e. slow to read / write), and that timings are unpredictable.

Equally the C language you are programming in makes it easy to write large programs with low risk of error. But each C-language instruction might be translated into many assembly-language instructions. If you have specific simple things that you need to do as predictably and quickly as possible, it can be better to drop into assembly-language for those.

For the arbitrary-pattern-onto-multiple-pins, you might consider setting up a timer to trigger DMA transfers to a BSRR register of a port. Once set up, that can be left to run while your C code goes away and does other things.

@Danish​ 

I'm actually in an internship in a company and I'm working on a project to generate TTL signals that we can manually set up in terms of delay. There is already a software that generate the chronogram and we already have a TTL generator that can process the information sent by the software and creating the desired signals. The aim of the project is to upgrade the current one.

But in the current one they used a PIC and they programmed in assembly to generate those signals. The subject of the project is to see if it's possible to programm in C# a TTL generator that has a resolution that is inferior to 500ns, or are we forced to program in assembly to achieve that?

Btw do you know how to start an assembly project on STM32CUBE IDE, just to test some functions?

Do you know a manner to achieve for example a 100ns determinist signal resolution without using assembly?

Do you know another microcontroler that has better output speed so I can play with the timers to have the desired deterministic signals?

MM..1
Chief II

STM F4 can do your job explained generator with 11,1ns granularity and use timer + pwm + DMA

For perfect timing you need HSE.

AN4776

Application note General-purpose timer cookbook for STM32 microcontrollers

Thank you

I will try, knowing that my goal is to reach a 100 ns resolution delay