1 /*
2  * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 #include "esp_err.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @file usb_console.h
20  * This file contains definitions of low-level USB console functions.
21  * These functions are not considered to be a public interface and
22  * should not be called by applications directly.
23  * Application interface to the USB console is provided either by
24  * "cdcacm" VFS driver, or by the USB CDC driver in TinyUSB.
25  */
26 
27 
28 /**
29  * RX/TX callback function type
30  * @param arg  callback-specific context pointer
31  */
32 typedef void (*esp_usb_console_cb_t)(void* arg);
33 
34 /**
35  * Initialize USB console output using ROM USB CDC driver.
36  * This function is called by the early startup code if USB CDC is
37  * selected as the console output option.
38  * @return
39  *  - ESP_OK on success
40  *  - ESP_ERR_NO_MEM
41  *  - other error codes from the interrupt allocator
42  */
43 esp_err_t esp_usb_console_init(void);
44 
45 /**
46  * Write a buffer to USB CDC
47  * @param buf  data to write
48  * @param size  size of the data, in bytes
49  * @return -1 on error, otherwise the number of bytes
50  */
51 ssize_t esp_usb_console_write_buf(const char* buf, size_t size);
52 
53 /**
54  * @brief Wait until all buffered USB CDC output is written
55  *
56  * @return ssize_t  Number of bytes written, or -1 if the driver is not initialized
57  */
58 ssize_t esp_usb_console_flush(void);
59 
60 /**
61  * @brief Read data from USB CDC
62  *
63  * May read less data than requested.
64  *
65  * @param buf  Buffer to read data into
66  * @param buf_size  Size of the buffer
67  * @return ssize_t  Number of bytes written into the buffer, or -1 if the driver is not initialized
68  */
69 ssize_t esp_usb_console_read_buf(char* buf, size_t buf_size);
70 
71 /**
72  * @brief Get the number of bytes available for reading from USB CDC
73  *
74  * @return ssize_t Number of bytes available, or -1 if the driver is not initialized
75  */
76 ssize_t esp_usb_console_available_for_read(void);
77 
78 /**
79  * @brief Check if data can be written into USB CDC
80  *
81  * @return true if data can be written now without blocking
82  */
83 bool esp_usb_console_write_available(void);
84 
85 /**
86  * @brief Set RX/TX callback functions to be called from ISR
87  *
88  * @param rx_cb  RX callback function
89  * @param tx_cb  TX callback function
90  * @param arg  callback-specific context pointer
91  * @return ESP_OK if the callbacks were set, ESP_ERR_INVALID_STATE if the driver is not initialized
92  */
93 esp_err_t esp_usb_console_set_cb(esp_usb_console_cb_t rx_cb, esp_usb_console_cb_t tx_cb, void* arg);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98