Software APIs
ottf_flow_control_functest.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 <stdbool.h>
6 #include <stdint.h>
7 
9 #include "sw/device/lib/base/status.h"
12 #include "sw/device/lib/testing/test_framework/check.h"
13 #include "sw/device/lib/testing/test_framework/ottf_console.h"
15 #include "sw/device/lib/testing/test_framework/ujson_ottf.h"
16 #include "sw/device/lib/ujson/ujson.h"
17 
18 OTTF_DEFINE_TEST_CONFIG(.enable_uart_flow_control = true);
19 
20 status_t ottf_flow_control_test(ujson_t *uj) {
21  // Adjust the delay in the wait loop so that the host test harness
22  // has enough time to starting sending data us on the UART. Because
23  // we aren't reading in the wait loop, we should get flow-control
24  // event while in the wait loop.
25  uint32_t delay = kDeviceType == kDeviceSimVerilator ? 1 : 500000;
26  for (size_t i = 0; i < 10; ++i) {
27  // Print a bunch of stuff so that ibex will be busy
28  // driving the transmitter while the host sends data
29  // to the UART.
30  base_printf("WAIT\r\n");
31  busy_spin_micros(delay);
32  }
33 
34  base_printf("Reading\r\n");
35  // Receive a line of text into a buffer.
36  uint8_t buf[256] = {0};
37  for (size_t i = 0; i < sizeof(buf) - 1; ++i) {
38  char ch = (char)TRY(ujson_getc(uj));
39  if (ch == '\n') {
40  break;
41  }
42  buf[i] = ch;
43  }
44 
45  // We'd better have gotten a flow control interrupt.
46  CHECK(ottf_console_get_flow_control_irqs() > 0);
47 
48  // Print out the received data so the test can check that it matches what was
49  // sent.
50  base_printf("RESULT:%s\r\n", buf);
51  return OK_STATUS();
52 }
53 
54 bool test_main(void) {
55  ujson_t uj = ujson_ottf_console();
56  status_t status = ottf_flow_control_test(&uj);
57  return status_ok(status);
58 }