18 #include "sw/device/lib/runtime/irq.h" 
   20 #include "sw/device/lib/testing/alert_handler_testutils.h" 
   21 #include "sw/device/lib/testing/aon_timer_testutils.h" 
   22 #include "sw/device/lib/testing/rstmgr_testutils.h" 
   23 #include "sw/device/lib/testing/rv_plic_testutils.h" 
   24 #include "sw/device/lib/testing/test_framework/FreeRTOSConfig.h" 
   25 #include "sw/device/lib/testing/test_framework/check.h" 
   30 OTTF_DEFINE_TEST_CONFIG();
 
   38   kWdogBarkMicros = 3 * 100,          
 
   39   kWdogBiteMicros = 4 * 100,          
 
   40   kEscalationPhase0Micros = 1 * 100,  
 
   44   kEscalationPhase0MicrosCpu = kEscalationPhase0Micros + 20,  
 
   45   kEscalationPhase1Micros = 5 * 100,                          
 
   46   kEscalationPhase2Micros = 100,                              
 
   50     kWdogBarkMicros < kWdogBiteMicros &&
 
   51         kWdogBarkMicros > kEscalationPhase0Micros &&
 
   52         kWdogBarkMicros < (kEscalationPhase0Micros + kEscalationPhase1Micros) &&
 
   53         kWdogBiteMicros < (kEscalationPhase0Micros + kEscalationPhase1Micros),
 
   54     "The wdog bark and bite shall happens during the escalation phase 1");
 
   60 static dif_aon_timer_t aon_timer;
 
   61 static dif_rv_plic_t plic;
 
   62 static dif_pwrmgr_t pwrmgr;
 
   63 static dif_rstmgr_t rstmgr;
 
   64 static dif_alert_handler_t alert_handler;
 
   73 void ottf_external_isr(uint32_t *exc_info) {
 
   86     LOG_ERROR(
"Unexpected aon timer interrupt %d", irq);
 
   91         &alert_handler, kDifAlertHandlerClassA, &state));
 
  100     bool is_cause = 
false;
 
  111     CHECK_DIF_OK(dif_alert_handler_irq_acknowledge(&alert_handler, irq));
 
  122 void init_peripherals(
void) {
 
  125   CHECK_DIF_OK(dif_pwrmgr_init(base_addr, &pwrmgr));
 
  128   CHECK_DIF_OK(dif_rstmgr_init(base_addr, &rstmgr));
 
  131   CHECK_DIF_OK(dif_aon_timer_init(base_addr, &aon_timer));
 
  134   CHECK_DIF_OK(dif_rv_plic_init(base_addr, &plic));
 
  136   rv_plic_testutils_irq_range_enable(
 
  141   CHECK_DIF_OK(dif_alert_handler_init(base_addr, &alert_handler));
 
  144 static uint32_t udiv64_slow_into_u32(uint64_t a, uint64_t b,
 
  146   const uint64_t result = 
udiv64_slow(a, b, rem_out);
 
  147   CHECK(result <= UINT32_MAX, 
"Result of division must fit in uint32_t");
 
  148   return (uint32_t)result;
 
  156 static void alert_handler_config(
void) {
 
  163        .duration_cycles = udiv64_slow_into_u32(
 
  167        .duration_cycles = udiv64_slow_into_u32(
 
  171        .duration_cycles = udiv64_slow_into_u32(
 
  176       .accumulator_threshold = 0,
 
  177       .irq_deadline_cycles =
 
  179       .escalation_phases = esc_phases,
 
  180       .escalation_phases_len = 
ARRAYSIZE(esc_phases),
 
  187       .alert_classes = alert_classes,
 
  190       .class_configs = class_config,
 
  192       .ping_timeout = kAlertHandlerTestutilsDefaultPingTimeout,
 
  195   CHECK_STATUS_OK(alert_handler_testutils_configure_all(&alert_handler, config,
 
  198   CHECK_DIF_OK(dif_alert_handler_irq_set_enabled(
 
  205 static void execute_test(dif_aon_timer_t *aon_timer) {
 
  206   uint32_t bark_cycles = 0;
 
  207   CHECK_STATUS_OK(aon_timer_testutils_get_aon_cycles_32_from_us(kWdogBarkMicros,
 
  209   uint32_t bite_cycles = 0;
 
  210   CHECK_STATUS_OK(aon_timer_testutils_get_aon_cycles_32_from_us(kWdogBiteMicros,
 
  214       "Wdog will bark after %u/%u us/cycles and bite after %u/%u us/cycles",
 
  215       (uint32_t)kWdogBarkMicros, bark_cycles, (uint32_t)kWdogBiteMicros,
 
  220       aon_timer_testutils_watchdog_config(aon_timer, bark_cycles, bite_cycles,
 
  224   dif_pwrmgr_alert_t alert = kDifPwrmgrAlertFatalFault;
 
  225   CHECK_DIF_OK(dif_pwrmgr_alert_force(&pwrmgr, alert));
 
  229   CHECK(
false, 
"The alert handler failed to escalate");
 
  234   irq_global_ctrl(
true);
 
  235   irq_external_ctrl(
true);
 
  240   rv_plic_testutils_irq_range_enable(&plic, kPlicTarget,
 
  244   alert_handler_config();
 
  248   rst_info = rstmgr_testutils_reason_get();
 
  249   rstmgr_testutils_reason_clear();
 
  253         "Wrong reset reason %02X", rst_info);
 
  256     LOG_INFO(
"Booting for the first time, starting test");
 
  257     execute_test(&aon_timer);
 
  259     LOG_INFO(
"Booting for the second time due to escalation reset");
 
  263     LOG_ERROR(
"Unexpected rst_info=0x%x", rst_info);