Guest User

USB String descriptor generator

a guest
Jul 10th, 2012
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. sd = {
  2.     "stringLanguage"            : 0x0409,
  3.     "stringVendor"              : "STMicroelectronics",
  4.     "stringDeviceDescriptor"    : "ChibiOS/Custom Hardware",
  5.     "stringSerialNumber"        : "2.5.0"
  6. }
  7.  
  8. def stringDescriptor(descriptor):
  9.     for i in descriptor:
  10.         print "static const uint8_t", i, "[] = {"
  11.         if(type(descriptor[i])==type(1)):
  12.             print "  USB_DESC_BYTE(4)," + \
  13.                 "                     /* bLength.                         */"
  14.             print "  USB_DESC_BYTE(USB_DESCRIPTOR_STRING)," \
  15.                 + " /* bDescriptorType.                 */"
  16.             print "  USB_DESC_WORD(" + hex(descriptor[i]) + ")" \
  17.                 + "                  /* wLANGID (U.S. English).          */"
  18.             print "}\n"
  19.             continue
  20.  
  21.         num=0
  22.         s = descriptor[i].encode("utf-16")
  23.         s = s[2:]
  24.         print "  USB_DESC_BYTE(" + str(len(s)+2) + ")" \
  25.             + "                    /* bLength.                         */"
  26.         print "  USB_DESC_BYTE(USB_DESCRIPTOR_STRING)," \
  27.             + "/* bDescriptorType.                 */"
  28.         print " ",
  29.         for j in s:
  30.             if(j)=='\x00':
  31.                 print "0,",
  32.             else:
  33.                 print "'" + j + "'" + ",",
  34.             num+=1
  35.             if(num==16):
  36.                 print "\n ",
  37.                 num =0
  38.         print "\n}\n"
  39.        
  40.  
  41. stringDescriptor(sd)
Advertisement
Add Comment
Please, Sign In to add comment