2022-06-13 08:09 AM
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?
2022-06-13 08:25 AM
You will not get reproducable delays. Flash prefetch for the called function, stack setup inside the called function, bus contention and code alignment will lead to substantial variance.
2022-06-13 08:51 AM
Try change delay func to inline
2022-06-14 12:14 AM
I tried but there is no effect
2022-06-14 12:26 AM
Even without a function called, for exemple using this inside the while doesn't get me results, the minimum I got was 460 ns:
timer_val=__HAL_TIM_GET_COUNTER(&htim1);
while (1)
{
if (__HAL_TIM_GET_COUNTER(&htim1)-timer_val>=1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
timer_val=__HAL_TIM_GET_COUNTER(&htim1);
}
}
So the best way to obtain what I want is in assembly ?
2022-06-14 04:07 AM
Multiple pin operations will prolong that operation in a much more controlled way. However the jump at the end of the loop will eat up a hard controllable number of cycles.
while(1) {
SetPin(pin);
SetPin(pin);
SetPin(pin);
ClearPin(pin);
ClearPin(pin);
}
2022-06-14 06:08 AM
Thank for your response but there is a deeper problem as I tried to see the resolution of the while loop using a LL-Driver instruction to get a more optimised way, but the minimum of the while loop is 344 ns (I saw it on a 100MHz Oscilloscope), I used this code:
LL_GPIO_TogglePin(GPIOA, LL_GPIO_PIN_1);
So how can I increase the while loop frequency?
2022-06-14 06:09 AM
To precise that there is a net performance between LL and HAL as for HAL instruction I got around 500ns
2022-06-14 01:59 PM
Enable compiler optimization.
JW
2022-06-14 02:13 PM
And disable interrupts. Any interrupt occurring during your tight loop (including SysTick) will ruin your tightly coded delays.