Skip to content

Commit f8d32a0

Browse files
committed
Parametric USB configuration for Arduino Due (experimental)
1 parent f8ec941 commit f8d32a0

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

hardware/arduino/sam/boards.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ arduino_due_x_dbg.upload.wait_for_upload_port=false
88
arduino_due_x_dbg.upload.native_usb=false
99
arduino_due_x_dbg.build.mcu=cortex-m3
1010
arduino_due_x_dbg.build.f_cpu=84000000L
11+
arduino_due_x_dbg.build.usb_product="Arduino Due"
1112
arduino_due_x_dbg.build.board=SAM_DUE
1213
arduino_due_x_dbg.build.core=arduino
13-
arduino_due_x_dbg.build.extra_flags=-D__SAM3X8E__ -mthumb -DUSB_PID={build.pid} -DUSB_VID={build.vid} -DUSBCON
14+
arduino_due_x_dbg.build.extra_flags=-D__SAM3X8E__ -mthumb {build.usb_flags}
1415
arduino_due_x_dbg.build.ldscript=linker_scripts/gcc/flash.ld
1516
arduino_due_x_dbg.build.variant=arduino_due_x
1617
arduino_due_x_dbg.build.variant_system_lib=libsam_sam3x8e_gcc_rel.a
@@ -26,9 +27,10 @@ arduino_due_x.upload.wait_for_upload_port=true
2627
arduino_due_x.upload.native_usb=true
2728
arduino_due_x.build.mcu=cortex-m3
2829
arduino_due_x.build.f_cpu=84000000L
30+
arduino_due_x.build.usb_product="Arduino Due"
2931
arduino_due_x.build.board=SAM_DUE
3032
arduino_due_x.build.core=arduino
31-
arduino_due_x.build.extra_flags=-D__SAM3X8E__ -mthumb -DUSB_PID={build.pid} -DUSB_VID={build.vid} -DUSBCON
33+
arduino_due_x.build.extra_flags=-D__SAM3X8E__ -mthumb {build.usb_flags}
3234
arduino_due_x.build.ldscript=linker_scripts/gcc/flash.ld
3335
arduino_due_x.build.variant=arduino_due_x
3436
arduino_due_x.build.variant_system_lib=libsam_sam3x8e_gcc_rel.a

hardware/arduino/sam/cores/arduino/USB/USBCore.cpp

+42-25
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ static char isEndpointHalt = 0;
4747
//==================================================================
4848

4949
extern const uint16_t STRING_LANGUAGE[];
50-
extern const uint16_t STRING_IPRODUCT[];
51-
extern const uint16_t STRING_IMANUFACTURER[];
50+
extern const uint8_t STRING_PRODUCT[];
51+
extern const uint8_t STRING_MANUFACTURER[];
5252
extern const DeviceDescriptor USB_DeviceDescriptor;
5353
extern const DeviceDescriptor USB_DeviceDescriptorA;
5454

@@ -57,23 +57,25 @@ const uint16_t STRING_LANGUAGE[2] = {
5757
0x0409 // English
5858
};
5959

60-
const uint16_t STRING_IPRODUCT[17] = {
61-
(3<<8) | (2+2*16),
62-
#if USB_PID == USB_PID_LEONARDO
63-
'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o'
64-
#elif USB_PID == USB_PID_MICRO
65-
'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ',' ',' '
66-
#elif USB_PID == USB_PID_DUE
67-
'A','r','d','u','i','n','o',' ','D','u','e',' ',' ',' ',' ',' '
60+
#ifndef USB_PRODUCT
61+
// Use a hardcoded product name if none is provided
62+
#if USB_PID == USB_PID_DUE
63+
#define USB_PRODUCT "Arduino Due"
6864
#else
69-
#error "Need an USB PID"
65+
#define USB_PRODUCT "USB IO Board"
66+
#endif
7067
#endif
71-
};
7268

