Software APIs
sw
host
tests
usbdev
usbdev_stream
usbdev_utils.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
#ifndef OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_UTILS_H_
5
#define OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_UTILS_H_
6
#include <cstdint>
7
#include <cstdio>
8
9
/**
10
* Open and configure a serial port connection to/from the USB device.
11
* The returned file descriptor may be passed directly to close().
12
*
13
* @param dev_name Device path of serial port.
14
* @param write Indicates whether to open for writing or reading.
15
* @return File descriptor, or negative to indicate failure.
16
*/
17
int
port_open(
const
char
*dev_name,
bool
write);
18
19
/**
20
* Receive a sequence of bytes from the USB device, non-blocking.
21
*
22
* @param in File descriptor.
23
* @param buf Buffer to receive data.
24
* @param len Buffer length (= maximum number of bytes to receive).
25
* @return Number of bytes received, or -ve to indicate failure.
26
*/
27
ssize_t recv_bytes(
int
in, uint8_t *buf,
size_t
len);
28
29
/**
30
* Send a sequence of bytes to the USB device, non-blocking.
31
*
32
* @param out File descriptor.
33
* @param data Data to be transmitted.
34
* @param len Number of bytes to be transmitted.
35
* @return Number of bytes transmitted, or -ve to indicate failure.
36
*/
37
38
ssize_t send_bytes(
int
out,
const
uint8_t *data,
size_t
len);
39
40
/**
41
* Dump a sequence of bytes as hexadecimal and ASCII for diagnostic purposes.
42
*
43
* @param out Output stream.
44
* @param data Data buffer.
45
* @param n Number of bytes.
46
*/
47
void
buffer_dump(FILE *out,
const
uint8_t *data,
size_t
n);
48
49
/**
50
* Return current monotonic wall time in microseconds.
51
*
52
* @return Current time.
53
*/
54
uint64_t time_us(
void
);
55
56
/**
57
* Return microseconds elapsed since the given monotonic wall time.
58
*
59
* @param start Monotonic wall time at the start of the time interval.
60
* @return Elapsed time in microseconds.
61
*/
62
inline
uint64_t elapsed_time(uint64_t start) {
return
time_us() - start; }
63
64
/**
65
* Collect a 16-bit quantity transmitted in USB byte ordering, without
66
* assuming alignment or host endianness.
67
*
68
* @param p Pointer to 2 bytes.
69
* @return The 16-bit quantity read.
70
*/
71
inline
uint16_t get_le16(
const
uint8_t *p) {
return
p[0] | (p[1] << 8); }
72
73
#endif
// OPENTITAN_SW_HOST_TESTS_USBDEV_USBDEV_STREAM_USBDEV_UTILS_H_
Return to
OpenTitan Documentation