Software APIs
uart.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_SILICON_CREATOR_LIB_DRIVERS_UART_H_
6#define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_UART_H_
7
8#include <stddef.h>
9#include <stdint.h>
10
12#include "sw/device/silicon_creator/lib/error.h"
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18/**
19 * Initialize the UART with the request parameters.
20 *
21 * @param precalculated_nco NCO value used to set the speed of the UART.
22 * @return kErrorOk if successful, else an error code.
23 *
24 * This symbol is marked as noinline because it is expected by several gdb
25 * e2e test to be able to set a breakpoint.
26 */
28void uart_init(uint32_t precalculated_nco);
29
30/**
31 * Enable the receiver on the UART.
32 */
33void uart_enable_receiver(void);
34
35/**
36 * Write a single byte to the UART.
37 *
38 * @param byte Byte to send.
39 */
40void uart_putchar(uint8_t byte);
41
42/**
43 * Read a single byte from the UART.
44 *
45 * @param timeout_ms How long to wait.
46 * @return The character received or -1 if timeout.
47 */
48int uart_getchar(uint32_t timeout_ms);
49
50/**
51 * Write a buffer to the UART.
52 *
53 * Writes the complete buffer to the UART and wait for transmision to complete.
54 *
55 * @param data Pointer to buffer to write.
56 * @param len Length of the buffer to write.
57 */
58void uart_write(const void *data, size_t len);
59
60/**
61 * Write an unsigned integer as hex to the UART.
62 *
63 * Note: this function does not block waiting for the transmitter to finish.
64 *
65 * @param val The value to write to the UART.
66 * @param len The length of the value in bytes (1, 2, or 4 bytes).
67 * @param after Packed ASCII values to write after the hex value.
68 */
69void uart_write_hex(uint32_t val, size_t len, uint32_t after);
70
71/**
72 * Write an immediate value to the UART a byte at a time.
73 *
74 * This function is used to print a few imporant user-facing strings
75 * from the ROM, but without having to dereference pointers. The
76 * string to be written is encoded as ASCII in an integer in little-endian
77 * order (ie: the string is reversed). A maximum of eight bytes can be printed
78 * in this way. A zero byte in the value terminates the output.
79 *
80 * Note: this function does not block waiting for the transmitter to finish.
81 *
82 * @param val The bytes to print.
83 */
84void uart_write_imm(uint64_t val);
85
86/**
87 * Read from the UART into a buffer.
88 *
89 * Reads up to `len` bytes into a buffer before the timeout.
90 *
91 * @param data Pointer to buffer to write.
92 * @param len Length of the buffer to write.
93 * @param timeout_ms The timeout to receive the complete buffer.
94 * @return Number of bytes written.
95 */
97size_t uart_read(uint8_t *data, size_t len, uint32_t timeout_ms);
98
99/**
100 * Returns true if UART TX is idle, otherwise returns false.
101 *
102 * @return Boolean indicating UART TX activity status.
103 */
105bool uart_tx_idle(void);
106
107/**
108 * Detect if a UART break is asserted for a given period of time.
109 *
110 * UART break must already be asserted upon entry to this function and must
111 * remain asserted for the duration of `timeout_ms`.
112 *
113 * @param timeout_us The time for which break must be asserted.
114 * @return kHardenedBoolTrue if break is asserted.
115 */
116hardened_bool_t uart_break_detect(uint32_t timeout_us);
117
118/**
119 * Sink a buffer to the UART.
120 *
121 * This is a wrapper for uart_write which conforms to the type signature
122 * required by the print library.
123 *
124 * @param uart Pointer a target so satisfy the shape of the sink API.
125 * @param data Pointer to buffer to write.
126 * @param len Length of the buffer to write.
127 * @return Number of bytes written.
128 */
130size_t uart_sink(void *uart, const char *data, size_t len);
131
132#ifdef __cplusplus
133}
134#endif
135
136#endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_UART_H_