diff --git a/examples/MassStorage/msc_external_flash/.skip.txt b/examples/MassStorage/msc_external_flash/.skip.txt index ae593a87..95010c8f 100644 --- a/examples/MassStorage/msc_external_flash/.skip.txt +++ b/examples/MassStorage/msc_external_flash/.skip.txt @@ -1,3 +1,4 @@ feather_esp32_v2 pico_rp2040_tinyusb_host CH32V20x_EVT +feather_rp2040_tinyusb diff --git a/examples/MassStorage/msc_external_flash_sdcard/.skip.txt b/examples/MassStorage/msc_external_flash_sdcard/.skip.txt index ae593a87..95010c8f 100644 --- a/examples/MassStorage/msc_external_flash_sdcard/.skip.txt +++ b/examples/MassStorage/msc_external_flash_sdcard/.skip.txt @@ -1,3 +1,4 @@ feather_esp32_v2 pico_rp2040_tinyusb_host CH32V20x_EVT +feather_rp2040_tinyusb diff --git a/examples/MassStorage/msc_sdfat/.skip.txt b/examples/MassStorage/msc_sdfat/.skip.txt index e4f9319b..42342d5e 100644 --- a/examples/MassStorage/msc_sdfat/.skip.txt +++ b/examples/MassStorage/msc_sdfat/.skip.txt @@ -1,2 +1,3 @@ feather_esp32_v2 pico_rp2040_tinyusb_host +feather_rp2040_tinyusb diff --git a/src/arduino/Adafruit_USBD_CDC.cpp b/src/arduino/Adafruit_USBD_CDC.cpp index 11c5673e..79bc6429 100644 --- a/src/arduino/Adafruit_USBD_CDC.cpp +++ b/src/arduino/Adafruit_USBD_CDC.cpp @@ -27,11 +27,15 @@ // esp32 use built-in core cdc #if CFG_TUD_CDC && !defined(ARDUINO_ARCH_ESP32) +// Include before "Arduino.h" because TinyUSB is part of platform cores +// When developing TinyUSB, it needs to include the local version, not the +// platform core version, which is what gets included by Arduino.h +#include "Adafruit_USBD_CDC.h" + #include "Arduino.h" #include "Adafruit_TinyUSB_API.h" -#include "Adafruit_USBD_CDC.h" #include "Adafruit_USBD_Device.h" #ifndef TINYUSB_API_VERSION @@ -170,6 +174,40 @@ int Adafruit_USBD_CDC::dtr(void) { return tud_cdc_n_connected(_instance); } +bool Adafruit_USBD_CDC::rts(void) { + return tud_cdc_n_get_line_state(_instance) & CDC_CONTROL_LINE_STATE_RTS; +} + +bool Adafruit_USBD_CDC::dsr(void) { + return tud_cdc_n_get_serial_state(_instance).dsr; +} + +bool Adafruit_USBD_CDC::dcd(void) { + return tud_cdc_n_get_serial_state(_instance).dcd; +} + +bool Adafruit_USBD_CDC::ri(void) { + return tud_cdc_n_get_serial_state(_instance).ri; +} + +void Adafruit_USBD_CDC::dsr(bool c) { + cdc_serial_state_t serial_state = tud_cdc_n_get_serial_state(_instance); + serial_state.dsr = c; + tud_cdc_n_set_serial_state(_instance, serial_state); +} + +void Adafruit_USBD_CDC::dcd(bool c) { + cdc_serial_state_t serial_state = tud_cdc_n_get_serial_state(_instance); + serial_state.dcd = c; + tud_cdc_n_set_serial_state(_instance, serial_state); +} + +void Adafruit_USBD_CDC::ri(bool c) { + cdc_serial_state_t serial_state = tud_cdc_n_get_serial_state(_instance); + serial_state.ri = c; + tud_cdc_n_set_serial_state(_instance, serial_state); +} + Adafruit_USBD_CDC::operator bool() { if (!isValid()) { return false; diff --git a/src/arduino/Adafruit_USBD_CDC.h b/src/arduino/Adafruit_USBD_CDC.h index 9c0952f3..3fb61423 100644 --- a/src/arduino/Adafruit_USBD_CDC.h +++ b/src/arduino/Adafruit_USBD_CDC.h @@ -59,7 +59,21 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface { uint8_t stopbits(void); uint8_t paritytype(void); uint8_t numbits(void); - int dtr(void); + + // Flow control bit getters. + int dtr(void); // pre-existing, I don't want to change the return type. + bool rts(void); + // bool cts(void); // NOT PART OF THE CDC ACM SPEC?! + bool dsr(void); + bool dcd(void); + bool ri(void); + + // Flow control bit setters. + // void cts(bool c); // NOT PART OF CDC ACM SPEC?! + void dsr(bool c); + void dcd(bool c); + void ri(bool c); + // Break is a little harder, it's an event, not a state. // Stream API virtual int available(void); diff --git a/src/arduino/Adafruit_USBD_Device.cpp b/src/arduino/Adafruit_USBD_Device.cpp index b6d10687..246bf628 100644 --- a/src/arduino/Adafruit_USBD_Device.cpp +++ b/src/arduino/Adafruit_USBD_Device.cpp @@ -162,21 +162,26 @@ void Adafruit_USBD_Device::task(void) { } void Adafruit_USBD_Device::clearConfiguration(void) { - tusb_desc_device_t const desc_dev = {.bLength = sizeof(tusb_desc_device_t), - .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, - .bDeviceClass = 0, - .bDeviceSubClass = 0, - .bDeviceProtocol = 0, - .bMaxPacketSize0 = - CFG_TUD_ENDPOINT0_SIZE, - .idVendor = USB_VID, - .idProduct = USB_PID, - .bcdDevice = 0x0100, - .iManufacturer = STRID_MANUFACTURER, - .iProduct = STRID_PRODUCT, - .iSerialNumber = STRID_SERIAL, - .bNumConfigurations = 0x01}; + tusb_desc_device_t const desc_dev = { + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, +#if CFG_TUSB_MCU == OPT_MCU_RP2040 // RP2040 only supports full speed + .bcdUSB = 0x0110, +#else + .bcdUSB = 0x0200, +#endif + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + .idVendor = USB_VID, + .idProduct = USB_PID, + .bcdDevice = 0x0100, + .iManufacturer = STRID_MANUFACTURER, + .iProduct = STRID_PRODUCT, + .iSerialNumber = STRID_SERIAL, + .bNumConfigurations = 0x01 + }; _desc_device = desc_dev; diff --git a/src/class/cdc/cdc.h b/src/class/cdc/cdc.h index da490a6d..a5a5c5f8 100644 --- a/src/class/cdc/cdc.h +++ b/src/class/cdc/cdc.h @@ -412,6 +412,49 @@ typedef struct TU_ATTR_PACKED TU_VERIFY_STATIC(sizeof(cdc_line_control_state_t) == 2, "size is not correct"); +//--------------------------------------------------------------------+ +// Notifications +//--------------------------------------------------------------------+ + +typedef union TU_ATTR_PACKED { + struct { + uint16_t dcd :1; + uint16_t dsr :1; + uint16_t break_err :1; + uint16_t ri :1; + uint16_t frame_err :1; + uint16_t parity_err :1; + uint16_t overrun_err :1; + uint16_t :9; + }; + uint16_t state; +} cdc_serial_state_t; + +TU_VERIFY_STATIC(sizeof(cdc_serial_state_t) == 2, "size is not correct"); + +typedef struct TU_ATTR_PACKED { + tusb_notification_t header; + union { + cdc_serial_state_t serial_state; + // Add more Notification "Data" payloads here as more are implemented. + }; +} cdc_notify_struct_t; + +TU_VERIFY_STATIC(sizeof(cdc_notify_struct_t) == 10, "size is not correct"); + +tu_static const cdc_notify_struct_t cdc_notify_serial_status = { + .header = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_INTERFACE, + .type = TUSB_REQ_TYPE_CLASS, + .direction = TUSB_DIR_IN, + }, + .bNotification = CDC_NOTIF_SERIAL_STATE, + .wValue = 0, + .wLength = sizeof(cdc_serial_state_t), + } +}; + TU_ATTR_PACKED_END // End of all packed definitions TU_ATTR_BIT_FIELD_ORDER_END diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index bf95a369..dd5ee4e8 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -54,6 +54,10 @@ typedef struct { // Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send) uint8_t line_state; + // Notify host of flow control bits: DSR, DCD, RI, and some error flags. + cdc_serial_state_t serial_state; + bool serial_state_changed; + /*------------- From this point, data is not cleared by bus reset -------------*/ char wanted_char; TU_ATTR_ALIGNED(4) cdc_line_coding_t line_coding; @@ -67,6 +71,7 @@ typedef struct { OSAL_MUTEX_DEF(rx_ff_mutex); OSAL_MUTEX_DEF(tx_ff_mutex); + } cdcd_interface_t; #define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char) @@ -74,6 +79,7 @@ typedef struct { typedef struct { TUD_EPBUF_DEF(epout, CFG_TUD_CDC_EP_BUFSIZE); TUD_EPBUF_DEF(epin, CFG_TUD_CDC_EP_BUFSIZE); + TUD_EPBUF_DEF(epnotif, CFG_TUD_CDC_EP_BUFSIZE); } cdcd_epbuf_t; //--------------------------------------------------------------------+ @@ -115,6 +121,32 @@ static bool _prep_out_transaction(uint8_t itf) { } } +bool _send_serial_state_notification(cdcd_interface_t *p_cdc) { + const uint8_t rhport = 0; + + if (!p_cdc->serial_state_changed) { + // Nothing to do. + return true; + } + + if (!usbd_edpt_claim(rhport, p_cdc->ep_notif)) { + // If claim failed, we're already in the middle of a transaction. + // cdcd_xfer_cb() will pick up this change. + return true; + } + + // We have the end point. Build and send the notification. + p_cdc->serial_state_changed = false; + + cdc_notify_struct_t notif_buf = cdc_notify_serial_status; + notif_buf.header.wIndex = p_cdc->itf_num; + notif_buf.serial_state = p_cdc->serial_state; + + cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[p_cdc->itf_num]; + memcpy(&p_epbuf->epnotif, ¬if_buf, sizeof(cdc_notify_struct_t)); + return usbd_edpt_xfer(rhport, p_cdc->ep_notif, (uint8_t *) &(p_epbuf->epnotif), sizeof(p_epbuf->epnotif)); +} + //--------------------------------------------------------------------+ // APPLICATION API //--------------------------------------------------------------------+ @@ -138,6 +170,19 @@ uint8_t tud_cdc_n_get_line_state(uint8_t itf) { return _cdcd_itf[itf].line_state; } +cdc_serial_state_t tud_cdc_n_get_serial_state(uint8_t itf) { + return _cdcd_itf[itf].serial_state; +} + +void tud_cdc_n_set_serial_state(uint8_t itf, cdc_serial_state_t serial_state) { + cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; + if (p_cdc->serial_state.state != serial_state.state) { + p_cdc->serial_state_changed = true; + p_cdc->serial_state = serial_state; + _send_serial_state_notification(p_cdc); + } +} + void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t* coding) { (*coding) = _cdcd_itf[itf].line_coding; } @@ -453,7 +498,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Identify which interface to use for (itf = 0; itf < CFG_TUD_CDC; itf++) { p_cdc = &_cdcd_itf[itf]; - if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in)) { + if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in) || (ep_addr == p_cdc->ep_notif)) { break; } } @@ -502,7 +547,11 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ } } - // nothing to do with notif endpoint for now + // Notifications + if (ep_addr == p_cdc->ep_notif) { + // Send any changes that may have come in while sending the previous change. + return _send_serial_state_notification(p_cdc); + } return true; } diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index de7d2055..fa3ec7b3 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -70,6 +70,12 @@ bool tud_cdc_n_connected(uint8_t itf); // Get current line state. Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send) uint8_t tud_cdc_n_get_line_state(uint8_t itf); +// Get current serial state. +cdc_serial_state_t tud_cdc_n_get_serial_state(uint8_t itf); + +// Set current serial state. +void tud_cdc_n_set_serial_state(uint8_t itf, cdc_serial_state_t ser_state); + // Get current line encoding: bit rate, stop bits parity etc .. void tud_cdc_n_get_line_coding(uint8_t itf, cdc_line_coding_t* coding); @@ -132,6 +138,14 @@ TU_ATTR_ALWAYS_INLINE static inline uint8_t tud_cdc_get_line_state(void) { return tud_cdc_n_get_line_state(0); } +TU_ATTR_ALWAYS_INLINE static inline cdc_serial_state_t tud_cdc_get_serial_state(void) { + return tud_cdc_n_get_serial_state(0); +} + +TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_set_serial_state(cdc_serial_state_t ser_state) { + tud_cdc_n_set_serial_state(0, ser_state); +} + TU_ATTR_ALWAYS_INLINE static inline void tud_cdc_get_line_coding(cdc_line_coding_t* coding) { tud_cdc_n_get_line_coding(0, coding); } diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 0855293a..ddf170ac 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -528,6 +528,27 @@ typedef struct TU_ATTR_PACKED { TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "size is not correct"); + +typedef struct TU_ATTR_PACKED { + union { + struct TU_ATTR_PACKED { + uint8_t recipient : 5; ///< Recipient type tusb_request_recipient_t. + uint8_t type : 2; ///< Request type tusb_request_type_t. + uint8_t direction : 1; ///< Direction type. tusb_dir_t + } bmRequestType_bit; + + uint8_t bmRequestType; + }; + + uint8_t bNotification; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} tusb_notification_t; + +TU_VERIFY_STATIC( sizeof(tusb_notification_t) == 8, "size is not correct"); + + TU_ATTR_PACKED_END // End of all packed definitions TU_ATTR_BIT_FIELD_ORDER_END