C STM32如何获取上次重置状态
我正在使用STM32F427,我想知道上次重置的原因。RCC时钟控制和状态寄存器RCC_CSR带有许多重置标志,但我无法获得任何有意义的值 通过读取该寄存器的值,我只得到0x03,这意味着LSI就绪且LSI打开,但如果我尝试通电、软件复位、低电压等,则不会设置有关复位的标志。我找到了获取复位标志的代码片段,如下所示,但所有标志仍然为0C STM32如何获取上次重置状态,c,arm,embedded,reset,stm32,C,Arm,Embedded,Reset,Stm32,我正在使用STM32F427,我想知道上次重置的原因。RCC时钟控制和状态寄存器RCC_CSR带有许多重置标志,但我无法获得任何有意义的值 通过读取该寄存器的值,我只得到0x03,这意味着LSI就绪且LSI打开,但如果我尝试通电、软件复位、低电压等,则不会设置有关复位的标志。我找到了获取复位标志的代码片段,如下所示,但所有标志仍然为0 if (RCC_GetFlagStatus(RCC_FLAG_SFTRST)) ... 你对如何获得更好的结果有什么建议吗?在读取这些重置标志之前是否需要一些配
if (RCC_GetFlagStatus(RCC_FLAG_SFTRST)) ...
你对如何获得更好的结果有什么建议吗?在读取这些重置标志之前是否需要一些配置
谢谢
if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST))...
将告诉您是否有软件重置
然后别忘了
RCC_ClearFlag();
启动后,在初始化任何其他外围设备之前,尽快读取RCC_CSR。首先初始化系统时钟是安全的(如果使用ST的库,则在SystemInit()中进行初始化)。@floppes建议:
启动后,在初始化任何其他外围设备之前,尽快读取RCC_CSR。首先初始化系统时钟是安全的(如果使用ST的库,则在SystemInit()中完成) 现在,为了确定确切的重置原因,这里有一个完整的函数来帮助您 请注意,所有重置标志都可以在微控制器的重置和时钟控制器(RCC)头文件中找到 例如:“STM32Cube_FW_F2_V1.7.0/Drivers/stm32fxxx_HAL_Driver/Inc/stm32fxxx_HAL_rcc.h” 以下是从“stm32f2x\u HAL\u RCC.h”复制和粘贴的
\u RCC\u GET\u FLAG()
宏及其输入的示例说明。全部的
以下函数中使用的重置标志来自此列表:
/**@是否设置了简要检查RCC标志。*@paramFLAG指定要检查的标志。
*此参数可以是以下值之一:
*@arg RCC\u FLAG\u HSIRDY:HSI振荡器时钟就绪。
*@arg RCC_FLAG_HSERDY:HSE振荡器时钟就绪。
*@arg RCC\u FLAG\u PLLRDY:主PLL时钟就绪。
*@arg RCC_FLAG_PLLI2SRDY:PLLI2S时钟就绪。
*@arg RCC_FLAG_LSERDY:LSE振荡器时钟就绪。
*@arg RCC\u FLAG\u LSIRDY:LSI振荡器时钟就绪。
*@arg RCC_FLAG_BORRST:POR/PDR或BOR重置。
*@arg RCC\u FLAG\u PINRST:引脚重置。
*@arg RCC_FLAG_porst:POR/PDR重置。
*@arg RCC\u FLAG\u SFTRST:软件重置。
*@arg RCC_FLAG_IWDGRST:独立看门狗重置。
*@arg RCC_FLAG_WWDGRST:窗口看门狗重置。
*@arg RCC_FLAG_LPWRRST:低功耗重置。
*@retval标志的新状态(对或错)。
*/
#定义RCC_标志_掩码((uint8_t)0x1FU)
#define(define)HAL(u)RCC(u)RCC(u)RCC(u)RCC(u)GET(u(u)FLAG(u(u)u)u)u)u(RCC)r)r)r)r)))r:(()))RCC)r)r)r;之后。问题是,在任何类型的mcu重置后,没有重置标志被设置为1…谢谢!它解决了我的问题,有flash init、sd卡init和其他一些例程。因为这不是我的代码,我不知道这些函数到底做了什么,但在这一切正常工作之前阅读CSR!谢谢
/// @brief Possible STM32 system reset causes
typedef enum reset_cause_e
{
RESET_CAUSE_UNKNOWN = 0,
RESET_CAUSE_LOW_POWER_RESET,
RESET_CAUSE_WINDOW_WATCHDOG_RESET,
RESET_CAUSE_INDEPENDENT_WATCHDOG_RESET,
RESET_CAUSE_SOFTWARE_RESET,
RESET_CAUSE_POWER_ON_POWER_DOWN_RESET,
RESET_CAUSE_EXTERNAL_RESET_PIN_RESET,
RESET_CAUSE_BROWNOUT_RESET,
} reset_cause_t;
/// @brief Obtain the STM32 system reset cause
/// @param None
/// @return The system reset cause
reset_cause_t reset_cause_get(void)
{
reset_cause_t reset_cause;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST))
{
reset_cause = RESET_CAUSE_LOW_POWER_RESET;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST))
{
reset_cause = RESET_CAUSE_WINDOW_WATCHDOG_RESET;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST))
{
reset_cause = RESET_CAUSE_INDEPENDENT_WATCHDOG_RESET;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST))
{
reset_cause = RESET_CAUSE_SOFTWARE_RESET; // This reset is induced by calling the ARM CMSIS `NVIC_SystemReset()` function!
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST))
{
reset_cause = RESET_CAUSE_POWER_ON_POWER_DOWN_RESET;
}
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST))
{
reset_cause = RESET_CAUSE_EXTERNAL_RESET_PIN_RESET;
}
// Needs to come *after* checking the `RCC_FLAG_PORRST` flag in order to ensure first that the reset cause is
// NOT a POR/PDR reset. See note below.
else if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST))
{
reset_cause = RESET_CAUSE_BROWNOUT_RESET;
}
else
{
reset_cause = RESET_CAUSE_UNKNOWN;
}
// 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();
return reset_cause;
}
// Note: any of the STM32 Hardware Abstraction Layer (HAL) Reset and Clock Controller (RCC) header
// files, such as "STM32Cube_FW_F7_V1.12.0/Drivers/STM32F7xx_HAL_Driver/Inc/stm32f7xx_hal_rcc.h",
// "STM32Cube_FW_F2_V1.7.0/Drivers/STM32F2xx_HAL_Driver/Inc/stm32f2xx_hal_rcc.h", etc., indicate that the
// brownout flag, `RCC_FLAG_BORRST`, will be set in the event of a "POR/PDR or BOR reset". This means that a
// Power-On Reset (POR), Power-Down Reset (PDR), OR Brownout Reset (BOR) will trip this flag. See the
// doxygen just above their definition for the `__HAL_RCC_GET_FLAG()` macro to see this:
// "@arg RCC_FLAG_BORRST: POR/PDR or BOR reset." <== indicates the Brownout Reset flag will *also* be set in
// the event of a POR/PDR.
// Therefore, you must check the Brownout Reset flag, `RCC_FLAG_BORRST`, *after* first checking the
// `RCC_FLAG_PORRST` flag in order to ensure first that the reset cause is NOT a POR/PDR reset.
/// @brief Obtain the system reset cause as an ASCII-printable name string from a reset cause type
/// @param[in] reset_cause The previously-obtained system reset cause
/// @return A null-terminated ASCII name string describing the system reset cause
const char * reset_cause_get_name(reset_cause_t reset_cause)
{
const char * reset_cause_name = "TBD";
switch (reset_cause)
{
case RESET_CAUSE_UNKNOWN:
reset_cause_name = "UNKNOWN";
break;
case RESET_CAUSE_LOW_POWER_RESET:
reset_cause_name = "LOW_POWER_RESET";
break;
case RESET_CAUSE_WINDOW_WATCHDOG_RESET:
reset_cause_name = "WINDOW_WATCHDOG_RESET";
break;
case RESET_CAUSE_INDEPENDENT_WATCHDOG_RESET:
reset_cause_name = "INDEPENDENT_WATCHDOG_RESET";
break;
case RESET_CAUSE_SOFTWARE_RESET:
reset_cause_name = "SOFTWARE_RESET";
break;
case RESET_CAUSE_POWER_ON_POWER_DOWN_RESET:
reset_cause_name = "POWER-ON_RESET (POR) / POWER-DOWN_RESET (PDR)";
break;
case RESET_CAUSE_EXTERNAL_RESET_PIN_RESET:
reset_cause_name = "EXTERNAL_RESET_PIN_RESET";
break;
case RESET_CAUSE_BROWNOUT_RESET:
reset_cause_name = "BROWNOUT_RESET (BOR)";
break;
}
return reset_cause_name;
}
reset_cause_t reset_cause = reset_cause_get();
printf("The system reset cause is \"%s\"\n", reset_cause_get_name(reset_cause));