Tidier method to define USB String Descriptors in code

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
HuEl_264296
Level 5
Level 5
First like given 25 sign-ins First solution authored

In the Cypress example projects for the CX3 device, the USB string descriptors are defined like this:

/* Standard manufacturer string descriptor */

const uint8_t CyCx3USBManufactureDscr[] =

{

    0x10,                               /* Descriptor size */

    CY_U3P_USB_STRING_DESCR,            /* Device descriptor type */

    'C',0x00,                           /* Manufacturer String */

    'y',0x00,

    'p',0x00,

    'r',0x00,

    'e',0x00,

    's',0x00,

    's',0x00

};

I would like to suggest a tidier method, which is easier to read, and less prone to bugs. First we define a macro:

#define USB_STRING_DESC(S)  ((char const*)&(struct{ char const p[2]; wchar_t const s[sizeof(S)]; }) { {(sizeof(S)), CY_U3P_USB_STRING_DESCR}, S})

Now we can define our descriptors using a single line each:

const uint8_t *CyCx3USBManufactureDscr = USB_STR_DESC(L"Cypress");

const uint8_t *CyCx3USBProductDscr     = USB_STR_DESC(L"CX3 Camera");

const uint8_t *CyCx3USBConfigSSDscr    = USB_STR_DESC(L"USB-3.0");

const uint8_t *CyCx3USBConfigHSDscr    = USB_STR_DESC(L"USB-2.1");

const uint8_t *CyCx3USBConfigFSDscr    = USB_STR_DESC(L"USB-1.1");

2 Replies