Skip to content

Commit df91878

Browse files
committed
WIP: works with just keyboard but not complex report descriptor
1 parent 31f5b6a commit df91878

11 files changed

Lines changed: 220 additions & 179 deletions

File tree

ports/atmel-samd/Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ endif
9696
ifeq ($(DEBUG), 1)
9797
# Turn on Python modules useful for debugging (e.g. uheap, ustack).
9898
CFLAGS += -ggdb
99-
CFLAGS += -flto
100-
CFLAGS += -fno-inline
99+
## CFLAGS += -flto
100+
CFLAGS += -fno-inline -fno-ipa-sra
101101
ifeq ($(CHIP_FAMILY), samd21)
102102
CFLAGS += -DENABLE_MICRO_TRACE_BUFFER
103103
endif
@@ -193,8 +193,7 @@ SRC_ASF := \
193193
hpl/systick/hpl_systick.c \
194194
hpl/usb/hpl_usb.c \
195195
usb/class/cdc/device/cdcdf_acm.c \
196-
usb/class/hid/device/hiddf_keyboard.c \
197-
usb/class/hid/device/hiddf_mouse.c \
196+
usb/class/hid/device/hiddf_generic.c \
198197
usb/class/msc/device/mscdf.c \
199198
usb/device/usbdc.c \
200199
usb/usb_protocol.c \
@@ -381,14 +380,19 @@ $(BUILD)/firmware.uf2: $(BUILD)/firmware.bin
381380
$(ECHO) "Create $@"
382381
python2 $(TOP)/tools/uf2/utils/uf2conv.py -b $(BOOTLOADER_SIZE) -c -o $@ $^
383382

384-
$(BUILD)/autogen_usb_descriptor.c: tools/gen_usb_descriptor.py Makefile
383+
$(BUILD)/autogen_usb_descriptor.c $(BUILD)/genhdr/autogen_usb_descriptor.h: autogen_usb_descriptor.intermediate
384+
385+
.INTERMEDIATE: autogen_usb_descriptor.intermediate
386+
387+
autogen_usb_descriptor.intermediate: tools/gen_usb_descriptor.py Makefile
385388
install -d $(BUILD)
386389
python3 tools/gen_usb_descriptor.py \
387390
--manufacturer $(USB_MANUFACTURER)\
388391
--product $(USB_PRODUCT)\
389392
--vid $(USB_VID)\
390393
--pid $(USB_PID)\
391-
$@
394+
--output_c_file $(BUILD)/autogen_usb_descriptor.c\
395+
--output_h_file $(BUILD)/genhdr/autogen_usb_descriptor.h
392396

393397
deploy: $(BUILD)/firmware.bin
394398
$(ECHO) "Writing $< to the board"

ports/atmel-samd/common-hal/usb_hid/Device.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
#include "py/runtime.h"
3232
#include "shared-bindings/microcontroller/__init__.h"
3333
#include "shared-bindings/usb_hid/Device.h"
34-
#include "tools/autogen_usb_descriptor.h"
34+
#include "genhdr/autogen_usb_descriptor.h"
3535

3636
#include "tick.h"
3737

38-
#include "usb/class/hid/device/hiddf_mouse.h"
39-
#include "usb/class/hid/device/hiddf_keyboard.h"
38+
#include "usb/class/hid/device/hiddf_generic.h"
4039

