Skip to content

Commit fe87499

Browse files
authored
Merge pull request #5874 from c1728p9/usb_fixes_and_improvements
USB fixes and improvements
2 parents dfa5533 + beaac15 commit fe87499

File tree

20 files changed

+199
-116
lines changed

20 files changed

+199
-116
lines changed

features/unsupported/USBDevice/USBAudio/USBAudio.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ USBAudio::USBAudio(uint32_t frequency_in, uint8_t channel_nb_in, uint32_t freque
5656

5757
volume = 0;
5858

59+
_build_configurationDesc();
60+
5961
// connect the device
6062
USBDevice::connect();
6163
}
@@ -377,8 +379,8 @@ void USBAudio::USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {
377379
FEATURE_UNIT_DESCRIPTOR_LENGTH + \
378380
2*OUTPUT_TERMINAL_DESCRIPTOR_LENGTH)
379381

380-
uint8_t * USBAudio::configurationDesc() {
381-
static uint8_t configDescriptor[] = {
382+
void USBAudio::_build_configurationDesc() {
383+
uint8_t configDescriptorTemp[] = {
382384
// Configuration 1
383385
CONFIGURATION_DESCRIPTOR_LENGTH, // bLength
384386
CONFIGURATION_DESCRIPTOR, // bDescriptorType
@@ -615,24 +617,28 @@ uint8_t * USBAudio::configurationDesc() {
615617
0x00, // bLockDelayUnits
616618
LSB(0x0000), // wLockDelay
617619
MSB(0x0000), // wLockDelay
618-
619-
// Terminator
620-
0 // bLength
621620
};
621+
622+
MBED_ASSERT(sizeof(configDescriptorTemp) == sizeof(configDescriptor));
623+
memcpy(configDescriptor, configDescriptorTemp, sizeof(configDescriptor));
624+
}
625+
626+
const uint8_t * USBAudio::configurationDesc() {
627+
622628
return configDescriptor;
623629
}
624630

625-
uint8_t * USBAudio::stringIinterfaceDesc() {
626-
static uint8_t stringIinterfaceDescriptor[] = {
631+
const uint8_t * USBAudio::stringIinterfaceDesc() {
632+
static const uint8_t stringIinterfaceDescriptor[] = {
627633
0x0c, //bLength
628634
STRING_DESCRIPTOR, //bDescriptorType 0x03
629635
'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
630636
};
631637
return stringIinterfaceDescriptor;
632638
}
633639

634-
uint8_t * USBAudio::stringIproductDesc() {
635-
static uint8_t stringIproductDescriptor[] = {
640+
const uint8_t * USBAudio::stringIproductDesc() {
641+
static const uint8_t stringIproductDescriptor[] = {
636642
0x16, //bLength
637643
STRING_DESCRIPTOR, //bDescriptorType 0x03
638644
'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio

features/unsupported/USBDevice/USBAudio/USBAudio.h

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,23 @@ class USBAudio: public USBDevice {
152152
*
153153
*/
154154
void attach(void(*fptr)(void)) {
155-
updateVol.attach(fptr);
155+
updateVol = Callback<void()>(fptr);
156156
}
157157
/** attach a handler to Tx Done
158158
*
159159
* @param function Function to attach
160160
*
161161
*/
162162
void attachTx(void(*fptr)(void)) {
163-
txDone.attach(fptr);
163+
txDone = Callback<void()>(fptr);
164164
}
165165
/** attach a handler to Rx Done
166166
*
167167
* @param function Function to attach
168168
*
169169
*/
170170
void attachRx(void(*fptr)(void)) {
171-
rxDone.attach(fptr);
171+
rxDone = Callback<void()>(fptr);
172172
}
173173

174174
/** Attach a nonstatic void/void member function to update the volume
@@ -179,15 +179,52 @@ class USBAudio: public USBDevice {
179179
*/
180180
template<typename T>
181181
void attach(T *tptr, void(T::*mptr)(void)) {
182-
updateVol.attach(tptr, mptr);
182+
updateVol = Callback<void()>(tptr, mptr);
183183
}
184+
/** Attach a nonstatic void/void member function to Tx Done
185+
*
186+
* @param tptr Object pointer
187+
* @param mptr Member function pointer
188+
*
189+
*/
184190
template<typename T>
185191
void attachTx(T *tptr, void(T::*mptr)(void)) {
186-
txDone.attach(tptr, mptr);
192+
txDone = Callback<void()>(tptr, mptr);
187193
}
194+
/** Attach a nonstatic void/void member function to Rx Done
195+
*
196+
* @param tptr Object pointer
197+
* @param mptr Member function pointer
198+
*
199+
*/
188200
template<typename T>
189201
void attachRx(T *tptr, void(T::*mptr)(void)) {
190-
rxDone.attach(tptr, mptr);
202+
rxDone = Callback<void()>(tptr, mptr);
203+
}
204+
205+
/** Attach a Callback to update the volume
206+
*
207+
* @param cb Callback to attach
208+
*
209+
*/
210+
void attach(Callback<void()> &cb) {
211+
updateVol = cb;
212+
}
213+
/** attach a Callback to Tx Done
214+
*
215+
* @param cb Callback to attach
216+
*
217+
*/
218+
void attachTx(Callback<void()> &cb) {
219+
txDone = cb;
220+
}
221+
/** attach a Callback to Rx Done
222+
*
223+
* @param cb Callback to attach
224+
*
225+
*/
226+
void attachRx(Callback<void()> &cb) {
227+
rxDone = cb;
191228
}
192229

193230

@@ -216,21 +253,21 @@ class USBAudio: public USBDevice {
216253
*
217254
* @returns pointer to the string product descriptor
218255
*/
219-
virtual uint8_t * stringIproductDesc();
256+
virtual const uint8_t * stringIproductDesc();
220257

221258
/*
222259
* Get string interface descriptor
223260
*
224261
* @returns pointer to the string interface descriptor
225262
*/
226-
virtual uint8_t * stringIinterfaceDesc();
263+
virtual const uint8_t * stringIinterfaceDesc();
227264

228265
/*
229266
* Get configuration descriptor
230267
*
231268
* @returns pointer to the configuration descriptor
232269
*/
233-
virtual uint8_t * configurationDesc();
270+
virtual const uint8_t * configurationDesc();
234271

235272
/*
236273
* Called by USBDevice layer. Set interface/alternate of the device.
@@ -270,6 +307,20 @@ class USBAudio: public USBDevice {
270307

271308
private:
272309

310+
/*
311+
* Call to rebuild the configuration descriptor
312+
*
313+
* This function should be called on creation or when any
314+
* value that is part of the configuration descriptor
315+
* changes.
316+
* @note This function uses ~200 bytes of stack so
317+
* make sure your stack is big enough for it.
318+
*/
319+
void _build_configurationDesc();
320+
321+
// configuration descriptor
322+
uint8_t configDescriptor[183];
323+
273324
// stream available ?
274325
volatile bool available;
275326

features/unsupported/USBDevice/USBDevice/USBDevice.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool USBDevice::requestGetDescriptor(void)
5858
printf("device descr\r\n");
5959
#endif
6060
transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
61-
transfer.ptr = deviceDesc();
61+
transfer.ptr = (uint8_t*)deviceDesc();
6262
transfer.direction = DEVICE_TO_HOST;
6363
success = true;
6464
}
@@ -77,7 +77,7 @@ bool USBDevice::requestGetDescriptor(void)
7777
transfer.remaining = configurationDesc()[2] \
7878
| (configurationDesc()[3] << 8);
7979

80-
transfer.ptr = configurationDesc();
80+
transfer.ptr = (uint8_t*)configurationDesc();
8181
transfer.direction = DEVICE_TO_HOST;
8282
success = true;
8383
}
@@ -94,7 +94,7 @@ bool USBDevice::requestGetDescriptor(void)
9494
printf("1\r\n");
9595
#endif
9696
transfer.remaining = stringLangidDesc()[0];
97-
transfer.ptr = stringLangidDesc();
97+
transfer.ptr = (uint8_t*)stringLangidDesc();
9898
transfer.direction = DEVICE_TO_HOST;
9999
success = true;
100100
break;
@@ -103,7 +103,7 @@ bool USBDevice::requestGetDescriptor(void)
103103
printf("2\r\n");
104104
#endif
105105
transfer.remaining = stringImanufacturerDesc()[0];
106-
transfer.ptr = stringImanufacturerDesc();
106+
transfer.ptr = (uint8_t*)stringImanufacturerDesc();
107107
transfer.direction = DEVICE_TO_HOST;
108108
success = true;
109109
break;
@@ -112,7 +112,7 @@ bool USBDevice::requestGetDescriptor(void)
112112
printf("3\r\n");
113113
#endif
114114
transfer.remaining = stringIproductDesc()[0];
115-
transfer.ptr = stringIproductDesc();
115+
transfer.ptr = (uint8_t*)stringIproductDesc();
116116
transfer.direction = DEVICE_TO_HOST;
117117
success = true;
118118
break;
@@ -121,7 +121,7 @@ bool USBDevice::requestGetDescriptor(void)
121121
printf("4\r\n");
122122
#endif
123123
transfer.remaining = stringIserialDesc()[0];
124-
transfer.ptr = stringIserialDesc();
124+
transfer.ptr = (uint8_t*)stringIserialDesc();
125125
transfer.direction = DEVICE_TO_HOST;
126126
success = true;
127127
break;
@@ -130,7 +130,7 @@ bool USBDevice::requestGetDescriptor(void)
130130
printf("5\r\n");
131131
#endif
132132
transfer.remaining = stringIConfigurationDesc()[0];
133-
transfer.ptr = stringIConfigurationDesc();
133+
transfer.ptr = (uint8_t*)stringIConfigurationDesc();
134134
transfer.direction = DEVICE_TO_HOST;
135135
success = true;
136136
break;
@@ -139,7 +139,7 @@ bool USBDevice::requestGetDescriptor(void)
139139
printf("6\r\n");
140140
#endif
141141
transfer.remaining = stringIinterfaceDesc()[0];
142-
transfer.ptr = stringIinterfaceDesc();
142+
transfer.ptr = (uint8_t*)stringIinterfaceDesc();
143143
transfer.direction = DEVICE_TO_HOST;
144144
success = true;
145145
break;
@@ -771,7 +771,7 @@ uint8_t * USBDevice::findDescriptor(uint8_t descriptorType)
771771
}
772772

773773
/* Start at first descriptor after the configuration descriptor */
774-
ptr = &(configurationDesc()[CONFIGURATION_DESCRIPTOR_LENGTH]);
774+
ptr = &(((uint8_t*)configurationDesc())[CONFIGURATION_DESCRIPTOR_LENGTH]);
775775

776776
do {
777777
if (ptr[1] /* bDescriptorType */ == descriptorType)
@@ -907,8 +907,8 @@ bool USBDevice::readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, u
907907

908908

909909

910-
uint8_t * USBDevice::deviceDesc() {
911-
static uint8_t deviceDescriptor[] = {
910+
const uint8_t * USBDevice::deviceDesc() {
911+
uint8_t deviceDescriptorTemp[] = {
912912
DEVICE_DESCRIPTOR_LENGTH, /* bLength */
913913
DEVICE_DESCRIPTOR, /* bDescriptorType */
914914
LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */
@@ -928,56 +928,58 @@ uint8_t * USBDevice::deviceDesc() {
928928
STRING_OFFSET_ISERIAL, /* iSerialNumber */
929929
0x01 /* bNumConfigurations */
930930
};
931+
MBED_ASSERT(sizeof(deviceDescriptorTemp) == sizeof(deviceDescriptor));
932+
memcpy(deviceDescriptor, deviceDescriptorTemp, sizeof(deviceDescriptor));
931933
return deviceDescriptor;
932934
}
933935

934-
uint8_t * USBDevice::stringLangidDesc() {
935-
static uint8_t stringLangidDescriptor[] = {
936+
const uint8_t * USBDevice::stringLangidDesc() {
937+
static const uint8_t stringLangidDescriptor[] = {
936938
0x04, /*bLength*/
937939
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
938940
0x09,0x04, /*bString Lang ID - 0x0409 - English*/
939941
};
940-
return stringLangidDescriptor;
942+
return (uint8_t *)stringLangidDescriptor;
941943
}
942944

943-
uint8_t * USBDevice::stringImanufacturerDesc() {
944-
static uint8_t stringImanufacturerDescriptor[] = {
945+
const uint8_t * USBDevice::stringImanufacturerDesc() {
946+
static const uint8_t stringImanufacturerDescriptor[] = {
945947
0x12, /*bLength*/
946948
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
947949
'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/
948950
};
949951
return stringImanufacturerDescriptor;
950952
}
951953

952-
uint8_t * USBDevice::stringIserialDesc() {
953-
static uint8_t stringIserialDescriptor[] = {
954+
const uint8_t * USBDevice::stringIserialDesc() {
955+
static const uint8_t stringIserialDescriptor[] = {
954956
0x16, /*bLength*/
955957
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
956958
'0',0,'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0, /*bString iSerial - 0123456789*/
957959
};
958960
return stringIserialDescriptor;
959961
}
960962

961-
uint8_t * USBDevice::stringIConfigurationDesc() {
962-
static uint8_t stringIconfigurationDescriptor[] = {
963+
const uint8_t * USBDevice::stringIConfigurationDesc() {
964+
static const uint8_t stringIconfigurationDescriptor[] = {
963965
0x06, /*bLength*/
964966
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
965967
'0',0,'1',0, /*bString iConfiguration - 01*/
966968
};
967969
return stringIconfigurationDescriptor;
968970
}
969971

970-
uint8_t * USBDevice::stringIinterfaceDesc() {
971-
static uint8_t stringIinterfaceDescriptor[] = {
972+
const uint8_t * USBDevice::stringIinterfaceDesc() {
973+
static const uint8_t stringIinterfaceDescriptor[] = {
972974
0x08, /*bLength*/
973975
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
974976
'U',0,'S',0,'B',0, /*bString iInterface - USB*/
975977
};
976978
return stringIinterfaceDescriptor;
977979
}
978980

979-
uint8_t * USBDevice::stringIproductDesc() {
980-
static uint8_t stringIproductDescriptor[] = {
981+
const uint8_t * USBDevice::stringIproductDesc() {
982+
static const uint8_t stringIproductDescriptor[] = {
981983
0x16, /*bLength*/
982984
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
983985
'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - USB DEVICE*/

0 commit comments

Comments
 (0)