diff --git a/cores/arduino/USBSerial.cpp b/cores/arduino/USBSerial.cpp index 256b5a1df8..c777105038 100644 --- a/cores/arduino/USBSerial.cpp +++ b/cores/arduino/USBSerial.cpp @@ -24,7 +24,8 @@ #include "usbd_desc.h" #include "wiring.h" -extern __IO uint32_t lineState; +extern __IO bool dtrState; +extern __IO bool rtsState; USBSerial SerialUSB; void serialEventUSB() __attribute__((weak)); @@ -175,24 +176,25 @@ uint8_t USBSerial::numbits() return 8; } +void USBSerial::dtr(bool enable) +{ + CDC_enableDTR(enable); +} + bool USBSerial::dtr(void) { - return false; + return dtrState; } bool USBSerial::rts(void) { - return false; + return rtsState; } USBSerial::operator bool() { - bool result = false; - if (lineState == 1) { - result = true; - } delay(10); - return result; + return dtrState; } #endif // USBCON && USBD_USE_CDC diff --git a/cores/arduino/USBSerial.h b/cores/arduino/USBSerial.h index 1628eef358..202c059daa 100644 --- a/cores/arduino/USBSerial.h +++ b/cores/arduino/USBSerial.h @@ -51,6 +51,8 @@ class USBSerial : public Stream { uint8_t stopbits(); uint8_t paritytype(); uint8_t numbits(); + + void dtr(bool enable); bool dtr(); bool rts(); enum { diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc.c b/cores/arduino/stm32/usb/cdc/usbd_cdc.c index b4e42b17b2..3c1663e3c6 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc.c +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc.c @@ -527,7 +527,7 @@ static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) } /** - * @brief USBD_CDC_Init + * @brief USBD_CDC_DeInit * DeInitialize the CDC layer * @param pdev: device instance * @param cfgidx: Configuration index diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc.h b/cores/arduino/stm32/usb/cdc/usbd_cdc.h index 814ffb2567..4e39780760 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc.h +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc.h @@ -73,6 +73,10 @@ extern "C" { #define CDC_SET_CONTROL_LINE_STATE 0x22U #define CDC_SEND_BREAK 0x23U +// Control Line State bits +#define CLS_DTR (1 << 0) +#define CLS_RTS (1 << 1) + /** * @} */ diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c index dbe8245a74..359969254f 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c @@ -47,11 +47,13 @@ USBD_HandleTypeDef hUSBD_Device_CDC; static bool CDC_initialized = false; +static bool CDC_DTR_enabled = true; /* Received Data over USB are stored in this buffer */ CDC_TransmitQueue_TypeDef TransmitQueue; CDC_ReceiveQueue_TypeDef ReceiveQueue; -__IO uint32_t lineState = 0; +__IO bool dtrState = false; /* lineState */ +__IO bool rtsState = false; __IO bool receivePended = true; static uint32_t transmitStart = 0; @@ -183,11 +185,13 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length) break; case CDC_SET_CONTROL_LINE_STATE: - lineState = - (((USBD_SetupReqTypedef *)pbuf)->wValue & 0x01) != 0; // Check DTR state - if (lineState) { // Reset the transmit timeout when the port is connected + // Check DTR state + dtrState = (CDC_DTR_enabled) ? (((USBD_SetupReqTypedef *)pbuf)->wValue & CLS_DTR) : true; + + if (dtrState) { // Reset the transmit timeout when the port is connected transmitStart = 0; } + rtsState = (((USBD_SetupReqTypedef *)pbuf)->wValue & CLS_RTS); #ifdef DTR_TOGGLING_SEQ dtr_toggling++; /* Count DTR toggling */ #endif @@ -301,7 +305,7 @@ bool CDC_connected() } return ((hUSBD_Device_CDC.dev_state == USBD_STATE_CONFIGURED) && (transmitTime < USB_CDC_TRANSMIT_TIMEOUT) - && lineState); + && dtrState); } void CDC_continue_transmit(void) @@ -350,6 +354,12 @@ bool CDC_resume_receive(void) return false; } +void CDC_enableDTR(bool enable) +{ + CDC_DTR_enabled = enable; + dtrState = true; +} + #endif /* USBD_USE_CDC */ #endif /* USBCON */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h index b415ac16de..fe05df536a 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h @@ -51,6 +51,7 @@ bool CDC_resume_receive(void); void CDC_init(void); void CDC_deInit(void); bool CDC_connected(void); +void CDC_enableDTR(bool enable); #ifdef __cplusplus }