Starting with version 5.046 of the CCS C Compiler and version 5.018 of CCSLOAD programming software has made it easy to create Unicode strings and write a user-provided serial number to the USB device descriptor during programming. In the device descriptor of a USB device a serial number string can be provided, but this is optional. If a serial number is provided in the device descriptor, two or more USB devices attached to a host cannot contain the same VID/PID/serial combination. Since USB has a mechanism for storing and reading serial numbers, it may be advantageous to a developer to use this mechanism instead of developing a custom mechanism on top of whatever USB protocol is being used. A challenge with using the standard USB mechanism, however, has been programming a user provided serial number due to the string being in Unicode and having the Unicode string packed in a table with other strings in the string table with a length field in front of the string. All of these problems have been solved in the latest tools from CCS.
First, a new pre-processor has been added to the CCS C Compiler:
When called, it converts the string x to a UTF-16 Unicode string. This is the format used by string descriptors in USB. For example:
char string = _unicode("Hello");
Since "Hello" is an ASCII string (no character has a value greater than 0x7F), all characters encoded will be 16bit - therefore the size of string would be the strlen("Hello")*2. string, when in Unicode, will not be null terminated. It is not null-terminated since 0x00 is a valid character in UTF-16 and not a null-terminator. That means to get the string length of string the user would have to use sizeof() instead of strlen().
Second, the #serialize() pre-processor has been updated in the CCS C Compiler to tell the CCS CCSLOAD programming software how to serialize a Unicode string. The new parameter added is unicode, which tells the #serialize which USB string to write during programming with the user provided serial number. Here is an example:
#serialize(id=USB_STRING_DESC, unicode=3, prompt="SN#")
USB_STRING_DESC is a table of USB strings and is read by the CCS C Compiler's USB stack when the host PC reads a string from the PIC®. A value of 3 is passed to the Unicode parameter, to tell the #serialize that the serial number should be encoded as a Unicode string and that it should overwrite the 4th string in USB_STRING_DESC (the first string in USB_STRING_DESC is 0). In the device descriptor for your USB device, the field identifying which string to use for the serial number should also have been set to 3.
A working example of this is provided in the file ex_usb_hid.c. When compiled and programmed onto the PIC®, CCSLOAD will ask the user for the serial number and write the serial number onto the string descriptor used for the serial number.
Like us on Facebook. Follow us on Twitter.
CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit http://www.ccsinfo.com.
PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.