Software APIs
aon_timer_sleep_wdog_sleep_pause_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 <limits.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8 
14 #include "sw/device/lib/testing/aon_timer_testutils.h"
15 #include "sw/device/lib/testing/pwrmgr_testutils.h"
16 #include "sw/device/lib/testing/rstmgr_testutils.h"
17 #include "sw/device/lib/testing/test_framework/check.h"
19 
21 
22 #define IDLE_TIME_US 10
23 #define WKUP_TIME_US 2000
24 OTTF_DEFINE_TEST_CONFIG();
25 
26 bool test_main(void) {
27  // Initialize pwrmgr.
28  dif_pwrmgr_t pwrmgr;
29  CHECK_DIF_OK(dif_pwrmgr_init(
31 
32  // Initialize rstmgr to check the reset reason.
33  dif_rstmgr_t rstmgr;
34  CHECK_DIF_OK(dif_rstmgr_init(
36 
37  // Initialize aon timer to use the wdog.
38  dif_aon_timer_t aon_timer;
39  CHECK_DIF_OK(dif_aon_timer_init(
41 
42  // Enable aon_timer watchdog reset.
43  // Set wdog as a reset source.
45  kDifPwrmgrResetRequestSourceTwo,
47 
48  // Check if there was a HW reset caused by the wdog bite.
49  dif_rstmgr_reset_info_bitfield_t rst_info = rstmgr_testutils_reason_get();
50  rstmgr_testutils_reason_clear();
51 
52  uint64_t wkup_cnt;
53  uint32_t wdog_cnt;
54  if (rst_info == kDifRstmgrResetInfoPor) {
55  LOG_INFO("Booting for the first time, setting wdog");
56 
57  // Configure watchdog sooner then wakeup, but with pause enabled.
58  uint64_t wkup_cycles = 0;
59  CHECK_STATUS_OK(aon_timer_testutils_get_aon_cycles_64_from_us(
60  WKUP_TIME_US, &wkup_cycles));
61 
62  // The actual expiration of the watchdog is unimportant, as the test
63  // mainly checks the count.
64  CHECK_STATUS_OK(aon_timer_testutils_watchdog_config(&aon_timer, UINT32_MAX,
65  UINT32_MAX, true));
66  CHECK_STATUS_OK(aon_timer_testutils_wakeup_config(&aon_timer, wkup_cycles));
67 
68  busy_spin_micros(IDLE_TIME_US);
69 
70  // Since watchdog was started first, its count should be larger.
71  CHECK_DIF_OK(dif_aon_timer_watchdog_get_count(&aon_timer, &wdog_cnt));
72  CHECK_DIF_OK(dif_aon_timer_wakeup_get_count(&aon_timer, &wkup_cnt));
73  CHECK(wdog_cnt >= wkup_cnt);
74 
75  // Deep sleep.
76  CHECK_STATUS_OK(pwrmgr_testutils_enable_low_power(
77  &pwrmgr, kDifPwrmgrWakeupRequestSourceFive,
78  /*domain_config=*/0));
79 
80  // Enter low power mode.
81  LOG_INFO("Issue WFI to enter sleep");
83  } else if (rst_info == kDifRstmgrResetInfoLowPowerExit) {
84  LOG_INFO("Booting for the second time due to wakeup");
85  // Since watchdog count was paused during low power, wakeup
86  // count should now be larger.
87  CHECK_DIF_OK(dif_aon_timer_wakeup_get_count(&aon_timer, &wkup_cnt));
88  CHECK_DIF_OK(dif_aon_timer_watchdog_get_count(&aon_timer, &wdog_cnt));
89  CHECK(wdog_cnt < wkup_cnt);
90 
91  } else {
92  LOG_ERROR("Got unexpected reset_info=0x%x", rst_info);
93  }
94 
95  return true;
96 }