42
42
#define USB_CONFIG_POWER 100
43
43
#endif
44
44
45
+ enum
46
+ {
47
+ STRID_LANGUAGE = 0 ,
48
+ STRID_MANUFACTURER,
49
+ STRID_PRODUCT,
50
+ STRID_SERIAL
51
+ };
52
+
45
53
Adafruit_USBD_Device USBDevice;
46
54
47
55
Adafruit_USBD_Device::Adafruit_USBD_Device (void )
@@ -63,9 +71,9 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
63
71
.idVendor = 0 ,
64
72
.idProduct = 0 ,
65
73
.bcdDevice = 0x0100 ,
66
- .iManufacturer = 0x01 ,
67
- .iProduct = 0x02 ,
68
- .iSerialNumber = 0x03 ,
74
+ .iManufacturer = STRID_MANUFACTURER ,
75
+ .iProduct = STRID_PRODUCT ,
76
+ .iSerialNumber = STRID_SERIAL ,
69
77
.bNumConfigurations = 0x01
70
78
};
71
79
@@ -93,28 +101,47 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
93
101
_itf_count = 0 ;
94
102
_epin_count = _epout_count = 1 ;
95
103
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 ;
99
111
}
100
112
101
113
// Add interface descriptor
102
114
// - Interface number will be updated to match current count
103
115
// - 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 )
105
117
{
106
118
uint8_t * desc = _desc_cfg+_desc_cfg_len;
107
119
uint16_t const len = itf.getDescriptor (_itf_count, desc, _desc_cfg_maxlen-_desc_cfg_len);
108
120
uint8_t * desc_end = desc+len;
109
121
110
122
if ( !len ) return false ;
111
123
124
+ // Parse interface descriptor to update
125
+ // - Interface Number & string descrioptor
126
+ // - Endpoint address
112
127
while (desc < desc_end)
113
128
{
114
129
if (desc[1 ] == TUSB_DESC_INTERFACE)
115
130
{
116
131
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
+ }
118
145
}else if (desc[1 ] == TUSB_DESC_ENDPOINT)
119
146
{
120
147
tusb_desc_endpoint_t * desc_ep = (tusb_desc_endpoint_t *) desc;
@@ -164,17 +191,17 @@ void Adafruit_USBD_Device::setDeviceVersion(uint16_t bcd)
164
191
165
192
void Adafruit_USBD_Device::setLanguageDescriptor (uint16_t language_id)
166
193
{
167
- _language_id = language_id;
194
+ _desc_str_arr[STRID_LANGUAGE] = ( const char *) (( uint32_t ) language_id) ;
168
195
}
169
196
170
197
void Adafruit_USBD_Device::setManufacturerDescriptor (const char *s)
171
198
{
172
- _manufacturer = s;
199
+ _desc_str_arr[STRID_MANUFACTURER] = s;
173
200
}
174
201
175
202
void Adafruit_USBD_Device::setProductDescriptor (const char *s)
176
203
{
177
- _product = s;
204
+ _desc_str_arr[STRID_PRODUCT] = s;
178
205
}
179
206
180
207
bool Adafruit_USBD_Device::begin (void )
@@ -204,24 +231,21 @@ uint16_t const* Adafruit_USBD_Device::descriptor_string_cb(uint8_t index, uint16
204
231
switch (index)
205
232
{
206
233
case 0 :
207
- _desc_str[1 ] = _language_id ;
234
+ _desc_str[1 ] = (( uint16_t ) (( uint32_t ) _desc_str_arr[STRID_LANGUAGE])) ;
208
235
chr_count = 1 ;
209
236
break ;
210
237
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
-
219
238
case 3 :
220
239
// serial Number
221
240
chr_count = this ->getSerialDescriptor (_desc_str+1 );
222
241
break ;
223
242
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 ;
225
249
}
226
250
227
251
// first byte is length (including header), second byte is string type
0 commit comments