Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nRF Desktop Failsafe module: migration to the Zephyr HWinfo driver and activation on the nRF54H20 DK #19142

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CONFIG_DESKTOP_BLE_SECURITY_FAIL_TIMEOUT_S=10
CONFIG_DESKTOP_BLE_LOW_LATENCY_LOCK=y

CONFIG_DESKTOP_WATCHDOG_ENABLE=y
CONFIG_DESKTOP_FAILSAFE_ENABLE=y

CONFIG_DESKTOP_CONFIG_CHANNEL_ENABLE=y
CONFIG_DESKTOP_CONFIG_CHANNEL_DFU_ENABLE=y
Expand Down
6 changes: 6 additions & 0 deletions applications/nrf_desktop/doc/failsafe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ Additionally, make sure that the following options are set as follows:
This is to ensure that the device will be blocked after a fatal error and then the watchdog will trigger the reboot.

After the reboot caused either by the watchdog or by the CPU lockup, the failsafe module erases the settings partition and clears the non-volatile settings data.

Implementation details
**********************

The failsafe module uses the Zephyr :ref:`zephyr:hwinfo_api` driver.
The module gets the reset reason information with the :c:func:`hwinfo_get_reset_cause` function and clears it with the :c:func:`hwinfo_clear_reset_cause` function.
1 change: 1 addition & 0 deletions applications/nrf_desktop/src/modules/Kconfig.failsafe
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ config DESKTOP_FAILSAFE_ENABLE
bool "Enable failsafe"
depends on WATCHDOG
depends on !RESET_ON_FATAL_ERROR
select HWINFO
help
When a device is rebooted by watchdog or due to the CPU lockup,
the settings partition will be erased.
Expand Down
57 changes: 43 additions & 14 deletions applications/nrf_desktop/src/modules/failsafe.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

#include <zephyr/kernel.h>
#include <helpers/nrfx_reset_reason.h>
#include <zephyr/drivers/hwinfo.h>
#include <zephyr/storage/flash_map.h>

#define MODULE failsafe
Expand All @@ -15,17 +15,27 @@
LOG_MODULE_REGISTER(MODULE, CONFIG_DESKTOP_FAILSAFE_LOG_LEVEL);


static bool failsafe_check(void)
static int failsafe_check(bool *failure_detected)
{
uint32_t mask = NRFX_RESET_REASON_DOG_MASK |
NRFX_RESET_REASON_LOCKUP_MASK;
int err;
uint32_t reas = 0;
static const uint32_t mask = RESET_WATCHDOG | RESET_CPU_LOCKUP;

uint32_t reas = nrfx_reset_reason_get();
err = hwinfo_get_reset_cause(&reas);
if (err) {
LOG_ERR("Failed to fetch reset cause: %d", err);
return err;
}

*failure_detected = ((reas & mask) != 0);

return (reas & mask) != 0;
LOG_INF("Reset reason (0x%08X) trigger %s", reas,
*failure_detected ? "active" : "inactive");

return err;
}

static void failsafe_erase(void)
static int failsafe_erase(void)
{
const struct flash_area *flash_area;
int err = flash_area_open(FIXED_PARTITION_ID(storage_partition),
Expand All @@ -37,15 +47,24 @@ static void failsafe_erase(void)
}

if (err) {
LOG_ERR("Failsafe cannot erase settings");
LOG_ERR("Failsafe cannot erase settings: %d", err);
} else {
LOG_WRN("Failsafe erased settings");
}

return err;
}

static void failsafe_clear(void)
static int failsafe_clear(void)
{
nrfx_reset_reason_clear(nrfx_reset_reason_get());
int err;

err = hwinfo_clear_reset_cause();
if (err) {
LOG_ERR("Failed to clear reset cause: %d", err);
}

return err;
}

static bool app_event_handler(const struct app_event_header *aeh)
Expand All @@ -55,17 +74,27 @@ static bool app_event_handler(const struct app_event_header *aeh)
cast_module_state_event(aeh);

if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
int err;
bool failure_detected;
static bool initialized;

__ASSERT_NO_MSG(!initialized);
initialized = true;

if (failsafe_check()) {
failsafe_erase();
err = failsafe_check(&failure_detected);
if (!err && failure_detected) {
err = failsafe_erase();
}
failsafe_clear();

module_set_state(MODULE_STATE_READY);
if (!err) {
err = failsafe_clear();
}

if (!err) {
module_set_state(MODULE_STATE_READY);
} else {
module_set_state(MODULE_STATE_ERROR);
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ nRF5340 Audio
nRF Desktop
-----------

|no_changes_yet_note|
* Updated:

* The :ref:`nrf_desktop_failsafe` to use the Zephyr :ref:`zephyr:hwinfo_api` driver for getting and clearing the reset reason information (see the :c:func:`hwinfo_get_reset_cause` and :c:func:`hwinfo_clear_reset_cause` functions).
The Zephyr :ref:`zephyr:hwinfo_api` driver replaces the nrfx reset reason helper dependency (see the :c:func:`nrfx_reset_reason_get` and :c:func:`nrfx_reset_reason_clear` functions).
* The release configuration for the :ref:`zephyr:nrf54h20dk_nrf54h20` board target to enable the :ref:`nrf_desktop_failsafe` (see the :ref:`CONFIG_DESKTOP_FAILSAFE_ENABLE <config_desktop_app_options>` Kconfig option).

nRF Machine Learning (Edge Impulse)
-----------------------------------
Expand Down
Loading