Skip to content

Commit cc3e0a0

Browse files
authored
Merge pull request #2189 from kamtom480/circuitpython-number-endpoint
Add an alternative way to number the USB endpoints
2 parents 4eb11fb + 810d802 commit cc3e0a0

File tree

4 files changed

+120
-36
lines changed

4 files changed

+120
-36
lines changed

ports/atmel-samd/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CIRCUITPY_FREQUENCYIO = 0
2222
CIRCUITPY_TOUCHIO_USE_NATIVE = 1
2323

2424
# SAMD21 needs separate endpoint pairs for MSC BULK IN and BULK OUT, otherwise it's erratic.
25-
USB_MSC_NUM_ENDPOINT_PAIRS = 2
25+
USB_MSC_EP_NUM_OUT = 1
2626
endif
2727

2828
# Put samd51-only choices here.

supervisor/supervisor.mk

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,71 @@ ifndef USB_HID_DEVICES
9494
USB_HID_DEVICES = "KEYBOARD,MOUSE,CONSUMER,GAMEPAD"
9595
endif
9696

97-
# SAMD21 needs separate endpoint pairs for MSC BULK IN and BULK OUT, otherwise it's erratic.
98-
ifndef USB_MSC_NUM_ENDPOINT_PAIRS
99-
USB_MSC_NUM_ENDPOINT_PAIRS = 1
100-
endif
101-
10297
ifndef USB_MSC_MAX_PACKET_SIZE
10398
USB_MSC_MAX_PACKET_SIZE = 64
10499
endif
105100

101+
ifndef USB_CDC_EP_NUM_NOTIFICATION
102+
USB_CDC_EP_NUM_NOTIFICATION = 0
103+
endif
104+
105+
ifndef USB_CDC_EP_NUM_DATA_OUT
106+
USB_CDC_EP_NUM_DATA_OUT = 0
107+
endif
108+
109+
ifndef USB_CDC_EP_NUM_DATA_IN
110+
USB_CDC_EP_NUM_DATA_IN = 0
111+
endif
112+
113+
ifndef USB_MSC_EP_NUM_OUT
114+
USB_MSC_EP_NUM_OUT = 0
115+
endif
116+
117+
ifndef USB_MSC_EP_NUM_IN
118+
USB_MSC_EP_NUM_IN = 0
119+
endif
120+
121+
ifndef USB_HID_EP_NUM_OUT
122+
USB_HID_EP_NUM_OUT = 0
123+
endif
124+
125+
ifndef USB_HID_EP_NUM_IN
126+
USB_HID_EP_NUM_IN = 0
127+
endif
128+
129+
ifndef USB_MIDI_EP_NUM_OUT
130+
USB_MIDI_EP_NUM_OUT = 0
131+
endif
132+
133+
ifndef USB_MIDI_EP_NUM_IN
134+
USB_MIDI_EP_NUM_IN = 0
135+
endif
136+
137+
USB_DESCRIPTOR_ARGS = \
138+
--manufacturer $(USB_MANUFACTURER)\
139+
--product $(USB_PRODUCT)\
140+
--vid $(USB_VID)\
141+
--pid $(USB_PID)\
142+
--serial_number_length $(USB_SERIAL_NUMBER_LENGTH)\
143+
--devices $(USB_DEVICES)\
144+
--hid_devices $(USB_HID_DEVICES)\
145+
--msc_max_packet_size $(USB_MSC_MAX_PACKET_SIZE)\
146+
--cdc_ep_num_notification $(USB_CDC_EP_NUM_NOTIFICATION)\
147+
--cdc_ep_num_data_out $(USB_CDC_EP_NUM_DATA_OUT)\
148+
--cdc_ep_num_data_in $(USB_CDC_EP_NUM_DATA_IN)\
149+
--msc_ep_num_out $(USB_MSC_EP_NUM_OUT)\
150+
--msc_ep_num_in $(USB_MSC_EP_NUM_IN)\
151+
--hid_ep_num_out $(USB_HID_EP_NUM_OUT)\
152+
--hid_ep_num_in $(USB_HID_EP_NUM_IN)\
153+
--midi_ep_num_out $(USB_MIDI_EP_NUM_OUT)\
154+
--midi_ep_num_in $(USB_MIDI_EP_NUM_IN)\
155+
--output_c_file $(BUILD)/autogen_usb_descriptor.c\
156+
--output_h_file $(BUILD)/genhdr/autogen_usb_descriptor.h
157+
158+
ifeq ($(USB_RENUMBER_ENDPOINTS), 0)
159+
USB_DESCRIPTOR_ARGS += --no-renumber_endpoints
160+
endif
161+
106162
SUPERVISOR_O = $(addprefix $(BUILD)/, $(SRC_SUPERVISOR:.c=.o)) $(BUILD)/autogen_display_resources.o
107163

