Skip to content

Commit f04c66b

Browse files
committed
USB: CDC: add api to manage the DTR usage
Fixes stm32duino#1193 Signed-off-by: Frederic Pillon <[email protected]>
1 parent c76df81 commit f04c66b

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

Diff for: cores/arduino/USBSerial.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#include "usbd_desc.h"
2525
#include "wiring.h"
2626

27-
extern __IO uint32_t lineState;
27+
extern __IO bool dtrState;
28+
extern __IO bool rtsState;
2829

2930
USBSerial SerialUSB;
3031
void serialEventUSB() __attribute__((weak));
@@ -175,9 +176,14 @@ uint8_t USBSerial::numbits()
175176
return 8;
176177
}
177178

179+
void USBSerial::dtr(bool enable)
180+
{
181+
CDC_enableDTR(enable);
182+
}
183+
178184
bool USBSerial::dtr(void)
179185
{
180-
return false;
186+
return dtrState;
181187
}
182188

183189
bool USBSerial::rts(void)
@@ -187,12 +193,8 @@ bool USBSerial::rts(void)
187193

188194
USBSerial::operator bool()
189195
{
190-
bool result = false;
191-
if (lineState == 1) {
192-
result = true;
193-
}
194196
delay(10);
195-
return result;
197+
return dtrState;
196198
}
197199

198200
#endif // USBCON && USBD_USE_CDC

Diff for: cores/arduino/USBSerial.h

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class USBSerial : public Stream {
5151
uint8_t stopbits();
5252
uint8_t paritytype();
5353
uint8_t numbits();
54+
55+
void dtr(bool enable);
5456
bool dtr();
5557
bool rts();
5658
enum {

Diff for: cores/arduino/stm32/usb/cdc/usbd_cdc.h

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ extern "C" {
7373
#define CDC_SET_CONTROL_LINE_STATE 0x22U
7474
#define CDC_SEND_BREAK 0x23U
7575

76+
// Control Line State bits
77+
#define CLS_DTR (1 << 0)
78+
#define CLS_RTS (1 << 1)
79+
7680
/**
7781
* @}
7882
*/

Diff for: cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747
USBD_HandleTypeDef hUSBD_Device_CDC;
4848

4949
static bool CDC_initialized = false;
50+
static bool CDC_DTR_enabled = true;
5051

5152
/* Received Data over USB are stored in this buffer */
5253
CDC_TransmitQueue_TypeDef TransmitQueue;
5354
CDC_ReceiveQueue_TypeDef ReceiveQueue;
54-
__IO uint32_t lineState = 0;
55+
__IO bool dtrState = false; /* lineState */
5556
__IO bool receivePended = true;
5657
static uint32_t transmitStart = 0;
5758

@@ -183,9 +184,10 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
183184
break;
184185

185186
case CDC_SET_CONTROL_LINE_STATE:
186-
lineState =
187-
(((USBD_SetupReqTypedef *)pbuf)->wValue & 0x01) != 0; // Check DTR state
188-
if (lineState) { // Reset the transmit timeout when the port is connected
187+
// Check DTR state
188+
dtrState = (CDC_DTR_enabled) ? (((USBD_SetupReqTypedef *)pbuf)->wValue & CLS_DTR) : true;
189+
190+
if (dtrState) { // Reset the transmit timeout when the port is connected
189191
transmitStart = 0;
190192
}
191193
#ifdef DTR_TOGGLING_SEQ
@@ -301,7 +303,7 @@ bool CDC_connected()
301303
}
302304
return ((hUSBD_Device_CDC.dev_state == USBD_STATE_CONFIGURED)
303305
&& (transmitTime < USB_CDC_TRANSMIT_TIMEOUT)
304-
&& lineState);
306+
&& dtrState);
305307
}
306308

307309
void CDC_continue_transmit(void)
@@ -350,6 +352,12 @@ bool CDC_resume_receive(void)
350352
return false;
351353
}
352354

355+
void CDC_enableDTR(bool enable)
356+
{
357+
CDC_DTR_enabled = enable;
358+
dtrState = true;
359+
}
360+
353361
#endif /* USBD_USE_CDC */
354362
#endif /* USBCON */
355363
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Diff for: cores/arduino/stm32/usb/cdc/usbd_cdc_if.h

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bool CDC_resume_receive(void);
5151
void CDC_init(void);
5252
void CDC_deInit(void);
5353
bool CDC_connected(void);
54+
void CDC_enableDTR(bool enable);
5455

5556
#ifdef __cplusplus
5657
}

0 commit comments

Comments
 (0)