Skip to content

USB fixes and improvements #5874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions features/unsupported/USBDevice/USBAudio/USBAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ USBAudio::USBAudio(uint32_t frequency_in, uint8_t channel_nb_in, uint32_t freque

volume = 0;

_build_configurationDesc();

// connect the device
USBDevice::connect();
}
Expand Down Expand Up @@ -377,8 +379,8 @@ void USBAudio::USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {
FEATURE_UNIT_DESCRIPTOR_LENGTH + \
2*OUTPUT_TERMINAL_DESCRIPTOR_LENGTH)

uint8_t * USBAudio::configurationDesc() {
static uint8_t configDescriptor[] = {
void USBAudio::_build_configurationDesc() {
uint8_t configDescriptorTemp[] = {
// Configuration 1
CONFIGURATION_DESCRIPTOR_LENGTH, // bLength
CONFIGURATION_DESCRIPTOR, // bDescriptorType
Expand Down Expand Up @@ -615,24 +617,28 @@ uint8_t * USBAudio::configurationDesc() {
0x00, // bLockDelayUnits
LSB(0x0000), // wLockDelay
MSB(0x0000), // wLockDelay

// Terminator
0 // bLength
};

MBED_ASSERT(sizeof(configDescriptorTemp) == sizeof(configDescriptor));
memcpy(configDescriptor, configDescriptorTemp, sizeof(configDescriptor));
}

const uint8_t * USBAudio::configurationDesc() {

return configDescriptor;
}

uint8_t * USBAudio::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = {
const uint8_t * USBAudio::stringIinterfaceDesc() {
static const uint8_t stringIinterfaceDescriptor[] = {
0x0c, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03
'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
};
return stringIinterfaceDescriptor;
}

uint8_t * USBAudio::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = {
const uint8_t * USBAudio::stringIproductDesc() {
static const uint8_t stringIproductDescriptor[] = {
0x16, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03
'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
Expand Down
69 changes: 60 additions & 9 deletions features/unsupported/USBDevice/USBAudio/USBAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,23 @@ class USBAudio: public USBDevice {
*
*/
void attach(void(*fptr)(void)) {
updateVol.attach(fptr);
updateVol = Callback<void()>(fptr);
}
/** attach a handler to Tx Done
*
* @param function Function to attach
*
*/
void attachTx(void(*fptr)(void)) {
txDone.attach(fptr);
txDone = Callback<void()>(fptr);
}
/** attach a handler to Rx Done
*
* @param function Function to attach
*
*/
void attachRx(void(*fptr)(void)) {
rxDone.attach(fptr);
rxDone = Callback<void()>(fptr);
}

/** Attach a nonstatic void/void member function to update the volume
Expand All @@ -179,15 +179,52 @@ class USBAudio: public USBDevice {
*/
template<typename T>
void attach(T *tptr, void(T::*mptr)(void)) {
updateVol.attach(tptr, mptr);
updateVol = Callback<void()>(tptr, mptr);
}
/** Attach a nonstatic void/void member function to Tx Done
*
* @param tptr Object pointer
* @param mptr Member function pointer
*
*/
template<typename T>
void attachTx(T *tptr, void(T::*mptr)(void)) {
txDone.attach(tptr, mptr);
txDone = Callback<void()>(tptr, mptr);
}
/** Attach a nonstatic void/void member function to Rx Done
*
* @param tptr Object pointer
* @param mptr Member function pointer
*
*/
template<typename T>
void attachRx(T *tptr, void(T::*mptr)(void)) {
rxDone.attach(tptr, mptr);
rxDone = Callback<void()>(tptr, mptr);
}

/** Attach a Callback to update the volume
*
* @param cb Callback to attach
*
*/
void attach(Callback<void()> &cb) {
updateVol = cb;
}
/** attach a Callback to Tx Done
*
* @param cb Callback to attach
*
*/
void attachTx(Callback<void()> &cb) {
txDone = cb;
}
/** attach a Callback to Rx Done
*
* @param cb Callback to attach
*
*/
void attachRx(Callback<void()> &cb) {
rxDone = cb;
}


Expand Down Expand Up @@ -216,21 +253,21 @@ class USBAudio: public USBDevice {
*
* @returns pointer to the string product descriptor
*/
virtual uint8_t * stringIproductDesc();
virtual const uint8_t * stringIproductDesc();

/*
* Get string interface descriptor
*
* @returns pointer to the string interface descriptor
*/
virtual uint8_t * stringIinterfaceDesc();
virtual const uint8_t * stringIinterfaceDesc();

/*
* Get configuration descriptor
*
* @returns pointer to the configuration descriptor
*/
virtual uint8_t * configurationDesc();
virtual const uint8_t * configurationDesc();

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

private:

/*
* Call to rebuild the configuration descriptor
*
* This function should be called on creation or when any
* value that is part of the configuration descriptor
* changes.
* @note This function uses ~200 bytes of stack so
* make sure your stack is big enough for it.
*/
void _build_configurationDesc();

// configuration descriptor
uint8_t configDescriptor[183];

// stream available ?
volatile bool available;

Expand Down
50 changes: 26 additions & 24 deletions features/unsupported/USBDevice/USBDevice/USBDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("device descr\r\n");
#endif
transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
transfer.ptr = deviceDesc();
transfer.ptr = (uint8_t*)deviceDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
}
Expand All @@ -77,7 +77,7 @@ bool USBDevice::requestGetDescriptor(void)
transfer.remaining = configurationDesc()[2] \
| (configurationDesc()[3] << 8);

transfer.ptr = configurationDesc();
transfer.ptr = (uint8_t*)configurationDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
}
Expand All @@ -94,7 +94,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("1\r\n");
#endif
transfer.remaining = stringLangidDesc()[0];
transfer.ptr = stringLangidDesc();
transfer.ptr = (uint8_t*)stringLangidDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
break;
Expand All @@ -103,7 +103,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("2\r\n");
#endif
transfer.remaining = stringImanufacturerDesc()[0];
transfer.ptr = stringImanufacturerDesc();
transfer.ptr = (uint8_t*)stringImanufacturerDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
break;
Expand All @@ -112,7 +112,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("3\r\n");
#endif
transfer.remaining = stringIproductDesc()[0];
transfer.ptr = stringIproductDesc();
transfer.ptr = (uint8_t*)stringIproductDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
break;
Expand All @@ -121,7 +121,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("4\r\n");
#endif
transfer.remaining = stringIserialDesc()[0];
transfer.ptr = stringIserialDesc();
transfer.ptr = (uint8_t*)stringIserialDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
break;
Expand All @@ -130,7 +130,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("5\r\n");
#endif
transfer.remaining = stringIConfigurationDesc()[0];
transfer.ptr = stringIConfigurationDesc();
transfer.ptr = (uint8_t*)stringIConfigurationDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
break;
Expand All @@ -139,7 +139,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("6\r\n");
#endif
transfer.remaining = stringIinterfaceDesc()[0];
transfer.ptr = stringIinterfaceDesc();
transfer.ptr = (uint8_t*)stringIinterfaceDesc();
transfer.direction = DEVICE_TO_HOST;
success = true;
break;
Expand Down Expand Up @@ -790,7 +790,7 @@ uint8_t * USBDevice::findDescriptor(uint8_t descriptorType)
}

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

do {
if (ptr[1] /* bDescriptorType */ == descriptorType)
Expand Down Expand Up @@ -926,8 +926,8 @@ bool USBDevice::readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, u



uint8_t * USBDevice::deviceDesc() {
static uint8_t deviceDescriptor[] = {
const uint8_t * USBDevice::deviceDesc() {
uint8_t deviceDescriptorTemp[] = {
DEVICE_DESCRIPTOR_LENGTH, /* bLength */
DEVICE_DESCRIPTOR, /* bDescriptorType */
LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */
Expand All @@ -947,56 +947,58 @@ uint8_t * USBDevice::deviceDesc() {
STRING_OFFSET_ISERIAL, /* iSerialNumber */
0x01 /* bNumConfigurations */
};
MBED_ASSERT(sizeof(deviceDescriptorTemp) == sizeof(deviceDescriptor));
memcpy(deviceDescriptor, deviceDescriptorTemp, sizeof(deviceDescriptor));
return deviceDescriptor;
}

uint8_t * USBDevice::stringLangidDesc() {
static uint8_t stringLangidDescriptor[] = {
const uint8_t * USBDevice::stringLangidDesc() {
static const uint8_t stringLangidDescriptor[] = {
0x04, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
0x09,0x04, /*bString Lang ID - 0x0409 - English*/
};
return stringLangidDescriptor;
return (uint8_t *)stringLangidDescriptor;
}

uint8_t * USBDevice::stringImanufacturerDesc() {
static uint8_t stringImanufacturerDescriptor[] = {
const uint8_t * USBDevice::stringImanufacturerDesc() {
static const uint8_t stringImanufacturerDescriptor[] = {
0x12, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/
};
return stringImanufacturerDescriptor;
}

uint8_t * USBDevice::stringIserialDesc() {
static uint8_t stringIserialDescriptor[] = {
const uint8_t * USBDevice::stringIserialDesc() {
static const uint8_t stringIserialDescriptor[] = {
0x16, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'0',0,'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0, /*bString iSerial - 0123456789*/
};
return stringIserialDescriptor;
}

uint8_t * USBDevice::stringIConfigurationDesc() {
static uint8_t stringIconfigurationDescriptor[] = {
const uint8_t * USBDevice::stringIConfigurationDesc() {
static const uint8_t stringIconfigurationDescriptor[] = {
0x06, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'0',0,'1',0, /*bString iConfiguration - 01*/
};
return stringIconfigurationDescriptor;
}

uint8_t * USBDevice::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = {
const uint8_t * USBDevice::stringIinterfaceDesc() {
static const uint8_t stringIinterfaceDescriptor[] = {
0x08, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'U',0,'S',0,'B',0, /*bString iInterface - USB*/
};
return stringIinterfaceDescriptor;
}

uint8_t * USBDevice::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = {
const uint8_t * USBDevice::stringIproductDesc() {
static const uint8_t stringIproductDescriptor[] = {
0x16, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - USB DEVICE*/
Expand Down
Loading