Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Host CDC #1809

Merged
merged 28 commits into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4f03695
adding tuh_cdc_mount_cb/tuh_cdc_umount_cb
hathach Dec 14, 2022
f0c51ea
cdc check for bNumEndpoints before checking for endpoint descriptor
hathach Dec 14, 2022
f62f973
minor rename
hathach Dec 14, 2022
bd1f7f8
add common EPSIZE for bulk/iso in full and highspeed
hathach Dec 15, 2022
4811b34
stub
hathach Dec 15, 2022
fc9321c
correct cdc usbh_driver_set_config_complete()
hathach Dec 16, 2022
7004914
fix hid host incorrect edpt release if failed to transmit
hathach Dec 20, 2022
854e522
finalizing cdc host, has a working read/write
hathach Dec 20, 2022
37529c4
fix ci
hathach Dec 21, 2022
d1ea384
rename TU_LOG_VAR to TU_LOG_PTR, print out setup of failed control tr…
hathach Dec 21, 2022
cb2af4c
minor debug log
hathach Dec 21, 2022
b3e63c3
updat cdc host app
hathach Dec 21, 2022
76021c7
rename tud_edpt_stream_write_xfer
hathach Dec 21, 2022
22b62f8
add tu_edpt_stream_write_zlp_if_needed()
hathach Dec 21, 2022
badb30a
correct cdc host app
hathach Dec 21, 2022
edc559c
fix ci
hathach Dec 21, 2022
84a483f
add more host cdc API
hathach Dec 21, 2022
cd9008e
add tuh_cdc_tx_complete_cb() callback
hathach Dec 21, 2022
9e8ea44
add tuh_cdc_write_clear, rename read_flush() to read_clear()
hathach Dec 21, 2022
8323e4b
moving edpt_stream API into common tusb.c
hathach Dec 21, 2022
e3c9d94
fix stream read count computation
hathach Dec 22, 2022
2d53612
finish moving edpt stream to tusb.c
hathach Dec 22, 2022
c99af90
fix typo
hathach Dec 22, 2022
11233e4
minor clean up
hathach Dec 22, 2022
05c119c
cdc host, add set line coding API
hathach Dec 22, 2022
14d45b5
correct host cdc enum
hathach Dec 22, 2022
f33883c
add tuh_cdc_get_local_line_coding()
hathach Dec 22, 2022
396716c
clean up
hathach Dec 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/host/cdc_msc_hid/CMakeLists.txt
Expand Up @@ -14,6 +14,7 @@ add_executable(${PROJECT})

# Example source
target_sources(${PROJECT} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/cdc_app.c
${CMAKE_CURRENT_SOURCE_DIR}/src/hid_app.c
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/src/msc_app.c
Expand Down
3 changes: 2 additions & 1 deletion examples/host/cdc_msc_hid/Makefile
Expand Up @@ -7,7 +7,8 @@ INC += \

# Example source
EXAMPLE_SOURCE = \
src/hid_app.c \
src/cdc_app.c \
src/hid_app.c \
src/main.c \
src/msc_app.c \

Expand Down
113 changes: 113 additions & 0 deletions examples/host/cdc_msc_hid/src/cdc_app.c
@@ -0,0 +1,113 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022, Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/

#include "tusb.h"
#include "bsp/board.h"

//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+


//------------- IMPLEMENTATION -------------//

size_t get_console_inputs(uint8_t* buf, size_t bufsize)
{
size_t count = 0;
while (count < bufsize)
{
int ch = board_getchar();
if ( ch <= 0 ) break;

buf[count] = (uint8_t) ch;
count++;
}

return count;
}

void cdc_app_task(void)
{
uint8_t buf[64+1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf)-1;

uint32_t count = get_console_inputs(buf, bufsize);
buf[count] = 0;

// loop over all mounted interfaces
for(uint8_t idx=0; idx<CFG_TUH_CDC; idx++)
{
if ( tuh_cdc_mounted(idx) )
{
// console --> cdc interfaces
if (count)
{
tuh_cdc_write(idx, buf, count);
tuh_cdc_write_flush(idx);
}
}
}
}

// Invoked when received new data
void tuh_cdc_rx_cb(uint8_t idx)
{
uint8_t buf[64+1]; // +1 for extra null character
uint32_t const bufsize = sizeof(buf)-1;

// forward cdc interfaces -> console
uint32_t count = tuh_cdc_read(idx, buf, bufsize);
buf[count] = 0;

printf((char*) buf);
}

void tuh_cdc_mount_cb(uint8_t idx)
{
tuh_cdc_itf_info_t itf_info = { 0 };
tuh_cdc_itf_get_info(idx, &itf_info);

printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);

#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
// CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration
// otherwise you need to call tuh_cdc_set_line_coding() first
cdc_line_coding_t line_coding = { 0 };
if ( tuh_cdc_get_local_line_coding(idx, &line_coding) )
{
printf(" Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity , line_coding.data_bits);
}
#endif
}

void tuh_cdc_umount_cb(uint8_t idx)
{
tuh_cdc_itf_info_t itf_info = { 0 };
tuh_cdc_itf_get_info(idx, &itf_info);

printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
}
29 changes: 3 additions & 26 deletions examples/host/cdc_msc_hid/src/main.c
Expand Up @@ -35,7 +35,7 @@
//--------------------------------------------------------------------+
void led_blinking_task(void);

extern void cdc_task(void);
extern void cdc_app_task(void);
extern void hid_app_task(void);

/*------------- MAIN -------------*/
Expand All @@ -52,38 +52,15 @@ int main(void)
{
// tinyusb host task
tuh_task();
led_blinking_task();

cdc_task();
led_blinking_task();
cdc_app_task();
hid_app_task();
}

return 0;
}

