Files
CherryUSB/class/cdc/usbd_cdc.c

127 lines
4.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2022, sakumisu
2022-02-08 11:44:46 +08:00
*
* SPDX-License-Identifier: Apache-2.0
2022-02-08 11:44:46 +08:00
*/
#include "usbd_core.h"
#include "usbd_cdc.h"
const char *stop_name[] = { "1", "1.5", "2" };
const char *parity_name[] = { "N", "O", "E", "M", "S" };
static int cdc_acm_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
2022-02-08 11:44:46 +08:00
{
USB_LOG_DBG("CDC Class request: "
2022-06-28 20:37:33 +08:00
"bRequest 0x%02x\r\n",
setup->bRequest);
2022-08-19 23:26:36 +08:00
struct cdc_line_coding line_coding;
bool dtr, rts;
uint8_t intf_num = LO_BYTE(setup->wIndex);
2022-02-08 11:44:46 +08:00
switch (setup->bRequest) {
case CDC_REQUEST_SET_LINE_CODING:
/*******************************************************************************/
/* Line Coding Structure */
/*-----------------------------------------------------------------------------*/
/* Offset | Field | Size | Value | Description */
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
/* 4 | bCharFormat | 1 | Number | Stop bits */
/* 0 - 1 Stop bit */
/* 1 - 1.5 Stop bits */
/* 2 - 2 Stop bits */
/* 5 | bParityType | 1 | Number | Parity */
/* 0 - None */
/* 1 - Odd */
/* 2 - Even */
/* 3 - Mark */
/* 4 - Space */
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
2022-08-19 23:26:36 +08:00
memcpy(&line_coding, *data, setup->wLength);
2022-06-28 20:37:33 +08:00
USB_LOG_DBG("Set intf:%d linecoding <%d %d %s %s>\r\n",
intf_num,
2022-08-19 23:26:36 +08:00
line_coding.dwDTERate,
line_coding.bDataBits,
parity_name[line_coding.bParityType],
stop_name[line_coding.bCharFormat]);
usbd_cdc_acm_set_line_coding(intf_num, &line_coding);
2022-02-08 11:44:46 +08:00
break;
2022-06-28 20:37:33 +08:00
case CDC_REQUEST_SET_CONTROL_LINE_STATE: {
2022-08-19 23:26:36 +08:00
dtr = (setup->wValue & 0x0001);
rts = (setup->wValue & 0x0002);
2022-06-28 20:37:33 +08:00
USB_LOG_DBG("Set intf:%d DTR 0x%x,RTS 0x%x\r\n",
intf_num,
2022-08-19 23:26:36 +08:00
dtr,
rts);
usbd_cdc_acm_set_dtr(intf_num, dtr);
usbd_cdc_acm_set_rts(intf_num, rts);
2022-06-28 20:37:33 +08:00
} break;
2022-02-08 11:44:46 +08:00
case CDC_REQUEST_GET_LINE_CODING:
usbd_cdc_acm_get_line_coding(intf_num, &line_coding);
2022-08-19 23:26:36 +08:00
memcpy(*data, &line_coding, 7);
2022-06-28 20:37:33 +08:00
*len = 7;
USB_LOG_DBG("Get intf:%d linecoding %d %d %d %d\r\n",
intf_num,
2022-08-19 23:26:36 +08:00
line_coding.dwDTERate,
line_coding.bCharFormat,
line_coding.bParityType,
line_coding.bDataBits);
2022-02-08 11:44:46 +08:00
break;
default:
USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest);
return -1;
}
return 0;
}
static void cdc_notify_handler(uint8_t event, void *arg)
{
switch (event) {
case USBD_EVENT_RESET:
break;
default:
break;
}
}
struct usbd_interface *usbd_cdc_acm_alloc_intf(void)
2022-02-08 11:44:46 +08:00
{
struct usbd_interface *intf = usb_malloc(sizeof(struct usbd_interface));
if (intf == NULL) {
USB_LOG_ERR("no mem to alloc intf\r\n");
return NULL;
}
intf->class_interface_handler = cdc_acm_class_interface_request_handler;
intf->class_endpoint_handler = NULL;
2022-02-08 11:44:46 +08:00
intf->vendor_handler = NULL;
intf->notify_handler = cdc_notify_handler;
return intf;
2022-02-08 11:44:46 +08:00
}
2022-02-12 18:49:06 +08:00
2022-08-19 23:26:36 +08:00
__WEAK void usbd_cdc_acm_set_line_coding(uint8_t intf, struct cdc_line_coding *line_coding)
{
}
__WEAK void usbd_cdc_acm_get_line_coding(uint8_t intf, struct cdc_line_coding *line_coding)
2022-02-12 18:49:06 +08:00
{
2022-08-19 23:26:36 +08:00
line_coding->dwDTERate = 2000000;
line_coding->bDataBits = 8;
line_coding->bParityType = 0;
line_coding->bCharFormat = 0;
2022-02-12 18:49:06 +08:00
}
2022-06-28 20:37:33 +08:00
__WEAK void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr)
2022-02-12 18:49:06 +08:00
{
}
2022-06-28 20:37:33 +08:00
__WEAK void usbd_cdc_acm_set_rts(uint8_t intf, bool rts)
2022-02-12 18:49:06 +08:00
{
}