2017-10-26 19:53:25 -04:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# path hacking
|
2017-11-02 16:00:50 -04:00
|
|
|
sys.path.append("../../tools/usb_descriptor")
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-03-24 18:29:12 -04:00
|
|
|
from adafruit_usb_descriptor import cdc, hid, msc, standard, util
|
2017-10-26 19:53:25 -04:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Generate USB descriptors.')
|
|
|
|
parser.add_argument('--manufacturer', type=str,
|
|
|
|
help='manufacturer of the device')
|
|
|
|
parser.add_argument('--product', type=str,
|
|
|
|
help='product name of the device')
|
|
|
|
parser.add_argument('--vid', type=lambda x: int(x, 16),
|
|
|
|
help='vendor id')
|
|
|
|
parser.add_argument('--pid', type=lambda x: int(x, 16),
|
|
|
|
help='product id')
|
|
|
|
parser.add_argument('--serial_number_length', type=int, default=32,
|
|
|
|
help='length needed for the serial number in digits')
|
2018-03-30 23:20:24 -04:00
|
|
|
parser.add_argument('--output_c_file', type=argparse.FileType('w'), required=True)
|
|
|
|
parser.add_argument('--output_h_file', type=argparse.FileType('w'), required=True)
|
2017-10-26 19:53:25 -04:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
class StringIndex:
|
|
|
|
"""Assign a monotonically increasing index to each unique string. Start with 0."""
|
|
|
|
string_to_index = {}
|
|
|
|
strings = []
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
@classmethod
|
|
|
|
def index(cls, string):
|
|
|
|
if string in cls.string_to_index:
|
|
|
|
return cls.string_to_index[string]
|
|
|
|
else:
|
|
|
|
idx = len(cls.strings)
|
|
|
|
cls.string_to_index[string] = idx
|
|
|
|
cls.strings.append(string)
|
|
|
|
return idx
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def strings_in_order(cls):
|
|
|
|
return cls.strings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# langid must be the 0th string descriptor
|
|
|
|
LANGID_INDEX = StringIndex.index("\u0409")
|
|
|
|
assert LANGID_INDEX == 0
|
|
|
|
SERIAL_NUMBER_INDEX = StringIndex.index("S" * args.serial_number_length)
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2017-11-02 16:00:50 -04:00
|
|
|
device = standard.DeviceDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="top",
|
2017-10-26 19:53:25 -04:00
|
|
|
idVendor=args.vid,
|
|
|
|
idProduct=args.pid,
|
2018-04-08 09:33:02 -04:00
|
|
|
iManufacturer=StringIndex.index(args.manufacturer),
|
|
|
|
iProduct=StringIndex.index(args.product),
|
|
|
|
iSerialNumber=SERIAL_NUMBER_INDEX)
|
|
|
|
|
|
|
|
# Interface numbers are interface-set local and endpoints are interface local
|
|
|
|
# until util.join_interfaces renumbers them.
|
|
|
|
|
|
|
|
cdc_union = cdc.Union(
|
|
|
|
description="CDC comm",
|
|
|
|
bMasterInterface=0x00, # Adjust this after interfaces are renumbered.
|
|
|
|
bSlaveInterface_list=[0x01]) # Adjust this after interfaces are renumbered.
|
|
|
|
|
|
|
|
cdc_call_management = cdc.CallManagement(
|
|
|
|
description="CDC comm",
|
|
|
|
bmCapabilities=0x01,
|
|
|
|
bDataInterface=0x01) # Adjust this after interfaces are renumbered.
|
|
|
|
|
|
|
|
cdc_comm_interface = standard.InterfaceDescriptor(
|
|
|
|
description="CDC comm",
|
|
|
|
bInterfaceClass=cdc.CDC_CLASS_COMM, # Communications Device Class
|
|
|
|
bInterfaceSubClass=cdc.CDC_SUBCLASS_ACM, # Abstract control model
|
|
|
|
bInterfaceProtocol=cdc.CDC_PROTOCOL_NONE,
|
|
|
|
iInterface=StringIndex.index("CircuitPython CDC control"),
|
|
|
|
subdescriptors=[
|
|
|
|
cdc.Header(
|
|
|
|
description="CDC comm",
|
|
|
|
bcdCDC=0x0110),
|
|
|
|
cdc_call_management,
|
|
|
|
cdc.AbstractControlManagement(
|
|
|
|
description="CDC comm",
|
|
|
|
bmCapabilities=0x02),
|
|
|
|
cdc_union,
|
|
|
|
standard.EndpointDescriptor(
|
|
|
|
description="CDC comm in",
|
|
|
|
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
|
|
|
|
wMaxPacketSize=0x0040,
|
|
|
|
bInterval=0x10)
|
|
|
|
])
|
|
|
|
|
|
|
|
cdc_data_interface = standard.InterfaceDescriptor(
|
|
|
|
description="CDC data",
|
|
|
|
bInterfaceClass=cdc.CDC_CLASS_DATA,
|
|
|
|
iInterface=StringIndex.index("CircuitPython CDC data"),
|
|
|
|
subdescriptors=[
|
|
|
|
standard.EndpointDescriptor(
|
|
|
|
description="CDC data out",
|
|
|
|
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_OUT,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK),
|
|
|
|
standard.EndpointDescriptor(
|
|
|
|
description="CDC data in",
|
|
|
|
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK),
|
|
|
|
])
|
|
|
|
|
|
|
|
cdc_interfaces = [cdc_comm_interface, cdc_data_interface]
|
2017-10-26 19:53:25 -04:00
|
|
|
|
|
|
|
msc_interfaces = [
|
2017-11-02 16:00:50 -04:00
|
|
|
standard.InterfaceDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="MSC",
|
2018-03-24 18:29:12 -04:00
|
|
|
bInterfaceClass=msc.MSC_CLASS,
|
|
|
|
bInterfaceSubClass=msc.MSC_SUBCLASS_TRANSPARENT,
|
|
|
|
bInterfaceProtocol=msc.MSC_PROTOCOL_BULK,
|
2018-04-08 09:33:02 -04:00
|
|
|
iInterface=StringIndex.index("CircuitPython Mass Storage"),
|
2017-10-26 19:53:25 -04:00
|
|
|
subdescriptors=[
|
2017-11-02 16:00:50 -04:00
|
|
|
standard.EndpointDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="MSC in",
|
2017-11-02 16:00:50 -04:00
|
|
|
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK),
|
|
|
|
standard.EndpointDescriptor(
|
2018-03-30 23:20:24 -04:00
|
|
|
description="MSC out",
|
2017-11-02 16:00:50 -04:00
|
|
|
bEndpointAddress=0x1 | standard.EndpointDescriptor.DIRECTION_OUT,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_BULK)
|
2017-10-26 19:53:25 -04:00
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
hid_report_descriptor = hid.ReportDescriptor.MOUSE_KEYBOARD_CONSUMER_SYS_CONTROL_REPORT
|
|
|
|
hid_report_ids = hid.ReportDescriptor.REPORT_IDS
|
|
|
|
hid_report_lengths = hid.ReportDescriptor.REPORT_LENGTHS
|
|
|
|
hid_max_report_length = max(hid_report_lengths.values())
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
# ASF4 expects keyboard and generic devices to have both in and out endpoints,
|
2018-04-02 19:08:18 -04:00
|
|
|
# and will fail (possibly silently) if both are not supplied.
|
2018-03-30 23:20:24 -04:00
|
|
|
hid_endpoint_in_descriptor = standard.EndpointDescriptor(
|
|
|
|
description="HID in",
|
|
|
|
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
|
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
|
|
|
|
bInterval=0x02)
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
hid_endpoint_out_descriptor = standard.EndpointDescriptor(
|
|
|
|
description="HID out",
|
2018-03-30 23:31:28 -04:00
|
|
|
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_OUT,
|
2018-03-30 23:20:24 -04:00
|
|
|
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT)
|
2018-03-24 18:29:12 -04:00
|
|
|
|
|
|
|
hid_interfaces = [
|
|
|
|
standard.InterfaceDescriptor(
|
2018-04-08 09:33:02 -04:00
|
|
|
description="HID Multiple Devices",
|
2018-03-30 23:20:24 -04:00
|
|
|
bInterfaceClass=hid.HID_CLASS,
|
|
|
|
bInterfaceSubClass=hid.HID_SUBCLASS_NOBOOT,
|
2018-04-08 09:33:02 -04:00
|
|
|
bInterfaceProtocol=hid.HID_PROTOCOL_NONE,
|
|
|
|
iInterface=StringIndex.index("CircuitPython HID"),
|
2018-03-24 18:29:12 -04:00
|
|
|
subdescriptors=[
|
2018-04-08 09:33:02 -04:00
|
|
|
hid.HIDDescriptor(
|
|
|
|
description="HID",
|
|
|
|
wDescriptorLength=len(bytes(hid_report_descriptor))),
|
2018-03-30 23:20:24 -04:00
|
|
|
hid_endpoint_in_descriptor,
|
|
|
|
hid_endpoint_out_descriptor,
|
2018-03-24 18:29:12 -04:00
|
|
|
]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
# This will renumber the endpoints to make them unique across descriptors,
|
|
|
|
# and renumber the interfaces in order. But we still need to fix up certain
|
|
|
|
# interface cross-references.
|
2018-04-08 15:59:19 -04:00
|
|
|
interfaces = util.join_interfaces(cdc_interfaces, msc_interfaces, hid_interfaces)
|
2018-04-08 09:33:02 -04:00
|
|
|
|
|
|
|
# Now adjust the CDC interface cross-references.
|
|
|
|
|
|
|
|
cdc_union.bMasterInterface = cdc_comm_interface.bInterfaceNumber
|
|
|
|
cdc_union.bSlaveInterface_list = [cdc_data_interface.bInterfaceNumber]
|
|
|
|
|
|
|
|
cdc_call_management.bDataInterface = cdc_data_interface.bInterfaceNumber
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
cdc_iad = standard.InterfaceAssociationDescriptor(
|
|
|
|
description="CDC IAD",
|
|
|
|
bFirstInterface=cdc_comm_interface.bInterfaceNumber,
|
2017-10-26 19:53:25 -04:00
|
|
|
bInterfaceCount=len(cdc_interfaces),
|
|
|
|
bFunctionClass=0x2, # Communications Device Class
|
|
|
|
bFunctionSubClass=0x2, # Abstract control model
|
2018-04-08 09:33:02 -04:00
|
|
|
bFunctionProtocol=0x1)
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2017-11-02 16:00:50 -04:00
|
|
|
configuration = standard.ConfigurationDescriptor(
|
2018-04-02 19:08:18 -04:00
|
|
|
description="Composite configuration",
|
2017-11-02 16:00:50 -04:00
|
|
|
wTotalLength=(standard.ConfigurationDescriptor.bLength +
|
2018-04-08 09:33:02 -04:00
|
|
|
cdc_iad.bLength +
|
2017-10-26 19:53:25 -04:00
|
|
|
sum([len(bytes(x)) for x in interfaces])),
|
|
|
|
bNumInterfaces=len(interfaces))
|
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
descriptor_list = []
|
|
|
|
descriptor_list.append(device)
|
|
|
|
descriptor_list.append(configuration)
|
2018-04-08 15:59:19 -04:00
|
|
|
descriptor_list.append(cdc_iad)
|
|
|
|
descriptor_list.extend(cdc_interfaces)
|
2018-04-08 09:33:02 -04:00
|
|
|
descriptor_list.extend(msc_interfaces)
|
|
|
|
# Put the CDC IAD just before the CDC interfaces.
|
|
|
|
# There appears to be a bug in the Windows composite USB driver that requests the
|
|
|
|
# HID report descriptor with the wrong interface number if the HID interface is not given
|
2018-04-08 15:59:19 -04:00
|
|
|
# first. However, it still fetches the descriptor anyway. We could reorder the interfaces but
|
|
|
|
# the Windows 7 Adafruit_usbser.inf file thinks CDC is at Interface 0, so we'll leave it
|
|
|
|
# there for backwards compatibility.
|
|
|
|
descriptor_list.extend(hid_interfaces)
|
2018-04-08 09:33:02 -04:00
|
|
|
|
|
|
|
string_descriptors = [standard.StringDescriptor(string) for string in StringIndex.strings_in_order()]
|
|
|
|
serial_number_descriptor = string_descriptors[SERIAL_NUMBER_INDEX]
|
|
|
|
descriptor_list.extend(string_descriptors)
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file = args.output_c_file
|
|
|
|
h_file = args.output_h_file
|
2017-10-26 19:53:25 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
|
|
|
|
c_file.write("""\
|
2018-03-24 18:29:12 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
#include "{H_FILE_NAME}"
|
|
|
|
|
2018-04-02 19:08:18 -04:00
|
|
|
#include "usb/device/usbdc.h"
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
""".format(H_FILE_NAME=h_file.name))
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
2018-03-24 18:29:12 -04:00
|
|
|
uint8_t usb_descriptors[] = {
|
|
|
|
""")
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
# Write out all the regular descriptors as one long array (that's how ASF4 does it).
|
2017-10-26 19:53:25 -04:00
|
|
|
descriptor_length = 0
|
|
|
|
serial_number_offset = None
|
|
|
|
for descriptor in descriptor_list:
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
|
|
|
// {DESCRIPTION} : {CLASS}
|
|
|
|
""".format(DESCRIPTION=descriptor.description,
|
|
|
|
CLASS=descriptor.__class__))
|
|
|
|
|
2017-10-26 19:53:25 -04:00
|
|
|
b = bytes(descriptor)
|
|
|
|
i = 0
|
2018-03-30 23:20:24 -04:00
|
|
|
|
2018-04-08 09:33:02 -04:00
|
|
|
if descriptor == serial_number_descriptor:
|
2018-03-30 23:20:24 -04:00
|
|
|
# Add two for bLength and bDescriptorType.
|
2017-10-26 19:53:25 -04:00
|
|
|
serial_number_offset = descriptor_length + 2
|
2018-03-30 23:20:24 -04:00
|
|
|
|
2017-10-26 19:53:25 -04:00
|
|
|
# This prints each subdescriptor on a separate line.
|
|
|
|
while i < len(b):
|
|
|
|
length = b[i]
|
|
|
|
for j in range(length):
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("0x{:02x}, ".format(b[i + j]))
|
|
|
|
c_file.write("\n")
|
2017-10-26 19:53:25 -04:00
|
|
|
i += length
|
|
|
|
descriptor_length += length
|
|
|
|
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
2018-03-24 18:29:12 -04:00
|
|
|
};
|
2018-03-30 23:20:24 -04:00
|
|
|
""")
|
|
|
|
|
|
|
|
# Now we values we need for the .h file.
|
|
|
|
h_file.write("""\
|
|
|
|
#ifndef MICROPY_INCLUDED_AUTOGEN_USB_DESCRIPTOR_H
|
|
|
|
#define MICROPY_INCLUDED_AUTOGEN_USB_DESCRIPTOR_H
|
|
|
|
|
|
|
|
#define SERIAL_NUMBER_OFFSET {SERIAL_NUMBER_OFFSET}
|
|
|
|
#define SERIAL_NUMBER_LENGTH {SERIAL_NUMBER_LENGTH}
|
|
|
|
uint8_t* serial_number;
|
|
|
|
|
|
|
|
uint8_t hid_report_descriptor[{HID_REPORT_DESCRIPTOR_LENGTH}];
|
|
|
|
#define USB_HID_ENDPOINT_IN {HID_ENDPOINT_IN_ADDRESS}
|
|
|
|
#define USB_HID_ENDPOINT_OUT {HID_ENDPOINT_OUT_ADDRESS}
|
|
|
|
|
|
|
|
"""
|
|
|
|
.format(SERIAL_NUMBER_OFFSET=serial_number_offset,
|
|
|
|
SERIAL_NUMBER_LENGTH=args.serial_number_length,
|
|
|
|
HID_REPORT_DESCRIPTOR_LENGTH=len(bytes(hid_report_descriptor)),
|
|
|
|
HID_ENDPOINT_IN_ADDRESS=hex(hid_endpoint_in_descriptor.bEndpointAddress),
|
|
|
|
HID_ENDPOINT_OUT_ADDRESS=hex(hid_endpoint_out_descriptor.bEndpointAddress)))
|
|
|
|
|
2018-04-02 19:08:18 -04:00
|
|
|
# Write out #define's that declare which endpoints are in use.
|
|
|
|
# These provide information for declaring cache sizes and perhaps other things at compile time
|
|
|
|
for interface in interfaces:
|
|
|
|
for subdescriptor in interface.subdescriptors:
|
|
|
|
if isinstance(subdescriptor, standard.EndpointDescriptor):
|
|
|
|
endpoint_num = subdescriptor.bEndpointAddress & standard.EndpointDescriptor.NUMBER_MASK
|
|
|
|
endpoint_in = ((subdescriptor.bEndpointAddress & standard.EndpointDescriptor.DIRECTION_MASK) ==
|
|
|
|
standard.EndpointDescriptor.DIRECTION_IN)
|
|
|
|
h_file.write("""\
|
|
|
|
#define USB_ENDPOINT_{NUMBER}_{DIRECTION}_USED 1
|
|
|
|
""".format(NUMBER=endpoint_num,
|
|
|
|
DIRECTION="IN" if endpoint_in else "OUT"))
|
|
|
|
|
|
|
|
h_file.write("\n")
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
# #define the report ID's used in the combined HID descriptor
|
|
|
|
for name, id in hid_report_ids.items():
|
|
|
|
h_file.write("""\
|
|
|
|
#define USB_HID_REPORT_ID_{NAME} {ID}
|
|
|
|
""".format(NAME=name,
|
|
|
|
ID = id))
|
|
|
|
|
2018-04-02 19:08:18 -04:00
|
|
|
h_file.write("\n")
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
# #define the report sizes used in the combined HID descriptor
|
|
|
|
for name, length in hid_report_lengths.items():
|
|
|
|
h_file.write("""\
|
|
|
|
#define USB_HID_REPORT_LENGTH_{NAME} {LENGTH}
|
|
|
|
""".format(NAME=name,
|
|
|
|
LENGTH=length))
|
2018-03-24 18:29:12 -04:00
|
|
|
|
2018-04-02 19:08:18 -04:00
|
|
|
h_file.write("\n")
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
h_file.write("""\
|
|
|
|
#define USB_HID_NUM_DEVICES {NUM_DEVICES}
|
|
|
|
#define USB_HID_MAX_REPORT_LENGTH {MAX_LENGTH}
|
|
|
|
""".format(NUM_DEVICES=len(hid_report_lengths),
|
|
|
|
MAX_LENGTH=hid_max_report_length))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Write out the report descriptor and info
|
|
|
|
c_file.write("""\
|
|
|
|
uint8_t hid_report_descriptor[{HID_DESCRIPTOR_LENGTH}] = {{
|
|
|
|
""".format(HID_DESCRIPTOR_LENGTH=len(bytes(hid_report_descriptor))))
|
|
|
|
|
|
|
|
for b in bytes(hid_report_descriptor):
|
|
|
|
c_file.write("0x{:02x}, ".format(b))
|
|
|
|
c_file.write("""
|
|
|
|
};
|
2018-03-24 18:29:12 -04:00
|
|
|
""")
|
|
|
|
|
2018-03-30 23:20:24 -04:00
|
|
|
c_file.write("""\
|
2018-03-24 18:29:12 -04:00
|
|
|
|
|
|
|
struct usbd_descriptors descriptor_bounds = {{usb_descriptors, usb_descriptors + sizeof(usb_descriptors)}};
|
|
|
|
uint8_t* serial_number = usb_descriptors + {SERIAL_NUMBER_OFFSET};
|
2018-03-30 23:20:24 -04:00
|
|
|
""".format(SERIAL_NUMBER_OFFSET=serial_number_offset))
|
|
|
|
|
|
|
|
h_file.write("""\
|
|
|
|
#endif // MICROPY_INCLUDED_AUTOGEN_USB_DESCRIPTOR_H
|
|
|
|
""")
|