cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 HAL_Delay doesn't work properly

DFile.1
Associate II

Hello,

I'm in the process of learning the STM32 MCU. I have the STM32F4DISCOVERYBOARD with a STM32F407VGT on it.

At first I tried the simple LED blink tutorial and noticed that the LED will light up, but won't blink.

I did some more attempts and figured that the HAL_Delay() function doesn't work properly.

These are my configurations in CubeMX; I didn't change anything afterwards in the initialization code generated by CubeMX.

0693W000001sF0SQAU.png0693W000001sEzyQAE.png

0693W000001sF0XQAU.png

Using CubeMX I defined an output pin, wrote a simple code where I can change the on and off time of the pin and connected it to an oscilloscope.

// #1
while(1)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(600);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);	
HAL_Delay(600);	
}

#1

0693W000001sF3qQAE.png#1: Signal stays high and has spike every 480ms

//#2
while(1)
{
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
	HAL_Delay(50);
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);	
	HAL_Delay(10);	
}

0693W000001sF7OQAU.png0693W000001sF7EQAU.png0693W000001sF6zQAE.png

#2: HIGH for 50ms and one impuls lasts for 33ms, LOW for 10ms as defined

I also tried the TooglePin function and also changed the clock frequency randomly, but nothing worked. Do you know where my mistakes is or what I could do in order to make HAL_Delay work properly?

I also read somewhere that the HAL_Delay is used for HAL functions. I guess the problem will affect them as well right?

5 REPLIES 5
TDK
Guru

Did you redefine HAL_Delay? Using FreeRTOS and mistakenly created two threads that do the same thing?

Observe the uwTick variable and see if it's increasing. Set a breakpoint in HAL_IncTick and see if it's called correctly.

Doesn't really make sense to me that "HAL_Delay(600);" would work exactly every other time.

If you feel a post has answered your question, please click "Accept as Solution".
DFile.1
Associate II

I did not redefine HAL_Delay neither used a FreeRTOS.

When I debugged the program using the simulator everything works as expected.

However using the STLink Debugger the program jumps to the following point somtimes:

0693W000001sL6qQAE.png

I also noticed that the impulses that are shorter (e.g. 33ms) than the defines 50ms occur every 478ms, which is the same time periode between the two "spikes" from the first code where HAL_Delay is 600ms.

I guess the Reset_Interrupt is fired every 478ms.

Do you have any ideas how to fix this problem?

KnarfB
Principal III

That's really unusual behaviour. Blinky should work as expected.

What happens if you step-by-step debug through the code?

Can you exclude hardware issues (under voltage/excessive current,...)?

Is it a used board? If so, the hardware watchdog IWDG could be running?

Are you using printf or thelike IO?

There are means to find out the reset cause programmatically (https://stackoverflow.com/questions/34196663/stm32-how-to-get-last-reset-status), but this seems overkill for now.

Attach a buildable project, issue likely outside the code you select to show.

Is PB5 used by anything else on the board?

Is there a watchdog set up?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DFile.1
Associate II

I enabled the IWDG in CubeMX and set the prescaler to 128 and the reload value to 4095. The reset occured every 16ms as expected.

So using STLink utility I checked the WDG_SW in the option bytes settings and this fixed the problem.

0693W000001sOTdQAM.png

I am only a bit confused why the other options are grayed e.g. IWDG_STOP.

Does the WDG_SW checkbox disable both watchdogs (IWDG and WWDG) ? In the UM0892 STLink utility description they only refer to "the watchdog".