From 771b4c7464f0ca30fd5e225c758167a7d1b36dd2 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 31 Aug 2021 13:02:34 -0700 Subject: [PATCH] Add two space saving knobs * Reduce the number of supported HID reports of IDs per descriptor. This saves ~200 bytes in the default HID objects. * (Not enabled) Compute QSTR attrs on init. This trades 1k RAM for flash. Flash is the default (1). --- ports/atmel-samd/mpconfigport.h | 3 +++ py/circuitpy_mpconfig.h | 17 +++++++++++++++++ py/qstr.c | 16 +++++++++++++++- shared-module/usb_hid/Device.c | 14 +++++++------- shared-module/usb_hid/Device.h | 15 +++++---------- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 6332c9d3fc..e200ce67db 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -61,6 +61,9 @@ #define MICROPY_FATFS_EXFAT 0 +// Only support simpler HID descriptors on SAMD21. +#define CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR (1) + #endif // SAMD21 //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 91e71078fb..8a34c346e4 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -717,6 +717,13 @@ void supervisor_run_background_tasks_if_tick(void); #define CIRCUITPY_VERBOSE_BLE 0 +// This trades ~1k flash space (1) for that much in RAM plus the cost to compute +// the values once on init (0). Only turn it off, when you really need the flash +// space and are willing to trade the RAM. +#ifndef CIRCUITPY_PRECOMPUTE_QSTR_ATTR +#define CIRCUITPY_PRECOMPUTE_QSTR_ATTR (1) +#endif + // USB settings // If the port requires certain USB endpoint numbers, define these in mpconfigport.h. @@ -761,6 +768,16 @@ void supervisor_run_background_tasks_if_tick(void); #define USB_HID_EP_NUM_IN (0) #endif +// The most complicated device currently known of is the head and eye tracker, which requires 5 +// report ids. +// https://usb.org/sites/default/files/hutrr74_-_usage_page_for_head_and_eye_trackers_0.pdf +// The default descriptors only use 1, so that is the minimum. +#ifndef CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR +#define CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR (6) +#elif CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR < 1 +#error "CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR must be at least 1" +#endif + #ifndef USB_MIDI_EP_NUM_OUT #define USB_MIDI_EP_NUM_OUT (0) #endif diff --git a/py/qstr.c b/py/qstr.c index 230a230909..9ea7ab83db 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -77,7 +77,8 @@ mp_uint_t qstr_compute_hash(const byte *data, size_t len) { return hash; } -const qstr_attr_t mp_qstr_const_attr[] = { +#if CIRCUITPY_PRECOMPUTE_QSTR_ATTR +const qstr_attr_t mp_qstr_const_attr[MP_QSTRnumber_of] = { #ifndef NO_QSTR #define QDEF(id, hash, len, str) { hash, len }, #define TRANSLATION(id, length, compressed ...) @@ -86,6 +87,9 @@ const qstr_attr_t mp_qstr_const_attr[] = { #undef QDEF #endif }; +#else +qstr_attr_t mp_qstr_const_attr[MP_QSTRnumber_of]; +#endif const qstr_pool_t mp_qstr_const_pool = { NULL, // no previous pool @@ -115,6 +119,16 @@ void qstr_init(void) { MP_STATE_VM(last_pool) = (qstr_pool_t *)&CONST_POOL; // we won't modify the const_pool since it has no allocated room left MP_STATE_VM(qstr_last_chunk) = NULL; + #if CIRCUITPY_PRECOMPUTE_QSTR_ATTR == 0 + if (mp_qstr_const_attr[MP_QSTR_circuitpython].len == 0) { + for (size_t i = 0; i < mp_qstr_const_pool.len; i++) { + size_t len = strlen(mp_qstr_const_pool.qstrs[i]); + mp_qstr_const_attr[i].hash = qstr_compute_hash((const byte *)mp_qstr_const_pool.qstrs[i], len); + mp_qstr_const_attr[i].len = len; + } + } + #endif + #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex)); #endif diff --git a/shared-module/usb_hid/Device.c b/shared-module/usb_hid/Device.c index e864b08873..71c6dea7af 100644 --- a/shared-module/usb_hid/Device.c +++ b/shared-module/usb_hid/Device.c @@ -81,7 +81,7 @@ const usb_hid_device_obj_t usb_hid_device_keyboard_obj = { .usage = 0x06, .num_report_ids = 1, .report_ids = { 0x01, }, - .in_report_lengths = { 8, 0, 0, 0, 0, 0, }, + .in_report_lengths = { 8, }, .out_report_lengths = { 1, }, }; @@ -131,7 +131,7 @@ const usb_hid_device_obj_t usb_hid_device_mouse_obj = { .usage = 0x02, .num_report_ids = 1, .report_ids = { 0x02, }, - .in_report_lengths = { 4, 0, 0, 0, 0, 0, }, + .in_report_lengths = { 4, }, .out_report_lengths = { 0, }, }; @@ -160,7 +160,7 @@ const usb_hid_device_obj_t usb_hid_device_consumer_control_obj = { .usage = 0x01, .num_report_ids = 1, .report_ids = { 0x03 }, - .in_report_lengths = { 2, 0, 0, 0, 0, 0, }, + .in_report_lengths = { 2, }, .out_report_lengths = { 0, }, }; @@ -170,7 +170,7 @@ STATIC size_t get_report_id_idx(usb_hid_device_obj_t *self, size_t report_id) { return i; } } - return MAX_REPORT_IDS_PER_DESCRIPTOR; + return CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR; } // See if report_id is used by this device. If it is -1, then return the sole report id used by this device, @@ -180,16 +180,16 @@ uint8_t common_hal_usb_hid_device_validate_report_id(usb_hid_device_obj_t *self, return self->report_ids[0]; } if (!(report_id_arg >= 0 && - get_report_id_idx(self, (size_t)report_id_arg) < MAX_REPORT_IDS_PER_DESCRIPTOR)) { + get_report_id_idx(self, (size_t)report_id_arg) < CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR)) { mp_raise_ValueError_varg(translate("Invalid %q"), MP_QSTR_report_id); } return (uint8_t)report_id_arg; } void common_hal_usb_hid_device_construct(usb_hid_device_obj_t *self, mp_obj_t report_descriptor, uint8_t usage_page, uint8_t usage, size_t num_report_ids, uint8_t *report_ids, uint8_t *in_report_lengths, uint8_t *out_report_lengths) { - if (num_report_ids > MAX_REPORT_IDS_PER_DESCRIPTOR) { + if (num_report_ids > CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR) { mp_raise_ValueError_varg(translate("More than %d report ids not supported"), - MAX_REPORT_IDS_PER_DESCRIPTOR); + CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR); } // report buffer pointers are NULL at start, and are created when USB is initialized. diff --git a/shared-module/usb_hid/Device.h b/shared-module/usb_hid/Device.h index a3c523acd5..5a09d19526 100644 --- a/shared-module/usb_hid/Device.h +++ b/shared-module/usb_hid/Device.h @@ -32,21 +32,16 @@ #include "py/obj.h" -// The most complicated device currently known of is the head and eye tracker, which requires 5 -// report ids. -// https://usb.org/sites/default/files/hutrr74_-_usage_page_for_head_and_eye_trackers_0.pdf -#define MAX_REPORT_IDS_PER_DESCRIPTOR (6) - typedef struct { mp_obj_base_t base; // Python buffer object whose contents are the descriptor. const uint8_t *report_descriptor; - uint8_t *in_report_buffers[MAX_REPORT_IDS_PER_DESCRIPTOR]; - uint8_t *out_report_buffers[MAX_REPORT_IDS_PER_DESCRIPTOR]; + uint8_t *in_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; + uint8_t *out_report_buffers[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; uint16_t report_descriptor_length; - uint8_t report_ids[MAX_REPORT_IDS_PER_DESCRIPTOR]; - uint8_t in_report_lengths[MAX_REPORT_IDS_PER_DESCRIPTOR]; - uint8_t out_report_lengths[MAX_REPORT_IDS_PER_DESCRIPTOR]; + uint8_t report_ids[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; + uint8_t in_report_lengths[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; + uint8_t out_report_lengths[CIRCUITPY_USB_HID_MAX_REPORT_IDS_PER_DESCRIPTOR]; uint8_t usage_page; uint8_t usage; uint8_t num_report_ids;