Open FFBoard
Open source force feedback firmware
cdc_host.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_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_CODING_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 data format
152bool tuh_cdc_set_data_format(uint8_t idx, uint8_t stop_bits, uint8_t parity, uint8_t data_bits, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
153
154// Request to Set Line Coding = baudrate + data format
155// Note: only implemented by ACM and CH34x, not supported by FTDI and CP210x yet
156bool 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);
157
158// Request to Get Line Coding (ACM only)
159// Should only use if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() never got invoked and
160// CFG_TUH_CDC_LINE_CODING_ON_ENUM is not defined
161// bool tuh_cdc_get_line_coding(uint8_t idx, cdc_line_coding_t* coding);
162
163// Connect by set both DTR, RTS
164TU_ATTR_ALWAYS_INLINE static inline
165bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
167}
168
169// Disconnect by clear both DTR, RTS
170TU_ATTR_ALWAYS_INLINE static inline
173}
174
175//--------------------------------------------------------------------+
176// CDC APPLICATION CALLBACKS
177//--------------------------------------------------------------------+
178
179// Invoked when a device with CDC interface is mounted
180// idx is index of cdc interface in the internal pool.
181TU_ATTR_WEAK extern void tuh_cdc_mount_cb(uint8_t idx);
182
183// Invoked when a device with CDC interface is unmounted
184TU_ATTR_WEAK extern void tuh_cdc_umount_cb(uint8_t idx);
185
186// Invoked when received new data
187TU_ATTR_WEAK extern void tuh_cdc_rx_cb(uint8_t idx);
188
189// Invoked when a TX is complete and therefore space becomes available in TX buffer
190TU_ATTR_WEAK extern void tuh_cdc_tx_complete_cb(uint8_t idx);
191
192//--------------------------------------------------------------------+
193// Internal Class Driver API
194//--------------------------------------------------------------------+
195bool cdch_init (void);
196bool cdch_deinit (void);
197bool cdch_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len);
198bool cdch_set_config (uint8_t dev_addr, uint8_t itf_num);
199bool cdch_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
200void cdch_close (uint8_t dev_addr);
201
202#ifdef __cplusplus
203 }
204#endif
205
206#endif /* _TUSB_CDC_HOST_H_ */
uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num)
Definition: cdc_host.c:276
bool tuh_cdc_write_clear(uint8_t idx)
Definition: cdc_host.c:354
bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
Definition: cdc_host.c:733
void cdch_close(uint8_t dev_addr)
Definition: cdc_host.c:650
bool tuh_cdc_set_data_format(uint8_t idx, uint8_t stop_bits, uint8_t parity, uint8_t data_bits, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
Definition: cdc_host.c:571
TU_ATTR_WEAK void tuh_cdc_mount_cb(uint8_t idx)
bool tuh_cdc_set_baudrate(uint8_t idx, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
Definition: cdc_host.c:548
uint32_t tuh_cdc_write_available(uint8_t idx)
Definition: cdc_host.c:361
bool tuh_cdc_read_clear(uint8_t idx)
Definition: cdc_host.c:393
static TU_ATTR_ALWAYS_INLINE bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
Definition: cdc_host.h:165
bool cdch_set_config(uint8_t dev_addr, uint8_t itf_num)
Definition: cdc_host.c:772
bool cdch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
Definition: cdc_host.c:668
uint32_t tuh_cdc_read(uint8_t idx, void *buffer, uint32_t bufsize)
Definition: cdc_host.c:372
bool tuh_cdc_itf_get_info(uint8_t idx, tuh_itf_info_t *info)
Definition: cdc_host.c:285
bool tuh_cdc_mounted(uint8_t idx)
Definition: cdc_host.c:307
TU_ATTR_WEAK void tuh_cdc_umount_cb(uint8_t idx)
bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t *line_coding)
Definition: cdc_host.c:327
bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
Definition: cdc_host.c:525
bool tuh_cdc_peek(uint8_t idx, uint8_t *ch)
Definition: cdc_host.c:386
bool 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)
Definition: cdc_host.c:597
TU_ATTR_WEAK void tuh_cdc_rx_cb(uint8_t idx)
bool tuh_cdc_get_rts(uint8_t idx)
Definition: cdc_host.c:320
TU_ATTR_WEAK void tuh_cdc_tx_complete_cb(uint8_t idx)
bool tuh_cdc_get_dtr(uint8_t idx)
Definition: cdc_host.c:313
uint32_t tuh_cdc_write(uint8_t idx, void const *buffer, uint32_t bufsize)
Definition: cdc_host.c:340
bool cdch_init(void)
Definition: cdc_host.c:624
static TU_ATTR_ALWAYS_INLINE bool tuh_cdc_connected(uint8_t idx)
Definition: cdc_host.h:92
static TU_ATTR_ALWAYS_INLINE bool tuh_cdc_disconnect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
Definition: cdc_host.h:171
uint32_t tuh_cdc_write_flush(uint8_t idx)
Definition: cdc_host.c:347
bool cdch_deinit(void)
Definition: cdc_host.c:641
uint32_t tuh_cdc_read_available(uint8_t idx)
Definition: cdc_host.c:379
uint8_t dev_addr
Definition: dcd_pic32mz.c:81
@ CDC_CONTROL_LINE_STATE_DTR
Definition: cdc.h:185
@ CDC_CONTROL_LINE_STATE_RTS
Definition: cdc.h:186
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
uint8_t daddr
Definition: usbh.c:264
uintptr_t user_data
Definition: usbh.c:262
tuh_xfer_cb_t complete_cb
Definition: usbh.c:261
void(* tuh_xfer_cb_t)(tuh_xfer_t *xfer)
Definition: usbh.h:44