diff --git a/hal/reset_reason_api.h b/hal/reset_reason_api.h index be1a19d85d..037d2665e2 100644 --- a/hal/reset_reason_api.h +++ b/hal/reset_reason_api.h @@ -20,6 +20,8 @@ #if DEVICE_RESET_REASON +#include + #ifdef __cplusplus extern "C" { #endif @@ -39,8 +41,7 @@ typedef enum { RESET_REASON_UNKNOWN /**< Unknown or unreadable reset reason **/ } reset_reason_t; -/** - * Fetch the reset reason for the last system reset +/** Fetch the reset reason for the last system reset * * Note: Some platforms contain reset reason registers that persist through * system resets. If the registers haven't been cleared before calling this @@ -51,8 +52,16 @@ typedef enum { */ reset_reason_t hal_reset_reason_get(void); -/** - * Clear the reset reason from registers + +/** Fetch the raw platform specific reset reason register value + * + * @return value containing the reset reason register for the given platform. + * If the platform contains reset reasons across multiple registers they + * will be concatenated here. + */ +uint32_t hal_reset_reason_get_raw(void); + +/** Clear the reset reason from registers * * Reset the value of the reset status registers, the reset reason will persist * between system resets on certain platforms so the registers should be cleared diff --git a/platform/mbed_reset_reason.cpp b/platform/mbed_reset_reason.cpp index 5ff5d5aad4..06f30ca24e 100644 --- a/platform/mbed_reset_reason.cpp +++ b/platform/mbed_reset_reason.cpp @@ -26,10 +26,20 @@ reset_reason_t mbed_reset_reason_get(void) // this function resets it. const static reset_reason_t reason = hal_reset_reason_get(); + // Call get raw to cache the reset reason before clearing the registers. + hal_reset_reason_get_raw(); hal_reset_reason_clear(); return reason; } + +uint32_t mbed_reset_reason_get_raw(void) +{ + const static uint32_t reason = hal_reset_reason_get_raw(); + + return reason; +} + } // namespace mbed #endif // DEVICE_RESET_REASON diff --git a/platform/mbed_reset_reason.h b/platform/mbed_reset_reason.h index 507461a32c..1c8b31b607 100644 --- a/platform/mbed_reset_reason.h +++ b/platform/mbed_reset_reason.h @@ -22,6 +22,8 @@ #include "reset_reason_api.h" +#include + namespace mbed { /** \addtogroup platform */ /** @{*/ @@ -44,6 +46,23 @@ namespace mbed { */ reset_reason_t mbed_reset_reason_get(void); + +/** Get the platform specific reason code for the last system reset. + * + * Platform specific reasons that are not covered by the reset_reason_t enum + * will cause the mbed_reset_reason_get function to return RESET_REASON_PLATFORM + * In order to get the actual reason the register value must be fetched directly + * using this function and interpreted in a platform specific manner. + * + * Example: + * @code + * if (mbed_reset_reason_get() == RESET_REASON_PLATFORM) { + * const uint32_t platform_reason = mbed_reset_reason_get_raw(); + * } + * @endcode + */ +uint32_t mbed_reset_reason_get_raw(void); + /**@}*/ /**@}*/ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/reset_reason.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/reset_reason.c index d8c8bedc88..924f0f1921 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/reset_reason.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/reset_reason.c @@ -74,6 +74,13 @@ reset_reason_t hal_reset_reason_get(void) return RESET_REASON_UNKNOWN; } + +uint32_t hal_reset_reason_get_raw(void) +{ + return (RCM_GetPreviousResetSources(RCM) & kRCM_SourceAll); +} + + void hal_reset_reason_clear(void) { #if (defined(FSL_FEATURE_RCM_HAS_SSRS) && FSL_FEATURE_RCM_HAS_SSRS) diff --git a/targets/TARGET_STM/reset_reason.c b/targets/TARGET_STM/reset_reason.c index 0d3ee42de3..3b7a82863c 100644 --- a/targets/TARGET_STM/reset_reason.c +++ b/targets/TARGET_STM/reset_reason.c @@ -51,6 +51,13 @@ reset_reason_t hal_reset_reason_get(void) return RESET_REASON_UNKNOWN; } + +uint32_t hal_reset_reason_get_raw(void) +{ + return RCC->CSR; +} + + void hal_reset_reason_clear(void) { __HAL_RCC_CLEAR_RESET_FLAGS();