//--------------------------------------------------------------------+
// USB CDC
//--------------------------------------------------------------------+
CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };

// invoked ISR context
void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes)
{
(void) event;
(void) pipe_id;
(void) xferred_bytes;

printf(serial_in_buffer);
tu_memclr(serial_in_buffer, sizeof(serial_in_buffer));

tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // waiting for next data
}

void cdc_task(void)
{

}

//--------------------------------------------------------------------+
// TinyUSB Callbacks
//--------------------------------------------------------------------+
Expand Down
11 changes: 11 additions & 0 deletions examples/host/cdc_msc_hid/src/tusb_config.h
Expand Up @@ -108,6 +108,17 @@
#define CFG_TUH_HID_EPIN_BUFSIZE 64
#define CFG_TUH_HID_EPOUT_BUFSIZE 64

//------------- CDC -------------//

// Set Line Control state on enumeration/mounted:
// DTR ( bit 0), RTS (bit 1)
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03

// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
// bit rate = 115200, 1 stop bit, no parity, 8 bit data width
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }


#ifdef __cplusplus
}
#endif
Expand Down
36 changes: 24 additions & 12 deletions src/class/cdc/cdc.h
Expand Up @@ -41,16 +41,6 @@
/** \defgroup ClassDriver_CDC_Common Common Definitions
* @{ */

// TODO remove
/// CDC Pipe ID, used to indicate which pipe the API is addressing to (Notification, Out, In)
typedef enum
{
CDC_PIPE_NOTIFICATION , ///< Notification pipe
CDC_PIPE_DATA_IN , ///< Data in pipe
CDC_PIPE_DATA_OUT , ///< Data out pipe
CDC_PIPE_ERROR , ///< Invalid Pipe ID
}cdc_pipeid_t;

//--------------------------------------------------------------------+
// CDC Communication Interface Class
//--------------------------------------------------------------------+
Expand Down Expand Up @@ -192,6 +182,28 @@ typedef enum
CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60,
}cdc_management_request_t;

enum
{
CDC_CONTROL_LINE_STATE_DTR = 0x01,
CDC_CONTROL_LINE_STATE_RTS = 0x02,
};

enum
{
CDC_LINE_CONDING_STOP_BITS_1 = 0, // 1 bit
CDC_LINE_CONDING_STOP_BITS_1_5 = 1, // 1.5 bits
CDC_LINE_CONDING_STOP_BITS_2 = 2, // 2 bits
};

enum
{
CDC_LINE_CODING_PARITY_NONE = 0,
CDC_LINE_CODING_PARITY_ODD = 1,
CDC_LINE_CODING_PARITY_EVEN = 2,
CDC_LINE_CODING_PARITY_MARK = 3,
CDC_LINE_CODING_PARITY_SPACE = 4,
};

//--------------------------------------------------------------------+
// Management Element Notification (Notification Endpoint)
//--------------------------------------------------------------------+
Expand Down Expand Up @@ -390,8 +402,8 @@ TU_VERIFY_STATIC(sizeof(cdc_line_coding_t) == 7, "size is not correct");

typedef struct TU_ATTR_PACKED
{
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.
uint16_t half_duplex_carrier_control : 1;
uint16_t dtr : 1;
uint16_t rts : 1;
uint16_t : 14;
} cdc_line_control_state_t;

Expand Down
1 change: 0 additions & 1 deletion src/class/cdc/cdc_device.h
Expand Up @@ -27,7 +27,6 @@
#ifndef _TUSB_CDC_DEVICE_H_
#define _TUSB_CDC_DEVICE_H_

#include "common/tusb_common.h"
#include "cdc.h"

//--------------------------------------------------------------------+
Expand Down