Skip to content

Custom Serial Number

luni64 edited this page Apr 25, 2020 · 4 revisions

Why

Sometimes it might be necessary to change the standard serial number a Teensy reports to the over USB. (The following examples are for Windows, other OS might have no or other issues...)

  • Homebrew boards
    Since the serial number of standard Teensies is set by PJRC during production, homebrew boards will not have a unique standard S/N. This might lead to issues with Windows which distinguishes USB devices by VID/PID/SN. If it finds two devices with identical VID/PID/SN it assigns an auto generated serial number to all but the first detected boards. If your PC application now uses the serial number to identify the Teensy it needs to connect things might get confusing for users.

  • Identify boards If PC applications communicate with a microcontroller board which is connected through USB Serial you usually need the user to tell the application which COM port your device is connected to. If you have more than one device connected it can get tedious for the user to identify the correct one. However, it is not difficult to read out the serial number from the device. If you have some unique serial number scheme say 'COOLDEV001' you can easily implement an autodetection of your Teensy.

How To

Fortunately, setting a custom serial number is quickly done. It is stored in the struct usb_string_serial_number which gets set in usb_desc.h with its default value. Fortunately the struct is defined weak and can be overridden by user code as shown in the following example.

extern "C"
{
    struct usb_string_descriptor_struct
    {
        uint8_t bLength;
        uint8_t bDescriptorType;
        uint16_t wString[10];
    };

    usb_string_descriptor_struct usb_string_serial_number =
    {
         22,  // 2 + 2*length of the sn string
         3,
         {'M','Y', 'S','N', '0', '0', '0','0', '1', 0},
    };
}

//-----------------
void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  delay(200);
}

This sets the serial number to "MYSN00001" as can be seen in the Windows device manager:

image

Please note: The bootloader will still report the standard serial number stored in the processor. Some uploaders (e.g. tyCommander) will get confused if a Teensy changes its serial number after uploading. The PJRC stock uploader works without problem.

Clone this wiki locally