Software APIs
ottf_console.h
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 #ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_OTTF_CONSOLE_H_
6 #define OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_OTTF_CONSOLE_H_
7 
8 #include <stdint.h>
9 
10 #include "sw/device/lib/base/status.h"
12 
13 /**
14  * Flow control state.
15  */
16 typedef enum ottf_console_flow_control {
17  /** No flow control enabled. */
18  kOttfConsoleFlowControlNone = 0,
19  /** Automatically determine the next flow control state. */
20  kOttfConsoleFlowControlAuto = 1,
21  /**
22  * Flow control is in the `Resume` state (safe to receive).
23  * This is also the ASCII code for `XON`.
24  */
25  kOttfConsoleFlowControlResume = 17,
26  /**
27  * Flow control is in the `Pause` state (risk of overrun).
28  * This is also the ASCII code for `XOFF`.
29  */
30  kOttfConsoleFlowControlPause = 19,
31 } ottf_console_flow_control_t;
32 
33 /**
34  * Returns a pointer to the OTTF console device DIF handle.
35  */
36 void *ottf_console_get(void);
37 
38 /**
39  * Initializes the OTTF console device and connects to the printf buffer.
40  */
41 void ottf_console_init(void);
42 
43 /**
44  * Configures the given UART to be used by the OTTF console.
45  *
46  * @param base_addr The base address of the UART to use.
47  */
48 void ottf_console_configure_uart(uintptr_t base_addr);
49 
50 /**
51  * Configures the given SPI device to be used by the OTTF console.
52  *
53  * @param base_addr The base address of the SPI device to use.
54  */
55 void ottf_console_configure_spi_device(uintptr_t base_addr);
56 
57 /**
58  * Manage flow control by inspecting the OTTF console device's receive FIFO.
59  *
60  * @param uart A UART handle.
61  * @param ctrl Set the next desired flow-control state.
62  * @return The new state.
63  */
64 status_t ottf_console_flow_control(const dif_uart_t *uart,
65  ottf_console_flow_control_t ctrl);
66 
67 /**
68  * Enable flow control for the OTTF console.
69  *
70  * Enables flow control on the UART associated with the OTTF console. Flow
71  * control is managed by enabling the RX watermark IRQ and sending a `Pause`
72  * (aka XOFF) when the RX FIFO depth reaches 16 bytes. A `Resume` (aka XON) is
73  * sent when the RX FIFO has been drained to 4 bytes.
74  *
75  * This function configures UART interrupts at the PLIC and enables interrupts
76  * at the CPU.
77  */
78 void ottf_console_flow_control_enable(void);
79 
80 /**
81  * Manage console flow control from interrupt context.
82  *
83  * Call this when a console UART interrupt triggers.
84  *
85  * @param exc_info The OTTF execution info passed to all ISRs.
86  * @return True if an RX Watermark IRQ was detected and handled. False
87  * otherwise.
88  */
89 bool ottf_console_flow_control_isr(uint32_t *exc_info);
90 
91 /**
92  * Returns the number of OTTF console flow control interrupts that have
93  * occurred.
94  */
95 uint32_t ottf_console_get_flow_control_irqs(void);
96 
97 /**
98  * Read data from the host via the OTTF SPI device console into a provided
99  * buffer.
100  *
101  * The function waits for spi upload commands, then transfers data in chunks
102  * until a transmission complete signal is received. If the size of data sent
103  * from the host is greater than the provided buffer, then the excess data will
104  * be discarded.
105  *
106  * @param buf_size The size, in bytes, of the `buf`.
107  * @param[out] buf A pointer to the location where the data should be stored.
108  * @return The number of bytes actually received from the host.
109  */
110 size_t ottf_console_spi_device_read(size_t buf_size, uint8_t *const buf);
111 
112 /**
113  * Write a buffer to the OTTF console.
114  *
115  * @param context An IO context
116  * @param buf The buffer to write to the OTTF console.
117  * @param len The length of the buffer.
118  * @return OK or an error.
119  */
120 status_t ottf_console_putbuf(void *io, const char *buf, size_t len);
121 
122 /**
123  * Get a single character from the OTTF console.
124  *
125  * @return The next character or an error.
126  */
127 status_t ottf_console_getc(void *io);
128 
129 #endif // OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_OTTF_CONSOLE_H_