108164
$(BUILD)/supervisor/shared/translate.o: $(HEADER_BUILD)/qstrdefs.generated.h
@@ -114,18 +170,7 @@ $(BUILD)/autogen_usb_descriptor.c $(BUILD)/genhdr/autogen_usb_descriptor.h: auto
114170
autogen_usb_descriptor.intermediate: ../../tools/gen_usb_descriptor.py Makefile | $(HEADER_BUILD)
115171
$(STEPECHO) "GEN $@"
116172
$(Q)install -d $(BUILD)/genhdr
117-
$(Q)$(PYTHON3) ../../tools/gen_usb_descriptor.py \
118-
--manufacturer $(USB_MANUFACTURER)\
119-
--product $(USB_PRODUCT)\
120-
--vid $(USB_VID)\
121-
--pid $(USB_PID)\
122-
--serial_number_length $(USB_SERIAL_NUMBER_LENGTH)\
123-
--devices $(USB_DEVICES)\
124-
--hid_devices $(USB_HID_DEVICES)\
125-
--msc_num_endpoint_pairs $(USB_MSC_NUM_ENDPOINT_PAIRS)\
126-
--msc_max_packet_size $(USB_MSC_MAX_PACKET_SIZE)\
127-
--output_c_file $(BUILD)/autogen_usb_descriptor.c\
128-
--output_h_file $(BUILD)/genhdr/autogen_usb_descriptor.h
173+
$(Q)$(PYTHON3) ../../tools/gen_usb_descriptor.py $(USB_DESCRIPTOR_ARGS)
129174

130175
CIRCUITPY_DISPLAY_FONT ?= "../../tools/fonts/ter-u12n.bdf"
131176

tools/gen_usb_descriptor.py

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,28 @@
3232
help='devices to include in descriptor (AUDIO includes MIDI support)')
3333
parser.add_argument('--hid_devices', type=lambda l: tuple(l.split(',')), default=DEFAULT_HID_DEVICES,
3434
help='HID devices to include in HID report descriptor')
35-
parser.add_argument('--msc_num_endpoint_pairs', type=int, default=1,
36-
help='Use 1 or 2 endpoint pairs for MSC (1 bidirectional, or 1 input + 1 output (required by SAMD21))')
3735
parser.add_argument('--msc_max_packet_size', type=int, default=64,
3836
help='Max packet size for MSC')
37+
parser.add_argument('--no-renumber_endpoints', dest='renumber_endpoints', action='store_false',
38+
help='use to not renumber endpoint')
39+
parser.add_argument('--cdc_ep_num_notification', type=int, default=0,
40+
help='endpoint number of CDC NOTIFICATION')
41+
parser.add_argument('--cdc_ep_num_data_out', type=int, default=0,
42+
help='endpoint number of CDC DATA OUT')
43+
parser.add_argument('--cdc_ep_num_data_in', type=int, default=0,
44+
help='endpoint number of CDC DATA IN')
45+
parser.add_argument('--msc_ep_num_out', type=int, default=0,
46+
help='endpoint number of MSC OUT')
47+
parser.add_argument('--msc_ep_num_in', type=int, default=0,
48+
help='endpoint number of MSC IN')
49+
parser.add_argument('--hid_ep_num_out', type=int, default=0,
50+
help='endpoint number of HID OUT')
51+
parser.add_argument('--hid_ep_num_in', type=int, default=0,
52+
help='endpoint number of HID IN')
53+
parser.add_argument('--midi_ep_num_out', type=int, default=0,
54+
help='endpoint number of MIDI OUT')
55+
parser.add_argument('--midi_ep_num_in', type=int, default=0,
56+
help='endpoint number of MIDI IN')
3957
parser.add_argument('--output_c_file', type=argparse.FileType('w'), required=True)
4058
parser.add_argument('--output_h_file', type=argparse.FileType('w'), required=True)
4159

@@ -49,9 +67,32 @@
4967
if unknown_hid_devices:
5068
raise ValueError("Unknown HID devices(s)", unknown_hid_devices)
5169

52-
if args.msc_num_endpoint_pairs not in (1, 2):
53-
raise ValueError("--msc_num_endpoint_pairs must be 1 or 2")
54-
70+
if not args.renumber_endpoints:
71+
if 'CDC' in args.devices:
72+
if args.cdc_ep_num_notification == 0:
73+
raise ValueError("CDC notification endpoint number must not be 0")
74+
elif args.cdc_ep_num_data_out == 0:
75+
raise ValueError("CDC data OUT endpoint number must not be 0")
76+
elif args.cdc_ep_num_data_in == 0:
77+
raise ValueError("CDC data IN endpoint number must not be 0")
78+
79+
if 'MSC' in args.devices:
80+
if args.msc_ep_num_out == 0:
81+
raise ValueError("MSC endpoint OUT number must not be 0")
82+
elif args.msc_ep_num_in == 0:
83+
raise ValueError("MSC endpoint IN number must not be 0")
84+
85+
if 'HID' in args.devices:
86+
if args.args.hid_ep_num_out == 0:
87+
raise ValueError("HID endpoint OUT number must not be 0")
88+
elif args.hid_ep_num_in == 0:
89+
raise ValueError("HID endpoint IN number must not be 0")
90+
91+
if 'AUDIO' in args.devices:
92+
if args.args.midi_ep_num_out == 0:
93+
raise ValueError("MIDI endpoint OUT number must not be 0")
94+
elif args.midi_ep_num_in == 0:
95+
raise ValueError("MIDI endpoint IN number must not be 0")
5596

