cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Blue pill doesn't execute code on power up, execution starts after i press reset

Mshar.2
Associate II

Hello,

I'm using stm32 blue pill and my code communicates with several sensor using UART and displays it on LCD using i2c. My code works perfectly fine on debug.

But when i power it again, it doesn't start execution. Although power LED is ON but code execution doesn't start. It only starts after i press reset button.

I read some other solutions saying changing capacitor next to reset button, or connecting BOOT0 to GND. but I'm not sure how to do it.

Can someone explain how can i do it through software.

Similar - https://community.st.com/s/question/0D50X0000ArSX5ISQW/why-my-mcu-doesnt-work-when-powerup-it-only-starts-code-if-i-do-a-reset

8 REPLIES 8
Mshar.2
Associate II

I can share a turn around until someone suggests a proper solution.

The root cause is that if the power doesn't rise as sharply, SystemClock_Config(); won't be able to initialize clocks and hence program won't run until reset manually.

So I gave a small delay before SystemClock_Config(); and now code executes automatically even when i connect to external power source.

0693W00000YAHUEQA5.png

Peter BENSCH
ST Employee

Regardless of the fact that fakes of the STM32F103 are usually equipped with so-called Blue Pill boards, the behaviour of which varies greatly depending on the counterfeiter: a loop like this

for(i=0;i<50000;i++){}

will normally disappear without a trace during optimisation because it does not perform any meaningful action. One used something like this as a time loop in the early years of MCUs, but this is very quickly recognised by the optimisation functions of the C compilers and rightly removed - if one does not deactivate or limit them.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
ONadr.1
Senior III

My experience is that this is usually caused by the crystal oscillator being slow to ramp up. The MCU then switches to the internal oscillator and the code ends up in an endless "ErrorCode" loop

Mshar.2
Associate II

Hello @ONadr.1​ 

Can you suggest some solution for it??

Mshar.2
Associate II

@Peter BENSCH​ 

Can i use HAL_Delay(100); over here? or will it be optimized too?

0693W00000YAOSZQA5.png 

Because I can't press reset button every time since MCU will be inside casing..

Peter BENSCH
ST Employee

Delay loops will not solve the problem, because as @ONadr.1​ also wrote, the issue is probably caused by unsuccessful start of the crystal initialisation. It might help to test other maximum times for the start of LSE and HSE in this routine.

However, it is impossible for STMicroelectronics to make statements about an STM32F103, which is almost certainly a fake (feel free to prove otherwise) and may well behave differently from an original part.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
ONadr.1
Senior III

You can try insert reset into ErrorHandler routine

void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
 HAL_NVIC_SystemReset(); //this can solve the problem, but not the root of problem
  /* USER CODE END Error_Handler_Debug */
}

worked for me thanks.