|
| 1 | +/* |
| 2 | + * Copyright 2025 Core Devices LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "drivers/watchdog.h" |
| 18 | + |
| 19 | +#include "util/bitset.h" |
| 20 | +#include "system/logging.h" |
| 21 | +#include <inttypes.h> |
| 22 | +#include "bf0_hal.h" |
| 23 | + |
| 24 | +#define WDT_RELOADER_TIMEOUT 3 |
| 25 | +#define WDT_REBOOT_TIMEOUT 8 |
| 26 | + |
| 27 | +static WDT_HandleTypeDef hwdt = { |
| 28 | + .Instance = hwp_wdt1, |
| 29 | +}; |
| 30 | + |
| 31 | +static int s_sys_freq = 0; |
| 32 | +static int s_irq_handle = 0; |
| 33 | + |
| 34 | +static HAL_StatusTypeDef wdt_set_timeout(WDT_HandleTypeDef *wdt, uint32_t reload_timeout) { |
| 35 | + HAL_StatusTypeDef status; |
| 36 | + wdt->Init.Reload = s_sys_freq * reload_timeout; |
| 37 | + wdt->Init.Reload2 = s_sys_freq * WDT_REBOOT_TIMEOUT; |
| 38 | + |
| 39 | + status = HAL_WDT_Init(wdt); |
| 40 | + PBL_ASSERTN(status == HAL_OK); |
| 41 | + |
| 42 | + __HAL_SYSCFG_Enable_WDT_REBOOT(1); |
| 43 | + return HAL_OK; |
| 44 | +} |
| 45 | + |
| 46 | +void wdt_reconfig(void) { |
| 47 | + HAL_WDT_Refresh(&hwdt); |
| 48 | + __HAL_WDT_STOP(&hwdt); |
| 49 | +} |
| 50 | + |
| 51 | +void WDT_IRQHandler(void) { |
| 52 | + if (s_irq_handle == 0) { |
| 53 | + s_irq_handle = 1; |
| 54 | + if (__HAL_SYSCFG_Get_Trigger_Assert_Flag() == 0) { |
| 55 | + HAL_HPAON_WakeCore(CORE_ID_LCPU); |
| 56 | + __HAL_SYSCFG_Trigger_Assert(); |
| 57 | + } |
| 58 | + wdt_reconfig(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void watchdog_init(void) { |
| 63 | + HAL_StatusTypeDef status; |
| 64 | + |
| 65 | + if (HAL_PMU_LXT_ENABLED()) { |
| 66 | + s_sys_freq = RC32K_FREQ; |
| 67 | + } else { |
| 68 | + s_sys_freq = RC10K_FREQ; |
| 69 | + } |
| 70 | + |
| 71 | + hwdt.Init.Reload = (uint32_t)WDT_RELOADER_TIMEOUT * s_sys_freq; |
| 72 | + hwdt.Init.Reload2 = (uint32_t)WDT_REBOOT_TIMEOUT * s_sys_freq; |
| 73 | + __HAL_WDT_STOP(&hwdt); |
| 74 | + __HAL_WDT_INT(&hwdt, 1); |
| 75 | + |
| 76 | + status = HAL_WDT_Init(&hwdt); |
| 77 | + PBL_ASSERTN(status == HAL_OK); |
| 78 | + |
| 79 | + __HAL_SYSCFG_Enable_WDT_REBOOT(1); |
| 80 | + return; |
| 81 | +} |
| 82 | + |
| 83 | +void watchdog_start(void) { __HAL_WDT_START(&hwdt); } |
| 84 | + |
| 85 | +void watchdog_feed(void) { HAL_WDT_Refresh(&hwdt); } |
| 86 | + |
| 87 | +bool watchdog_check_reset_flag(void) { return (HAL_PMU_GET_WSR() & PMUC_WSR_WDT1) != 0; } |
| 88 | + |
| 89 | +McuRebootReason watchdog_clear_reset_flag(void) { |
| 90 | + uint32_t wsr = HAL_PMU_GET_WSR(); |
| 91 | + uint32_t boot = SystemPowerOnModeGet(); |
| 92 | + HAL_PMU_CLEAR_WSR(0xFFFFFFFF); |
| 93 | + |
| 94 | + McuRebootReason mcu_reboot_reason = { |
| 95 | + .brown_out_reset = 0, |
| 96 | + .pin_reset = (((wsr & PMUC_WSR_PIN0) != 0) || ((wsr & PMUC_WSR_PIN1) != 0)), |
| 97 | + .power_on_reset = (boot & PM_COLD_BOOT) != 0, |
| 98 | + .software_reset = (boot & PM_REBOOT_BOOT) != 0, |
| 99 | + .independent_watchdog_reset = 0, |
| 100 | + .window_watchdog_reset = (wsr & PMUC_WSR_WDT1) != 0, |
| 101 | + .low_power_manager_reset = 0, |
| 102 | + }; |
| 103 | + |
| 104 | + return mcu_reboot_reason; |
| 105 | +} |
0 commit comments