get linecoding by users
This commit is contained in:
@@ -9,54 +9,16 @@
|
|||||||
const char *stop_name[] = { "1", "1.5", "2" };
|
const char *stop_name[] = { "1", "1.5", "2" };
|
||||||
const char *parity_name[] = { "N", "O", "E", "M", "S" };
|
const char *parity_name[] = { "N", "O", "E", "M", "S" };
|
||||||
|
|
||||||
struct usbd_cdc {
|
|
||||||
struct cdc_line_coding line_coding;
|
|
||||||
bool dtr;
|
|
||||||
bool rts;
|
|
||||||
uint8_t intf_num;
|
|
||||||
usb_slist_t list;
|
|
||||||
};
|
|
||||||
|
|
||||||
static usb_slist_t usbd_cdc_head = USB_SLIST_OBJECT_INIT(usbd_cdc_head);
|
|
||||||
|
|
||||||
#ifndef CONFIG_USBDEV_CDC_ACM_UART
|
|
||||||
struct usbd_cdc g_usbd_cdc_acm_class;
|
|
||||||
static void usbd_cdc_acm_reset(void)
|
|
||||||
{
|
|
||||||
memset(&g_usbd_cdc_acm_class, 0, sizeof(struct usbd_cdc));
|
|
||||||
g_usbd_cdc_acm_class.line_coding.dwDTERate = 2000000;
|
|
||||||
g_usbd_cdc_acm_class.line_coding.bDataBits = 8;
|
|
||||||
g_usbd_cdc_acm_class.line_coding.bParityType = 0;
|
|
||||||
g_usbd_cdc_acm_class.line_coding.bCharFormat = 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
||||||
{
|
{
|
||||||
USB_LOG_DBG("CDC Class request: "
|
USB_LOG_DBG("CDC Class request: "
|
||||||
"bRequest 0x%02x\r\n",
|
"bRequest 0x%02x\r\n",
|
||||||
setup->bRequest);
|
setup->bRequest);
|
||||||
|
|
||||||
struct usbd_cdc *current_cdc_acm_class = NULL;
|
struct cdc_line_coding line_coding;
|
||||||
|
bool dtr, rts;
|
||||||
uint8_t intf = LO_BYTE(setup->wIndex);
|
uint8_t intf = LO_BYTE(setup->wIndex);
|
||||||
#ifdef CONFIG_USBDEV_CDC_ACM_UART
|
|
||||||
usb_slist_t *i;
|
|
||||||
|
|
||||||
usb_slist_for_each(i, &usbd_cdc_head)
|
|
||||||
{
|
|
||||||
struct usbd_cdc *cdc_acm_class = usb_slist_entry(i, struct usbd_cdc, list);
|
|
||||||
if (cdc_acm_class->intf_num == intf) {
|
|
||||||
current_cdc_acm_class = cdc_acm_class;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current_cdc_acm_class == NULL) {
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
current_cdc_acm_class = &g_usbd_cdc_acm_class;
|
|
||||||
#endif
|
|
||||||
switch (setup->bRequest) {
|
switch (setup->bRequest) {
|
||||||
case CDC_REQUEST_SET_LINE_CODING:
|
case CDC_REQUEST_SET_LINE_CODING:
|
||||||
|
|
||||||
@@ -77,37 +39,37 @@ static int cdc_acm_class_request_handler(struct usb_setup_packet *setup, uint8_t
|
|||||||
/* 4 - Space */
|
/* 4 - Space */
|
||||||
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
|
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
|
||||||
/*******************************************************************************/
|
/*******************************************************************************/
|
||||||
memcpy(¤t_cdc_acm_class->line_coding, *data, setup->wLength);
|
memcpy(&line_coding, *data, setup->wLength);
|
||||||
USB_LOG_DBG("Set intf:%d linecoding <%d %d %s %s>\r\n",
|
USB_LOG_DBG("Set intf:%d linecoding <%d %d %s %s>\r\n",
|
||||||
intf,
|
intf,
|
||||||
current_cdc_acm_class->line_coding.dwDTERate,
|
line_coding.dwDTERate,
|
||||||
current_cdc_acm_class->line_coding.bDataBits,
|
line_coding.bDataBits,
|
||||||
parity_name[current_cdc_acm_class->line_coding.bParityType],
|
parity_name[line_coding.bParityType],
|
||||||
stop_name[current_cdc_acm_class->line_coding.bCharFormat]);
|
stop_name[line_coding.bCharFormat]);
|
||||||
usbd_cdc_acm_set_line_coding(intf, current_cdc_acm_class->line_coding.dwDTERate, current_cdc_acm_class->line_coding.bDataBits,
|
usbd_cdc_acm_set_line_coding(intf, &line_coding);
|
||||||
current_cdc_acm_class->line_coding.bParityType, current_cdc_acm_class->line_coding.bCharFormat);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CDC_REQUEST_SET_CONTROL_LINE_STATE: {
|
case CDC_REQUEST_SET_CONTROL_LINE_STATE: {
|
||||||
current_cdc_acm_class->dtr = (setup->wValue & 0x0001);
|
dtr = (setup->wValue & 0x0001);
|
||||||
current_cdc_acm_class->rts = (setup->wValue & 0x0002);
|
rts = (setup->wValue & 0x0002);
|
||||||
USB_LOG_DBG("Set intf:%d DTR 0x%x,RTS 0x%x\r\n",
|
USB_LOG_DBG("Set intf:%d DTR 0x%x,RTS 0x%x\r\n",
|
||||||
intf,
|
intf,
|
||||||
current_cdc_acm_class->dtr,
|
dtr,
|
||||||
current_cdc_acm_class->rts);
|
rts);
|
||||||
usbd_cdc_acm_set_dtr(intf, current_cdc_acm_class->dtr);
|
usbd_cdc_acm_set_dtr(intf, dtr);
|
||||||
usbd_cdc_acm_set_rts(intf, current_cdc_acm_class->rts);
|
usbd_cdc_acm_set_rts(intf, rts);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case CDC_REQUEST_GET_LINE_CODING:
|
case CDC_REQUEST_GET_LINE_CODING:
|
||||||
*data = (uint8_t *)(¤t_cdc_acm_class->line_coding);
|
usbd_cdc_acm_get_line_coding(intf, &line_coding);
|
||||||
|
memcpy(*data, &line_coding, 7);
|
||||||
*len = 7;
|
*len = 7;
|
||||||
USB_LOG_DBG("Get intf:%d linecoding %d %d %d %d\r\n",
|
USB_LOG_DBG("Get intf:%d linecoding %d %d %d %d\r\n",
|
||||||
intf,
|
intf,
|
||||||
current_cdc_acm_class->line_coding.dwDTERate,
|
line_coding.dwDTERate,
|
||||||
current_cdc_acm_class->line_coding.bCharFormat,
|
line_coding.bCharFormat,
|
||||||
current_cdc_acm_class->line_coding.bParityType,
|
line_coding.bParityType,
|
||||||
current_cdc_acm_class->line_coding.bDataBits);
|
line_coding.bDataBits);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -122,34 +84,12 @@ static void cdc_notify_handler(uint8_t event, void *arg)
|
|||||||
{
|
{
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case USBD_EVENT_RESET:
|
case USBD_EVENT_RESET:
|
||||||
#ifndef CONFIG_USBDEV_CDC_ACM_UART
|
|
||||||
usbd_cdc_acm_reset();
|
|
||||||
#endif
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int usbd_cdc_acm_alloc(uint8_t intf)
|
|
||||||
{
|
|
||||||
struct usbd_cdc *cdc_acm_class = usb_malloc(sizeof(struct usbd_cdc));
|
|
||||||
|
|
||||||
if (cdc_acm_class == NULL) {
|
|
||||||
USB_LOG_ERR("no memory to alloc for cdc_acm_class\r\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
memset(cdc_acm_class, 0, sizeof(struct usbd_cdc));
|
|
||||||
cdc_acm_class->line_coding.dwDTERate = 2000000;
|
|
||||||
cdc_acm_class->line_coding.bDataBits = 8;
|
|
||||||
cdc_acm_class->line_coding.bParityType = 0;
|
|
||||||
cdc_acm_class->line_coding.bCharFormat = 0;
|
|
||||||
cdc_acm_class->intf_num = intf;
|
|
||||||
|
|
||||||
usb_slist_add_tail(&usbd_cdc_head, &cdc_acm_class->list);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf)
|
void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf)
|
||||||
{
|
{
|
||||||
static usbd_class_t *last_class = NULL;
|
static usbd_class_t *last_class = NULL;
|
||||||
@@ -164,15 +104,20 @@ void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf)
|
|||||||
intf->vendor_handler = NULL;
|
intf->vendor_handler = NULL;
|
||||||
intf->notify_handler = cdc_notify_handler;
|
intf->notify_handler = cdc_notify_handler;
|
||||||
usbd_class_add_interface(devclass, intf);
|
usbd_class_add_interface(devclass, intf);
|
||||||
#ifdef CONFIG_USBDEV_CDC_ACM_UART
|
|
||||||
usbd_cdc_acm_alloc(intf->intf_num);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
__WEAK void usbd_cdc_acm_set_line_coding(uint8_t intf, uint32_t baudrate, uint8_t databits, uint8_t parity, uint8_t stopbits)
|
__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)
|
||||||
|
{
|
||||||
|
line_coding->dwDTERate = 2000000;
|
||||||
|
line_coding->bDataBits = 8;
|
||||||
|
line_coding->bParityType = 0;
|
||||||
|
line_coding->bCharFormat = 0;
|
||||||
|
}
|
||||||
|
|
||||||
__WEAK void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr)
|
__WEAK void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ extern "C" {
|
|||||||
void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf);
|
void usbd_cdc_add_acm_interface(usbd_class_t *devclass, usbd_interface_t *intf);
|
||||||
|
|
||||||
/* Setup request command callback api */
|
/* Setup request command callback api */
|
||||||
void usbd_cdc_acm_set_line_coding(uint8_t intf, uint32_t baudrate, uint8_t databits, uint8_t parity, uint8_t stopbits);
|
void usbd_cdc_acm_set_line_coding(uint8_t intf, struct cdc_line_coding *line_coding);
|
||||||
|
void usbd_cdc_acm_get_line_coding(uint8_t intf, struct cdc_line_coding *line_coding);
|
||||||
void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr);
|
void usbd_cdc_acm_set_dtr(uint8_t intf, bool dtr);
|
||||||
void usbd_cdc_acm_set_rts(uint8_t intf, bool rts);
|
void usbd_cdc_acm_set_rts(uint8_t intf, bool rts);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user