Skip to content

Commit cf6ba78

Browse files
authored
Merge pull request stm32duino#1382 from fpistm/USB_linestate
USB CDC line state
2 parents 7ce7ab8 + 843fce7 commit cf6ba78

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

Diff for: cores/arduino/USBSerial.cpp

+10-8
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,24 +176,25 @@ 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)
184190
{
185-
return false;
191+
return rtsState;
186192
}
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.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
527527
}
528528

529529
/**
530-
* @brief USBD_CDC_Init
530+
* @brief USBD_CDC_DeInit
531531
* DeInitialize the CDC layer
532532
* @param pdev: device instance
533533
* @param cfgidx: Configuration index

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@
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 */
56+
__IO bool rtsState = false;
5557
__IO bool receivePended = true;
5658
static uint32_t transmitStart = 0;
5759

@@ -183,11 +185,13 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
183185
break;
184186

185187
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
188+
// Check DTR state
189+
dtrState = (CDC_DTR_enabled) ? (((USBD_SetupReqTypedef *)pbuf)->wValue & CLS_DTR) : true;
190+
191+
if (dtrState) { // Reset the transmit timeout when the port is connected
189192
transmitStart = 0;
190193
}
194+
rtsState = (((USBD_SetupReqTypedef *)pbuf)->wValue & CLS_RTS);
191195
#ifdef DTR_TOGGLING_SEQ
192196
dtr_toggling++; /* Count DTR toggling */
193197
#endif
@@ -301,7 +305,7 @@ bool CDC_connected()
301305
}
302306
return ((hUSBD_Device_CDC.dev_state == USBD_STATE_CONFIGURED)
303307
&& (transmitTime < USB_CDC_TRANSMIT_TIMEOUT)
304-
&& lineState);
308+
&& dtrState);
305309
}
306310

307311
void CDC_continue_transmit(void)
@@ -350,6 +354,12 @@ bool CDC_resume_receive(void)
350354
return false;
351355
}
352356

357+
void CDC_enableDTR(bool enable)
358+
{
359+
CDC_DTR_enabled = enable;
360+
dtrState = true;
361+
}
362+
353363
#endif /* USBD_USE_CDC */
354364
#endif /* USBCON */
355365
/************************ (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)