I had a need to have many of the same USB HID devices on the same system. I needed a way to update the serial number of each unit to a unique address.
Using PIC18f26j50 and MikroC
What I did was set the address of the string array used for the serial number in the USB descriptor file to an absolute location. (0x2000)
//Product serial number
const struct{
char bLength;
char bDscType;
unsigned int string[8];
}strd4={
18, //sizeof this descriptor string
0x03,
{'b','b','b','b','b','b','b','b'}
}absolute 0x2000; // <-- Set to 0x2000 absolute location
Then in my main program I have a function that sets up the new serial number, erases the flash at 0x2000, and finally writes the new serial number to flash.
void updateSerial(void){
unsigned char serialnum[64]={18,0x03,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0,'a',0}; // Expected format. Change the values inside the ''
char i=0; // Had to use all the 0's above ^ because the origional data was written as integers, I am hacking this with chars
for (i=18;i<64;i++){
serialnum[i]=0xff; // Set remaining unused positions to 0xff so that we do not have random data.
}
FLASH_Erase_1024(0x2000); //Have to erase first. OUCH! have to erase in blocks of 1024!
FLASH_Write_64(0x2000,serialnum); //Write the new Serial Number.
asm reset; // Need to reset PIC after Serial number update
}
This successfully changes the USB HID serial number for the device from bbbbbbbb to aaaaaaaa.
You should only call this function when needed. You do not want to wear out the flash memory if it is run every boot.
This is just a skeleton, you still need to add the code to generate your unique serial number (Read from another IC?..., Assigned by the computer they are attached to?... etc...)
Hopefully someone else will find this helpful.