How to Reset STM32 MCU Programatically?
While the STM32 controller is reset at the power cycle, sometimes it is needed to reset the controller programmatically. This is needed in the process of severe system failure, misconfiguration of internal hardware, or during the firmware update process. So, how can we reset the STM32 Microcontroller programmatically?
The answer to the question on How to Reset the STM32 Microcontroller is the following. The correct function for this is NVIC_SystemReset(). Call this function from any place of code and it will never return, but reset the controller. The alternate function is HAL_NVIC_SystemReset(), just an alias for the above.
Here is the sample code of how to invoke STM32 system reset at a Hard_Fault occasion:
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
NVIC_SystemReset();
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}
The realization of the function is dependant on the core, so one should look to core_cm0.h - core_cm7.h files for the implementation, if needed.