mikroSDK Reference Manual
cdc_device.h
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
53//--------------------------------------------------------------------+
54// Application API (Multiple Ports)
55// CFG_TUD_CDC > 1
56//--------------------------------------------------------------------+
57
58// Check if terminal is connected to this port
59bool tud_cdc_n_connected (uint8_t itf);
60
61// Get current line state. Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
62uint8_t tud_cdc_n_get_line_state (uint8_t itf);
63
64// Get current line encoding: bit rate, stop bits parity etc ..
65void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding);
66
67// Set special character that will trigger tud_cdc_rx_wanted_cb() callback on receiving
68void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted);
69
70// Get the number of bytes available for reading
71uint32_t tud_cdc_n_available (uint8_t itf);
72
73// Read received bytes
74uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
75
76// Read a byte, return -1 if there is none
77static inline
78int32_t tud_cdc_n_read_char (uint8_t itf);
79
80// Clear the received FIFO
81void tud_cdc_n_read_flush (uint8_t itf);
82
83// Get a byte from FIFO without removing it
84bool tud_cdc_n_peek (uint8_t itf, uint8_t* ui8);
85
86// Write bytes to TX FIFO, data may remain in the FIFO for a while
87uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
88
89// Write a byte
90static inline
91uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
92
93// Write a null-terminated string
94static inline
95uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str);
96
97// Force sending data if possible, return number of forced bytes
98uint32_t tud_cdc_n_write_flush (uint8_t itf);
99
100// Return the number of bytes (characters) available for writing to TX FIFO buffer in a single n_write operation.
101uint32_t tud_cdc_n_write_available (uint8_t itf);
102
103// Clear the transmit FIFO
104bool tud_cdc_n_write_clear (uint8_t itf);
105
106//--------------------------------------------------------------------+
107// Application API (Single Port)
108//--------------------------------------------------------------------+
109static inline bool tud_cdc_connected (void);
110static inline uint8_t tud_cdc_get_line_state (void);
111static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding);
112static inline void tud_cdc_set_wanted_char (char wanted);
113
114static inline uint32_t tud_cdc_available (void);
115static inline int32_t tud_cdc_read_char (void);
116static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize);
117static inline void tud_cdc_read_flush (void);
118static inline bool tud_cdc_peek (uint8_t* ui8);
119
120static inline uint32_t tud_cdc_write_char (char ch);
121static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize);
122static inline uint32_t tud_cdc_write_str (char const* str);
123static inline uint32_t tud_cdc_write_flush (void);
124static inline uint32_t tud_cdc_write_available (void);
125static inline bool tud_cdc_write_clear (void);
126
127//--------------------------------------------------------------------+
128// Application Callback API (weak is optional)
129//--------------------------------------------------------------------+
130
131// Invoked when received new data
132TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf);
133
134// Invoked when received `wanted_char`
135TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
136
137// Invoked when a TX is complete and therefore space becomes available in TX buffer
138TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf);
139
140// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE
141TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
142
143// Invoked when line coding is change via SET_LINE_CODING
144TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding);
145
146// Invoked when received send break
147TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms);
148
149//--------------------------------------------------------------------+
150// Inline Functions
151//--------------------------------------------------------------------+
152static inline int32_t tud_cdc_n_read_char (uint8_t itf)
153{
154 uint8_t ch;
155 return tud_cdc_n_read(itf, &ch, 1) ? (int32_t) ch : -1;
156}
157
158static inline uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
159{
160 return tud_cdc_n_write(itf, &ch, 1);
161}
162
163static inline uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str)
164{
165 return tud_cdc_n_write(itf, str, strlen(str));
166}
167
168static inline bool tud_cdc_connected (void)
169{
170 return tud_cdc_n_connected(0);
171}
172
173static inline uint8_t tud_cdc_get_line_state (void)
174{
175 return tud_cdc_n_get_line_state(0);
176}
177
178static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding)
179{
180 tud_cdc_n_get_line_coding(0, coding);
181}
182
183static inline void tud_cdc_set_wanted_char (char wanted)
184{
185 tud_cdc_n_set_wanted_char(0, wanted);
186}
187
188static inline uint32_t tud_cdc_available (void)
189{
190 return tud_cdc_n_available(0);
191}
192
193static inline int32_t tud_cdc_read_char (void)
194{
195 return tud_cdc_n_read_char(0);
196}
197
198static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize)
199{
200 return tud_cdc_n_read(0, buffer, bufsize);
201}
202
203static inline void tud_cdc_read_flush (void)
204{
205 tud_cdc_n_read_flush(0);
206}
207
208static inline bool tud_cdc_peek (uint8_t* ui8)
209{
210 return tud_cdc_n_peek(0, ui8);
211}
212
213static inline uint32_t tud_cdc_write_char (char ch)
214{
215 return tud_cdc_n_write_char(0, ch);
216}
217
218static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize)
219{
220 return tud_cdc_n_write(0, buffer, bufsize);
221}
222
223static inline uint32_t tud_cdc_write_str (char const* str)
224{
225 return tud_cdc_n_write_str(0, str);
226}
227
228static inline uint32_t tud_cdc_write_flush (void)
229{
230 return tud_cdc_n_write_flush(0);
231}
232
233static inline uint32_t tud_cdc_write_available(void)
234{
235 return tud_cdc_n_write_available(0);
236}
237
238static inline bool tud_cdc_write_clear(void)
239{
240 return tud_cdc_n_write_clear(0);
241}
242
246//--------------------------------------------------------------------+
247// INTERNAL USBD-CLASS DRIVER API
248//--------------------------------------------------------------------+
249void cdcd_init (void);
250void cdcd_reset (uint8_t rhport);
251uint16_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
252bool cdcd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
253bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
254
255#ifdef __cplusplus
256 }
257#endif
258
259#endif /* _TUSB_CDC_DEVICE_H_ */
AUDIO Channel Cluster Descriptor (4.1)
Definition audio.h:647