Software APIs
usb_logging.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_USB_LOGGING_H_
6 #define OPENTITAN_SW_DEVICE_LIB_TESTING_USB_LOGGING_H_
7 #include "sw/device/lib/base/status.h"
8 #include "sw/device/lib/testing/usb_testutils.h"
9 
10 // Maximum number of streams supported by logging over USB.
11 enum {
12  kUSBLogMaxStreams = 4u,
13 };
14 
15 /**
16  * Initiate logging of stdout traffic over USB, using internal state
17  * structure and a single stream, for minimal external code.
18  *
19  * @return The result of the operation.
20  */
21 
23 status_t usb_logging_enable(void);
24 
25 /**
26  * Generic initialization of logging streams via USB device; traffic delivery
27  * for each stream may optionally be reliable, meaning that the calling code
28  * will block if there is insufficient buffer space and/or the USB host has not
29  * yet received earlier traffic.
30  *
31  * To integrate with other code that is using the USB testutils, specify the
32  * context pointer for the usb_testutils layer, along with the number of the
33  * first endpoint to used for logging.
34  *
35  * If logging is the only user of the USB device, simply pass NULL and ep_first
36  * may be zero.
37  *
38  * @param usbutils Context for usb_testutils layer or NULL.
39  * @param nstreams Number of logging streams.
40  * @param ep_first First endpoint number to be used.
41  * @param mreliable Bitmap of which logging streams shall provide reliable
42  * delivery.
43  * @param remap Is remapping of non-printable characters required?
44  * @return The result of the operation.
45  */
46 
48 status_t usb_logging_init(usb_testutils_ctx_t *usbutils, uint8_t ep_first,
49  uint8_t nstreams, uint32_t reliable, bool remap);
50 
51 /**
52  * Send a log message via the given stream.
53  *
54  * @param stream Stream number.
55  * @param text The text to be sent.
56  * @return The result of the operation.
57  */
58 // Use of result not required for unreliable logging traffic.
59 status_t usb_logging_text(uint8_t stream, const char *text);
60 
61 /**
62  * Send a sequence of bytes via the given stream.
63  *
64  * @param stream Stream number.
65  * @param data Start of byte sequence.
66  * @param len Number of bytes to be sent.
67  * @return The result of the operation.
68  */
69 // Use of result not required for unreliable logging traffic.
70 status_t usb_logging_data(uint8_t stream, const uint8_t *data, unsigned len);
71 
72 /**
73  * Ensure that all log data on this stream is flushed upstream to the host;
74  * this should be employed only where absolutely necessary, to ensure visibility
75  * of specific output.
76  *
77  * In normal usage, log data will be periodically flushed
78  */
79 status_t usb_logging_flush(uint8_t stream);
80 
81 /**
82  * Finalize USB logging streams, optionally waiing until all buffered logging
83  * data has been transmitted to the host.
84  *
85  * @param wait Whether to wait for all buffered data to be transmitted.
86  * @param disconnect Indicates whether to disconnect from the USB.
87  * @return The result of the operation.
88  */
89 
91 status_t usb_logging_fin(bool wait, bool disconnect);
92 
93 #endif // OPENTITAN_SW_DEVICE_LIB_TESTING_USB_LOGGING_H_