73-
const uint16_t STRING_IMANUFACTURER[12] = {
74-
(3<<8) | (2+2*11),
75-
'A','r','d','u','i','n','o',' ','L','L','C'
76-
};
69+
const uint8_t STRING_PRODUCT[] = USB_PRODUCT;
70+
71+
#if USB_VID == 0x2341
72+
#define USB_MANUFACTURER "Arduino LLC"
73+
#elif !defined(USB_MANUFACTURER)
74+
// Fall through to unknown if no manufacturer name was provided in a macro
75+
#define USB_MANUFACTURER "Unknown"
76+
#endif
77+
78+
const uint8_t STRING_MANUFACTURER[12] = USB_MANUFACTURER;
7779

7880
#ifdef CDC_ENABLED
7981
#define DEVICE_CLASS 0x02
@@ -241,6 +243,21 @@ int USBD_SendControl(uint8_t flags, const void* d, uint32_t len)
241243
return length;
242244
}
243245

246+
// Send a USB descriptor string. The string is stored as a
247+
// plain ASCII string but is sent out as UTF-16 with the
248+
// correct 2-byte prefix
249+
static bool USB_SendStringDescriptor(const uint8_t *string, int wLength) {
250+
uint16_t buff[64];
251+
int l = 1;
252+
wLength-=2;
253+
while (*string && wLength>0) {
254+
buff[l++] = (uint8_t)(*string++);
255+
wLength-=2;
256+
}
257+
buff[0] = (3<<8) | (l*2);
258+
return USBD_SendControl(0, (uint8_t*)buff, l*2);
259+
}
260+
244261
// Does not timeout or cross fifo boundaries
245262
// Will only work for transfers <= 64 bytes
246263
// TODO
@@ -400,19 +417,19 @@ static bool USBD_SendDescriptor(Setup& setup)
400417
TRACE_CORE(puts("=> USBD_SendDescriptor : USB_STRING_DESCRIPTOR_TYPE\r\n");)
401418
if (setup.wValueL == 0) {
402419
desc_addr = (const uint8_t*)&STRING_LANGUAGE;
403-
}
420+
}
404421
else if (setup.wValueL == IPRODUCT) {
405-
desc_addr = (const uint8_t*)&STRING_IPRODUCT;
406-
}
422+
return USB_SendStringDescriptor(STRING_PRODUCT, setup.wLength);
423+
}
407424
else if (setup.wValueL == IMANUFACTURER) {
408-
desc_addr = (const uint8_t*)&STRING_IMANUFACTURER;
409-
}
425+
return USB_SendStringDescriptor(STRING_MANUFACTURER, setup.wLength);
426+
}
410427
else {
411428
return false;
412-
}
413-
if( *desc_addr > setup.wLength ) {
414-
desc_length = setup.wLength;
415-
}
429+
}
430+
if( *desc_addr > setup.wLength ) {
431+
desc_length = setup.wLength;
432+
}
416433
}
417434
else if (USB_DEVICE_QUALIFIER == t)
418435
{

hardware/arduino/sam/platform.txt

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ build.extra_flags=
3333

3434
compiler.libsam.c.flags="-I{build.system.path}/libsam" "-I{build.system.path}/CMSIS/CMSIS/Include/" "-I{build.system.path}/CMSIS/Device/ATMEL/"
3535

36+
# USB Flags
37+
# ---------
38+
build.usb_flags=-DUSB_VID={build.vid} -DUSB_PID={build.pid} -DUSBCON -DUSB_MANUFACTURER={build.usb_manufacturer} '-DUSB_PRODUCT={build.usb_product}'
39+
40+
# Default usb manufacturer will be replaced at compile time using
41+
# numeric vendor ID if available or by board's specific value.
42+
build.usb_manufacturer="Unknown"
43+
44+
3645
# SAM3 compile patterns
3746
# ---------------------
3847

0 commit comments

Comments
 (0)