基于库的STM32软件复位(库V3.5)
在官方软件库的 core_cm3.h 文件里直接提供了系统复位的函数 :
/* ################################## Reset function ############################################ */
/**
* @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 */
}
但是Cortex-M3权威指南中提到一个要注意的问题:
从SYSRESETREQ 被置为有效,到复位发生器执行复位命令, 往往会有一个延时。在此延时期间,处理器仍然可以响应中断请求。但我们的本意往往是要 让此次执行到此为止,不要再做任何其它事情了。所以,最好在发出复位请求前,先把 FAULTMASK 置位。所以最好在复位前将FAULTMASK 置位。
在官方软件库的 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);
}
最后,通过调用整合的SoftReset函数即可进行软件复位了:
/**
* @brief Reset the mcu by software
*
* @param none
*
* @note use the 3.5 version of the firmware library.
*/
void SoftReset(void)
{
__set_FAULTMASK(1); // 关闭所有中断
NVIC_SystemReset(); // 复位
}