Skip to content

Commit 0df3bfb

Browse files
committed
update cdc_msc/cdc_msc_freertos to also support notification
1 parent 41606a5 commit 0df3bfb

File tree

7 files changed

+42
-30
lines changed

7 files changed

+42
-30
lines changed

examples/device/cdc_dual_ports/src/usb_descriptors.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
//--------------------------------------------------------------------+
4343
// Device Descriptors
4444
//--------------------------------------------------------------------+
45-
tusb_desc_device_t const desc_device =
46-
{
45+
tusb_desc_device_t const desc_device = {
4746
.bLength = sizeof(tusb_desc_device_t),
4847
.bDescriptorType = TUSB_DESC_DEVICE,
4948
.bcdUSB = USB_BCD,
@@ -68,16 +67,14 @@ tusb_desc_device_t const desc_device =
6867

6968
// Invoked when received GET DEVICE DESCRIPTOR
7069
// Application return pointer to descriptor
71-
uint8_t const * tud_descriptor_device_cb(void)
72-
{
70+
uint8_t const *tud_descriptor_device_cb(void) {
7371
return (uint8_t const *) &desc_device;
7472
}
7573

7674
//--------------------------------------------------------------------+
7775
// Configuration Descriptor
7876
//--------------------------------------------------------------------+
79-
enum
80-
{
77+
enum {
8178
ITF_NUM_CDC_0 = 0,
8279
ITF_NUM_CDC_0_DATA,
8380
ITF_NUM_CDC_1,
@@ -193,7 +190,7 @@ uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {
193190
// Application return pointer to descriptor
194191
// Descriptor contents must exist long enough for transfer to complete
195192
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
196-
(void) index;// for multiple configurations
193+
(void) index; // for multiple configurations
197194

198195
#if TUD_OPT_HIGH_SPEED
199196
// Although we are highspeed, host may be fullspeed.

examples/device/cdc_msc/src/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ void cdc_task(void) {
119119
tud_cdc_write(buf, count);
120120
tud_cdc_write_flush();
121121
}
122+
123+
// Press on-board button to send Uart status notification
124+
static uint32_t btn_prev = 0;
125+
static cdc_notify_uart_state_t uart_state = { .value = 0 };
126+
const uint32_t btn = board_button_read();
127+
if (!btn_prev && btn) {
128+
uart_state.dsr ^= 1;
129+
tud_cdc_notify_uart_state(&uart_state);
130+
}
131+
btn_prev = btn;
122132
}
123133
}
124134

examples/device/cdc_msc/src/tusb_config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
//--------------------------------------------------------------------
8888

8989
#ifndef CFG_TUD_ENDPOINT0_SIZE
90-
#define CFG_TUD_ENDPOINT0_SIZE 64
90+
#define CFG_TUD_ENDPOINT0_SIZE 64
9191
#endif
9292

9393
//------------- CLASS -------------//
@@ -97,6 +97,8 @@
9797
#define CFG_TUD_MIDI 0
9898
#define CFG_TUD_VENDOR 0
9999

100+
#define CFG_TUD_CDC_NOTIFY 1 // Enable use of notification endpoint
101+
100102
// CDC FIFO size of TX and RX
101103
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
102104
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

examples/device/cdc_msc/src/usb_descriptors.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ tusb_desc_device_t const desc_device = {
5252
.bDeviceClass = TUSB_CLASS_MISC,
5353
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
5454
.bDeviceProtocol = MISC_PROTOCOL_IAD,
55-
5655
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
5756

5857
.idVendor = USB_VID,
@@ -131,7 +130,7 @@ uint8_t const desc_fs_configuration[] = {
131130
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
132131

133132
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
134-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
133+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
135134

136135
// Interface number, string index, EP Out & EP In address, EP size
137136
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
@@ -146,7 +145,7 @@ uint8_t const desc_hs_configuration[] = {
146145
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
147146

148147
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
149-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
148+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
150149

151150
// Interface number, string index, EP Out & EP In address, EP size
152151
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 512),
@@ -197,7 +196,6 @@ uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {
197196

198197
#endif // highspeed
199198

200-
201199
// Invoked when received GET CONFIGURATION DESCRIPTOR
202200
// Application return pointer to descriptor
203201
// Descriptor contents must exist long enough for transfer to complete
@@ -256,14 +254,14 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
256254
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
257255
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
258256

259-
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
257+
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) { return NULL; }
260258

261259
const char *str = string_desc_arr[index];
262260

263261
// Cap at max char
264262
chr_count = strlen(str);
265263
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
266-
if ( chr_count > max_count ) chr_count = max_count;
264+
if ( chr_count > max_count ) { chr_count = max_count; }
267265

268266
// Convert ASCII string into UTF-16
269267
for ( size_t i = 0; i < chr_count; i++ ) {
@@ -274,6 +272,5 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
274272

275273
// first byte is length (including header), second byte is string type
276274
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
277-
278275
return _desc_str;
279276
}

examples/device/cdc_msc_freertos/src/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ void cdc_task(void *params) {
189189
}
190190

191191
tud_cdc_write_flush();
192+
193+
// Press on-board button to send Uart status notification
194+
static uint32_t btn_prev = 0;
195+
static cdc_notify_uart_state_t uart_state = { .value = 0 };
196+
const uint32_t btn = board_button_read();
197+
if (!btn_prev && btn) {
198+
uart_state.dsr ^= 1;
199+
tud_cdc_notify_uart_state(&uart_state);
200+
}
201+
btn_prev = btn;
192202
}
193203

194204
// For ESP32-Sx this delay is essential to allow idle how to run and reset watchdog

examples/device/cdc_msc_freertos/src/tusb_config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104
#define CFG_TUD_MIDI 0
105105
#define CFG_TUD_VENDOR 0
106106

107+
#define CFG_TUD_CDC_NOTIFY 1 // Enable use of notification endpoint
108+
107109
// CDC FIFO size of TX and RX
108110
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
109111
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

examples/device/cdc_msc_freertos/src/usb_descriptors.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ tusb_desc_device_t const desc_device = {
5252
.bDeviceClass = TUSB_CLASS_MISC,
5353
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
5454
.bDeviceProtocol = MISC_PROTOCOL_IAD,
55-
5655
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
5756

5857
.idVendor = USB_VID,
@@ -131,7 +130,7 @@ uint8_t const desc_fs_configuration[] =
131130
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
132131

133132
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
134-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
133+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
135134

136135
// Interface number, string index, EP Out & EP In address, EP size
137136
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
@@ -147,7 +146,7 @@ uint8_t const desc_hs_configuration[] =
147146
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
148147

149148
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
150-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
149+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
151150

152151
// Interface number, string index, EP Out & EP In address, EP size
153152
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 512),
@@ -176,16 +175,14 @@ tusb_desc_device_qualifier_t const desc_device_qualifier =
176175
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
177176
// device_qualifier descriptor describes information about a high-speed capable device that would
178177
// change if the device were operating at the other speed. If not highspeed capable stall this request.
179-
uint8_t const* tud_descriptor_device_qualifier_cb(void)
180-
{
178+
uint8_t const* tud_descriptor_device_qualifier_cb(void) {
181179
return (uint8_t const*) &desc_device_qualifier;
182180
}
183181

184182
// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
185183
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
186184
// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
187-
uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
188-
{
185+
uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index) {
189186
(void) index; // for multiple configurations
190187

191188
// if link speed is high return fullspeed config, and vice versa
@@ -204,13 +201,12 @@ uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
204201
// Invoked when received GET CONFIGURATION DESCRIPTOR
205202
// Application return pointer to descriptor
206203
// Descriptor contents must exist long enough for transfer to complete
207-
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
208-
{
204+
uint8_t const * tud_descriptor_configuration_cb(uint8_t index) {
209205
(void) index; // for multiple configurations
210206

211207
#if TUD_OPT_HIGH_SPEED
212208
// Although we are highspeed, host may be fullspeed.
213-
return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration;
209+
return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration;
214210
#else
215211
return desc_fs_configuration;
216212
#endif
@@ -229,8 +225,7 @@ enum {
229225
};
230226

231227
// array of pointer to string descriptors
232-
char const *string_desc_arr[] =
233-
{
228+
char const *string_desc_arr[] = {
234229
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
235230
"TinyUSB", // 1: Manufacturer
236231
"TinyUSB Device", // 2: Product
@@ -261,14 +256,14 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
261256
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
262257
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
263258

264-
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
259+
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) { return NULL; }
265260

266261
const char *str = string_desc_arr[index];
267262

268263
// Cap at max char
269264
chr_count = strlen(str);
270265
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
271-
if ( chr_count > max_count ) chr_count = max_count;
266+
if ( chr_count > max_count ) { chr_count = max_count; }
272267

273268
// Convert ASCII string into UTF-16
274269
for ( size_t i = 0; i < chr_count; i++ ) {
@@ -279,6 +274,5 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
279274

280275
// first byte is length (including header), second byte is string type
281276
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
282-
283277
return _desc_str;
284278
}

0 commit comments

Comments
 (0)