Skip to content
This repository was archived by the owner on May 18, 2021. It is now read-only.

Commit f992006

Browse files
committed
support string descriptor for interface
1 parent 54319f3 commit f992006

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

Adafruit_USBD_Device.cpp

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
#define USB_CONFIG_POWER 100
4343
#endif
4444

45+
enum
46+
{
47+
STRID_LANGUAGE = 0,
48+
STRID_MANUFACTURER,
49+
STRID_PRODUCT,
50+
STRID_SERIAL
51+
};
52+
4553
Adafruit_USBD_Device USBDevice;
4654

4755
Adafruit_USBD_Device::Adafruit_USBD_Device(void)
@@ -63,9 +71,9 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
6371
.idVendor = 0,
6472
.idProduct = 0,
6573
.bcdDevice = 0x0100,
66-
.iManufacturer = 0x01,
67-
.iProduct = 0x02,
68-
.iSerialNumber = 0x03,
74+
.iManufacturer = STRID_MANUFACTURER,
75+
.iProduct = STRID_PRODUCT,
76+
.iSerialNumber = STRID_SERIAL,
6977
.bNumConfigurations = 0x01
7078
};
7179

@@ -93,28 +101,47 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
93101
_itf_count = 0;
94102
_epin_count = _epout_count = 1;
95103

96-
_language_id = USB_LANGUAGE;
97-
_manufacturer = USB_MANUFACTURER;
98-
_product = USB_PRODUCT;
104+
memset(_desc_str_arr, 0, sizeof(_desc_str_arr));
105+
_desc_str_arr[STRID_LANGUAGE] = (const char*) ((uint32_t) USB_LANGUAGE);
106+
_desc_str_arr[STRID_MANUFACTURER] = USB_MANUFACTURER;
107+
_desc_str_arr[STRID_PRODUCT] = USB_PRODUCT;
108+
// STRID_SERIAL is platform dependent
109+
110+
_desc_str_count = 4;
99111
}
100112

101113
// Add interface descriptor
102114
// - Interface number will be updated to match current count
103115
// - Endpoint number is updated to be unique
104-
bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface& itf)
116+
bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface& itf, const char* desc_str)
105117
{
106118
uint8_t* desc = _desc_cfg+_desc_cfg_len;
107119
uint16_t const len = itf.getDescriptor(_itf_count, desc, _desc_cfg_maxlen-_desc_cfg_len);
108120
uint8_t* desc_end = desc+len;
109121

110122
if ( !len ) return false;
111123

124+
// Parse interface descriptor to update
125+
// - Interface Number & string descrioptor
126+
// - Endpoint address
112127
while (desc < desc_end)
113128
{
114129
if (desc[1] == TUSB_DESC_INTERFACE)
115130
{
116131
tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) desc;
117-
if (desc_itf->bAlternateSetting == 0) _itf_count++;
132+
if (desc_itf->bAlternateSetting == 0)
133+
{
134+
_itf_count++;
135+
if (desc_str && (_desc_str_count < STRING_DESCRIPTOR_MAX) )
136+
{
137+
_desc_str_arr[_desc_str_count] = desc_str;
138+
desc_itf->iInterface = _desc_str_count;
139+
_desc_str_count++;
140+
141+
// only assign string index to first interface
142+
desc_str = NULL;
143+
}
144+
}
118145
}else if (desc[1] == TUSB_DESC_ENDPOINT)
119146
{
120147
tusb_desc_endpoint_t* desc_ep = (tusb_desc_endpoint_t*) desc;
@@ -164,17 +191,17 @@ void Adafruit_USBD_Device::setDeviceVersion(uint16_t bcd)
164191

165192
void Adafruit_USBD_Device::setLanguageDescriptor (uint16_t language_id)
166193
{
167-
_language_id = language_id;
194+
_desc_str_arr[STRID_LANGUAGE] = (const char*) ((uint32_t) language_id);
168195
}
169196

170197
void Adafruit_USBD_Device::setManufacturerDescriptor(const char *s)
171198
{
172-
_manufacturer = s;
199+
_desc_str_arr[STRID_MANUFACTURER] = s;
173200
}
174201

175202
void Adafruit_USBD_Device::setProductDescriptor(const char *s)
176203
{
177-
_product = s;
204+
_desc_str_arr[STRID_PRODUCT] = s;
178205
}
179206

180207
bool Adafruit_USBD_Device::begin(void)
@@ -204,24 +231,21 @@ uint16_t const* Adafruit_USBD_Device::descriptor_string_cb(uint8_t index, uint16
204231
switch (index)
205232
{
206233
case 0:
207-
_desc_str[1] = _language_id;
234+
_desc_str[1] = ((uint16_t) ((uint32_t) _desc_str_arr[STRID_LANGUAGE]));
208235
chr_count = 1;
209236
break;
210237

211-
case 1:
212-
chr_count = strcpy_utf16(_manufacturer, _desc_str + 1, 32);
213-
break;
214-
215-
case 2:
216-
chr_count = strcpy_utf16(_product, _desc_str + 1, 32);
217-
break;
218-
219238
case 3:
220239
// serial Number
221240
chr_count = this->getSerialDescriptor(_desc_str+1);
222241
break;
223242

224-
default: return NULL;
243+
default:
244+
// Invalid index
245+
if (index >= _desc_str_count ) return NULL;
246+
247+
chr_count = strcpy_utf16(_desc_str_arr[index], _desc_str + 1, 32);
248+
break;
225249
}
226250

227251
// first byte is length (including header), second byte is string type

Adafruit_USBD_Device.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Adafruit_USBD_Interface
3636
class Adafruit_USBD_Device
3737
{
3838
private:
39+
enum { STRING_DESCRIPTOR_MAX = 8 };
40+
3941
tusb_desc_device_t _desc_device;
4042

4143
uint8_t *_desc_cfg;
@@ -48,16 +50,13 @@ class Adafruit_USBD_Device
4850
uint8_t _epin_count;
4951
uint8_t _epout_count;
5052

51-
uint16_t _language_id;
52-
const char *_manufacturer;
53-
const char *_product;
54-
55-
// const char*
53+
const char* _desc_str_arr[STRING_DESCRIPTOR_MAX];
54+
uint8_t _desc_str_count;
5655

5756
public:
5857
Adafruit_USBD_Device(void);
5958

60-
bool addInterface(Adafruit_USBD_Interface& itf);
59+
bool addInterface(Adafruit_USBD_Interface& itf, const char* desc_str = NULL);
6160
void setDescriptorBuffer(uint8_t* buf, uint32_t buflen);
6261

6362
void setID(uint16_t vid, uint16_t pid);

0 commit comments

Comments
 (0)