STM32 软重启失败解决方法

常规重启方法

__set_FAULTMASK(1); //关闭所有中断 
 NVIC_SystemReset(); //复位

找不到 __set_FAULTMASK 函数定义

需要在头文件定义

__STATIC_INLINE void Set_FAULTMASK(uint32_t faultMask)
{
  register uint32_t __regFaultMask       __ASM("faultmask");
  __regFaultMask = (faultMask & (uint32_t)1U);
}

NVIC_SystemReset 软重启 失败的解决方法

找到 NVIC_SystemReset 函数的定义,

#define NVIC_SystemReset            __NVIC_SystemReset

继续跳转到 __NVIC_SystemReset 的定义

__NO_RETURN __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();
  }
}

其中的 SCB_AIRCR_SYSRESETREQ_Msk 修改为 SCB_AIRCR_VECTRESET_Msk 即可。

经验分享 程序员 微信小程序 职场和发展