Skip to content

Commit 2777df4

Browse files
authored
Merge pull request hathach#1809 from hathach/host-cdc
Support Host CDC
2 parents 97984b4 + 396716c commit 2777df4

File tree

19 files changed

+1087
-262
lines changed

19 files changed

+1087
-262
lines changed

examples/host/cdc_msc_hid/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_executable(${PROJECT})
1414

1515
# Example source
1616
target_sources(${PROJECT} PUBLIC
17+
${CMAKE_CURRENT_SOURCE_DIR}/src/cdc_app.c
1718
${CMAKE_CURRENT_SOURCE_DIR}/src/hid_app.c
1819
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
1920
${CMAKE_CURRENT_SOURCE_DIR}/src/msc_app.c

examples/host/cdc_msc_hid/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ INC += \
77

88
# Example source
99
EXAMPLE_SOURCE = \
10-
src/hid_app.c \
10+
src/cdc_app.c \
11+
src/hid_app.c \
1112
src/main.c \
1213
src/msc_app.c \
1314

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2022, 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+
#include "tusb.h"
28+
#include "bsp/board.h"
29+
30+
//--------------------------------------------------------------------+
31+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
32+
//--------------------------------------------------------------------+
33+
34+
35+
//------------- IMPLEMENTATION -------------//
36+
37+
size_t get_console_inputs(uint8_t* buf, size_t bufsize)
38+
{
39+
size_t count = 0;
40+
while (count < bufsize)
41+
{
42+
int ch = board_getchar();
43+
if ( ch <= 0 ) break;
44+
45+
buf[count] = (uint8_t) ch;
46+
count++;
47+
}
48+
49+
return count;
50+
}
51+
52+
void cdc_app_task(void)
53+
{
54+
uint8_t buf[64+1]; // +1 for extra null character
55+
uint32_t const bufsize = sizeof(buf)-1;
56+
57+
uint32_t count = get_console_inputs(buf, bufsize);
58+
buf[count] = 0;
59+
60+
// loop over all mounted interfaces
61+
for(uint8_t idx=0; idx<CFG_TUH_CDC; idx++)
62+
{
63+
if ( tuh_cdc_mounted(idx) )
64+
{
65+
// console --> cdc interfaces
66+
if (count)
67+
{
68+
tuh_cdc_write(idx, buf, count);
69+
tuh_cdc_write_flush(idx);
70+
}
71+
}
72+
}
73+
}
74+
75+
// Invoked when received new data
76+
void tuh_cdc_rx_cb(uint8_t idx)
77+
{
78+
uint8_t buf[64+1]; // +1 for extra null character
79+
uint32_t const bufsize = sizeof(buf)-1;
80+
81+
// forward cdc interfaces -> console
82+
uint32_t count = tuh_cdc_read(idx, buf, bufsize);
83+
buf[count] = 0;
84+
85+
printf((char*) buf);
86+
}
87+
88+
void tuh_cdc_mount_cb(uint8_t idx)
89+
{
90+
tuh_cdc_itf_info_t itf_info = { 0 };
91+
tuh_cdc_itf_get_info(idx, &itf_info);
92+
93+
printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
94+
95+
#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
96+
// CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration
97+
// otherwise you need to call tuh_cdc_set_line_coding() first
98+
cdc_line_coding_t line_coding = { 0 };
99+
if ( tuh_cdc_get_local_line_coding(idx, &line_coding) )
100+
{
101+
printf(" Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
102+
printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity , line_coding.data_bits);
103+
}
104+
#endif
105+
}
106+
107+
void tuh_cdc_umount_cb(uint8_t idx)
108+
{
109+
tuh_cdc_itf_info_t itf_info = { 0 };
110+
tuh_cdc_itf_get_info(idx, &itf_info);
111+
112+
printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
113+
}

examples/host/cdc_msc_hid/src/main.c

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//--------------------------------------------------------------------+
3636
void led_blinking_task(void);
3737

38-
extern void cdc_task(void);
38+
extern void cdc_app_task(void);
3939
extern void hid_app_task(void);
4040

4141
/*------------- MAIN -------------*/
@@ -52,38 +52,15 @@ int main(void)
5252
{
5353
// tinyusb host task
5454
tuh_task();
55-
led_blinking_task();
5655

57-
cdc_task();
56+
led_blinking_task();
57+
cdc_app_task();
5858
hid_app_task();
5959
}
6060

6161
return 0;
6262
}
6363