5697
class StringIndex:
5798
"""Assign a monotonically increasing index to each unique string. Start with 0."""
@@ -122,7 +163,7 @@ def strings_in_order(cls):
122163
cdc_union,
123164
standard.EndpointDescriptor(
124165
description="CDC comm in",
125-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
166+
bEndpointAddress=args.cdc_ep_num_notification | standard.EndpointDescriptor.DIRECTION_IN,
126167
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
127168
wMaxPacketSize=0x0040,
128169
bInterval=0x10)
@@ -135,11 +176,11 @@ def strings_in_order(cls):
135176
subdescriptors=[
136177
standard.EndpointDescriptor(
137178
description="CDC data out",
138-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_OUT,
179+
bEndpointAddress=args.cdc_ep_num_data_out | standard.EndpointDescriptor.DIRECTION_OUT,
139180
bmAttributes=standard.EndpointDescriptor.TYPE_BULK),
140181
standard.EndpointDescriptor(
141182
description="CDC data in",
142-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
183+
bEndpointAddress=args.cdc_ep_num_data_in | standard.EndpointDescriptor.DIRECTION_IN,
143184
bmAttributes=standard.EndpointDescriptor.TYPE_BULK),
144185
])
145186

@@ -155,15 +196,13 @@ def strings_in_order(cls):
155196
subdescriptors=[
156197
standard.EndpointDescriptor(
157198
description="MSC in",
158-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
199+
bEndpointAddress=args.msc_ep_num_in | standard.EndpointDescriptor.DIRECTION_IN,
159200
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
160201
bInterval=0,
161202
wMaxPacketSize=args.msc_max_packet_size),
162203
standard.EndpointDescriptor(
163204
description="MSC out",
164-
# SAMD21 needs to use a separate pair of endpoints for MSC.
165-
bEndpointAddress=((0x1 if args.msc_num_endpoint_pairs == 2 else 0x0) |
166-
standard.EndpointDescriptor.DIRECTION_OUT),
205+
bEndpointAddress=(args.msc_ep_num_out | standard.EndpointDescriptor.DIRECTION_OUT),
167206
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
168207
bInterval=0,
169208
wMaxPacketSize=args.msc_max_packet_size)
@@ -201,13 +240,13 @@ def strings_in_order(cls):
201240
# and will fail (possibly silently) if both are not supplied.
202241
hid_endpoint_in_descriptor = standard.EndpointDescriptor(
203242
description="HID in",
204-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
243+
bEndpointAddress=args.hid_ep_num_in | standard.EndpointDescriptor.DIRECTION_IN,
205244
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
206245
bInterval=8)
207246

208247
hid_endpoint_out_descriptor = standard.EndpointDescriptor(
209248
description="HID out",
210-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_OUT,
249+
bEndpointAddress=args.hid_ep_num_out | standard.EndpointDescriptor.DIRECTION_OUT,
211250
bmAttributes=standard.EndpointDescriptor.TYPE_INTERRUPT,
212251
bInterval=8)
213252

@@ -271,12 +310,12 @@ def strings_in_order(cls):
271310
),
272311
standard.EndpointDescriptor(
273312
description="MIDI data out to CircuitPython",
274-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_OUT,
313+
bEndpointAddress=args.midi_ep_num_out | standard.EndpointDescriptor.DIRECTION_OUT,
275314
bmAttributes=standard.EndpointDescriptor.TYPE_BULK),
276315
midi.DataEndpointDescriptor(baAssocJack=[midi_in_jack_emb]),
277316
standard.EndpointDescriptor(
278317
description="MIDI data in from CircuitPython",
279-
bEndpointAddress=0x0 | standard.EndpointDescriptor.DIRECTION_IN,
318+
bEndpointAddress=args.midi_ep_num_in | standard.EndpointDescriptor.DIRECTION_IN,
280319
bmAttributes=standard.EndpointDescriptor.TYPE_BULK,
281320
bInterval = 0x0),
282321
midi.DataEndpointDescriptor(baAssocJack=[midi_out_jack_emb]),
@@ -320,7 +359,7 @@ def strings_in_order(cls):
320359
# util.join_interfaces() will renumber the endpoints to make them unique across descriptors,
321360
# and renumber the interfaces in order. But we still need to fix up certain
322361
# interface cross-references.
323-
interfaces = util.join_interfaces(*interfaces_to_join)
362+
interfaces = util.join_interfaces(interfaces_to_join, renumber_endpoints=args.renumber_endpoints)
324363

325364
# Now adjust the CDC interface cross-references.
326365

0 commit comments

Comments
 (0)