Skip to content

Commit 296e858

Browse files
authored
Merge pull request #2116 from dhalbert/choose-usb-devices-xac
Choose which USB and USB HID devices to include at compile-time
2 parents 28e17fe + f3af2a6 commit 296e858

File tree

6 files changed

+418
-382
lines changed

6 files changed

+418
-382
lines changed

ports/atmel-samd/mpconfigport.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ ifeq ($(CHIP_FAMILY),samd21)
2020
CIRCUITPY_AUDIOMIXER = 0
2121
CIRCUITPY_FREQUENCYIO = 0
2222
CIRCUITPY_TOUCHIO_USE_NATIVE = 1
23+
24+
# SAMD21 needs separate endpoint pairs for MSC BULK IN and BULK OUT, otherwise it's erratic.
25+
USB_MSC_NUM_ENDPOINT_PAIRS = 2
2326
endif
2427

2528
# Put samd51-only choices here.

shared-module/usb_hid/Device.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t*
4848
// Wait until interface is ready, timeout = 2 seconds
4949
uint64_t end_ticks = ticks_ms + 2000;
5050
while ( (ticks_ms < end_ticks) && !tud_hid_ready() ) {
51-
#ifdef MICROPY_VM_HOOK_LOOP
52-
MICROPY_VM_HOOK_LOOP;
53-
#endif
51+
RUN_BACKGROUND_TASKS;
5452
}
5553

5654
if ( !tud_hid_ready() ) {

shared-module/usb_hid/__init__.c

Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -24,131 +24,4 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "py/obj.h"
28-
#include "py/mphal.h"
29-
#include "py/runtime.h"
30-
31-
#include "genhdr/autogen_usb_descriptor.h"
32-
#include "shared-module/usb_hid/Device.h"
33-
#include "shared-bindings/usb_hid/Device.h"
34-
#include "tusb.h"
35-
36-
#ifdef USB_HID_REPORT_ID_KEYBOARD
37-
static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD];
38-
#endif
39-
40-
#ifdef USB_HID_REPORT_ID_MOUSE
41-
static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE];
42-
#endif
43-
44-
#ifdef USB_HID_REPORT_ID_CONSUMER
45-
static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER];
46-
#endif
47-
48-
#ifdef USB_HID_REPORT_ID_SYS_CONTROL
49-
static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL];
50-
#endif
51-
52-
#ifdef USB_HID_REPORT_ID_GAMEPAD
53-
static uint8_t gamepad_report_buffer[USB_HID_REPORT_LENGTH_GAMEPAD];
54-
#endif
55-
56-
#ifdef USB_HID_REPORT_ID_DIGITIZER
57-
static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER];
58-
#endif
59-
60-
usb_hid_device_obj_t usb_hid_devices[] = {
61-
#ifdef USB_HID_REPORT_ID_KEYBOARD
62-
{
63-
.base = { .type = &usb_hid_device_type } ,
64-
.report_buffer = keyboard_report_buffer ,
65-
.report_id = USB_HID_REPORT_ID_KEYBOARD ,
66-
.report_length = USB_HID_REPORT_LENGTH_KEYBOARD ,
67-
.usage_page = HID_USAGE_PAGE_DESKTOP ,
68-
.usage = HID_USAGE_DESKTOP_KEYBOARD ,
69-
},
70-
#endif
71-
72-
#ifdef USB_HID_REPORT_ID_MOUSE
73-
{
74-
.base = { .type = &usb_hid_device_type } ,
75-
.report_buffer = mouse_report_buffer ,
76-
.report_id = USB_HID_REPORT_ID_MOUSE ,
77-
.report_length = USB_HID_REPORT_LENGTH_MOUSE ,
78-
.usage_page = HID_USAGE_PAGE_DESKTOP ,
79-
.usage = HID_USAGE_DESKTOP_MOUSE ,
80-
},
81-
#endif
82-
83-
#ifdef USB_HID_REPORT_ID_CONSUMER
84-
{
85-
.base = { .type = &usb_hid_device_type } ,
86-
.report_buffer = consumer_report_buffer ,
87-
.report_id = USB_HID_REPORT_ID_CONSUMER ,
88-
.report_length = USB_HID_REPORT_LENGTH_CONSUMER ,
89-
.usage_page = HID_USAGE_PAGE_CONSUMER ,
90-
.usage = HID_USAGE_CONSUMER_CONTROL ,
91-
},
92-
#endif
93-
94-
#ifdef USB_HID_REPORT_ID_SYS_CONTROL
95-
{
96-
.base = { .type = &usb_hid_device_type } ,
97-
.report_buffer = sys_control_report_buffer ,
98-
.report_id = USB_HID_REPORT_ID_SYS_CONTROL ,
99-
.report_length = USB_HID_REPORT_LENGTH_SYS_CONTROL ,
100-
.usage_page = HID_USAGE_PAGE_DESKTOP ,
101-
.usage = HID_USAGE_DESKTOP_SYSTEM_CONTROL ,
102-
},
103-
#endif
104-
105-
#ifdef USB_HID_REPORT_ID_GAMEPAD
106-
{
107-
.base = { .type = &usb_hid_device_type } ,
108-
.report_buffer = gamepad_report_buffer ,
109-
.report_id = USB_HID_REPORT_ID_GAMEPAD ,
110-
.report_length = USB_HID_REPORT_LENGTH_GAMEPAD ,
111-
.usage_page = HID_USAGE_PAGE_DESKTOP ,
112-
.usage = HID_USAGE_DESKTOP_GAMEPAD ,
113-
},
114-
#endif
115-
116-
#ifdef USB_HID_REPORT_ID_DIGITIZER
117-
{
118-
.base = { .type = &usb_hid_device_type } ,
119-
.report_buffer = digitizer_report_buffer ,
120-
.report_id = USB_HID_REPORT_ID_DIGITIZER ,
121-
.report_length = USB_HID_REPORT_LENGTH_DIGITIZER ,
122-
.usage_page = 0x0D ,
123-
.usage = 0x02 ,
124-
},
125-
#endif
126-
};
127-
128-
129-
mp_obj_tuple_t common_hal_usb_hid_devices = {
130-
.base = {
131-
.type = &mp_type_tuple,
132-
},
133-
.len = USB_HID_NUM_DEVICES,
134-
.items = {
135-
#if USB_HID_NUM_DEVICES >= 1
136-
(mp_obj_t) &usb_hid_devices[0],
137-
#endif
138-
#if USB_HID_NUM_DEVICES >= 2
139-
(mp_obj_t) &usb_hid_devices[1],
140-
#endif
141-
#if USB_HID_NUM_DEVICES >= 3
142-
(mp_obj_t) &usb_hid_devices[2],
143-
#endif
144-
#if USB_HID_NUM_DEVICES >= 4
145-
(mp_obj_t) &usb_hid_devices[3],
146-
#endif
147-
#if USB_HID_NUM_DEVICES >= 5
148-
(mp_obj_t) &usb_hid_devices[4],
149-
#endif
150-
#if USB_HID_NUM_DEVICES >= 6
151-
(mp_obj_t) &usb_hid_devices[5],
152-
#endif
153-
}
154-
};
27+
// Nothing needed here. Tables of HID devices are generated in autogen_usb_descriptor.c at compile-time.

