15 #include "sw/device/lib/testing/aon_timer_testutils.h"
16 #include "sw/device/lib/testing/pwrmgr_testutils.h"
17 #include "sw/device/lib/testing/rstmgr_testutils.h"
18 #include "sw/device/lib/testing/test_framework/check.h"
21 static const dt_pwrmgr_t kPwrmgrDt = 0;
22 static_assert(kDtPwrmgrCount == 1,
"this library expects exactly one pwrmgr");
23 static const dt_rstmgr_t kRstmgrDt = 0;
24 static_assert(kDtRstmgrCount == 1,
"this library expects exactly one rstmgr");
25 static const dt_aon_timer_t kAonTimerDt = 0;
26 static_assert(kDtAonTimerCount == 1,
27 "this library expects exactly one aon_timer");
29 OTTF_DEFINE_TEST_CONFIG();
37 static void config_wdog(
const dif_aon_timer_t *aon_timer,
38 const dif_pwrmgr_t *pwrmgr, uint64_t bark_time_us,
39 uint64_t bite_time_us) {
40 uint32_t bark_cycles = 0;
41 CHECK_STATUS_OK(aon_timer_testutils_get_aon_cycles_32_from_us(bark_time_us,
43 uint32_t bite_cycles = 0;
44 CHECK_STATUS_OK(aon_timer_testutils_get_aon_cycles_32_from_us(bite_time_us,
47 LOG_INFO(
"Wdog will bark after %u us and bite after %u us",
48 (uint32_t)bark_time_us, (uint32_t)bite_time_us);
55 CHECK_STATUS_OK(aon_timer_testutils_watchdog_config(
56 aon_timer, (uint32_t)bark_cycles, (uint32_t)bite_cycles,
false));
62 static void wdog_bite_test(
const dif_aon_timer_t *aon_timer,
63 const dif_pwrmgr_t *pwrmgr, uint64_t bark_time_us) {
64 uint64_t bite_time_us = bark_time_us * 2;
65 config_wdog(aon_timer, pwrmgr, bark_time_us, bite_time_us);
67 CHECK(bark_time_us <= UINT32_MAX,
"bark_time_us must fit in a uint32_t");
71 uint64_t aon_timer_clock_freq_hz = dt_clock_frequency(
72 dt_aon_timer_clock(kDtAonTimerAon, kDtAonTimerClockAon));
74 (uint32_t)bark_time_us +
75 (uint32_t)
udiv64_slow(5 * 1000000 + aon_timer_clock_freq_hz - 1,
76 aon_timer_clock_freq_hz, NULL);
80 bool is_pending =
false;
81 CHECK_DIF_OK(dif_aon_timer_irq_is_pending(
82 aon_timer, kDifAonTimerIrqWdogTimerBark, &is_pending));
83 CHECK(is_pending,
"Wdog bark irq did not rise after %u microseconds",
89 CHECK(
false,
"Timeout waiting for Wdog bite reset!");
95 static void sleep_wdog_bite_test(
const dif_aon_timer_t *aon_timer,
96 const dif_pwrmgr_t *pwrmgr,
97 uint64_t bark_time_us) {
98 uint64_t bite_time_us = bark_time_us * 2;
99 config_wdog(aon_timer, pwrmgr, bark_time_us, bite_time_us);
102 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(pwrmgr, wakeup_sources, 0));
106 CHECK(
false,
"Fail to enter in low power mode!");
112 CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
117 kDtAonTimerResetReqAonTimer, &reset_sources));
120 kDtAonTimerWakeupWkupReq, &wakeup_sources));
124 CHECK_DIF_OK(dif_rstmgr_init_from_dt(kRstmgrDt, &rstmgr));
127 dif_aon_timer_t aon_timer;
128 CHECK_DIF_OK(dif_aon_timer_init_from_dt(kAonTimerDt, &aon_timer));
132 rst_info = rstmgr_testutils_reason_get();
133 rstmgr_testutils_reason_clear();
136 rst_info == kDifRstmgrResetInfoWatchdog ||
139 "Wrong reset reason %02X", rst_info);
142 LOG_INFO(
"Booting for the first time, setting wdog");
144 wdog_bite_test(&aon_timer, &pwrmgr, 200);
145 }
else if (rst_info == kDifRstmgrResetInfoWatchdog) {
146 LOG_INFO(
"Booting for the second time due to wdog bite reset");
148 sleep_wdog_bite_test(&aon_timer, &pwrmgr, 200);
149 }
else if (rst_info ==
151 LOG_INFO(
"Booting for the third time due to wdog bite reset during sleep");