Skip to content

Commit b42d298

Browse files
committed
Merge branch 'master' into fix-fifo-memory-overflow
2 parents 660343d + 6e23c59 commit b42d298

37 files changed

+1242
-407
lines changed

.github/workflows/pre-commit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ jobs:
3838
3939
- name: Build Fuzzer
4040
run: |
41+
export CC=clang
42+
export CXX=clang++
4143
fuzz_harness=$(ls -d test/fuzz/device/*/)
4244
for h in $fuzz_harness
4345
do

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/audio/audio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,13 @@ typedef struct TU_ATTR_PACKED
721721
uint8_t bLength ; ///< Size of this descriptor, in bytes: 17.
722722
uint8_t bDescriptorType ; ///< Descriptor Type. Value: TUSB_DESC_CS_INTERFACE.
723723
uint8_t bDescriptorSubType ; ///< Descriptor SubType. Value: AUDIO_CS_AC_INTERFACE_INPUT_TERMINAL.
724+
uint8_t bTerminalID ; ///< Constant uniquely identifying the Terminal within the audio function. This value is used in all requests to address this terminal.
724725
uint16_t wTerminalType ; ///< Constant characterizing the type of Terminal. See: audio_terminal_type_t for USB streaming and audio_terminal_input_type_t for other input types.
725726
uint8_t bAssocTerminal ; ///< ID of the Output Terminal to which this Input Terminal is associated.
726727
uint8_t bCSourceID ; ///< ID of the Clock Entity to which this Input Terminal is connected.
727728
uint8_t bNrChannels ; ///< Number of logical output channels in the Terminal’s output audio channel cluster.
728729
uint32_t bmChannelConfig ; ///< Describes the spatial location of the logical channels. See:audio_channel_config_t.
730+
uint8_t iChannelNames ; ///< Index of a string descriptor, describing the name of the first logical channel.
729731
uint16_t bmControls ; ///< See: audio_terminal_input_control_pos_t.
730732
uint8_t iTerminal ; ///< Index of a string descriptor, describing the Input Terminal.
731733
} audio_desc_input_terminal_t;

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.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ typedef struct
6262
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
6363
uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
6464

65-
#if CFG_FIFO_MUTEX
66-
osal_mutex_def_t rx_ff_mutex;
67-
osal_mutex_def_t tx_ff_mutex;
68-
#endif
65+
OSAL_MUTEX_DEF(rx_ff_mutex);
66+
OSAL_MUTEX_DEF(tx_ff_mutex);
6967

7068
// Endpoint Transfer buffer
7169
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
@@ -248,10 +246,8 @@ void cdcd_init(void)
248246
// In this way, the most current data is prioritized.
249247
tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true);
250248

251-
#if CFG_FIFO_MUTEX
252249
tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, osal_mutex_create(&p_cdc->rx_ff_mutex));
253250
tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex), NULL);
254-
#endif
255251
}
256252
}
257253

@@ -436,7 +432,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
436432
// Received new data
437433
if ( ep_addr == p_cdc->ep_out )
438434
{
439-
tu_fifo_write_n(&p_cdc->rx_ff, &p_cdc->epout_buf, (uint16_t) xferred_bytes);
435+
tu_fifo_write_n(&p_cdc->rx_ff, p_cdc->epout_buf, (uint16_t) xferred_bytes);
440436

441437
// Check for wanted char and invoke callback if needed
442438
if ( tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1) )

src/class/cdc/cdc_device.h

Lines changed: 2 additions & 3 deletions
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
//--------------------------------------------------------------------+
@@ -81,7 +80,7 @@ int32_t tud_cdc_n_read_char (uint8_t itf);
8180
// Clear the received FIFO
8281
void tud_cdc_n_read_flush (uint8_t itf);
8382

84-
// Get a byte from FIFO at the specified position without removing it
83+
// Get a byte from FIFO without removing it
8584
bool tud_cdc_n_peek (uint8_t itf, uint8_t* ui8);
8685

8786
// Write bytes to TX FIFO, data may remain in the FIFO for a while
@@ -135,7 +134,7 @@ TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf);
135134
// Invoked when received `wanted_char`
136135
TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
137136

138-
// Invoked when space becomes available in TX buffer
137+
// Invoked when a TX is complete and therefore space becomes available in TX buffer
139138
TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf);
140139

141140
// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE

0 commit comments

Comments
 (0)