Skip to content

Instantly share code, notes, and snippets.

@DavidEGrayson
Created December 9, 2014 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidEGrayson/bd12b8aed2f62ffb6989 to your computer and use it in GitHub Desktop.
Save DavidEGrayson/bd12b8aed2f62ffb6989 to your computer and use it in GitHub Desktop.
Adds USB serial number support for sketches to Arduino 1.5.8. See http://forum.pololu.com/viewtopic.php?f=10&t=9312&p=42202#p42202
--- hardware/arduino/avr/cores/arduino/USBCore.cpp Fri Nov 7 10:14:04 2014
+++ hardware/arduino/avr/cores/arduino/USBCore.cpp Tue Dec 9 11:09:30 2014
@@ -18,6 +18,8 @@
#include "USBAPI.h"
+#include <avr/boot.h>
+
#if defined(USBCON)
#define EP_TYPE_CONTROL 0x00
@@ -80,10 +81,10 @@
// DEVICE DESCRIPTOR
const DeviceDescriptor USB_DeviceDescriptor =
- D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
+ D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
const DeviceDescriptor USB_DeviceDescriptorA =
- D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
+ D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1);
//==================================================================
//==================================================================
@@ -432,6 +433,28 @@
return true;
}
+static char nibbleToHex(uint8_t n)
+{
+ if (n <= 9) { return '0' + n; }
+ else { return 'a' + (n - 10); }
+}
+
+static bool USB_SendSerialNumber() {
+ SendControl(2 + 20 * 2);
+ SendControl(3);
+ for(uint8_t i = 14; i < 24; i++) {
+ uint8_t b = boot_signature_byte_get(i);
+ if (!(SendControl(nibbleToHex(b >> 4)) &&
+ SendControl(0) &&
+ SendControl(nibbleToHex(b & 0xF)) &&
+ SendControl(0)))
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
// Does not timeout or cross fifo boundaries
// Will only work for transfers <= 64 bytes
// TODO
@@ -509,6 +532,9 @@
}
else if (setup.wValueL == IMANUFACTURER) {
return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER));
+ }
+ else if (setup.wValueL == ISERIAL) {
+ return USB_SendSerialNumber();
}
else
return false;
--- hardware/arduino/avr/cores/arduino/USBDesc.h Fri Nov 7 10:14:04 2014
+++ hardware/arduino/avr/cores/arduino/USBDesc.h Tue Dec 9 10:49:24 2014
@@ -61,3 +61,4 @@
#define IMANUFACTURER 1
#define IPRODUCT 2
+#define ISERIAL 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment