Skip to content

Commit 61b060c

Browse files
sandeepmistrycmaglie
authored andcommitted
Slim serial number to hex string conversion
1 parent 9370464 commit 61b060c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cores/arduino/USB/USBCore.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,24 @@ uint32_t USBDeviceClass::sendConfiguration(uint32_t maxlen)
177177
return true;
178178
}
179179

180+
static void utox(uint32_t val, char* s) {
181+
// lead zero pad
182+
for (uint32_t i = 0x10000000; i > 1; i = i >> 4) {
183+
if (val < i) {
184+
*s++ = '0';
185+
}
186+
}
187+
188+
// convert value to hex string
189+
utoa(val, s, 16);
190+
191+
// make string uppercase
192+
while (s && *s) {
193+
*s = toupper(*s);
194+
s++;
195+
}
196+
}
197+
180198
bool USBDeviceClass::sendDescriptor(USBSetup &setup)
181199
{
182200
uint8_t t = setup.wValueH;
@@ -228,7 +246,11 @@ bool USBDeviceClass::sendDescriptor(USBSetup &setup)
228246
#define SERIAL_NUMBER_WORD_3 *(volatile uint32_t*)(0x0080A048)
229247

230248
char name[ISERIAL_MAX_LEN];
231-
sprintf(name, "%8X%8X%8X%8X", SERIAL_NUMBER_WORD_0, SERIAL_NUMBER_WORD_1, SERIAL_NUMBER_WORD_2, SERIAL_NUMBER_WORD_3);
249+
utox(SERIAL_NUMBER_WORD_0, &name[0]);
250+
utox(SERIAL_NUMBER_WORD_1, &name[8]);
251+
utox(SERIAL_NUMBER_WORD_2, &name[16]);
252+
utox(SERIAL_NUMBER_WORD_3, &name[24]);
253+
232254
PluggableUSB().getShortName(&name[32]);
233255
return sendStringDescriptor((uint8_t*)name, setup.wLength);
234256
#endif

0 commit comments

Comments
 (0)