Dynamic Serial Number at runtime

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
opcode
Posts: 4
Joined: Wed Feb 25, 2009 7:57 pm

Dynamic Serial Number at runtime

Post by opcode » Wed Feb 25, 2009 8:19 pm

I have spent quite some time trying to implement a dynamic serial number or device id or any other method of being able to select any one of several identical usb devices without success :(
The Idea is to have several identical devices eg powerswitches, running identical firmware but be individually addressable by means of setting the serial number or device ID at runtime (or even at boot time) via jumper or dipswitch.

I understand that you must define in usbconfig.h:

Code: Select all

#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER USB_PROP_IS_DYNAMIC


and also implement in main.c:

Code: Select all

uchar usbFunctionDescriptor(usbRequest_t *rq)
{
   uchar *p = 0, len = 0;
   if(rq->wValue.bytes[1] == ????????)
   {
   ???????     
   }
   usbMsgPtr = p;
   return len;
}


Does anyone have any working example or information on how to do this ?

Thanks.

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Post by Grendel » Thu Feb 26, 2009 3:52 am

I didn't use a dynamic serial no. in my project but various other dynamic descriptors -- try something like this:

Code: Select all

uchar usbFunctionDescriptor(usbRequest_t *rq)
{
   uchar *p = 0, len = 0;
   if(rq->wValue.bytes[1] == USBDESCR_STRING &&
      rq->wValue.bytes[0] == [your serial no. str index])
   {
     [build your serial no. str descr. here, have p point to it]

     usbMsgPtr = p;
     len = [your serial no. str descr. len]
   }
   return len;
}


Note that if you build the descriptor in RAM USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER needs to be defined as (USB_PROP_IS_DYNAMIC | USB_PROP_IS_RAM). If you have predefined descriptors in FLASH, USB_PROP_IS_DYNAMIC will do.

opcode
Posts: 4
Joined: Wed Feb 25, 2009 7:57 pm

Post by opcode » Thu Feb 26, 2009 7:14 pm

Thanks Grendel That was enough additional info to get it working :)

in usbconfig.h

Code: Select all

#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER    (USB_PROP_IS_DYNAMIC | USB_PROP_IS_RAM)



in main.c

Code: Select all

#define SERIAL_NUMBER_LENGTH 6 // the number of characters required for your serial number

static int  serialNumberDescriptor[SERIAL_NUMBER_LENGTH + 1];

uchar usbFunctionDescriptor(usbRequest_t *rq)
{
   uchar len = 0;
   usbMsgPtr = 0;
   if (rq->wValue.bytes[1] == USBDESCR_STRING && rq->wValue.bytes[0] == 3) // 3 is the type of string descriptor, in this case the device serial number
   {
      usbMsgPtr = (uchar*)serialNumberDescriptor;
      len = sizeof(serialNumberDescriptor);
   }
   return len;
}


static void SetSerial(void)
{
   serialNumberDescriptor[0] = USB_STRING_DESCRIPTOR_HEADER(SERIAL_NUMBER_LENGTH);
   serialNumberDescriptor[1] = 'S';
   serialNumberDescriptor[2] = 'e';
   serialNumberDescriptor[3] = 'r';
   serialNumberDescriptor[4] = 'i';
   serialNumberDescriptor[5] = 'a';
   serialNumberDescriptor[6] = 'l';
}



You must call SetSerial() before the USB is initialized to generate the serial number!

Bob.

Post Reply