mikroSDK Reference Manual
cdc_host.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_HOST_H_
28#define _TUSB_CDC_HOST_H_
29
30#include "cdc.h"
31
32#ifdef __cplusplus
33 extern "C" {
34#endif
35
36//--------------------------------------------------------------------+
37// Class Driver Configuration
38//--------------------------------------------------------------------+
39
40// Set Line Control state on enumeration/mounted: DTR ( bit 0), RTS (bit 1)
41#ifndef CFG_TUH_CDC_LINE_CONTROL_ON_ENUM
42#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0
43#endif
44
45// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
46//#ifndef CFG_TUH_CDC_LINE_CODING_ON_ENUM
47//#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
48//#endif
49
50// RX FIFO size
51#ifndef CFG_TUH_CDC_RX_BUFSIZE
52#define CFG_TUH_CDC_RX_BUFSIZE USBH_EPSIZE_BULK_MAX
53#endif
54
55// RX Endpoint size
56#ifndef CFG_TUH_CDC_RX_EPSIZE
57#define CFG_TUH_CDC_RX_EPSIZE USBH_EPSIZE_BULK_MAX
58#endif
59
60// TX FIFO size
61#ifndef CFG_TUH_CDC_TX_BUFSIZE
62#define CFG_TUH_CDC_TX_BUFSIZE USBH_EPSIZE_BULK_MAX
63#endif
64
65// TX Endpoint size
66#ifndef CFG_TUH_CDC_TX_EPSIZE
67#define CFG_TUH_CDC_TX_EPSIZE USBH_EPSIZE_BULK_MAX
68#endif
69
70//--------------------------------------------------------------------+
71// Application API
72//--------------------------------------------------------------------+
73
74// Get Interface index from device address + interface number
75// return TUSB_INDEX_INVALID_8 (0xFF) if not found
76uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num);
77
78// Get Interface information
79// return true if index is correct and interface is currently mounted
80bool tuh_cdc_itf_get_info(uint8_t idx, tuh_itf_info_t* info);
81
82// Check if a interface is mounted
83bool tuh_cdc_mounted(uint8_t idx);
84
85// Get current DTR status
86bool tuh_cdc_get_dtr(uint8_t idx);
87
88// Get current RTS status
89bool tuh_cdc_get_rts(uint8_t idx);
90
91// Check if interface is connected (DTR active)
92TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx)
93{
94 return tuh_cdc_get_dtr(idx);
95}
96
97// Get local (saved/cached) version of line coding.
98// This function should return correct values if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding()
99// are invoked previously or CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined.
100// NOTE: This function does not make any USB transfer request to device.
101bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding);
102
103//--------------------------------------------------------------------+
104// Write API
105//--------------------------------------------------------------------+
106
107// Get the number of bytes available for writing
108uint32_t tuh_cdc_write_available(uint8_t idx);
109
110// Write to cdc interface
111uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize);
112
113// Force sending data if possible, return number of forced bytes
114uint32_t tuh_cdc_write_flush(uint8_t idx);
115
116// Clear the transmit FIFO
117bool tuh_cdc_write_clear(uint8_t idx);
118
119//--------------------------------------------------------------------+
120// Read API
121//--------------------------------------------------------------------+
122
123// Get the number of bytes available for reading
124uint32_t tuh_cdc_read_available(uint8_t idx);
125
126// Read from cdc interface
127uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize);
128
129// Get a byte from RX FIFO without removing it
130bool tuh_cdc_peek(uint8_t idx, uint8_t* ch);
131
132// Clear the received FIFO
133bool tuh_cdc_read_clear (uint8_t idx);
134
135//--------------------------------------------------------------------+
136// Control Endpoint (Request) API
137// Each Function will make a USB control transfer request to/from device
138// - If complete_cb is provided, the function will return immediately and invoke
139// the callback when request is complete.
140// - If complete_cb is NULL, the function will block until request is complete.
141// - In this case, user_data should be pointed to xfer_result_t to hold the transfer result.
142// - The function will return true if transfer is successful, false otherwise.
143//--------------------------------------------------------------------+
144
145// Request to Set Control Line State: DTR (bit 0), RTS (bit 1)
146bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
147
148// Request to set baudrate
149bool tuh_cdc_set_baudrate(uint8_t idx, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
150
151// Request to Set Line Coding (ACM only)
152// Should only use if you don't work with serial devices such as FTDI/CP210x
153bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
154
155// Request to Get Line Coding (ACM only)
156// Should only use if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() never got invoked and
157// CFG_TUH_CDC_LINE_CODING_ON_ENUM is not defined
158// bool tuh_cdc_get_line_coding(uint8_t idx, cdc_line_coding_t* coding);
159
160// Connect by set both DTR, RTS
161TU_ATTR_ALWAYS_INLINE static inline
162bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
163{
164 return tuh_cdc_set_control_line_state(idx, CDC_CONTROL_LINE_STATE_DTR | CDC_CONTROL_LINE_STATE_RTS, complete_cb, user_data);
165}
166
167// Disconnect by clear both DTR, RTS
168TU_ATTR_ALWAYS_INLINE static inline
169bool tuh_cdc_disconnect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
170{
171 return tuh_cdc_set_control_line_state(idx, 0x00, complete_cb, user_data);
172}
173
174//--------------------------------------------------------------------+
175// CDC APPLICATION CALLBACKS
176//--------------------------------------------------------------------+
177
178// Invoked when a device with CDC interface is mounted
179// idx is index of cdc interface in the internal pool.
180TU_ATTR_WEAK extern void tuh_cdc_mount_cb(uint8_t idx);
181
182// Invoked when a device with CDC interface is unmounted
183TU_ATTR_WEAK extern void tuh_cdc_umount_cb(uint8_t idx);
184
185// Invoked when received new data
186TU_ATTR_WEAK extern void tuh_cdc_rx_cb(uint8_t idx);
187
188// Invoked when a TX is complete and therefore space becomes available in TX buffer
189TU_ATTR_WEAK extern void tuh_cdc_tx_complete_cb(uint8_t idx);
190
191//--------------------------------------------------------------------+
192// Internal Class Driver API
193//--------------------------------------------------------------------+
194void cdch_init (void);
195bool cdch_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len);
196bool cdch_set_config (uint8_t dev_addr, uint8_t itf_num);
197bool cdch_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
198void cdch_close (uint8_t dev_addr);
199
200#ifdef __cplusplus
201 }
202#endif
203
204#endif /* _TUSB_CDC_HOST_H_ */
AUDIO Channel Cluster Descriptor (4.1)
Definition audio.h:647
Definition usbh.h:74