Merge pull request #5874 from c1728p9/usb_fixes_and_improvements

USB fixes and improvements
pull/5917/merge
Cruz Monrreal 2018-01-26 10:37:13 -06:00 committed by GitHub
commit fe87499af8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 199 additions and 116 deletions

View File

@ -56,6 +56,8 @@ USBAudio::USBAudio(uint32_t frequency_in, uint8_t channel_nb_in, uint32_t freque
volume = 0; volume = 0;
_build_configurationDesc();
// connect the device // connect the device
USBDevice::connect(); USBDevice::connect();
} }
@ -377,8 +379,8 @@ void USBAudio::USBCallback_requestCompleted(uint8_t * buf, uint32_t length) {
FEATURE_UNIT_DESCRIPTOR_LENGTH + \ FEATURE_UNIT_DESCRIPTOR_LENGTH + \
2*OUTPUT_TERMINAL_DESCRIPTOR_LENGTH) 2*OUTPUT_TERMINAL_DESCRIPTOR_LENGTH)
uint8_t * USBAudio::configurationDesc() { void USBAudio::_build_configurationDesc() {
static uint8_t configDescriptor[] = { uint8_t configDescriptorTemp[] = {
// Configuration 1 // Configuration 1
CONFIGURATION_DESCRIPTOR_LENGTH, // bLength CONFIGURATION_DESCRIPTOR_LENGTH, // bLength
CONFIGURATION_DESCRIPTOR, // bDescriptorType CONFIGURATION_DESCRIPTOR, // bDescriptorType
@ -615,15 +617,19 @@ uint8_t * USBAudio::configurationDesc() {
0x00, // bLockDelayUnits 0x00, // bLockDelayUnits
LSB(0x0000), // wLockDelay LSB(0x0000), // wLockDelay
MSB(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; return configDescriptor;
} }
uint8_t * USBAudio::stringIinterfaceDesc() { const uint8_t * USBAudio::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = { static const uint8_t stringIinterfaceDescriptor[] = {
0x0c, //bLength 0x0c, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 STRING_DESCRIPTOR, //bDescriptorType 0x03
'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
@ -631,8 +637,8 @@ uint8_t * USBAudio::stringIinterfaceDesc() {
return stringIinterfaceDescriptor; return stringIinterfaceDescriptor;
} }
uint8_t * USBAudio::stringIproductDesc() { const uint8_t * USBAudio::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = { static const uint8_t stringIproductDescriptor[] = {
0x16, //bLength 0x16, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 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 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio

View File

@ -152,7 +152,7 @@ public:
* *
*/ */
void attach(void(*fptr)(void)) { void attach(void(*fptr)(void)) {
updateVol.attach(fptr); updateVol = Callback<void()>(fptr);
} }
/** attach a handler to Tx Done /** attach a handler to Tx Done
* *
@ -160,7 +160,7 @@ public:
* *
*/ */
void attachTx(void(*fptr)(void)) { void attachTx(void(*fptr)(void)) {
txDone.attach(fptr); txDone = Callback<void()>(fptr);
} }
/** attach a handler to Rx Done /** attach a handler to Rx Done
* *
@ -168,7 +168,7 @@ public:
* *
*/ */
void attachRx(void(*fptr)(void)) { void attachRx(void(*fptr)(void)) {
rxDone.attach(fptr); rxDone = Callback<void()>(fptr);
} }
/** Attach a nonstatic void/void member function to update the volume /** Attach a nonstatic void/void member function to update the volume
@ -179,15 +179,52 @@ public:
*/ */
template<typename T> template<typename T>
void attach(T *tptr, void(T::*mptr)(void)) { 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> template<typename T>
void attachTx(T *tptr, void(T::*mptr)(void)) { 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> template<typename T>
void attachRx(T *tptr, void(T::*mptr)(void)) { 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;
} }
@ -216,21 +253,21 @@ protected:
* *
* @returns pointer to the string product descriptor * @returns pointer to the string product descriptor
*/ */
virtual uint8_t * stringIproductDesc(); virtual const uint8_t * stringIproductDesc();
/* /*
* Get string interface descriptor * Get string interface descriptor
* *
* @returns pointer to the string interface descriptor * @returns pointer to the string interface descriptor
*/ */
virtual uint8_t * stringIinterfaceDesc(); virtual const uint8_t * stringIinterfaceDesc();
/* /*
* Get configuration descriptor * Get configuration descriptor
* *
* @returns pointer to the 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. * Called by USBDevice layer. Set interface/alternate of the device.
@ -270,6 +307,20 @@ protected:
private: 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 ? // stream available ?
volatile bool available; volatile bool available;

View File

@ -58,7 +58,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("device descr\r\n"); printf("device descr\r\n");
#endif #endif
transfer.remaining = DEVICE_DESCRIPTOR_LENGTH; transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
transfer.ptr = deviceDesc(); transfer.ptr = (uint8_t*)deviceDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
} }
@ -77,7 +77,7 @@ bool USBDevice::requestGetDescriptor(void)
transfer.remaining = configurationDesc()[2] \ transfer.remaining = configurationDesc()[2] \
| (configurationDesc()[3] << 8); | (configurationDesc()[3] << 8);
transfer.ptr = configurationDesc(); transfer.ptr = (uint8_t*)configurationDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
} }
@ -94,7 +94,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("1\r\n"); printf("1\r\n");
#endif #endif
transfer.remaining = stringLangidDesc()[0]; transfer.remaining = stringLangidDesc()[0];
transfer.ptr = stringLangidDesc(); transfer.ptr = (uint8_t*)stringLangidDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
break; break;
@ -103,7 +103,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("2\r\n"); printf("2\r\n");
#endif #endif
transfer.remaining = stringImanufacturerDesc()[0]; transfer.remaining = stringImanufacturerDesc()[0];
transfer.ptr = stringImanufacturerDesc(); transfer.ptr = (uint8_t*)stringImanufacturerDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
break; break;
@ -112,7 +112,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("3\r\n"); printf("3\r\n");
#endif #endif
transfer.remaining = stringIproductDesc()[0]; transfer.remaining = stringIproductDesc()[0];
transfer.ptr = stringIproductDesc(); transfer.ptr = (uint8_t*)stringIproductDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
break; break;
@ -121,7 +121,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("4\r\n"); printf("4\r\n");
#endif #endif
transfer.remaining = stringIserialDesc()[0]; transfer.remaining = stringIserialDesc()[0];
transfer.ptr = stringIserialDesc(); transfer.ptr = (uint8_t*)stringIserialDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
break; break;
@ -130,7 +130,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("5\r\n"); printf("5\r\n");
#endif #endif
transfer.remaining = stringIConfigurationDesc()[0]; transfer.remaining = stringIConfigurationDesc()[0];
transfer.ptr = stringIConfigurationDesc(); transfer.ptr = (uint8_t*)stringIConfigurationDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
break; break;
@ -139,7 +139,7 @@ bool USBDevice::requestGetDescriptor(void)
printf("6\r\n"); printf("6\r\n");
#endif #endif
transfer.remaining = stringIinterfaceDesc()[0]; transfer.remaining = stringIinterfaceDesc()[0];
transfer.ptr = stringIinterfaceDesc(); transfer.ptr = (uint8_t*)stringIinterfaceDesc();
transfer.direction = DEVICE_TO_HOST; transfer.direction = DEVICE_TO_HOST;
success = true; success = true;
break; break;
@ -771,7 +771,7 @@ uint8_t * USBDevice::findDescriptor(uint8_t descriptorType)
} }
/* Start at first descriptor after the configuration descriptor */ /* Start at first descriptor after the configuration descriptor */
ptr = &(configurationDesc()[CONFIGURATION_DESCRIPTOR_LENGTH]); ptr = &(((uint8_t*)configurationDesc())[CONFIGURATION_DESCRIPTOR_LENGTH]);
do { do {
if (ptr[1] /* bDescriptorType */ == descriptorType) if (ptr[1] /* bDescriptorType */ == descriptorType)
@ -907,8 +907,8 @@ bool USBDevice::readEP_NB(uint8_t endpoint, uint8_t * buffer, uint32_t * size, u
uint8_t * USBDevice::deviceDesc() { const uint8_t * USBDevice::deviceDesc() {
static uint8_t deviceDescriptor[] = { uint8_t deviceDescriptorTemp[] = {
DEVICE_DESCRIPTOR_LENGTH, /* bLength */ DEVICE_DESCRIPTOR_LENGTH, /* bLength */
DEVICE_DESCRIPTOR, /* bDescriptorType */ DEVICE_DESCRIPTOR, /* bDescriptorType */
LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */ LSB(USB_VERSION_2_0), /* bcdUSB (LSB) */
@ -928,20 +928,22 @@ uint8_t * USBDevice::deviceDesc() {
STRING_OFFSET_ISERIAL, /* iSerialNumber */ STRING_OFFSET_ISERIAL, /* iSerialNumber */
0x01 /* bNumConfigurations */ 0x01 /* bNumConfigurations */
}; };
MBED_ASSERT(sizeof(deviceDescriptorTemp) == sizeof(deviceDescriptor));
memcpy(deviceDescriptor, deviceDescriptorTemp, sizeof(deviceDescriptor));
return deviceDescriptor; return deviceDescriptor;
} }
uint8_t * USBDevice::stringLangidDesc() { const uint8_t * USBDevice::stringLangidDesc() {
static uint8_t stringLangidDescriptor[] = { static const uint8_t stringLangidDescriptor[] = {
0x04, /*bLength*/ 0x04, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
0x09,0x04, /*bString Lang ID - 0x0409 - English*/ 0x09,0x04, /*bString Lang ID - 0x0409 - English*/
}; };
return stringLangidDescriptor; return (uint8_t *)stringLangidDescriptor;
} }
uint8_t * USBDevice::stringImanufacturerDesc() { const uint8_t * USBDevice::stringImanufacturerDesc() {
static uint8_t stringImanufacturerDescriptor[] = { static const uint8_t stringImanufacturerDescriptor[] = {
0x12, /*bLength*/ 0x12, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/ 'm',0,'b',0,'e',0,'d',0,'.',0,'o',0,'r',0,'g',0, /*bString iManufacturer - mbed.org*/
@ -949,8 +951,8 @@ uint8_t * USBDevice::stringImanufacturerDesc() {
return stringImanufacturerDescriptor; return stringImanufacturerDescriptor;
} }
uint8_t * USBDevice::stringIserialDesc() { const uint8_t * USBDevice::stringIserialDesc() {
static uint8_t stringIserialDescriptor[] = { static const uint8_t stringIserialDescriptor[] = {
0x16, /*bLength*/ 0x16, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 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*/ '0',0,'1',0,'2',0,'3',0,'4',0,'5',0,'6',0,'7',0,'8',0,'9',0, /*bString iSerial - 0123456789*/
@ -958,8 +960,8 @@ uint8_t * USBDevice::stringIserialDesc() {
return stringIserialDescriptor; return stringIserialDescriptor;
} }
uint8_t * USBDevice::stringIConfigurationDesc() { const uint8_t * USBDevice::stringIConfigurationDesc() {
static uint8_t stringIconfigurationDescriptor[] = { static const uint8_t stringIconfigurationDescriptor[] = {
0x06, /*bLength*/ 0x06, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'0',0,'1',0, /*bString iConfiguration - 01*/ '0',0,'1',0, /*bString iConfiguration - 01*/
@ -967,8 +969,8 @@ uint8_t * USBDevice::stringIConfigurationDesc() {
return stringIconfigurationDescriptor; return stringIconfigurationDescriptor;
} }
uint8_t * USBDevice::stringIinterfaceDesc() { const uint8_t * USBDevice::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = { static const uint8_t stringIinterfaceDescriptor[] = {
0x08, /*bLength*/ 0x08, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ STRING_DESCRIPTOR, /*bDescriptorType 0x03*/
'U',0,'S',0,'B',0, /*bString iInterface - USB*/ 'U',0,'S',0,'B',0, /*bString iInterface - USB*/
@ -976,8 +978,8 @@ uint8_t * USBDevice::stringIinterfaceDesc() {
return stringIinterfaceDescriptor; return stringIinterfaceDescriptor;
} }
uint8_t * USBDevice::stringIproductDesc() { const uint8_t * USBDevice::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = { static const uint8_t stringIproductDescriptor[] = {
0x16, /*bLength*/ 0x16, /*bLength*/
STRING_DESCRIPTOR, /*bDescriptorType 0x03*/ 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*/ 'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 /*bString iProduct - USB DEVICE*/

View File

@ -165,60 +165,60 @@ public:
virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; }; virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate) { return false; };
/* /*
* Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength. * Get device descriptor.
* *
* @returns pointer to the device descriptor * @returns pointer to the device descriptor
*/ */
virtual uint8_t * deviceDesc(); virtual const uint8_t * deviceDesc();
/* /*
* Get configuration descriptor * Get configuration descriptor
* *
* @returns pointer to the configuration descriptor * @returns pointer to the configuration descriptor
*/ */
virtual uint8_t * configurationDesc(){return NULL;}; virtual const uint8_t * configurationDesc(){return NULL;};
/* /*
* Get string lang id descriptor * Get string lang id descriptor
* *
* @return pointer to the string lang id descriptor * @return pointer to the string lang id descriptor
*/ */
virtual uint8_t * stringLangidDesc(); virtual const uint8_t * stringLangidDesc();
/* /*
* Get string manufacturer descriptor * Get string manufacturer descriptor
* *
* @returns pointer to the string manufacturer descriptor * @returns pointer to the string manufacturer descriptor
*/ */
virtual uint8_t * stringImanufacturerDesc(); virtual const uint8_t * stringImanufacturerDesc();
/* /*
* Get string product descriptor * Get string product descriptor
* *
* @returns pointer to the string product descriptor * @returns pointer to the string product descriptor
*/ */
virtual uint8_t * stringIproductDesc(); virtual const uint8_t * stringIproductDesc();
/* /*
* Get string serial descriptor * Get string serial descriptor
* *
* @returns pointer to the string serial descriptor * @returns pointer to the string serial descriptor
*/ */
virtual uint8_t * stringIserialDesc(); virtual const uint8_t * stringIserialDesc();
/* /*
* Get string configuration descriptor * Get string configuration descriptor
* *
* @returns pointer to the string configuration descriptor * @returns pointer to the string configuration descriptor
*/ */
virtual uint8_t * stringIConfigurationDesc(); virtual const uint8_t * stringIConfigurationDesc();
/* /*
* Get string interface descriptor * Get string interface descriptor
* *
* @returns pointer to the string interface descriptor * @returns pointer to the string interface descriptor
*/ */
virtual uint8_t * stringIinterfaceDesc(); virtual const uint8_t * stringIinterfaceDesc();
/* /*
* Get the length of the report descriptor * Get the length of the report descriptor
@ -242,6 +242,7 @@ protected:
uint16_t VENDOR_ID; uint16_t VENDOR_ID;
uint16_t PRODUCT_ID; uint16_t PRODUCT_ID;
uint16_t PRODUCT_RELEASE; uint16_t PRODUCT_RELEASE;
uint8_t deviceDescriptor[18];
private: private:
bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket); bool addRateFeedbackEndpoint(uint8_t endpoint, uint32_t maxPacket);

View File

@ -105,7 +105,7 @@ bool USBHID::USBCallback_request() {
&& (reportDescLength() != 0)) && (reportDescLength() != 0))
{ {
transfer->remaining = reportDescLength(); transfer->remaining = reportDescLength();
transfer->ptr = reportDesc(); transfer->ptr = (uint8_t*)reportDesc();
transfer->direction = DEVICE_TO_HOST; transfer->direction = DEVICE_TO_HOST;
success = true; success = true;
} }
@ -177,8 +177,8 @@ bool USBHID::USBCallback_setConfiguration(uint8_t configuration) {
} }
uint8_t * USBHID::stringIinterfaceDesc() { const uint8_t * USBHID::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = { static const uint8_t stringIinterfaceDescriptor[] = {
0x08, //bLength 0x08, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 STRING_DESCRIPTOR, //bDescriptorType 0x03
'H',0,'I',0,'D',0, //bString iInterface - HID 'H',0,'I',0,'D',0, //bString iInterface - HID
@ -186,8 +186,8 @@ uint8_t * USBHID::stringIinterfaceDesc() {
return stringIinterfaceDescriptor; return stringIinterfaceDescriptor;
} }
uint8_t * USBHID::stringIproductDesc() { const uint8_t * USBHID::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = { static const uint8_t stringIproductDescriptor[] = {
0x16, //bLength 0x16, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 STRING_DESCRIPTOR, //bDescriptorType 0x03
'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device 'H',0,'I',0,'D',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 //bString iProduct - HID device
@ -197,8 +197,8 @@ uint8_t * USBHID::stringIproductDesc() {
uint8_t * USBHID::reportDesc() { const uint8_t * USBHID::reportDesc() {
static uint8_t reportDescriptor[] = { uint8_t reportDescriptorTemp[] = {
USAGE_PAGE(2), LSB(0xFFAB), MSB(0xFFAB), USAGE_PAGE(2), LSB(0xFFAB), MSB(0xFFAB),
USAGE(2), LSB(0x0200), MSB(0x0200), USAGE(2), LSB(0x0200), MSB(0x0200),
COLLECTION(1), 0x01, // Collection (Application) COLLECTION(1), 0x01, // Collection (Application)
@ -218,6 +218,8 @@ uint8_t * USBHID::reportDesc() {
END_COLLECTION(0), END_COLLECTION(0),
}; };
reportLength = sizeof(reportDescriptor); reportLength = sizeof(reportDescriptor);
MBED_ASSERT(sizeof(reportDescriptorTemp) == sizeof(reportDescriptor));
memcpy(reportDescriptor, reportDescriptorTemp, sizeof(reportDescriptor));
return reportDescriptor; return reportDescriptor;
} }
@ -227,8 +229,8 @@ uint8_t * USBHID::reportDesc() {
+ (1 * HID_DESCRIPTOR_LENGTH) \ + (1 * HID_DESCRIPTOR_LENGTH) \
+ (2 * ENDPOINT_DESCRIPTOR_LENGTH)) + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
uint8_t * USBHID::configurationDesc() { const uint8_t * USBHID::configurationDesc() {
static uint8_t configurationDescriptor[] = { uint8_t configurationDescriptorTemp[] = {
CONFIGURATION_DESCRIPTOR_LENGTH, // bLength CONFIGURATION_DESCRIPTOR_LENGTH, // bLength
CONFIGURATION_DESCRIPTOR, // bDescriptorType CONFIGURATION_DESCRIPTOR, // bDescriptorType
LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
@ -275,5 +277,7 @@ uint8_t * USBHID::configurationDesc() {
MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
1, // bInterval (milliseconds) 1, // bInterval (milliseconds)
}; };
MBED_ASSERT(sizeof(configurationDescriptorTemp) == sizeof(configurationDescriptor));
memcpy(configurationDescriptor, configurationDescriptorTemp, sizeof(configurationDescriptor));
return configurationDescriptor; return configurationDescriptor;
} }

View File

@ -98,13 +98,14 @@ public:
protected: protected:
uint16_t reportLength; uint16_t reportLength;
uint8_t reportDescriptor[27];
/* /*
* Get the Report descriptor * Get the Report descriptor
* *
* @returns pointer to the report descriptor * @returns pointer to the report descriptor
*/ */
virtual uint8_t * reportDesc(); virtual const uint8_t * reportDesc();
/* /*
* Get the length of the report descriptor * Get the length of the report descriptor
@ -118,21 +119,21 @@ protected:
* *
* @returns pointer to the string product descriptor * @returns pointer to the string product descriptor
*/ */
virtual uint8_t * stringIproductDesc(); virtual const uint8_t * stringIproductDesc();
/* /*
* Get string interface descriptor * Get string interface descriptor
* *
* @returns pointer to the string interface descriptor * @returns pointer to the string interface descriptor
*/ */
virtual uint8_t * stringIinterfaceDesc(); virtual const uint8_t * stringIinterfaceDesc();
/* /*
* Get configuration descriptor * Get configuration descriptor
* *
* @returns pointer to the configuration descriptor * @returns pointer to the configuration descriptor
*/ */
virtual uint8_t * configurationDesc(); virtual const uint8_t * configurationDesc();
/* /*
@ -164,6 +165,7 @@ protected:
virtual bool USBCallback_setConfiguration(uint8_t configuration); virtual bool USBCallback_setConfiguration(uint8_t configuration);
private: private:
uint8_t configurationDescriptor[41];
HID_REPORT outputReport; HID_REPORT outputReport;
uint8_t output_length; uint8_t output_length;
uint8_t input_length; uint8_t input_length;

View File

@ -352,8 +352,8 @@ const KEYMAP keymap[KEYMAP_SIZE] = {
}; };
#endif #endif
uint8_t * USBKeyboard::reportDesc() { const uint8_t * USBKeyboard::reportDesc() {
static uint8_t reportDescriptor[] = { static const uint8_t reportDescriptor[] = {
USAGE_PAGE(1), 0x01, // Generic Desktop USAGE_PAGE(1), 0x01, // Generic Desktop
USAGE(1), 0x06, // Keyboard USAGE(1), 0x06, // Keyboard
COLLECTION(1), 0x01, // Application COLLECTION(1), 0x01, // Application
@ -501,8 +501,8 @@ bool USBKeyboard::mediaControl(MEDIA_KEY key) {
+ (1 * HID_DESCRIPTOR_LENGTH) \ + (1 * HID_DESCRIPTOR_LENGTH) \
+ (2 * ENDPOINT_DESCRIPTOR_LENGTH)) + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
uint8_t * USBKeyboard::configurationDesc() { const uint8_t * USBKeyboard::configurationDesc() {
static uint8_t configurationDescriptor[] = { uint8_t configurationDescriptorTemp[] = {
CONFIGURATION_DESCRIPTOR_LENGTH, // bLength CONFIGURATION_DESCRIPTOR_LENGTH, // bLength
CONFIGURATION_DESCRIPTOR, // bDescriptorType CONFIGURATION_DESCRIPTOR, // bDescriptorType
LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
@ -549,5 +549,7 @@ uint8_t * USBKeyboard::configurationDesc() {
MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
1, // bInterval (milliseconds) 1, // bInterval (milliseconds)
}; };
MBED_ASSERT(sizeof(configurationDescriptorTemp) == sizeof(configurationDescriptor));
memcpy(configurationDescriptor, configurationDescriptorTemp, sizeof(configurationDescriptor));
return configurationDescriptor; return configurationDescriptor;
} }

View File

@ -148,7 +148,7 @@ public:
* *
* @returns pointer to the report descriptor * @returns pointer to the report descriptor
*/ */
virtual uint8_t * reportDesc(); virtual const uint8_t * reportDesc();
/* /*
* Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
@ -173,7 +173,7 @@ protected:
* *
* @returns pointer to the configuration descriptor * @returns pointer to the configuration descriptor
*/ */
virtual uint8_t * configurationDesc(); virtual const uint8_t * configurationDesc();
private: private:
//dummy otherwise it doesn,t compile (we must define all methods of an abstract class) //dummy otherwise it doesn,t compile (we must define all methods of an abstract class)
@ -181,6 +181,7 @@ private:
return -1; return -1;
}; };
uint8_t configurationDescriptor[41];
uint8_t lock_status; uint8_t lock_status;
}; };

View File

@ -103,10 +103,10 @@ bool USBMouse::release(uint8_t button_) {
} }
uint8_t * USBMouse::reportDesc() { const uint8_t * USBMouse::reportDesc() {
if (mouse_type == REL_MOUSE) { if (mouse_type == REL_MOUSE) {
static uint8_t reportDescriptor[] = { static const uint8_t reportDescriptor[] = {
USAGE_PAGE(1), 0x01, // Genric Desktop USAGE_PAGE(1), 0x01, // Genric Desktop
USAGE(1), 0x02, // Mouse USAGE(1), 0x02, // Mouse
COLLECTION(1), 0x01, // Application COLLECTION(1), 0x01, // Application
@ -141,7 +141,7 @@ uint8_t * USBMouse::reportDesc() {
reportLength = sizeof(reportDescriptor); reportLength = sizeof(reportDescriptor);
return reportDescriptor; return reportDescriptor;
} else if (mouse_type == ABS_MOUSE) { } else if (mouse_type == ABS_MOUSE) {
static uint8_t reportDescriptor[] = { static const uint8_t reportDescriptor[] = {
USAGE_PAGE(1), 0x01, // Generic Desktop USAGE_PAGE(1), 0x01, // Generic Desktop
USAGE(1), 0x02, // Mouse USAGE(1), 0x02, // Mouse
COLLECTION(1), 0x01, // Application COLLECTION(1), 0x01, // Application
@ -192,8 +192,8 @@ uint8_t * USBMouse::reportDesc() {
+ (1 * HID_DESCRIPTOR_LENGTH) \ + (1 * HID_DESCRIPTOR_LENGTH) \
+ (2 * ENDPOINT_DESCRIPTOR_LENGTH)) + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
uint8_t * USBMouse::configurationDesc() { const uint8_t * USBMouse::configurationDesc() {
static uint8_t configurationDescriptor[] = { uint8_t configurationDescriptorTemp[] = {
CONFIGURATION_DESCRIPTOR_LENGTH, // bLength CONFIGURATION_DESCRIPTOR_LENGTH, // bLength
CONFIGURATION_DESCRIPTOR, // bDescriptorType CONFIGURATION_DESCRIPTOR, // bDescriptorType
LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
@ -240,5 +240,7 @@ uint8_t * USBMouse::configurationDesc() {
MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
1, // bInterval (milliseconds) 1, // bInterval (milliseconds)
}; };
MBED_ASSERT(sizeof(configurationDescriptorTemp) == sizeof(configurationDescriptor));
memcpy(configurationDescriptor, configurationDescriptorTemp, sizeof(configurationDescriptor));
return configurationDescriptor; return configurationDescriptor;
} }

View File

@ -190,7 +190,7 @@ class USBMouse: public USBHID
* *
* @returns pointer to the report descriptor * @returns pointer to the report descriptor
*/ */
virtual uint8_t * reportDesc(); virtual const uint8_t * reportDesc();
protected: protected:
/* /*
@ -198,11 +198,12 @@ class USBMouse: public USBHID
* *
* @returns pointer to the configuration descriptor * @returns pointer to the configuration descriptor
*/ */
virtual uint8_t * configurationDesc(); virtual const uint8_t * configurationDesc();
private: private:
MOUSE_TYPE mouse_type; MOUSE_TYPE mouse_type;
uint8_t button; uint8_t button;
uint8_t configurationDescriptor[41];
bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z); bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
}; };

View File

@ -348,9 +348,9 @@ const KEYMAP keymap[KEYMAP_SIZE] = {
#endif #endif
uint8_t * USBMouseKeyboard::reportDesc() { const uint8_t * USBMouseKeyboard::reportDesc() {
if (mouse_type == REL_MOUSE) { if (mouse_type == REL_MOUSE) {
static uint8_t reportDescriptor[] = { static const uint8_t reportDescriptor[] = {
// Keyboard // Keyboard
USAGE_PAGE(1), 0x01, USAGE_PAGE(1), 0x01,
USAGE(1), 0x06, USAGE(1), 0x06,
@ -442,7 +442,7 @@ uint8_t * USBMouseKeyboard::reportDesc() {
reportLength = sizeof(reportDescriptor); reportLength = sizeof(reportDescriptor);
return reportDescriptor; return reportDescriptor;
} else if (mouse_type == ABS_MOUSE) { } else if (mouse_type == ABS_MOUSE) {
static uint8_t reportDescriptor[] = { static const uint8_t reportDescriptor[] = {
// Keyboard // Keyboard
USAGE_PAGE(1), 0x01, USAGE_PAGE(1), 0x01,

View File

@ -195,7 +195,7 @@ class USBMouseKeyboard: public USBHID, public Stream
* *
* @returns pointer to the report descriptor * @returns pointer to the report descriptor
*/ */
virtual uint8_t * reportDesc(); virtual const uint8_t * reportDesc();
/* /*
* Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys

View File

@ -157,8 +157,8 @@ bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
} }
uint8_t * USBMIDI::stringIinterfaceDesc() { const uint8_t * USBMIDI::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = { static const uint8_t stringIinterfaceDescriptor[] = {
0x0c, //bLength 0x0c, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 STRING_DESCRIPTOR, //bDescriptorType 0x03
'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
@ -166,8 +166,8 @@ uint8_t * USBMIDI::stringIinterfaceDesc() {
return stringIinterfaceDescriptor; return stringIinterfaceDescriptor;
} }
uint8_t * USBMIDI::stringIproductDesc() { const uint8_t * USBMIDI::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = { static const uint8_t stringIproductDescriptor[] = {
0x16, //bLength 0x16, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 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 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
@ -176,8 +176,8 @@ uint8_t * USBMIDI::stringIproductDesc() {
} }
uint8_t * USBMIDI::configurationDesc() { const uint8_t * USBMIDI::configurationDesc() {
static uint8_t configDescriptor[] = { static const uint8_t configDescriptor[] = {
// configuration descriptor // configuration descriptor
0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50, 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,

View File

@ -85,21 +85,21 @@ protected:
* *
* @returns pointer to the string product descriptor * @returns pointer to the string product descriptor
*/ */
virtual uint8_t * stringIproductDesc(); virtual const uint8_t * stringIproductDesc();
/* /*
* Get string interface descriptor * Get string interface descriptor
* *
* @returns pointer to the string interface descriptor * @returns pointer to the string interface descriptor
*/ */
virtual uint8_t * stringIinterfaceDesc(); virtual const uint8_t * stringIinterfaceDesc();
/* /*
* Get configuration descriptor * Get configuration descriptor
* *
* @returns pointer to the configuration descriptor * @returns pointer to the configuration descriptor
*/ */
virtual uint8_t * configurationDesc(); virtual const uint8_t * configurationDesc();
private: private:
uint8_t data[MAX_MIDI_MESSAGE_SIZE+1]; uint8_t data[MAX_MIDI_MESSAGE_SIZE+1];

View File

@ -589,8 +589,8 @@ bool USBMSD::USBCallback_setConfiguration(uint8_t configuration) {
} }
uint8_t * USBMSD::stringIinterfaceDesc() { const uint8_t * USBMSD::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = { static const uint8_t stringIinterfaceDescriptor[] = {
0x08, //bLength 0x08, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 STRING_DESCRIPTOR, //bDescriptorType 0x03
'M',0,'S',0,'D',0 //bString iInterface - MSD 'M',0,'S',0,'D',0 //bString iInterface - MSD
@ -598,8 +598,8 @@ uint8_t * USBMSD::stringIinterfaceDesc() {
return stringIinterfaceDescriptor; return stringIinterfaceDescriptor;
} }
uint8_t * USBMSD::stringIproductDesc() { const uint8_t * USBMSD::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = { static const uint8_t stringIproductDescriptor[] = {
0x12, //bLength 0x12, //bLength
STRING_DESCRIPTOR, //bDescriptorType 0x03 STRING_DESCRIPTOR, //bDescriptorType 0x03
'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio 'M',0,'b',0,'e',0,'d',0,' ',0,'M',0,'S',0,'D',0 //bString iProduct - Mbed Audio
@ -608,8 +608,8 @@ uint8_t * USBMSD::stringIproductDesc() {
} }
uint8_t * USBMSD::configurationDesc() { const uint8_t * USBMSD::configurationDesc() {
static uint8_t configDescriptor[] = { static const uint8_t configDescriptor[] = {
// Configuration 1 // Configuration 1
9, // bLength 9, // bLength

View File

@ -139,21 +139,21 @@ protected:
* *
* @returns pointer to the string product descriptor * @returns pointer to the string product descriptor
*/ */
virtual uint8_t * stringIproductDesc(); virtual const uint8_t * stringIproductDesc();
/* /*
* Get string interface descriptor * Get string interface descriptor
* *
* @returns pointer to the string interface descriptor * @returns pointer to the string interface descriptor
*/ */
virtual uint8_t * stringIinterfaceDesc(); virtual const uint8_t * stringIinterfaceDesc();
/* /*
* Get configuration descriptor * Get configuration descriptor
* *
* @returns pointer to the configuration descriptor * @returns pointer to the configuration descriptor
*/ */
virtual uint8_t * configurationDesc(); virtual const uint8_t * configurationDesc();
/* /*
* Callback called when a packet is received * Callback called when a packet is received

View File

@ -144,8 +144,8 @@ bool USBCDC::readEP_NB(uint8_t * buffer, uint32_t * size) {
} }
uint8_t * USBCDC::deviceDesc() { const uint8_t * USBCDC::deviceDesc() {
static uint8_t deviceDescriptor[] = { uint8_t deviceDescriptorTemp[] = {
18, // bLength 18, // bLength
1, // bDescriptorType 1, // bDescriptorType
0x10, 0x01, // bcdUSB 0x10, 0x01, // bcdUSB
@ -161,11 +161,13 @@ uint8_t * USBCDC::deviceDesc() {
3, // iSerialNumber 3, // iSerialNumber
1 // bNumConfigurations 1 // bNumConfigurations
}; };
MBED_ASSERT(sizeof(deviceDescriptorTemp) == sizeof(deviceDescriptor));
memcpy(deviceDescriptor, deviceDescriptorTemp, sizeof(deviceDescriptor));
return deviceDescriptor; return deviceDescriptor;
} }
uint8_t * USBCDC::stringIinterfaceDesc() { const uint8_t * USBCDC::stringIinterfaceDesc() {
static uint8_t stringIinterfaceDescriptor[] = { static const uint8_t stringIinterfaceDescriptor[] = {
0x08, 0x08,
STRING_DESCRIPTOR, STRING_DESCRIPTOR,
'C',0,'D',0,'C',0, 'C',0,'D',0,'C',0,
@ -173,8 +175,8 @@ uint8_t * USBCDC::stringIinterfaceDesc() {
return stringIinterfaceDescriptor; return stringIinterfaceDescriptor;
} }
uint8_t * USBCDC::stringIproductDesc() { const uint8_t * USBCDC::stringIproductDesc() {
static uint8_t stringIproductDescriptor[] = { static const uint8_t stringIproductDescriptor[] = {
0x16, 0x16,
STRING_DESCRIPTOR, STRING_DESCRIPTOR,
'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
@ -185,8 +187,8 @@ uint8_t * USBCDC::stringIproductDesc() {
#define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7) #define CONFIG1_DESC_SIZE (9+8+9+5+5+4+5+7+9+7+7)
uint8_t * USBCDC::configurationDesc() { const uint8_t * USBCDC::configurationDesc() {
static uint8_t configDescriptor[] = { static const uint8_t configDescriptor[] = {
// configuration descriptor // configuration descriptor
9, // bLength 9, // bLength
2, // bDescriptorType 2, // bDescriptorType

View File

@ -46,28 +46,28 @@ protected:
* *
* @returns pointer to the device descriptor * @returns pointer to the device descriptor
*/ */
virtual uint8_t * deviceDesc(); virtual const uint8_t * deviceDesc();
/* /*
* Get string product descriptor * Get string product descriptor
* *
* @returns pointer to the string product descriptor * @returns pointer to the string product descriptor
*/ */
virtual uint8_t * stringIproductDesc(); virtual const uint8_t * stringIproductDesc();
/* /*
* Get string interface descriptor * Get string interface descriptor
* *
* @returns pointer to the string interface descriptor * @returns pointer to the string interface descriptor
*/ */
virtual uint8_t * stringIinterfaceDesc(); virtual const uint8_t * stringIinterfaceDesc();
/* /*
* Get configuration descriptor * Get configuration descriptor
* *
* @returns pointer to the configuration descriptor * @returns pointer to the configuration descriptor
*/ */
virtual uint8_t * configurationDesc(); virtual const uint8_t * configurationDesc();
/* /*
* Send a buffer * Send a buffer

View File

@ -127,7 +127,7 @@ public:
template<typename T> template<typename T>
void attach(T* tptr, void (T::*mptr)(void)) { void attach(T* tptr, void (T::*mptr)(void)) {
if((mptr != NULL) && (tptr != NULL)) { if((mptr != NULL) && (tptr != NULL)) {
rx.attach(tptr, mptr); rx = Callback<void()>(mptr, tptr);
} }
} }
@ -138,10 +138,19 @@ public:
*/ */
void attach(void (*fptr)(void)) { void attach(void (*fptr)(void)) {
if(fptr != NULL) { if(fptr != NULL) {
rx.attach(fptr); rx = Callback<void()>(fptr);
} }
} }
/**
* Attach a Callback called when a packet is received
*
* @param cb Callback to attach
*/
void attach(Callback<void()> &cb) {
rx = cb;
}
/** /**
* Attach a callback to call when serial's settings are changed. * Attach a callback to call when serial's settings are changed.
* *

View File

@ -500,7 +500,7 @@ void USBHAL::usbisr(void) {
if (istat & 1<<7) { if (istat & 1<<7) {
if (USB0->ENDPOINT[0].ENDPT & USB_ENDPT_EPSTALL_MASK) if (USB0->ENDPOINT[0].ENDPT & USB_ENDPT_EPSTALL_MASK)
USB0->ENDPOINT[0].ENDPT &= ~USB_ENDPT_EPSTALL_MASK; USB0->ENDPOINT[0].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
USB0->ISTAT |= USB_ISTAT_STALL_MASK; USB0->ISTAT = USB_ISTAT_STALL_MASK;
} }
// token interrupt // token interrupt
@ -555,13 +555,13 @@ void USBHAL::usbisr(void) {
// sleep interrupt // sleep interrupt
if (istat & 1<<4) { if (istat & 1<<4) {
USB0->ISTAT |= USB_ISTAT_SLEEP_MASK; USB0->ISTAT = USB_ISTAT_SLEEP_MASK;
} }
// error interrupt // error interrupt
if (istat & USB_ISTAT_ERROR_MASK) { if (istat & USB_ISTAT_ERROR_MASK) {
USB0->ERRSTAT = 0xFF; USB0->ERRSTAT = 0xFF;
USB0->ISTAT |= USB_ISTAT_ERROR_MASK; USB0->ISTAT = USB_ISTAT_ERROR_MASK;
} }
} }