4140
static uint32_t usb_hid_send_report(usb_hid_device_obj_t *self, uint8_t* report, uint8_t len) {
4241

@@ -60,22 +59,16 @@ static uint32_t usb_hid_send_report(usb_hid_device_obj_t *self, uint8_t* report,
6059
// Copy the data only when endpoint is ready to send. The previous
6160
// buffer load gets zero'd out when transaction completes, so if
6261
// you copy before it's ready, only zeros will get sent.
63-
memcpy(self->report_buffer, report, len);
6462

65-
switch (self->kind) {
66-
case USB_HID_MOUSE:
67-
status = hiddf_mouse_write(self->report_buffer, self->report_length);
68-
break;
69-
70-
case USB_HID_KEYBOARD:
71-
status = hiddf_keyboard_write(self->report_buffer, self->report_length);
72-
break;
73-
74-
default:
75-
mp_raise_ValueError("Unknown HID device");
63+
// Prefix with a report id if one is supplied
64+
if (self->report_id > 0) {
65+
self->report_buffer[0] = self->report_id;
66+
memcpy(&(self->report_buffer[1]), report, len);
67+
} else {
68+
memcpy(self->report_buffer, report, len);
7669
}
7770

78-
return status;
71+
return hiddf_generic_write(self->report_buffer, self->report_length);
7972
}
8073

8174
void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* report, uint8_t len) {
@@ -98,18 +91,14 @@ uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self) {
9891

9992

10093
void usb_hid_init() {
101-
usb_hid_devices[USB_HID_DEVICE_MOUSE].base.type = &usb_hid_device_type;
102-
usb_hid_devices[USB_HID_DEVICE_MOUSE].endpoint = hid_mouse_endpoint_in;
103-
104-
usb_hid_devices[USB_HID_DEVICE_KEYBOARD].base.type = &usb_hid_device_type;
105-
usb_hid_devices[USB_HID_DEVICE_KEYBOARD].endpoint = hid_keyboard_endpoint_in;
10694
}
10795

10896
void usb_hid_reset() {
10997
// We don't actually reset. We just set a report that is empty to prevent
11098
// long keypresses and such.
111-
uint8_t report[8] = {0, 0, 0, 0, 0, 0, 0, 0};
99+
uint8_t report[USB_HID_MAX_REPORT_LENGTH] = {0};
112100

113-
usb_hid_send_report(&usb_hid_devices[USB_HID_DEVICE_MOUSE], report, UDI_HID_MOUSE_REPORT_SIZE);
114-
usb_hid_send_report(&usb_hid_devices[USB_HID_DEVICE_KEYBOARD], report, UDI_HID_KBD_REPORT_SIZE);
101+
for (size_t i = 0; i < USB_HID_NUM_DEVICES; i++) {
102+
usb_hid_send_report(&usb_hid_devices[i], report, usb_hid_devices[i].report_length);
103+
}
115104
}

ports/atmel-samd/common-hal/usb_hid/Device.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,20 @@
3232

3333
#include "py/obj.h"
3434

35-
#define UDI_HID_MOUSE_REPORT_SIZE 4
36-
#define UDI_HID_KBD_REPORT_SIZE 8
37-
38-
enum usb_hid_device_kind {
39-
USB_HID_UNKNOWN,
40-
USB_HID_MOUSE,
41-
USB_HID_KEYBOARD,
42-
};
35+
#include "genhdr/autogen_usb_descriptor.h"
4336

4437
typedef struct {
4538
mp_obj_base_t base;
46-
enum usb_hid_device_kind kind;
47-
uint8_t endpoint;
48-
uint8_t report_length;
4939
uint8_t* report_buffer;
40+
uint8_t endpoint;
41+
uint8_t report_id; // If non-zero, prefix report with given id.
42+
uint8_t report_length; // Length not including Report ID.
5043
uint8_t usage_page;
5144
uint8_t usage;
5245

5346
} usb_hid_device_obj_t;
5447

55-
usb_hid_device_obj_t usb_hid_devices[2];
56-
// Indices into usb_hid_devices:
57-
#define USB_HID_DEVICE_MOUSE 0
58-
#define USB_HID_DEVICE_KEYBOARD 1
48+
usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES];
5949

6050
void usb_hid_init(void);
6151
void usb_hid_reset(void);

ports/atmel-samd/common-hal/usb_hid/__init__.c

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,63 @@
3232

3333
#include "shared-bindings/usb_hid/Device.h"
3434

35-
#include "tools/autogen_usb_descriptor.h"
35+
#include "genhdr/autogen_usb_descriptor.h"
3636

37-
static uint8_t mouse_report_buffer[UDI_HID_MOUSE_REPORT_SIZE];
38-
static uint8_t kbd_report_buffer[UDI_HID_KBD_REPORT_SIZE];
37+
// Buffers are report size + 1 to include the Report ID prefix byte
38+
static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD + 1];
39+
static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE + 1];
40+
static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER + 1];
41+
static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL + 1];
3942

