From b171b28a75df1ddc52d166bad6835a43eecd8811 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Fri, 16 Aug 2024 14:08:29 +0800 Subject: refactor(class/cdc): rename usbd_cdc to usbd_cdc_acm --- SConscript | 2 +- cherryusb.cmake | 2 +- class/cdc/usbd_cdc.c | 118 ---------------------------------------- class/cdc/usbd_cdc.h | 24 ++------ class/cdc/usbd_cdc_acm.c | 118 ++++++++++++++++++++++++++++++++++++++++ class/cdc/usbd_cdc_acm.h | 29 ++++++++++ demo/cdc_acm_hid_msc_template.c | 2 +- demo/cdc_acm_msc_template.c | 2 +- demo/cdc_acm_multi_template.c | 2 +- demo/cdc_acm_template.c | 2 +- demo/winusb1.0_template.c | 2 +- demo/winusb2.0_cdc_template.c | 2 +- 12 files changed, 159 insertions(+), 146 deletions(-) delete mode 100644 class/cdc/usbd_cdc.c create mode 100644 class/cdc/usbd_cdc_acm.c create mode 100644 class/cdc/usbd_cdc_acm.h diff --git a/SConscript b/SConscript index 4270ee37..a589f5f1 100644 --- a/SConscript +++ b/SConscript @@ -81,7 +81,7 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']): LIBS = ['libpusb2_dc_a32_softfp_neon.a'] if GetDepend(['PKG_CHERRYUSB_DEVICE_CDC_ACM']): - src += Glob('class/cdc/usbd_cdc.c') + src += Glob('class/cdc/usbd_cdc_acm.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_HID']): src += Glob('class/hid/usbd_hid.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_MSC']): diff --git a/cherryusb.cmake b/cherryusb.cmake index f82dd86c..f5d6fa51 100644 --- a/cherryusb.cmake +++ b/cherryusb.cmake @@ -44,7 +44,7 @@ ${CMAKE_CURRENT_LIST_DIR}/class/vendor/wifi if(CONFIG_CHERRYUSB_DEVICE) list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/core/usbd_core.c) if(CONFIG_CHERRYUSB_DEVICE_CDC_ACM) - list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/class/cdc/usbd_cdc.c) + list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/class/cdc/usbd_cdc_acm.c) endif() if(CONFIG_CHERRYUSB_DEVICE_HID) list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/class/hid/usbd_hid.c) diff --git a/class/cdc/usbd_cdc.c b/class/cdc/usbd_cdc.c deleted file mode 100644 index 50d13d7d..00000000 --- a/class/cdc/usbd_cdc.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2022, sakumisu - * - * SPDX-License-Identifier: Apache-2.0 - */ -#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(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) -{ - USB_LOG_DBG("CDC Class request: " - "bRequest 0x%02x\r\n", - setup->bRequest); - - struct cdc_line_coding line_coding; - bool dtr, rts; - uint8_t intf_num = LO_BYTE(setup->wIndex); - - 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). */ - /*******************************************************************************/ - memcpy(&line_coding, *data, setup->wLength); - USB_LOG_DBG("Set intf:%d linecoding <%d %d %s %s>\r\n", - intf_num, - line_coding.dwDTERate, - line_coding.bDataBits, - parity_name[line_coding.bParityType], - stop_name[line_coding.bCharFormat]); - - usbd_cdc_acm_set_line_coding(busid, intf_num, &line_coding); - break; - - case CDC_REQUEST_SET_CONTROL_LINE_STATE: - dtr = (setup->wValue & 0x0001); - rts = (setup->wValue & 0x0002); - USB_LOG_DBG("Set intf:%d DTR 0x%x,RTS 0x%x\r\n", - intf_num, - dtr, - rts); - usbd_cdc_acm_set_dtr(busid, intf_num, dtr); - usbd_cdc_acm_set_rts(busid, intf_num, rts); - break; - - case CDC_REQUEST_GET_LINE_CODING: - usbd_cdc_acm_get_line_coding(busid, intf_num, &line_coding); - memcpy(*data, &line_coding, 7); - *len = 7; - USB_LOG_DBG("Get intf:%d linecoding %d %d %d %d\r\n", - intf_num, - line_coding.dwDTERate, - line_coding.bCharFormat, - line_coding.bParityType, - line_coding.bDataBits); - break; - case CDC_REQUEST_SEND_BREAK: - usbd_cdc_acm_send_break(busid, intf_num); - break; - default: - USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest); - return -1; - } - - return 0; -} - -struct usbd_interface *usbd_cdc_acm_init_intf(uint8_t busid, struct usbd_interface *intf) -{ - intf->class_interface_handler = cdc_acm_class_interface_request_handler; - intf->class_endpoint_handler = NULL; - intf->vendor_handler = NULL; - intf->notify_handler = NULL; - - return intf; -} - -__WEAK void usbd_cdc_acm_set_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding) -{ -} - -__WEAK void usbd_cdc_acm_get_line_coding(uint8_t busid, 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 busid, uint8_t intf, bool dtr) -{ -} - -__WEAK void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts) -{ -} - -__WEAK void usbd_cdc_acm_send_break(uint8_t busid, uint8_t intf) -{ -} diff --git a/class/cdc/usbd_cdc.h b/class/cdc/usbd_cdc.h index ebf3d029..2cf3df1a 100644 --- a/class/cdc/usbd_cdc.h +++ b/class/cdc/usbd_cdc.h @@ -1,29 +1,13 @@ /* - * Copyright (c) 2022, sakumisu + * Copyright (c) 2024, sakumisu * * SPDX-License-Identifier: Apache-2.0 */ #ifndef USBD_CDC_H #define USBD_CDC_H -#include "usb_cdc.h" +// legacy for old version -#ifdef __cplusplus -extern "C" { -#endif +#include "usbd_cdc_acm.h" -/* Init cdc acm interface driver */ -struct usbd_interface *usbd_cdc_acm_init_intf(uint8_t busid, struct usbd_interface *intf); - -/* Setup request command callback api */ -void usbd_cdc_acm_set_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding); -void usbd_cdc_acm_get_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding); -void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr); -void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts); -void usbd_cdc_acm_send_break(uint8_t busid, uint8_t intf); - -#ifdef __cplusplus -} -#endif - -#endif /* USBD_CDC_H */ +#endif \ No newline at end of file diff --git a/class/cdc/usbd_cdc_acm.c b/class/cdc/usbd_cdc_acm.c new file mode 100644 index 00000000..1ef83028 --- /dev/null +++ b/class/cdc/usbd_cdc_acm.c @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "usbd_core.h" +#include "usbd_cdc_acm.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(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) +{ + USB_LOG_DBG("CDC Class request: " + "bRequest 0x%02x\r\n", + setup->bRequest); + + struct cdc_line_coding line_coding; + bool dtr, rts; + uint8_t intf_num = LO_BYTE(setup->wIndex); + + 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). */ + /*******************************************************************************/ + memcpy(&line_coding, *data, setup->wLength); + USB_LOG_DBG("Set intf:%d linecoding <%d %d %s %s>\r\n", + intf_num, + line_coding.dwDTERate, + line_coding.bDataBits, + parity_name[line_coding.bParityType], + stop_name[line_coding.bCharFormat]); + + usbd_cdc_acm_set_line_coding(busid, intf_num, &line_coding); + break; + + case CDC_REQUEST_SET_CONTROL_LINE_STATE: + dtr = (setup->wValue & 0x0001); + rts = (setup->wValue & 0x0002); + USB_LOG_DBG("Set intf:%d DTR 0x%x,RTS 0x%x\r\n", + intf_num, + dtr, + rts); + usbd_cdc_acm_set_dtr(busid, intf_num, dtr); + usbd_cdc_acm_set_rts(busid, intf_num, rts); + break; + + case CDC_REQUEST_GET_LINE_CODING: + usbd_cdc_acm_get_line_coding(busid, intf_num, &line_coding); + memcpy(*data, &line_coding, 7); + *len = 7; + USB_LOG_DBG("Get intf:%d linecoding %d %d %d %d\r\n", + intf_num, + line_coding.dwDTERate, + line_coding.bCharFormat, + line_coding.bParityType, + line_coding.bDataBits); + break; + case CDC_REQUEST_SEND_BREAK: + usbd_cdc_acm_send_break(busid, intf_num); + break; + default: + USB_LOG_WRN("Unhandled CDC Class bRequest 0x%02x\r\n", setup->bRequest); + return -1; + } + + return 0; +} + +struct usbd_interface *usbd_cdc_acm_init_intf(uint8_t busid, struct usbd_interface *intf) +{ + intf->class_interface_handler = cdc_acm_class_interface_request_handler; + intf->class_endpoint_handler = NULL; + intf->vendor_handler = NULL; + intf->notify_handler = NULL; + + return intf; +} + +__WEAK void usbd_cdc_acm_set_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding) +{ +} + +__WEAK void usbd_cdc_acm_get_line_coding(uint8_t busid, 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 busid, uint8_t intf, bool dtr) +{ +} + +__WEAK void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts) +{ +} + +__WEAK void usbd_cdc_acm_send_break(uint8_t busid, uint8_t intf) +{ +} diff --git a/class/cdc/usbd_cdc_acm.h b/class/cdc/usbd_cdc_acm.h new file mode 100644 index 00000000..662c238c --- /dev/null +++ b/class/cdc/usbd_cdc_acm.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef USBD_CDC_ACM_H +#define USBD_CDC_ACM_H + +#include "usb_cdc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Init cdc acm interface driver */ +struct usbd_interface *usbd_cdc_acm_init_intf(uint8_t busid, struct usbd_interface *intf); + +/* Setup request command callback api */ +void usbd_cdc_acm_set_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding); +void usbd_cdc_acm_get_line_coding(uint8_t busid, uint8_t intf, struct cdc_line_coding *line_coding); +void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr); +void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts); +void usbd_cdc_acm_send_break(uint8_t busid, uint8_t intf); + +#ifdef __cplusplus +} +#endif + +#endif /* USBD_CDC_ACM_H */ diff --git a/demo/cdc_acm_hid_msc_template.c b/demo/cdc_acm_hid_msc_template.c index 11804144..e64a0a52 100644 --- a/demo/cdc_acm_hid_msc_template.c +++ b/demo/cdc_acm_hid_msc_template.c @@ -5,7 +5,7 @@ */ #include "usbd_core.h" #include "usbd_msc.h" -#include "usbd_cdc.h" +#include "usbd_cdc_acm.h" #include "usbd_hid.h" /*!< endpoint address */ diff --git a/demo/cdc_acm_msc_template.c b/demo/cdc_acm_msc_template.c index be0dc3d5..9b53aa8b 100644 --- a/demo/cdc_acm_msc_template.c +++ b/demo/cdc_acm_msc_template.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "usbd_core.h" -#include "usbd_cdc.h" +#include "usbd_cdc_acm.h" #include "usbd_msc.h" /*!< endpoint address */ diff --git a/demo/cdc_acm_multi_template.c b/demo/cdc_acm_multi_template.c index ace26368..3d9dabf4 100644 --- a/demo/cdc_acm_multi_template.c +++ b/demo/cdc_acm_multi_template.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "usbd_core.h" -#include "usbd_cdc.h" +#include "usbd_cdc_acm.h" /*!< endpoint address */ #define CDC_IN_EP 0x81 diff --git a/demo/cdc_acm_template.c b/demo/cdc_acm_template.c index c822a436..402f9346 100644 --- a/demo/cdc_acm_template.c +++ b/demo/cdc_acm_template.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "usbd_core.h" -#include "usbd_cdc.h" +#include "usbd_cdc_acm.h" /*!< endpoint address */ #define CDC_IN_EP 0x81 diff --git a/demo/winusb1.0_template.c b/demo/winusb1.0_template.c index 25b6e3b9..fddf8be1 100644 --- a/demo/winusb1.0_template.c +++ b/demo/winusb1.0_template.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "usbd_core.h" -#include "usbd_cdc.h" +#include "usbd_cdc_acm.h" #define WCID_VENDOR_CODE 0x17 diff --git a/demo/winusb2.0_cdc_template.c b/demo/winusb2.0_cdc_template.c index c4940672..811f6309 100644 --- a/demo/winusb2.0_cdc_template.c +++ b/demo/winusb2.0_cdc_template.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "usbd_core.h" -#include "usbd_cdc.h" +#include "usbd_cdc_acm.h" #define WINUSB_IN_EP 0x81 #define WINUSB_OUT_EP 0x02 -- cgit v1.3.1