5 #include "sw/device/tests/pwrmgr_sleep_resets_lib.h"
24 #include "sw/device/lib/runtime/irq.h"
26 #include "sw/device/lib/testing/alert_handler_testutils.h"
27 #include "sw/device/lib/testing/aon_timer_testutils.h"
28 #include "sw/device/lib/testing/pwrmgr_testutils.h"
29 #include "sw/device/lib/testing/rstmgr_testutils.h"
30 #include "sw/device/lib/testing/rv_plic_testutils.h"
31 #include "sw/device/lib/testing/test_framework/check.h"
38 kWdogBarkMicros < kWdogBiteMicros &&
39 kWdogBarkMicros > kEscalationPhase0Micros &&
40 kWdogBarkMicros < (kEscalationPhase0Micros + kEscalationPhase1Micros) &&
41 kWdogBiteMicros < (kEscalationPhase0Micros + kEscalationPhase1Micros),
42 "The wdog bark and bite should happen during the escalation phase 1");
46 dif_alert_handler_t *alert_handler;
47 dif_aon_timer_t *aon_timer;
49 dif_sysrst_ctrl_t *sysrst_ctrl_aon;
53 dif_rv_plic_t plic_actual;
54 dif_alert_handler_t alert_handler_actual;
55 dif_aon_timer_t aon_timer_actual;
56 dif_pwrmgr_t pwrmgr_actual;
57 dif_sysrst_ctrl_t sysrst_ctrl_aon_actual;
58 dif_rstmgr_t rstmgr_actual;
60 void init_peripherals(
void) {
65 pwrmgr = &pwrmgr_actual;
68 CHECK_DIF_OK(dif_sysrst_ctrl_init(
70 &sysrst_ctrl_aon_actual));
71 sysrst_ctrl_aon = &sysrst_ctrl_aon_actual;
77 rstmgr = &rstmgr_actual;
80 CHECK_DIF_OK(dif_aon_timer_init(
83 aon_timer = &aon_timer_actual;
89 flash_ctrl = &flash_ctrl_actual;
92 CHECK_DIF_OK(dif_rv_plic_init(
96 rv_plic_testutils_irq_range_enable(
101 CHECK_DIF_OK(dif_alert_handler_init(
103 &alert_handler_actual));
104 alert_handler = &alert_handler_actual;
107 void config_alert_handler(
void) {
111 uint32_t cycles[4] = {0};
112 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
113 kEscalationPhase0Micros, &cycles[0]));
114 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
115 kEscalationPhase1Micros, &cycles[1]));
116 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(
117 kEscalationPhase2Micros, &cycles[2]));
118 CHECK_STATUS_OK(alert_handler_testutils_get_cycles_from_us(10, &cycles[3]));
124 cycles[0] * alert_handler_testutils_cycle_rescaling_factor()},
128 cycles[1] * alert_handler_testutils_cycle_rescaling_factor()},
132 cycles[2] * alert_handler_testutils_cycle_rescaling_factor()}};
136 .accumulator_threshold = 0,
137 .irq_deadline_cycles =
138 cycles[3] * alert_handler_testutils_cycle_rescaling_factor(),
139 .escalation_phases = esc_phases,
140 .escalation_phases_len =
ARRAYSIZE(esc_phases),
147 .alert_classes = alert_classes,
150 .class_configs = class_config,
152 .ping_timeout = kAlertHandlerTestutilsDefaultPingTimeout,
155 CHECK_STATUS_OK(alert_handler_testutils_configure_all(alert_handler, config,
158 CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
167 kDifPwrmgrResetRequestSourceOne,
169 LOG_INFO(
"Reset Request SourceOne is set");
177 .detection_time_threshold = 10,
179 .embedded_controller_reset_duration = 10};
190 CHECK_DIF_OK(dif_pinmux_init(
194 sysrst_ctrl_aon, sysrst_ctrl_input_change_config));
200 void config_wdog(uint64_t bark_micros, uint64_t bite_micros) {
201 uint32_t bark_cycles = 0;
203 aon_timer_testutils_get_aon_cycles_32_from_us(bark_micros, &bark_cycles));
204 uint32_t bite_cycles = 0;
206 aon_timer_testutils_get_aon_cycles_32_from_us(bite_micros, &bite_cycles));
208 LOG_INFO(
"Wdog will bark after %u microseconds (%u aon cycles)",
209 (uint32_t)bark_micros, bark_cycles);
210 LOG_INFO(
"Wdog will bite after %u microseconds (%u aon cycles)",
211 (uint32_t)bite_micros, bite_cycles);
214 CHECK_STATUS_OK(aon_timer_testutils_watchdog_config(aon_timer, bark_cycles,
215 bite_cycles,
false));
218 kDifPwrmgrResetRequestSourceTwo,
222 void trigger_escalation(
void) {
226 kWdogBarkMicros * alert_handler_testutils_cycle_rescaling_factor(),
227 kWdogBiteMicros * alert_handler_testutils_cycle_rescaling_factor());
229 dif_pwrmgr_alert_t alert = kDifPwrmgrAlertFatalFault;
230 CHECK_DIF_OK(dif_pwrmgr_alert_force(pwrmgr, alert));
234 CHECK(
false,
"Timeout waiting for escalation to occur.");
237 void prepare_for_wdog(pwrmgr_sleep_resets_lib_modes_t mode) {
238 if (mode == kPwrmgrSleepResetsLibModesActive) {
242 bool deep_sleep = mode == kPwrmgrSleepResetsLibModesDeepSleep;
246 : kDifPwrmgrDomainOptionUsbClockInLowPower |
249 kDifPwrmgrDomainOptionMainPowerInLowPower;
253 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
254 pwrmgr, kDifPwrmgrWakeupRequestSourceTwo, config));
258 CHECK(
false,
"Failed to reset!");
261 void prepare_for_sysrst(pwrmgr_sleep_resets_lib_modes_t mode) {
262 if (mode == kPwrmgrSleepResetsLibModesActive) {
263 LOG_INFO(
"Sysrst reset in active mode");
267 bool deep_sleep = mode == kPwrmgrSleepResetsLibModesDeepSleep;
271 : kDifPwrmgrDomainOptionUsbClockInLowPower |
274 kDifPwrmgrDomainOptionMainPowerInLowPower;
275 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
276 pwrmgr, kDifPwrmgrWakeupRequestSourceOne, config));
280 LOG_INFO(
"Sysrst reset in %s sleep mode", deep_sleep ?
"deep" :
"normal");
285 CHECK(
false,
"Failed to reset!");
288 void ottf_external_isr(uint32_t *exc_info) {
301 (dif_aon_timer_irq_t)(irq_id -
307 kDifAlertHandlerClassA));
308 CHECK_DIF_OK(dif_aon_timer_irq_acknowledge(aon_timer, irq));
311 "AON Timer Wdog should not bark");
322 alert_handler, kDifAlertHandlerClassA, &state));
326 CHECK_DIF_OK(dif_alert_handler_irq_acknowledge(alert_handler, irq));