9 #include "sw/device/lib/runtime/irq.h"
11 #include "sw/device/lib/testing/flash_ctrl_testutils.h"
12 #include "sw/device/lib/testing/pwrmgr_testutils.h"
13 #include "sw/device/lib/testing/rand_testutils.h"
14 #include "sw/device/lib/testing/rv_plic_testutils.h"
15 #include "sw/device/lib/testing/test_framework/check.h"
17 #include "sw/device/lib/testing/test_framework/ottf_utils.h"
20 #include "sw/device/lib/testing/autogen/isr_testutils.h"
22 #include "flash_ctrl_regs.h"
23 #include "pinmux_regs.h"
27 OTTF_DEFINE_TEST_CONFIG(.enable_uart_flow_control =
true);
30 static dif_pwrmgr_t pwrmgr;
31 static dif_pinmux_t pinmux;
32 static dif_rv_plic_t plic;
35 static const uint32_t kNumDio = 16;
39 #define NUM_DIRECT_DIO 5
40 static const uint32_t kDirectDio[NUM_DIRECT_DIO] = {6, 12, 13, 14, 15};
42 static plic_isr_ctx_t plic_ctx = {.rv_plic = &plic,
45 static pwrmgr_isr_ctx_t pwrmgr_isr_ctx = {
48 .expected_irq = kDifPwrmgrIrqWakeup,
55 int8_t sival_mio_pad = -1;
57 int8_t sival_wakeup_detector_idx = -1;
59 bool sival_ready_to_sleep =
false;
64 void ottf_external_isr(uint32_t *exc_info) {
65 dif_pwrmgr_irq_t irq_id;
68 isr_testutils_pwrmgr_isr(plic_ctx, pwrmgr_isr_ctx, &peripheral, &irq_id);
72 "IRQ peripheral: %d is incorrect", peripheral);
73 CHECK(irq_id == kDifPwrmgrIrqWakeup,
"IRQ ID: %d is incorrect", irq_id);
83 irq_global_ctrl(
true);
84 irq_external_ctrl(
true);
87 CHECK_DIF_OK(dif_pwrmgr_init(
89 CHECK_DIF_OK(dif_rv_plic_init(
91 CHECK_DIF_OK(dif_pinmux_init(
100 flash_ctrl_testutils_default_region_access(&flash_ctrl_state,
113 if (UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) ==
true) {
118 wakeup_detector_selected =
119 rand_testutils_gen32_range(0, PINMUX_PARAM_N_WKUP_DETECT - 1);
121 CHECK_STATUS_OK(flash_ctrl_testutils_write(
123 (uint32_t)(&wakeup_detector_idx) -
125 0, &wakeup_detector_selected, kDifFlashCtrlPartitionTypeData, 1));
127 LOG_INFO(
"detector %d is selected", wakeup_detector_selected);
142 uint32_t deep_powerdown_en = rand_testutils_gen32_range(0, 1);
148 uint32_t pad_sel = 0;
151 if (deep_powerdown_en == 0) {
165 pad_sel = rand_testutils_gen32_range(0, kNumDio - 1 - NUM_DIRECT_DIO);
167 for (
int i = 0; i < NUM_DIRECT_DIO; i++) {
168 if (pad_sel >= kDirectDio[i]) {
172 LOG_INFO(
"Pad Selection: %d / %d", mio0_dio1, pad_sel);
176 pad_sel = rand_testutils_gen32_range(2, kTopEarlgreyPinmuxInselLast);
178 OTTF_WAIT_FOR(sival_mio_pad != -1, 1000000);
179 pad_sel = (uint32_t)sival_mio_pad + 2;
182 LOG_INFO(
"Pad Selection: %d / %d", mio0_dio1, pad_sel - 2);
185 if (mio0_dio1 == 0) {
202 &pinmux, wakeup_detector_selected, wakeup_cfg));
204 if (deep_powerdown_en == 0) {
206 pwrmgr_domain_cfg |= kDifPwrmgrDomainOptionMainPowerInLowPower;
210 OTTF_WAIT_FOR(sival_ready_to_sleep != 0, 1000000);
214 CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
215 &pwrmgr, kDifPwrmgrWakeupRequestSourceThree, pwrmgr_domain_cfg));
217 LOG_INFO(
"Entering low power mode.");
221 if (UNWRAP(pwrmgr_testutils_is_wakeup_reason(
222 &pwrmgr, kDifPwrmgrWakeupRequestSourceThree)) ==
true) {
223 LOG_INFO(
"Test in post-sleep pin wakeup phase");
224 uint32_t wakeup_cause;
228 wakeup_detector_selected = wakeup_detector_idx;
230 OTTF_WAIT_FOR(sival_wakeup_detector_idx != -1, 1000000);
231 wakeup_detector_selected = (uint32_t)sival_wakeup_detector_idx;
234 LOG_INFO(
"wakeup_cause: %x %d %d", wakeup_cause,
235 1 << wakeup_detector_selected, wakeup_detector_selected);
236 CHECK(wakeup_cause == 1 << wakeup_detector_selected);
245 LOG_ERROR(
"Unexpected wakeup detected: type = %d, request_source = %d",
253 #undef NUM_DIRECT_DIO
254 #undef NUM_LOCKED_MIO