1. 硬件复位
硬件复位如下图,直接将RESET引脚拉低即可,如下:
2. 软件复位
软件复位库函数:NVIC_SystemReset();
STM32F1XX系列中,复位函数在core_cm3.h文件中:
/**
* @brief Initiate a system reset request.
*
* Initiate a system reset request to reset the MCU
*/
static __INLINE void NVIC_SystemReset(void)
{
SCB->AIRCR = ((0x5FA << SCB_AIRCR_VECTKEY_Pos) |
(SCB->AIRCR & SCB_AIRCR_PRIGROUP_Msk) |
SCB_AIRCR_SYSRESETREQ_Msk); /* Keep priority group unchanged */
__DSB(); /* Ensure completion of memory access */
while(1); /* wait until reset */
}
STM32F4XX系列中,复位函数在core_cm4.h文件中:
/**
\brief System Reset
\details Initiates a system reset request to reset the MCU.
*/
__STATIC_INLINE void NVIC_SystemReset(void)
{
__DSB(); /* Ensure all outstanding memory accesses included
buffered write are completed before reset */
SCB->AIRCR = (uint32_t)((0x5FAUL << SCB_AIRCR_VECTKEY_Pos) |
(SCB->AIRCR & SCB_AIRCR_PRIGROUP_Msk) |
SCB_AIRCR_SYSRESETREQ_Msk ); /* Keep priority group unchanged */
__DSB(); /* Ensure completion of memory access */
for(;;) /* wait until reset */
{
__NOP();
}
}
在HAL库中又进行了封装,在stm32f4xx_hal_cortex.c中,调用上面和下面两个函数效果是一样的。
/**
* @brief Initiates a system reset request to reset the MCU.
* @retval None
*/
void HAL_NVIC_SystemReset(void)
{
/* System Reset */
NVIC_SystemReset();
}
禁止可屏蔽中断库函数:__set_FAULTMASK(1);
参考网上一些博主说的,在调用复位函数和真正复位之间还有一段延迟,在这段时间单片机还是可以正常处理中断等程序的,为了避免这种情况,应该把相应的中断都屏蔽掉,这里会用到下面这个中断屏蔽相关的函数;
可以注意到这些函数名在M3和M4中都是一样的,M3中函数的定义在core_cm3.h中,如下:
/**
* @brief Set the Fault Mask value
*
* @param faultMask faultMask value
*
* Set the fault mask register
*/
static __INLINE void __set_FAULTMASK(uint32_t faultMask)
{
register uint32_t __regFaultMask __ASM("faultmask");
__regFaultMask = (faultMask & 1);
}
在M3的权威指南中可以看到这个寄存器的功能就是禁止所有的可屏蔽中断,如下:
总结
所在一般如果需要软复位只要调用上面两个库函数即可:
/*
*函数功能:STM32软复位函数
*/
void Stm32_SoftReset(void)
{
__set_FAULTMASK(1);//禁止所有的可屏蔽中断
NVIC_SystemReset();//软件复位
}