Skip to content

Commit

Permalink
cdc: add uart status notification support.
Browse files Browse the repository at this point in the history
  • Loading branch information
HiFiPhile committed Apr 11, 2024
1 parent 5b37d9b commit ea95e7d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/class/cdc/cdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,35 @@ typedef struct TU_ATTR_PACKED

TU_VERIFY_STATIC(sizeof(cdc_line_control_state_t) == 2, "size is not correct");

//--------------------------------------------------------------------+
// Notifications
//--------------------------------------------------------------------+
typedef struct TU_ATTR_PACKED
{
uint16_t bRxCarrier : 1;
uint16_t bTxCarrier : 1;
uint16_t bBreak : 1;
uint16_t bRingSignal : 1;
uint16_t bFraming : 1;
uint16_t bParity : 1;
uint16_t bOverRun : 1;
uint16_t : 9;
} cdc_uart_state_t;

typedef struct TU_ATTR_PACKED
{
uint8_t bmRequestType;
uint8_t bNotification;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
cdc_uart_state_t bmUartState;
} cdc_notif_serial_state_t;

TU_VERIFY_STATIC(sizeof(cdc_notif_serial_state_t) == 10, "size is not correct");

#define CDC_REQ_TYPE_NOTIF 0xA1 ///< Direction IN; Type Class; Recipient Interface

TU_ATTR_PACKED_END // End of all packed definitions
TU_ATTR_BIT_FIELD_ORDER_END

Expand Down
24 changes: 23 additions & 1 deletion src/class/cdc/cdc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef struct
OSAL_MUTEX_DEF(tx_ff_mutex);

// Endpoint Transfer buffer
CFG_TUSB_MEM_ALIGN cdc_notif_serial_state_t serial_state_buf;
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EP_BUFSIZE];

Expand Down Expand Up @@ -133,6 +134,27 @@ void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding)
(*coding) = _cdcd_itf[itf].line_coding;
}

bool tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state)
{
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];

// Skip if usb is not ready yet
TU_VERIFY( tud_ready(), 0 );

// claim endpoint
TU_VERIFY(usbd_edpt_claim(p_cdc->rhport, p_cdc->ep_notif));

p_cdc->serial_state_buf.bmRequestType = CDC_REQ_TYPE_NOTIF;
p_cdc->serial_state_buf.bNotification = CDC_NOTIF_SERIAL_STATE;
p_cdc->serial_state_buf.wValue = 0;
p_cdc->serial_state_buf.wIndex = p_cdc->itf_num;
p_cdc->serial_state_buf.wLength = 2;
p_cdc->serial_state_buf.bmUartState = state;

// transfer
return usbd_edpt_xfer(p_cdc->rhport, p_cdc->ep_notif, (uint8_t *)&p_cdc->serial_state_buf, sizeof(p_cdc->serial_state_buf));
}

void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
{
_cdcd_itf[itf].wanted_char = wanted;
Expand Down Expand Up @@ -458,7 +480,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
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 ) ) break;
if ( ( ep_addr == p_cdc->ep_out ) || ( ep_addr == p_cdc->ep_in ) || ( ep_addr == p_cdc->ep_notif )) break;
}
TU_ASSERT(itf < CFG_TUD_CDC);

Expand Down
9 changes: 9 additions & 0 deletions src/class/cdc/cdc_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ uint8_t tud_cdc_n_get_line_state (uint8_t itf);
// 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);

// Send UART status notification: DCD, DSR etc ..
bool tud_cdc_n_send_uart_state (uint8_t itf, cdc_uart_state_t state);

// Set special character that will trigger tud_cdc_rx_wanted_cb() callback on receiving
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted);

Expand Down Expand Up @@ -109,6 +112,7 @@ bool tud_cdc_n_write_clear (uint8_t itf);
static inline bool tud_cdc_connected (void);
static inline uint8_t tud_cdc_get_line_state (void);
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding);
static inline bool tud_cdc_send_uart_state (cdc_uart_state_t state);
static inline void tud_cdc_set_wanted_char (char wanted);

static inline uint32_t tud_cdc_available (void);
Expand Down Expand Up @@ -180,6 +184,11 @@ static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding)
tud_cdc_n_get_line_coding(0, coding);
}

static inline bool tud_cdc_send_uart_state (cdc_uart_state_t state)
{
return tud_cdc_n_send_uart_state(0, state);
}

static inline void tud_cdc_set_wanted_char (char wanted)
{
tud_cdc_n_set_wanted_char(0, wanted);
Expand Down

0 comments on commit ea95e7d

Please sign in to comment.