Software APIs
example_sival.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 
6 #include "sw/device/lib/testing/test_framework/check.h"
8 #include "sw/device/lib/testing/test_framework/ottf_utils.h"
9 
10 /* We need control flow for the ujson messages exchanged
11  * with the host in OTTF_WAIT_FOR on real devices. */
12 OTTF_DEFINE_TEST_CONFIG(.enable_uart_flow_control = true);
13 
14 enum {
15  kTestTimeoutMicros = 1000 * 1000, /* one second */
16 };
17 
18 enum phase_t {
19  /* Wait for host to move to next phase. */
20  kWaitHost1 = 0,
21  /* Wait for device to move to next phase. */
22  kWaitDevice1 = 1,
23  /* Wait for host to move to next phase. */
24  kWaitHost2 = 2,
25  /* Test finished. */
26  kTestDone = 3,
27 };
28 
29 /* Use a known size integer for the debugger. Mark this as
30  * volatile so it can be changed by the host/DV env. */
31 volatile uint32_t kPhase = kWaitHost1;
32 
33 bool test_main(void) {
34  LOG_INFO("Waiting for host");
35  /* Wait until host changes the phase. On DV, this will simply until
36  * the environment change the value of the variable. On a real device,
37  * this will wait for ujson messages from the host that read/write
38  * memory. The condition is check after each message and the test will
39  * be aborted the condition is still false after the timeout. */
40  OTTF_WAIT_FOR(kPhase != kWaitHost1, kTestTimeoutMicros);
41  /* Make sure that host moved to the expected phase. */
42  CHECK(kPhase == kWaitDevice1, "kPhase should be kWaitDevice1 (%d) but is %d",
43  kWaitDevice1, kPhase);
44  LOG_INFO("Change phase to kWaitHost2");
45  /* Change phase, the host will verify that the phase matches. */
46  kPhase = kWaitHost2;
47  /* Wait until host changes the phase again. */
48  OTTF_WAIT_FOR(kPhase != kWaitHost2, kTestTimeoutMicros);
49  CHECK(kPhase == kTestDone, "kPhase should be kTestDone (%d) but is %d",
50  kTestDone, kPhase);
51  return true;
52 }