64-
//--------------------------------------------------------------------+
65-
// USB CDC
66-
//--------------------------------------------------------------------+
67-
CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };
68-
69-
// invoked ISR context
70-
void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes)
71-
{
72-
(void) event;
73-
(void) pipe_id;
74-
(void) xferred_bytes;
75-
76-
printf(serial_in_buffer);
77-
tu_memclr(serial_in_buffer, sizeof(serial_in_buffer));
78-
79-
tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // waiting for next data
80-
}
81-
82-
void cdc_task(void)
83-
{
84-
85-
}
86-
8764
//--------------------------------------------------------------------+
8865
// TinyUSB Callbacks
8966
//--------------------------------------------------------------------+

examples/host/cdc_msc_hid/src/tusb_config.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@
108108
#define CFG_TUH_HID_EPIN_BUFSIZE 64
109109
#define CFG_TUH_HID_EPOUT_BUFSIZE 64
110110

111+
//------------- CDC -------------//
112+
113+
// Set Line Control state on enumeration/mounted:
114+
// DTR ( bit 0), RTS (bit 1)
115+
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03
116+
117+
// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
118+
// bit rate = 115200, 1 stop bit, no parity, 8 bit data width
119+
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
120+
121+
111122
#ifdef __cplusplus
112123
}
113124
#endif

src/class/cdc/cdc.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@
4141
/** \defgroup ClassDriver_CDC_Common Common Definitions
4242
* @{ */
4343

44-
// TODO remove
45-
/// CDC Pipe ID, used to indicate which pipe the API is addressing to (Notification, Out, In)
46-
typedef enum
47-
{
48-
CDC_PIPE_NOTIFICATION , ///< Notification pipe
49-
CDC_PIPE_DATA_IN , ///< Data in pipe
50-
CDC_PIPE_DATA_OUT , ///< Data out pipe
51-
CDC_PIPE_ERROR , ///< Invalid Pipe ID
52-
}cdc_pipeid_t;
53-
5444
//--------------------------------------------------------------------+
5545
// CDC Communication Interface Class
5646
//--------------------------------------------------------------------+
@@ -192,6 +182,28 @@ typedef enum
192182
CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60,
193183
}cdc_management_request_t;
194184

185+
enum
186+
{
187+
CDC_CONTROL_LINE_STATE_DTR = 0x01,
188+
CDC_CONTROL_LINE_STATE_RTS = 0x02,
189+
};
190+
191+
enum
192+
{
193+
CDC_LINE_CONDING_STOP_BITS_1 = 0, // 1 bit
194+
CDC_LINE_CONDING_STOP_BITS_1_5 = 1, // 1.5 bits
195+
CDC_LINE_CONDING_STOP_BITS_2 = 2, // 2 bits
196+
};
197+
198+
enum
199+
{
200+
CDC_LINE_CODING_PARITY_NONE = 0,
201+
CDC_LINE_CODING_PARITY_ODD = 1,
202+
CDC_LINE_CODING_PARITY_EVEN = 2,
203+
CDC_LINE_CODING_PARITY_MARK = 3,
204+
CDC_LINE_CODING_PARITY_SPACE = 4,
205+
};
206+
195207
//--------------------------------------------------------------------+
196208
// Management Element Notification (Notification Endpoint)
197209
//--------------------------------------------------------------------+
@@ -390,8 +402,8 @@ TU_VERIFY_STATIC(sizeof(cdc_line_coding_t) == 7, "size is not correct");
390402

391403
typedef struct TU_ATTR_PACKED
392404
{
393-
uint16_t dte_is_present : 1; ///< Indicates to DCE if DTE is presentor not. This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR.
394-
uint16_t half_duplex_carrier_control : 1;
405+
uint16_t dtr : 1;
406+
uint16_t rts : 1;
395407
uint16_t : 14;
396408
} cdc_line_control_state_t;
397409

src/class/cdc/cdc_device.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#ifndef _TUSB_CDC_DEVICE_H_
2828
#define _TUSB_CDC_DEVICE_H_
2929

30-
#include "common/tusb_common.h"
3130
#include "cdc.h"
3231

3332
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)