summaryrefslogtreecommitdiff
path: root/examples/device/cdc_msc
diff options
context:
space:
mode:
authorHiFiPhile <[email protected]>2025-07-11 15:47:55 +0200
committerHiFiPhile <[email protected]>2025-07-11 15:47:55 +0200
commitf074815405f9c510f3f1eb57f3cbdab40e9e1caa (patch)
tree27157de2ae8ef662974a9d87a8119c12f3fbdffb /examples/device/cdc_msc
parent69f6b57772ad4f2923dcf48aabaaba044085c4b0 (diff)
parent29c2af7651f9075a63a036e94c7bbe3d0d00c31a (diff)
Merge branch 'master' into feature/usbtmc_vendor-specific
Diffstat (limited to 'examples/device/cdc_msc')
-rw-r--r--examples/device/cdc_msc/src/main.c10
-rw-r--r--examples/device/cdc_msc/src/msc_disk.c18
-rw-r--r--examples/device/cdc_msc/src/tusb_config.h4
-rw-r--r--examples/device/cdc_msc/src/usb_descriptors.c11
4 files changed, 27 insertions, 16 deletions
diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c
index f36c910d7..5cd93e7dd 100644
--- a/examples/device/cdc_msc/src/main.c
+++ b/examples/device/cdc_msc/src/main.c
@@ -119,6 +119,16 @@ void cdc_task(void) {
tud_cdc_write(buf, count);
tud_cdc_write_flush();
}
+
+ // Press on-board button to send Uart status notification
+ static uint32_t btn_prev = 0;
+ static cdc_notify_uart_state_t uart_state = { .value = 0 };
+ const uint32_t btn = board_button_read();
+ if (!btn_prev && btn) {
+ uart_state.dsr ^= 1;
+ tud_cdc_notify_uart_state(&uart_state);
+ }
+ btn_prev = btn;
}
}
diff --git a/examples/device/cdc_msc/src/msc_disk.c b/examples/device/cdc_msc/src/msc_disk.c
index 9645f4bfc..6fd42dd80 100644
--- a/examples/device/cdc_msc/src/msc_disk.c
+++ b/examples/device/cdc_msc/src/msc_disk.c
@@ -116,18 +116,20 @@ uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
README_CONTENTS
};
-// Invoked when received SCSI_CMD_INQUIRY
-// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
-void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
+// Invoked when received SCSI_CMD_INQUIRY, v2 with full inquiry response
+// Some inquiry_resp's fields are already filled with default values, application can update them
+// Return length of inquiry response, typically sizeof(scsi_inquiry_resp_t) (36 bytes), can be longer if included vendor data.
+uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t* inquiry_resp) {
(void) lun;
-
const char vid[] = "TinyUSB";
const char pid[] = "Mass Storage";
const char rev[] = "1.0";
- memcpy(vendor_id, vid, strlen(vid));
- memcpy(product_id, pid, strlen(pid));
- memcpy(product_rev, rev, strlen(rev));
+ memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
+ memcpy(inquiry_resp->product_id, pid, strlen(pid));
+ memcpy(inquiry_resp->product_rev, rev, strlen(rev));
+
+ return sizeof(scsi_inquiry_resp_t); // 36 bytes
}
// Invoked when received Test Unit Ready command.
@@ -184,7 +186,7 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buff
}
// Check for overflow of offset + bufsize
- if (offset + bufsize > DISK_BLOCK_SIZE) {
+ if (lba * DISK_BLOCK_SIZE + offset + bufsize > DISK_BLOCK_NUM * DISK_BLOCK_SIZE) {
return -1;
}
diff --git a/examples/device/cdc_msc/src/tusb_config.h b/examples/device/cdc_msc/src/tusb_config.h
index 03e0e649c..811d464e9 100644
--- a/examples/device/cdc_msc/src/tusb_config.h
+++ b/examples/device/cdc_msc/src/tusb_config.h
@@ -87,7 +87,7 @@
//--------------------------------------------------------------------
#ifndef CFG_TUD_ENDPOINT0_SIZE
-#define CFG_TUD_ENDPOINT0_SIZE 64
+#define CFG_TUD_ENDPOINT0_SIZE 64
#endif
//------------- CLASS -------------//
@@ -97,6 +97,8 @@
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0
+#define CFG_TUD_CDC_NOTIFY 1 // Enable use of notification endpoint
+
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
diff --git a/examples/device/cdc_msc/src/usb_descriptors.c b/examples/device/cdc_msc/src/usb_descriptors.c
index 4b6b88041..edcee0462 100644
--- a/examples/device/cdc_msc/src/usb_descriptors.c
+++ b/examples/device/cdc_msc/src/usb_descriptors.c
@@ -52,7 +52,6 @@ tusb_desc_device_t const desc_device = {
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
-
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = USB_VID,
@@ -131,7 +130,7 @@ uint8_t const desc_fs_configuration[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
- TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
// Interface number, string index, EP Out & EP In address, EP size
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
@@ -146,7 +145,7 @@ uint8_t const desc_hs_configuration[] = {
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
- TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 16, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
// Interface number, string index, EP Out & EP In address, EP size
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) {
#endif // highspeed
-
// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// 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) {
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
- if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
+ if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) { return NULL; }
const char *str = string_desc_arr[index];
// Cap at max char
chr_count = strlen(str);
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
- if ( chr_count > max_count ) chr_count = max_count;
+ if ( chr_count > max_count ) { chr_count = max_count; }
// Convert ASCII string into UTF-16
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) {
// first byte is length (including header), second byte is string type
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
-
return _desc_str;
}