Skip to content

Commit

Permalink
Merge pull request #1382 from fpistm/USB_linestate
Browse files Browse the repository at this point in the history
USB CDC line state
  • Loading branch information
fpistm authored May 5, 2021
2 parents 7ce7ab8 + 843fce7 commit cf6ba78
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
18 changes: 10 additions & 8 deletions cores/arduino/USBSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions cores/arduino/USBSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/stm32/usb/cdc/usbd_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions cores/arduino/stm32/usb/cdc/usbd_cdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/**
* @}
*/
Expand Down
20 changes: 15 additions & 5 deletions cores/arduino/stm32/usb/cdc/usbd_cdc_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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****/
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/stm32/usb/cdc/usbd_cdc_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit cf6ba78

Please sign in to comment.