Open FFBoard
Open source force feedback firmware
cdc_device.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#ifndef TUSB_CDC_DEVICE_H_
28#define TUSB_CDC_DEVICE_H_
29
30#include "cdc.h"
31
32//--------------------------------------------------------------------+
33// Class Driver Configuration
34//--------------------------------------------------------------------+
35#if !defined(CFG_TUD_CDC_EP_BUFSIZE) && defined(CFG_TUD_CDC_EPSIZE)
36 #warning CFG_TUD_CDC_EPSIZE is renamed to CFG_TUD_CDC_EP_BUFSIZE, please update to use the new name
37 #define CFG_TUD_CDC_EP_BUFSIZE CFG_TUD_CDC_EPSIZE
38#endif
39
40#ifndef CFG_TUD_CDC_EP_BUFSIZE
41 #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
42#endif
43
44#ifdef __cplusplus
45 extern "C" {
46#endif
47
48//--------------------------------------------------------------------+
49// Driver Configuration
50//--------------------------------------------------------------------+
51
52typedef struct TU_ATTR_PACKED {
53 uint8_t rx_persistent : 1; // keep rx fifo on bus reset or disconnect
54 uint8_t tx_persistent : 1; // keep tx fifo on bus reset or disconnect
55} tud_cdc_configure_fifo_t;
56
57// Configure CDC FIFOs behavior
58bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const* cfg);
59
60//--------------------------------------------------------------------+
61// Application API (Multiple Ports) i.e. CFG_TUD_CDC > 1
62//--------------------------------------------------------------------+
63
64// Check if interface is ready
65bool tud_cdc_n_ready(uint8_t itf);
66
67// Check if terminal is connected to this port
68bool tud_cdc_n_connected(uint8_t itf);
69
70// Get current line state. Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
71uint8_t tud_cdc_n_get_line_state(uint8_t itf);
72
73// Get current line encoding: bit rate, stop bits parity etc ..
74void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t* coding);
75
76// Set special character that will trigger tud_cdc_rx_wanted_cb() callback on receiving
77void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted);
78
79// Get the number of bytes available for reading
80uint32_t tud_cdc_n_available(uint8_t itf);
81
82// Read received bytes
83uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize);
84
85// Read a byte, return -1 if there is none
86TU_ATTR_ALWAYS_INLINE static inline int32_t tud_cdc_n_read_char(uint8_t itf) {
87 uint8_t ch;
88 return tud_cdc_n_read(itf, &ch, 1) ? (int32_t) ch : -1;
89}
90
91// Clear the received FIFO
92void tud_cdc_n_read_flush(uint8_t itf);
93
94// Get a byte from FIFO without removing it
95bool tud_cdc_n_peek(uint8_t itf, uint8_t* ui8);
96
97// Write bytes to TX FIFO, data may remain in the FIFO for a while
98uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize);
99
100// Write a byte
101TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_n_write_char(uint8_t itf, char ch) {
102 return tud_cdc_n_write(itf, &ch, 1);
103}
104
105// Write a null-terminated string
106TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_n_write_str(uint8_t itf, char const* str) {
107 return tud_cdc_n_write(itf, str, strlen(str));
108}
109
110// Force sending data if possible, return number of forced bytes
111uint32_t tud_cdc_n_write_flush(uint8_t itf);
112
113// Return the number of bytes (characters) available for writing to TX FIFO buffer in a single n_write operation.
114uint32_t tud_cdc_n_write_available(uint8_t itf);
115
116// Clear the transmit FIFO
117bool tud_cdc_n_write_clear(uint8_t itf);
118
119//--------------------------------------------------------------------+
120// Application API (Single Port)
121//--------------------------------------------------------------------+
122
123TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_ready(void) {
124 return tud_cdc_n_ready(0);
125}
126
127TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_connected(void) {
128 return tud_cdc_n_connected(0);
129}
130
131TU_ATTR_ALWAYS_INLINE static inline uint8_t tud_cdc_get_line_state(void) {
132 return tud_cdc_n_get_line_state(0);
133}
134
135TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_get_line_coding(cdc_line_coding_t* coding) {
136 tud_cdc_n_get_line_coding(0, coding);
137}
138
139TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_set_wanted_char(char wanted) {
140 tud_cdc_n_set_wanted_char(0, wanted);
141}
142
143TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_available(void) {
144 return tud_cdc_n_available(0);
145}
146
147TU_ATTR_ALWAYS_INLINE static inline int32_t tud_cdc_read_char(void) {
148 return tud_cdc_n_read_char(0);
149}
150
151TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_read(void* buffer, uint32_t bufsize) {
152 return tud_cdc_n_read(0, buffer, bufsize);
153}
154
155TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_read_flush(void) {
157}
158
159TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_peek(uint8_t* ui8) {
160 return tud_cdc_n_peek(0, ui8);
161}
162
163TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_char(char ch) {
164 return tud_cdc_n_write_char(0, ch);
165}
166
167TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write(void const* buffer, uint32_t bufsize) {
168 return tud_cdc_n_write(0, buffer, bufsize);
169}
170
171TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_str(char const* str) {
172 return tud_cdc_n_write_str(0, str);
173}
174
175TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_flush(void) {
176 return tud_cdc_n_write_flush(0);
177}
178
179TU_ATTR_ALWAYS_INLINE static inline uint32_t tud_cdc_write_available(void) {
181}
182
183TU_ATTR_ALWAYS_INLINE static inline bool tud_cdc_write_clear(void) {
184 return tud_cdc_n_write_clear(0);
185}
186
187//--------------------------------------------------------------------+
188// Application Callback API (weak is optional)
189//--------------------------------------------------------------------+
190
191// Invoked when received new data
192TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf);
193
194// Invoked when received `wanted_char`
195TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
196
197// Invoked when a TX is complete and therefore space becomes available in TX buffer
198TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf);
199
200// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE
201TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
202
203// Invoked when line coding is change via SET_LINE_CODING
204TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding);
205
206// Invoked when received send break
207// \param[in] itf interface for which send break was received.
208// \param[in] duration_ms the length of time, in milliseconds, of the break signal. If a value of FFFFh, then the
209// device will send a break until another SendBreak request is received with value 0000h.
210TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms);
211
212//--------------------------------------------------------------------+
213// INTERNAL USBD-CLASS DRIVER API
214//--------------------------------------------------------------------+
215void cdcd_init (void);
216bool cdcd_deinit (void);
217void cdcd_reset (uint8_t rhport);
218uint16_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
219bool cdcd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
220bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
221
222#ifdef __cplusplus
223 }
224#endif
225
226#endif /* _TUSB_CDC_DEVICE_H_ */
static TU_ATTR_ALWAYS_INLINE bool tud_cdc_peek(uint8_t *ui8)
Definition: cdc_device.h:159
uint32_t tud_cdc_n_write_flush(uint8_t itf)
Definition: cdc_device.c:187
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
Definition: cdc_device.h:101
uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
Definition: cdc_device.c:295
static TU_ATTR_ALWAYS_INLINE bool tud_cdc_write_clear(void)
Definition: cdc_device.h:183
bool tud_cdc_n_ready(uint8_t itf)
Definition: cdc_device.c:123
bool tud_cdc_n_write_clear(uint8_t itf)
Definition: cdc_device.c:219
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_write_str(char const *str)
Definition: cdc_device.h:171
TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms)
bool cdcd_deinit(void)
Definition: cdc_device.c:260
void tud_cdc_n_set_wanted_char(uint8_t itf, char wanted)
Definition: cdc_device.c:140
void cdcd_reset(uint8_t rhport)
Definition: cdc_device.c:282
bool tud_cdc_n_peek(uint8_t itf, uint8_t *ui8)
Definition: cdc_device.c:158
static TU_ATTR_ALWAYS_INLINE void tud_cdc_set_wanted_char(char wanted)
Definition: cdc_device.h:139
TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf)
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_write_available(void)
Definition: cdc_device.h:179
static TU_ATTR_ALWAYS_INLINE void tud_cdc_read_flush(void)
Definition: cdc_device.h:155
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_write_flush(void)
Definition: cdc_device.h:175
TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf)
static TU_ATTR_ALWAYS_INLINE bool tud_cdc_ready(void)
Definition: cdc_device.h:123
void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t *coding)
Definition: cdc_device.c:136
void cdcd_init(void)
Definition: cdc_device.c:226
static TU_ATTR_ALWAYS_INLINE uint8_t tud_cdc_get_line_state(void)
Definition: cdc_device.h:131
uint8_t tud_cdc_n_get_line_state(uint8_t itf)
Definition: cdc_device.c:132
bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
Definition: cdc_device.c:355
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_n_write_str(uint8_t itf, char const *str)
Definition: cdc_device.h:106
bool tud_cdc_n_connected(uint8_t itf)
Definition: cdc_device.c:127
static TU_ATTR_ALWAYS_INLINE int32_t tud_cdc_n_read_char(uint8_t itf)
Definition: cdc_device.h:86
static TU_ATTR_ALWAYS_INLINE void tud_cdc_get_line_coding(cdc_line_coding_t *coding)
Definition: cdc_device.h:135
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_read(void *buffer, uint32_t bufsize)
Definition: cdc_device.h:151
static TU_ATTR_ALWAYS_INLINE int32_t tud_cdc_read_char(void)
Definition: cdc_device.h:147
bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
Definition: cdc_device.c:426
uint32_t tud_cdc_n_write_available(uint8_t itf)
Definition: cdc_device.c:215
void tud_cdc_n_read_flush(uint8_t itf)
Definition: cdc_device.c:162
TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const *cfg)
Definition: cdc_device.c:117
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_write(void const *buffer, uint32_t bufsize)
Definition: cdc_device.h:167
uint32_t tud_cdc_n_read(uint8_t itf, void *buffer, uint32_t bufsize)
Definition: cdc_device.c:151
uint32_t tud_cdc_n_write(uint8_t itf, void const *buffer, uint32_t bufsize)
Definition: cdc_device.c:171
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_write_char(char ch)
Definition: cdc_device.h:163
static TU_ATTR_ALWAYS_INLINE bool tud_cdc_connected(void)
Definition: cdc_device.h:127
static TU_ATTR_ALWAYS_INLINE uint32_t tud_cdc_available(void)
Definition: cdc_device.h:143
uint32_t tud_cdc_n_available(uint8_t itf)
Definition: cdc_device.c:147
TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const *p_line_coding)
TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
uint8_t const * buffer
Definition: midi_device.h:100
uint32_t bufsize
Definition: midi_device.h:95
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
xfer_result_t
Definition: tusb_types.h:236
CFG_TUH_MEM_ALIGN tusb_control_request_t request
Definition: usbh.c:259
volatile uint8_t stage
Definition: usbh.c:265