stm32_查找复位的原因
第一步:加入库函数,使用官方库原来就有的可以直接调用
/**
* @brief Checks whether the specified RCC flag is set or not.
* @param RCC_FLAG: specifies the flag to check.
*
* For @b STM32_Connectivity_line_devices, this parameter can be one of the
* following values:
* @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready
* @arg RCC_FLAG_HSERDY: HSE oscillator clock ready
* @arg RCC_FLAG_PLLRDY: PLL clock ready
* @arg RCC_FLAG_PLL2RDY: PLL2 clock ready
* @arg RCC_FLAG_PLL3RDY: PLL3 clock ready
* @arg RCC_FLAG_LSERDY: LSE oscillator clock ready
* @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready
* @arg RCC_FLAG_PINRST: Pin reset
* @arg RCC_FLAG_PORRST: POR/PDR reset
* @arg RCC_FLAG_SFTRST: Software reset
* @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
* @arg RCC_FLAG_WWDGRST: Window Watchdog reset
* @arg RCC_FLAG_LPWRRST: Low Power reset
*
* For @b other_STM32_devices, this parameter can be one of the following values:
* @arg RCC_FLAG_HSIRDY: HSI oscillator clock ready
* @arg RCC_FLAG_HSERDY: HSE oscillator clock ready
* @arg RCC_FLAG_PLLRDY: PLL clock ready
* @arg RCC_FLAG_LSERDY: LSE oscillator clock ready
* @arg RCC_FLAG_LSIRDY: LSI oscillator clock ready
* @arg RCC_FLAG_PINRST: Pin reset
* @arg RCC_FLAG_PORRST: POR/PDR reset
* @arg RCC_FLAG_SFTRST: Software reset
* @arg RCC_FLAG_IWDGRST: Independent Watchdog reset
* @arg RCC_FLAG_WWDGRST: Window Watchdog reset
* @arg RCC_FLAG_LPWRRST: Low Power reset
*
* @retval The new state of RCC_FLAG (SET or RESET).
*/
FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
{
uint32_t tmp = 0;
uint32_t statusreg = 0;
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_RCC_FLAG(RCC_FLAG));
/* Get the RCC register index */
tmp = RCC_FLAG >> 5;
if (tmp == 1) /* The flag to check is in CR register */
{
statusreg = RCC->CR;
}
else if (tmp == 2) /* The flag to check is in BDCR register */
{
statusreg = RCC->BDCR;
}
else /* The flag to check is in CSR register */
{
statusreg = RCC->CSR;
}
/* Get the flag position */
tmp = RCC_FLAG & 0x1f;
if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
/* Return the flag status */
return bitstatus;
}
第二步:在复位初始化代码中加入打印复位源的代码
if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
//这是上电复位
NRF_LOG_INFO("1");
}
if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
//这是外部RST管脚复位
NRF_LOG_INFO("2");
}
if (RCC_GetFlagStatus(RCC_FLAG_SFTRST)!= RESET)
{
NRF_LOG_INFO("3");
}
if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST)!= RESET)
{
NRF_LOG_INFO("4");
}
if (RCC_GetFlagStatus(RCC_FLAG_WWDGRST)!= RESET)
{
NRF_LOG_INFO("5");
}
if (RCC_GetFlagStatus(RCC_FLAG_LPWRRST)!= RESET)
{
NRF_LOG_INFO("6");
}
if (RCC_GetFlagStatus(RCC_FLAG_OBLRST)!= RESET)
{
NRF_LOG_INFO("7");
}
RCC->CSR |= CSR_RMVF_Set;//清除RCC中复位标志
若提示CSR_RMVF_Set未定义则在前面定义
#define CSR_RMVF_Set ((uint32_t)0x01000000)
这样就能够找到使单片机复位的原因,不同原因的标志位可以点进去看,里面还会有详细的解释。