supervisor/supervisor.mk

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ else
8686
CFLAGS += -DUSB_AVAILABLE
8787
endif
8888

89+
ifndef USB_DEVICES
90+
USB_DEVICES = "CDC,MSC,AUDIO,HID"
91+
endif
92+
93+
ifndef USB_HID_DEVICES
94+
USB_HID_DEVICES = "KEYBOARD,MOUSE,CONSUMER,GAMEPAD"
95+
endif
96+
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+
89102
SUPERVISOR_O = $(addprefix $(BUILD)/, $(SRC_SUPERVISOR:.c=.o)) $(BUILD)/autogen_display_resources.o
90103

91104
$(BUILD)/supervisor/shared/translate.o: $(HEADER_BUILD)/qstrdefs.generated.h
@@ -103,6 +116,9 @@ autogen_usb_descriptor.intermediate: ../../tools/gen_usb_descriptor.py Makefile
103116
--vid $(USB_VID)\
104117
--pid $(USB_PID)\
105118
--serial_number_length $(USB_SERIAL_NUMBER_LENGTH)\
119+
--devices $(USB_DEVICES)\
120+
--hid_devices $(USB_HID_DEVICES)\
121+
--msc_num_endpoint_pairs $(USB_MSC_NUM_ENDPOINT_PAIRS)\
106122
--output_c_file $(BUILD)/autogen_usb_descriptor.c\
107123
--output_h_file $(BUILD)/genhdr/autogen_usb_descriptor.h
108124

0 commit comments

Comments
 (0)