Skip to content

Commit

Permalink
[nrf toup] Saving boot reason on nRF54h20
Browse files Browse the repository at this point in the history
-Use NRF_RESETINFO register to get boot reason

Signed-off-by: Konrad Grucel <[email protected]>
  • Loading branch information
kg-nordicsemi committed Dec 10, 2024
1 parent 6c1a9c5 commit cae80de
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/platform/SaveBootReasonDFUSuit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef NRF_SAVE_BOOT_REASON_H__
#define NRF_SAVE_BOOT_REASON_H__

inline int SoftwareBootReasonSUIT __attribute__((section(".noinit")));

inline int getSoftwareRebootReasonSUIT()
{
return SoftwareBootReasonSUIT;
}

inline void setSoftwareRebootReasonSUIT(int reason)
{
SoftwareBootReasonSUIT = reason;
}

#endif
52 changes: 51 additions & 1 deletion src/platform/Zephyr/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,66 @@ const size_t kMaxHeapSize = CONFIG_SRAM_BASE_ADDRESS + KB(CONFIG_SRAM_SIZE) - PO

#endif

#ifdef CONFIG_SOC_SERIES_NRF54HX
#include <hal/nrf_resetinfo.h>
#include <platform/SaveBootReasonDFUSuit.h>
#endif

namespace chip {
namespace DeviceLayer {

namespace {

BootReasonType DetermineBootReason()
{
#ifdef CONFIG_HWINFO

#if defined(CONFIG_SOC_SERIES_NRF54HX) || defined(CONFIG_HWINFO)
uint32_t reason;
#endif

#ifdef CONFIG_SOC_SERIES_NRF54HX

bool isSoftwareBootReasonSUITInitialized = false;
if (!isSoftwareBootReasonSUITInitialized)
{
setSoftwareRebootReasonSUIT(0);
isSoftwareBootReasonSUITInitialized = true;
}

reason = nrf_resetinfo_resetreas_global_get(NRF_RESETINFO);

if (reason == RESETINFO_RESETREAS_GLOBAL_ResetValue)
{
return BootReasonType::kSoftwareReset;
}

if (reason & RESETINFO_RESETREAS_GLOBAL_RESETPORONLY_Msk)
{
return BootReasonType::kBrownOutReset;
}

if (reason & RESETINFO_RESETREAS_GLOBAL_DOG_Msk)
{
return BootReasonType::kHardwareWatchdogReset;
}

if ((reason & (RESETINFO_RESETREAS_GLOBAL_RESETPIN_Msk | RESETINFO_RESETREAS_GLOBAL_RESETPOR_Msk)) ==
(RESETINFO_RESETREAS_GLOBAL_RESETPIN_Msk | RESETINFO_RESETREAS_GLOBAL_RESETPOR_Msk))
{
return BootReasonType::kPowerOnReboot;
}

if ((reason & (RESETINFO_RESETREAS_GLOBAL_RESETPOR_Msk | RESETINFO_RESETREAS_GLOBAL_SECSREQ_Msk)) ==
(RESETINFO_RESETREAS_GLOBAL_RESETPOR_Msk | RESETINFO_RESETREAS_GLOBAL_SECSREQ_Msk))
{
if (GetSoftwareRebootReason() == SoftwareRebootReason::kSoftwareUpdate)
{
return BootReasonType::kSoftwareUpdateCompleted;
}
}
#endif

#ifdef CONFIG_HWINFO
if (hwinfo_get_reset_cause(&reason) != 0)
{
return BootReasonType::kUnspecified;
Expand Down
5 changes: 5 additions & 0 deletions src/platform/nrfconnect/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
#include <zephyr/logging/log.h>
#include <zephyr/pm/device.h>

#ifdef CONFIG_SOC_SERIES_NRF54HX
#include <platform/SaveBootReasonDFUSuit.h>
#endif

namespace chip {
namespace {
#ifdef CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE
Expand Down Expand Up @@ -182,6 +186,7 @@ CHIP_ERROR OTAImageProcessorImpl::Apply()
PlatformMgr().HandleServerShuttingDown();
k_msleep(CHIP_DEVICE_CONFIG_SERVER_SHUTDOWN_ACTIONS_SLEEP_MS);
#ifdef CONFIG_DFU_TARGET_SUIT
SetSoftwareRebootReason(SoftwareRebootReason::kSoftwareUpdate);
dfu_target_suit_reboot();
#else
Reboot(SoftwareRebootReason::kSoftwareUpdate);
Expand Down
35 changes: 33 additions & 2 deletions src/platform/nrfconnect/Reboot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
#include <hal/nrf_power.h>
#endif

#include <platform/SaveBootReasonDFUSuit.h>

namespace chip {
namespace DeviceLayer {

#if defined(CONFIG_ARCH_POSIX) || defined(CONFIG_SOC_SERIES_NRF54HX)
#if defined(CONFIG_ARCH_POSIX)

void Reboot(SoftwareRebootReason reason)
{
Expand All @@ -40,7 +42,7 @@ SoftwareRebootReason GetSoftwareRebootReason()
return SoftwareRebootReason::kOther;
}

#else
#elif !(defined(CONFIG_SOC_SERIES_NRF54HX))

using RetainedReason = decltype(nrf_power_gpregret_get(NRF_POWER, 0));

Expand Down Expand Up @@ -73,6 +75,35 @@ SoftwareRebootReason GetSoftwareRebootReason()
}
}

#else

using RetainedReason = decltype(getSoftwareRebootReasonSUIT());

constexpr RetainedReason EncodeReason(SoftwareRebootReason reason)
{
// Set MSB to avoid collission with Zephyr's pre-defined reboot reasons.
constexpr RetainedReason kCustomReasonFlag = 0x80;

return static_cast<RetainedReason>(reason) | kCustomReasonFlag;
}

void SetSoftwareRebootReason(SoftwareRebootReason reason)
{
const RetainedReason retainedReason = EncodeReason(reason);
setSoftwareRebootReasonSUIT(retainedReason);
}

SoftwareRebootReason GetSoftwareRebootReason()
{
switch (getSoftwareRebootReasonSUIT())
{
case EncodeReason(SoftwareRebootReason::kSoftwareUpdate):
setSoftwareRebootReasonSUIT(0);
return SoftwareRebootReason::kSoftwareUpdate;
default:
return SoftwareRebootReason::kOther;
}
}
#endif

} // namespace DeviceLayer
Expand Down
1 change: 1 addition & 0 deletions src/platform/nrfconnect/Reboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum class SoftwareRebootReason : uint8_t

[[noreturn]] void Reboot(SoftwareRebootReason reason);
SoftwareRebootReason GetSoftwareRebootReason();
void SetSoftwareRebootReason(SoftwareRebootReason reason);

} // namespace DeviceLayer
} // namespace chip

0 comments on commit cae80de

Please sign in to comment.