Skip to content

Commit 25ebd44

Browse files
committed
Allow complete override of USB SerialNumber
1 parent ecdbde9 commit 25ebd44

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

cores/arduino/USB/PluggableUSB.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ int PluggableUSB_::getDescriptor(USBSetup& setup)
5252
return 0;
5353
}
5454

55-
void PluggableUSB_::getShortName(char *iSerialNum)
55+
uint8_t PluggableUSB_::getShortName(char *iSerialNum)
5656
{
5757
PluggableUSBModule* node;
58+
uint8_t size;
5859
for (node = rootNode; node; node = node->next) {
59-
iSerialNum += node->getShortName(iSerialNum);
60+
uint8_t len = node->getShortName(iSerialNum);
61+
iSerialNum += len;
62+
size += len;
6063
}
6164
*iSerialNum = 0;
65+
return size;
6266
}
6367

6468
bool PluggableUSB_::setup(USBSetup& setup)

cores/arduino/USB/PluggableUSB.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class PluggableUSB_ {
5656
int getInterface(uint8_t* interfaceCount);
5757
int getDescriptor(USBSetup& setup);
5858
bool setup(USBSetup& setup);
59-
void getShortName(char *iSerialNum);
59+
uint8_t getShortName(char *iSerialNum);
6060

6161
private:
6262
uint8_t lastIf;

cores/arduino/USB/USBAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class USBDeviceClass {
7777
uint32_t sendControl(int /* ep */, const void *data, uint32_t len) { return sendControl(data, len); }
7878
uint32_t recvControl(void *data, uint32_t len);
7979
uint32_t sendConfiguration(uint32_t maxlen);
80-
bool sendStringDescriptor(const uint8_t *string, uint8_t maxlen);
80+
bool sendStringDescriptor(const uint8_t *string, uint32_t maxlen);
8181
void initControl(int end);
8282
uint8_t SendInterfaces(uint32_t* total);
8383
void packMessages(bool val);

cores/arduino/USB/USBCore.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ static EPHandler *epHandlers[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
9898
// Send a USB descriptor string. The string is stored as a
9999
// plain ASCII string but is sent out as UTF-16 with the
100100
// correct 2-byte prefix
101-
bool USBDeviceClass::sendStringDescriptor(const uint8_t *string, uint8_t maxlen)
101+
bool USBDeviceClass::sendStringDescriptor(const uint8_t *string, uint32_t maxlen)
102102
{
103103
if (maxlen < 2)
104104
return false;
105105

106-
uint8_t buffer[maxlen];
106+
uint8_t* buffer = (uint8_t*)malloc(maxlen);
107107
buffer[0] = strlen((const char*)string) * 2 + 2;
108108
buffer[1] = 0x03;
109109

@@ -114,7 +114,9 @@ bool USBDeviceClass::sendStringDescriptor(const uint8_t *string, uint8_t maxlen)
114114
buffer[i] = 0;
115115
}
116116

117-
return USBDevice.sendControl(buffer, i);
117+
bool ret = USBDevice.sendControl(buffer, i);
118+
free(buffer);
119+
return ret;
118120
}
119121

120122
bool _dry_run = false;
@@ -231,22 +233,28 @@ bool USBDeviceClass::sendDescriptor(USBSetup &setup)
231233
return sendStringDescriptor(STRING_MANUFACTURER, setup.wLength);
232234
}
233235
else if (setup.wValueL == ISERIAL) {
234-
#ifdef PLUGGABLE_USB_ENABLED
236+
char name[ISERIAL_MAX_LEN];
237+
memset(name, 0, sizeof(name));
238+
uint8_t idx = 0;
239+
#ifdef CDC_ENABLED
235240
// from section 9.3.3 of the datasheet
236241
#define SERIAL_NUMBER_WORD_0 *(volatile uint32_t*)(0x0080A00C)
237242
#define SERIAL_NUMBER_WORD_1 *(volatile uint32_t*)(0x0080A040)
238243
#define SERIAL_NUMBER_WORD_2 *(volatile uint32_t*)(0x0080A044)
239244
#define SERIAL_NUMBER_WORD_3 *(volatile uint32_t*)(0x0080A048)
240245

241-
char name[ISERIAL_MAX_LEN];
242246
utox8(SERIAL_NUMBER_WORD_0, &name[0]);
243247
utox8(SERIAL_NUMBER_WORD_1, &name[8]);
244248
utox8(SERIAL_NUMBER_WORD_2, &name[16]);
245249
utox8(SERIAL_NUMBER_WORD_3, &name[24]);
246-
247-
PluggableUSB().getShortName(&name[32]);
248-
return sendStringDescriptor((uint8_t*)name, setup.wLength);
250+
idx += 32;
251+
#endif
252+
#ifdef PLUGGABLE_USB_ENABLED
253+
idx += PluggableUSB().getShortName(&name[idx]);
249254
#endif
255+
if (idx > 0) {
256+
return sendStringDescriptor((uint8_t*)name, setup.wLength);
257+
}
250258
}
251259
else {
252260
return false;

0 commit comments

Comments
 (0)