uint16_t reset_cause =0;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST))
{
reset_cause = 1;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST))
{
reset_cause = 2;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST))
{
reset_cause = 3;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST))
{
reset_cause = 4; // This reset is induced by calling the ARM CMSIS `NVIC_SystemReset()` function!
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST))
{
reset_cause = 5;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST))
{
reset_cause = 6;
}
else
{
reset_cause = 7;
}
// Clear all the reset flags or else they will remain set during future resets until system power is fully removed.
__HAL_RCC_CLEAR_RESET_FLAGS();