40-
usb_hid_device_obj_t usb_hid_devices[2] = {
43+
usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = {
4144
{
42-
.kind = USB_HID_MOUSE,
43-
.report_length = UDI_HID_MOUSE_REPORT_SIZE,
45+
.base = { .type = &usb_hid_device_type },
46+
.report_buffer = keyboard_report_buffer,
47+
.endpoint = USB_HID_ENDPOINT_IN,
48+
.report_id = USB_HID_REPORT_ID_KEYBOARD,
49+
.report_length = USB_HID_REPORT_LENGTH_KEYBOARD,
50+
.usage_page = 0x01,
51+
.usage = 0x06,
52+
},
53+
{
54+
.base = { .type = &usb_hid_device_type },
4455
.report_buffer = mouse_report_buffer,
56+
.endpoint = USB_HID_ENDPOINT_IN,
57+
.report_id = USB_HID_REPORT_ID_MOUSE,
58+
.report_length = USB_HID_REPORT_LENGTH_MOUSE,
4559
.usage_page = 0x01,
4660
.usage = 0x02,
4761
},
4862
{
49-
.kind = USB_HID_KEYBOARD,
50-
.report_length = UDI_HID_KBD_REPORT_SIZE,
51-
.report_buffer = kbd_report_buffer,
63+
.base = { .type = &usb_hid_device_type },
64+
.report_buffer = consumer_report_buffer,
65+
.endpoint = USB_HID_ENDPOINT_IN,
66+
.report_id = USB_HID_REPORT_ID_CONSUMER,
67+
.report_length = USB_HID_REPORT_LENGTH_CONSUMER,
68+
.usage_page = 0x0C,
69+
.usage = 0x01,
70+
},
71+
{
72+
.base = { .type = &usb_hid_device_type },
73+
.report_buffer = sys_control_report_buffer,
74+
.endpoint = USB_HID_ENDPOINT_IN,
75+
.report_id = USB_HID_REPORT_ID_SYS_CONTROL,
76+
.report_length = USB_HID_REPORT_LENGTH_SYS_CONTROL,
5277
.usage_page = 0x01,
53-
.usage = 0x06,
54-
}
78+
.usage = 0x80,
79+
},
5580
};
5681

57-
// TODO(tannewt): Make this a mp_obj_tuple_t when it is dynamically allocated.
58-
// until then we hard code it to two entries so LTO is happy.
59-
mp_obj_tuple2_t common_hal_usb_hid_devices = {
82+
83+
mp_obj_tuple_t common_hal_usb_hid_devices = {
6084
.base = {
6185
.type = &mp_type_tuple,
6286
},
63-
.len = 2,
87+
.len = USB_HID_NUM_DEVICES,
6488
.items = {
65-
(mp_obj_t) &usb_hid_devices[USB_HID_DEVICE_MOUSE],
66-
(mp_obj_t) &usb_hid_devices[USB_HID_DEVICE_KEYBOARD],
89+
(mp_obj_t) &usb_hid_devices[0],
90+
(mp_obj_t) &usb_hid_devices[1],
91+
(mp_obj_t) &usb_hid_devices[2],
92+
(mp_obj_t) &usb_hid_devices[3],
6793
}
6894
};

ports/atmel-samd/supervisor/serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "common-hal/usb_hid/Device.h"
3232

3333
#include "usb.h"
34-
#include "tools/autogen_usb_descriptor.h"
34+
#include "genhdr/autogen_usb_descriptor.h"
3535

3636
// Serial number as hex characters. This writes directly to the USB
3737
// descriptor.

ports/atmel-samd/tools/autogen_usb_descriptor.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)