Software APIs
pwrmgr_deep_sleep_power_glitch_test.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
9 #include "sw/device/lib/testing/pwrmgr_testutils.h"
10 #include "sw/device/lib/testing/rstmgr_testutils.h"
11 #include "sw/device/lib/testing/test_framework/check.h"
13 
15 
16 OTTF_DEFINE_TEST_CONFIG();
17 
18 // When the test first runs the rstmgr's `reset_info` CSR should have the POR
19 // bit set, the code clears reset_info and puts the chip in deep sleep. The WFI
20 // causes core_sleeping to rise, and that causes the SV side to glitch the main
21 // power rail, causing a pwrmgr internally generated reset. The next time the
22 // test runs is after the power glitch reset, which is confirmed reading the
23 // `reset_info` CSR.
24 bool test_main(void) {
25  dif_pwrmgr_t pwrmgr;
26  dif_rstmgr_t rstmgr;
27 
28  // Initialize pwrmgr since this will put the chip in deep sleep.
29  CHECK_DIF_OK(dif_pwrmgr_init(
31 
32  // Initialize rstmgr since this will check some registers.
33  CHECK_DIF_OK(dif_rstmgr_init(
35 
36  // Notice we are clearing rstmgr's RESET_INFO, so after the power glitch there
37  // is only one bit set.
38 
39  if (UNWRAP(rstmgr_testutils_is_reset_info(&rstmgr, kDifRstmgrResetInfoPor))) {
40  LOG_INFO("Powered up for the first time, begin test");
41 
42  CHECK(UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) == true);
43 
44  CHECK_STATUS_OK(rstmgr_testutils_pre_reset(&rstmgr));
45 
46  // Configure deep sleep.
47  CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
48  &pwrmgr, kDifPwrmgrWakeupRequestSourceFive, 0));
49 
50  // This causes core_sleeping to rise and triggers the injection of the
51  // power glitch. Enter low power mode.
52  LOG_INFO("Issue WFI to enter sleep");
54  } else {
55  LOG_INFO("Checking reset status.");
56  CHECK_STATUS_OK(rstmgr_testutils_post_reset(
57  &rstmgr,
59  0, 0, 0));
60  LOG_INFO("Reset status indicates a power glitch and a deep sleep wakeup");
61  }
62  return true;
63 }