From b4f5dd06706cf7e802a0570f4ecf0c3d150e282a Mon Sep 17 00:00:00 2001 From: Kizarm Date: Sun, 13 Oct 2024 13:27:24 +0200 Subject: [PATCH] repair string descriptors --- V203/usb/cdc/ch32v203/cdc_class.cpp | 38 ++++++---------------------- V203/usb/cdc/ch32v203/ch32v20x_usb.h | 5 ---- V203/usb/cdc/usb_desc.cpp | 32 +++++++++++------------ V203/usb/cdc/usb_desc.h | 15 +++-------- 4 files changed, 27 insertions(+), 63 deletions(-) diff --git a/V203/usb/cdc/ch32v203/cdc_class.cpp b/V203/usb/cdc/ch32v203/cdc_class.cpp index 12d02e2..f92f73b 100644 --- a/V203/usb/cdc/ch32v203/cdc_class.cpp +++ b/V203/usb/cdc/ch32v203/cdc_class.cpp @@ -295,37 +295,15 @@ void cdc_class::SetupTokenHandler() { break; /* get usb string descriptor */ - case USB_DESCR_TYP_STRING: - switch( (uint8_t)( USBFS_SetupReqValue & 0xFF ) ) { - /* Descriptor 0, Language descriptor */ - case DEF_STRING_DESC_LANG: - pUSBFS_Descr = MyLangDescr; - len = DEF_USBD_LANG_DESC_LEN; - break; - - /* Descriptor 1, Manufacturers String descriptor */ - case DEF_STRING_DESC_MANU: - pUSBFS_Descr = MyManuInfo; - len = DEF_USBD_MANU_DESC_LEN; - break; - - /* Descriptor 2, Product String descriptor */ - case DEF_STRING_DESC_PROD: - pUSBFS_Descr = MyProdInfo; - len = DEF_USBD_PROD_DESC_LEN; - break; - - /* Descriptor 3, Serial-number String descriptor */ - case DEF_STRING_DESC_SERN: - pUSBFS_Descr = MySerNumInfo; - len = DEF_USBD_SN_DESC_LEN; - break; - - default: - errflag = 0xFF; - break; + case USB_DESCR_TYP_STRING: { + const uint8_t order = USBFS_SetupReqValue & 0xFF; + if (order < DEF_MAX_STRINGS) { + pUSBFS_Descr = StringDescArray [order]; + len = pUSBFS_Descr [0]; + } else { + errflag = 0xFF; } - break; + } break; default : errflag = 0xFF; diff --git a/V203/usb/cdc/ch32v203/ch32v20x_usb.h b/V203/usb/cdc/ch32v203/ch32v20x_usb.h index da3d2aa..1fc312c 100644 --- a/V203/usb/cdc/ch32v203/ch32v20x_usb.h +++ b/V203/usb/cdc/ch32v203/ch32v20x_usb.h @@ -60,11 +60,6 @@ extern "C" { #define USB_SYNCH_FRAME 0x0C #endif -#define DEF_STRING_DESC_LANG 0x00 -#define DEF_STRING_DESC_MANU 0x01 -#define DEF_STRING_DESC_PROD 0x02 -#define DEF_STRING_DESC_SERN 0x03 - /* USB hub class request code */ #ifndef HUB_GET_DESCRIPTOR #define HUB_GET_STATUS 0x00 diff --git a/V203/usb/cdc/usb_desc.cpp b/V203/usb/cdc/usb_desc.cpp index 42f8109..781b085 100644 --- a/V203/usb/cdc/usb_desc.cpp +++ b/V203/usb/cdc/usb_desc.cpp @@ -61,25 +61,23 @@ const uint8_t MyCfgDescr[] = /* Endpoint descriptor */ 0x07, 0x05, 0x83, 0x02, (uint8_t)DEF_USBD_ENDP3_SIZE, (uint8_t)( DEF_USBD_ENDP3_SIZE >> 8 ), 0x00, }; -#define USB_DTYPE_STRING 3u -/* Language Descriptor */ -const uint8_t MyLangDescr[] = { - 0x04, 0x03, 0x09, 0x04 -}; +#define DEF_STRDESC(p,n) w_text<(sizeof(p)>>1)>n={sizeof(n)-2u,3u,{p}} template struct w_text { uint8_t len, typ; const char16_t str [N]; -}__attribute__((packed)); +}; +static const DEF_STRDESC((u"Kizarm Labs."), str_1); +static const DEF_STRDESC((u"USB <=> USART"),str_2); +static const DEF_STRDESC((u"0001"), str_3); +/* Language Descriptor */ +static const uint8_t LangDescr[] = { + 0x04, 0x03, 0x09, 0x04 +}; +const uint8_t * StringDescArray [DEF_MAX_STRINGS] = { + LangDescr, + reinterpret_cast (&str_1), + reinterpret_cast (&str_2), + reinterpret_cast (&str_3), +}; -#define WSTR1 (u"Kizarm Labs.") -#define WSTR2 (u"USB <=> USART") -#define WSTR3 (u"0001") - -static const w_text<(sizeof WSTR1 >> 1u)> str_1 = {sizeof (str_1) - 2u, USB_DTYPE_STRING, {WSTR1}}; -static const w_text<(sizeof WSTR2 >> 1u)> str_2 = {sizeof (str_2) - 2u, USB_DTYPE_STRING, {WSTR2}}; -static const w_text<(sizeof WSTR3 >> 1u)> str_3 = {sizeof (str_3) - 2u, USB_DTYPE_STRING, {WSTR3}}; - -const uint8_t * MyManuInfo = reinterpret_cast (&str_1); -const uint8_t * MyProdInfo = reinterpret_cast (&str_2); -const uint8_t * MySerNumInfo = reinterpret_cast (&str_3); diff --git a/V203/usb/cdc/usb_desc.h b/V203/usb/cdc/usb_desc.h index 57d89f2..9b578b9 100644 --- a/V203/usb/cdc/usb_desc.h +++ b/V203/usb/cdc/usb_desc.h @@ -50,23 +50,16 @@ #define DEF_USBD_DEVICE_DESC_LEN ((uint8_t)MyDevDescr[0]) #define DEF_USBD_CONFIG_DESC_LEN ((uint16_t)MyCfgDescr[2] + (uint16_t)(MyCfgDescr[3] << 8)) #define DEF_USBD_REPORT_DESC_LEN 0 -#define DEF_USBD_LANG_DESC_LEN ((uint16_t)MyLangDescr[0]) -#define DEF_USBD_MANU_DESC_LEN ((uint16_t)MyManuInfo[0]) -#define DEF_USBD_PROD_DESC_LEN ((uint16_t)MyProdInfo[0]) -#define DEF_USBD_SN_DESC_LEN ((uint16_t)MySerNumInfo[0]) +#define DEF_MAX_STRINGS (4) /******************************************************************************/ /* external variables */ #ifdef __cplusplus extern "C" { #endif // __cplusplus -extern const uint8_t MyDevDescr[ ]; -extern const uint8_t MyCfgDescr[ ]; -extern const uint8_t MyLangDescr[ ]; - -extern const uint8_t * MyManuInfo; -extern const uint8_t * MyProdInfo; -extern const uint8_t * MySerNumInfo; + extern const uint8_t MyDevDescr[ ]; + extern const uint8_t MyCfgDescr[ ]; + extern const uint8_t * StringDescArray [DEF_MAX_STRINGS]; #ifdef __cplusplus }; #endif // __cplusplus