Software APIs
pwrmgr_normal_sleep_por_reset_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 
5 #include <assert.h>
6 #include <limits.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 
18 #include "sw/device/lib/testing/pwrmgr_testutils.h"
19 #include "sw/device/lib/testing/rstmgr_testutils.h"
20 #include "sw/device/lib/testing/test_framework/FreeRTOSConfig.h"
21 #include "sw/device/lib/testing/test_framework/check.h"
23 
25 
26 OTTF_DEFINE_TEST_CONFIG(.enable_uart_flow_control = true);
27 
28 /**
29  * Objects to access the peripherals used in this test via dif API.
30  */
31 static dif_pwrmgr_t pwrmgr;
32 static dif_sysrst_ctrl_t sysrst_ctrl_aon;
33 static dif_rstmgr_t rstmgr;
34 
35 /**
36  * Initialize the peripherals used in this test.
37  */
38 static void init_peripherals(void) {
39  // Initialize pwrmgr.
40  CHECK_DIF_OK(dif_pwrmgr_init(
42 
43  // Initialize sysrst_ctrl.
44  CHECK_DIF_OK(dif_sysrst_ctrl_init(
46  &sysrst_ctrl_aon));
47 
48  // Initialize rstmgr to check the reset reason.
49  CHECK_DIF_OK(dif_rstmgr_init(
51 }
52 
53 /**
54  * Configure the sysrst.
55  */
56 static void config_sysrst(const dif_pwrmgr_t *pwrmgr,
57  const dif_sysrst_ctrl_t *sysrst_ctrl_aon) {
58  LOG_INFO("sysrst enabled");
59 
60  // Set sysrst as a reset source.
62  kDifPwrmgrResetRequestSourceOne,
64  LOG_INFO("Reset Request SourceOne is set");
65 
66  // Configure sysrst key combo
67  // reset pulse : 50 us
68  // detect durration : 50 us
69 
70  dif_sysrst_ctrl_key_combo_config_t sysrst_ctrl_key_combo_config = {
72  .detection_time_threshold = 10,
74  .embedded_controller_reset_duration = 10};
75 
77  sysrst_ctrl_aon, kDifSysrstCtrlKeyCombo0, sysrst_ctrl_key_combo_config));
78  // Configure sysrst input change
79  // debounce duration : 100 us
80  dif_sysrst_ctrl_input_change_config_t sysrst_ctrl_input_change_config = {
81  .input_changes = kDifSysrstCtrlInputAll, .debounce_time_threshold = 20};
82 
83  // Configure pinmux
84  dif_pinmux_t pinmux;
85  CHECK_DIF_OK(dif_pinmux_init(
87 
89  sysrst_ctrl_aon, sysrst_ctrl_input_change_config));
90 
91  CHECK_DIF_OK(dif_pinmux_input_select(
94 }
95 
96 static void normal_sleep_por(const dif_pwrmgr_t *pwrmgr) {
97  // Place device into low power and immediately wake.
99  config = kDifPwrmgrDomainOptionUsbClockInLowPower |
102  kDifPwrmgrDomainOptionMainPowerInLowPower;
103 
104  // Program the pwrmgr to go to deep sleep state (clocks off).
105  CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(pwrmgr, 0, config));
106  LOG_INFO("Ready for pad POR");
107  // Enter in low power mode.
109 }
110 
111 bool test_main(void) {
112  init_peripherals();
113 
114  // Check if there was a HW reset caused by expected cases
116  rst_info = rstmgr_testutils_reason_get();
117  rstmgr_testutils_reason_clear();
118  LOG_INFO("Reset info 0x%x", rst_info);
119  if (rst_info == kDifRstmgrResetInfoPor) {
120  config_sysrst(&pwrmgr, &sysrst_ctrl_aon);
121  LOG_INFO("Setting sleep mode");
122  normal_sleep_por(&pwrmgr);
123  CHECK(false, "This is unreachable");
124  } else if ((rst_info & kDifRstmgrResetInfoSysRstCtrl) != 0) {
125  // This means the host/sim_dv side found the POR case to work so all
126  // went well.
127  return true;
128  } else {
129  LOG_ERROR("Wrong reset reason %02X", rst_info);
130  }
131  return false;
132 }