Skip to content

Commit b945df9

Browse files
committed
[USB] use plugged modules name to create iSerial field
1 parent bf609ec commit b945df9

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

Diff for: hardware/arduino/avr/cores/arduino/PluggableUSB.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ int PluggableUSB_::getDescriptor(USBSetup& setup)
5050
return 0;
5151
}
5252

53+
void PluggableUSB_::getShortName(char *iSerialNum)
54+
{
55+
PluggableUSBModule* node;
56+
for (node = rootNode; node; node = node->next) {
57+
iSerialNum += node->getShortName(iSerialNum);
58+
}
59+
*iSerialNum = 0;
60+
}
61+
5362
bool PluggableUSB_::setup(USBSetup& setup)
5463
{
5564
PluggableUSBModule* node;

Diff for: hardware/arduino/avr/cores/arduino/PluggableUSB.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class PluggableUSBModule {
3535
virtual bool setup(USBSetup& setup) = 0;
3636
virtual int getInterface(uint8_t* interfaceCount) = 0;
3737
virtual int getDescriptor(USBSetup& setup) = 0;
38+
virtual uint8_t getShortName(char *name) { name[0] = 'A'+pluggedInterface; return 1; }
3839

3940
uint8_t pluggedInterface;
4041
uint8_t pluggedEndpoint;
@@ -55,6 +56,7 @@ class PluggableUSB_ {
5556
int getInterface(uint8_t* interfaceCount);
5657
int getDescriptor(USBSetup& setup);
5758
bool setup(USBSetup& setup);
59+
void getShortName(char *iSerialNum);
5860

5961
private:
6062
uint8_t lastIf;

Diff for: hardware/arduino/avr/cores/arduino/USBCore.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "USBAPI.h"
2020
#include "PluggableUSB.h"
21+
#include <stdlib.h>
2122

2223
#if defined(USBCON)
2324

@@ -69,10 +70,10 @@ const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER;
6970

7071
// DEVICE DESCRIPTOR
7172
const DeviceDescriptor USB_DeviceDescriptor =
72-
D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
73+
D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
7374

7475
const DeviceDescriptor USB_DeviceDescriptorB =
75-
D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
76+
D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
7677

7778
//==================================================================
7879
//==================================================================
@@ -409,11 +410,12 @@ int USB_SendControl(u8 flags, const void* d, int len)
409410
// Send a USB descriptor string. The string is stored in PROGMEM as a
410411
// plain ASCII string but is sent out as UTF-16 with the correct 2-byte
411412
// prefix
412-
static bool USB_SendStringDescriptor(const u8*string_P, u8 string_len) {
413+
static bool USB_SendStringDescriptor(const u8*string_P, u8 string_len, uint8_t flags) {
413414
SendControl(2 + string_len * 2);
414415
SendControl(3);
416+
bool pgm = flags & TRANSFER_PGM;
415417
for(u8 i = 0; i < string_len; i++) {
416-
bool r = SendControl(pgm_read_byte(&string_P[i]));
418+
bool r = SendControl(pgm ? pgm_read_byte(&string_P[i]) : string_P[i]);
417419
r &= SendControl(0); // high byte
418420
if(!r) {
419421
return false;
@@ -495,10 +497,17 @@ bool SendDescriptor(USBSetup& setup)
495497
desc_addr = (const u8*)&STRING_LANGUAGE;
496498
}
497499
else if (setup.wValueL == IPRODUCT) {
498-
return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT));
500+
return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT), TRANSFER_PGM);
499501
}
500502
else if (setup.wValueL == IMANUFACTURER) {
501-
return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER));
503+
return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER), TRANSFER_PGM);
504+
}
505+
else if (setup.wValueL == ISERIAL) {
506+
#ifdef PLUGGABLE_USB_ENABLED
507+
char name[ISERIAL_MAX_LEN];
508+
PluggableUSB().getShortName(name);
509+
return USB_SendStringDescriptor((uint8_t*)name, strlen(name), 0);
510+
#endif
502511
}
503512
else
504513
return false;

Diff for: hardware/arduino/avr/cores/arduino/USBDesc.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#define USB_ENDPOINTS 5 // AtMegaxxU2
2525
#endif
2626

27+
#define ISERIAL_MAX_LEN 20
28+
2729
#define CDC_INTERFACE_COUNT 2
2830
#define CDC_ENPOINT_COUNT 3
2931

@@ -39,6 +41,6 @@
3941
#define CDC_RX CDC_ENDPOINT_OUT
4042
#define CDC_TX CDC_ENDPOINT_IN
4143

42-
#define IMANUFACTURER 1
43-
#define IPRODUCT 2
44-
44+
#define IMANUFACTURER 1
45+
#define IPRODUCT 2
46+
#define ISERIAL 3

Diff for: hardware/arduino/avr/libraries/HID/HID.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ int HID_::getDescriptor(USBSetup& setup)
5757
return total;
5858
}
5959

60+
uint8_t HID_::getShortName(char *name)
61+
{
62+
name[0] = 'H';
63+
name[1] = 'I';
64+
name[2] = 'D';
65+
name[3] = 'A' + (descriptorSize & 0x0F);
66+
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
67+
return 5;
68+
}
69+
6070
void HID_::AppendDescriptor(HIDSubDescriptor *node)
6171
{
6272
if (!rootNode) {

Diff for: hardware/arduino/avr/libraries/HID/HID.h

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class HID_ : public PluggableUSBModule
9696
int getInterface(uint8_t* interfaceCount);
9797
int getDescriptor(USBSetup& setup);
9898
bool setup(USBSetup& setup);
99+
uint8_t getShortName(char* name);
99100

100101
private:
101102
uint8_t epType[1];

0 commit comments

Comments
 (0)