57 lines
1.6 KiB
C
57 lines
1.6 KiB
C
#include <zephyr/kernel.h>
|
|
#include <zephyr/logging/log.h>
|
|
#include <zephyr/sys/printk.h>
|
|
#include <zephyr/logging/log_ctrl.h>
|
|
#include <zephyr/sys/reboot.h>
|
|
|
|
#include <settings.h>
|
|
|
|
#if IS_ENABLED(CONFIG_SOC_SERIES_NRF52X)
|
|
#include <hal/nrf_power.h>
|
|
#elif IS_ENABLED(CONFIG_SOC_SERIES_STM32G0X)
|
|
#include <stm32_ll_rtc.h>
|
|
#endif
|
|
|
|
LOG_MODULE_REGISTER(utils, LOG_LEVEL_DBG);
|
|
|
|
/* Wir nutzen Register 0 für unseren Reboot-Status */
|
|
#define REBOOT_STATUS_REG_IDX 0
|
|
|
|
void reboot_with_status(uint8_t status)
|
|
{
|
|
app_settings_save_pending_now();
|
|
#if IS_ENABLED(CONFIG_SOC_SERIES_NRF52X)
|
|
/* Korrigierter Aufruf mit Register-Index 0 */
|
|
nrf_power_gpregret_set(NRF_POWER, REBOOT_STATUS_REG_IDX, (uint32_t)status);
|
|
#elif IS_ENABLED(CONFIG_SOC_SERIES_STM32G0X)
|
|
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, status);
|
|
#endif
|
|
|
|
LOG_INF("Setting Reboot-Status 0x%02x and resetting...", status);
|
|
|
|
k_sleep(K_MSEC(100));
|
|
LOG_PANIC();
|
|
|
|
sys_reboot(SYS_REBOOT_COLD);
|
|
}
|
|
|
|
uint8_t get_reboot_status(void)
|
|
{
|
|
uint8_t status = 0;
|
|
#if IS_ENABLED(CONFIG_SOC_SERIES_NRF52X)
|
|
/* Korrigierter Aufruf mit Register-Index 0 */
|
|
status = (uint8_t)nrf_power_gpregret_get(NRF_POWER, REBOOT_STATUS_REG_IDX);
|
|
/* Register nach dem Lesen löschen */
|
|
nrf_power_gpregret_set(NRF_POWER, REBOOT_STATUS_REG_IDX, 0);
|
|
#elif IS_ENABLED(CONFIG_SOC_SERIES_STM32G0X)
|
|
status = (uint8_t)LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0);
|
|
/* Register nach dem Lesen löschen */
|
|
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, 0);
|
|
#endif
|
|
|
|
if (status != 0) {
|
|
printk("Reboot status detected: 0x%02x\n", status);
|
|
}
|
|
return status;
|
|
}
|