Skip to content

Commit 1ae69b5

Browse files
committed
debugging, with instrumentation
1 parent 24c6295 commit 1ae69b5

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

shared-bindings/microcontroller/Processor.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ const mp_obj_property_t mcu_processor_reset_reason_obj = {
9797
//|
9898
//| Is `None` if the temperature is not available."""
9999
//|
100+
extern volatile float indicator;
100101
STATIC mp_obj_t mcu_processor_get_temperature(mp_obj_t self) {
101-
float temperature = common_hal_mcu_processor_get_temperature();
102+
float temperature = indicator; // common_hal_mcu_processor_get_temperature();
102103
return isnan(temperature) ? mp_const_none : mp_obj_new_float(temperature);
103104
}
104105

shared-bindings/usb_hid/Device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ STATIC mp_obj_t usb_hid_device_make_new(const mp_obj_type_t *type, size_t n_args
136136
// It's not the actual argument that's out of range, but its elements.
137137
// But the error message is close enough.
138138
MP_OBJ_SMALL_INT_VALUE(mp_obj_subscr(report_ids, i_obj, MP_OBJ_SENTINEL)),
139-
1, 255, MP_QSTR_report_ids);
139+
0, 255, MP_QSTR_report_ids);
140140

141141
in_report_lengths_array[i] = (uint8_t)mp_arg_validate_int_range(
142142
MP_OBJ_SMALL_INT_VALUE(mp_obj_subscr(in_report_lengths, i_obj, MP_OBJ_SENTINEL)),

shared-bindings/usb_hid/__init__.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
//| """Tuple of all active HID device interfaces.
4141
//| The default set of devices is ``Device.KEYBOARD, Device.MOUSE, Device.CONSUMER_CONTROL``,
4242
//| On boards where `usb_hid` is disabled by default, `devices` is an empty tuple.
43+
//|
44+
//| If a boot device is enabled by `usb_hid.enable)`, *and* the host has requested a boot device,
45+
//| the `devices` tuple is *replaced* when ``code.py`` starts with a single-element tuple
46+
//| containing a `Device` that describes the boot device chosen (keyboard or mouse).
47+
//| The request for a boot device overrides any other HID devices.
4348
//| """
4449
//|
4550

@@ -84,12 +89,12 @@ MP_DEFINE_CONST_FUN_OBJ_0(usb_hid_disable_obj, usb_hid_disable);
8489
//| Boot devices implement a fixed, predefined report descriptor, defined in
8590
//| https://www.usb.org/sites/default/files/hid1_12.pdf, Appendix B. A USB host
8691
//| can request to use the boot device if the USB device says it is available.
87-
//| Usually only a BIOS or other kind of boot-time, limited-functionality
92+
//| Usually only a BIOS or other kind of limited-functionality
8893
//| host needs boot keyboard support.
8994
//|
9095
//| For example, to make a boot keyboard available, you can use this code::
9196
//|
92-
//| usb_hid.enable((Device.KEYBOARD), boot_device=1)
97+
//| usb_hid.enable((Device.KEYBOARD), boot_device=1) # 1 for a keyboard
9398
//|
9499
//| If the host requests the boot keyboard, the report descriptor provided by `Device.KEYBOARD`
95100
//| will be ignored, and the predefined report descriptor will be used.
@@ -104,11 +109,11 @@ STATIC mp_obj_t usb_hid_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t
104109
enum { ARG_devices, ARG_boot_device };
105110
static const mp_arg_t allowed_args[] = {
106111
{ MP_QSTR_devices, MP_ARG_REQUIRED | MP_ARG_OBJ },
107-
{ MP_QSTR_boot_device, MP_ARG_OBJ, {.u_int = 0} },
112+
{ MP_QSTR_boot_device, MP_ARG_INT, {.u_int = 0} },
108113
};
109114

110115
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
111-
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
116+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
112117

113118
mp_obj_t devices = args[ARG_devices].u_obj;
114119
const mp_int_t len = mp_obj_get_int(mp_obj_len(devices));
@@ -128,18 +133,16 @@ STATIC mp_obj_t usb_hid_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t
128133

129134
return mp_const_none;
130135
}
131-
MP_DEFINE_CONST_FUN_OBJ_KW(usb_hid_enable_obj, 2, usb_hid_enable);
136+
MP_DEFINE_CONST_FUN_OBJ_KW(usb_hid_enable_obj, 1, usb_hid_enable);
132137

133138
//| def get_boot_device() -> int:
134139
//| """
135140
//| :return: the boot device requested by the host, if any.
136141
//| Returns 0 if the host did not request a boot device, or if `usb_hid.enable()`
137142
//| was called with `boot_device=0`, the default, which disables boot device support.
138-
//| If the host did request aboot device,
143+
//| If the host did request a boot device,
139144
//| returns the value of ``boot_device`` set in `usb_hid.enable()`:
140145
//| ``1`` for a boot keyboard, or ``2`` for boot mouse.
141-
//| Your device driver should check and act on this value if the keyboard or mouse
142-
//| device you specified provides reports that differ from the standard boot keyboard or mouse.
143146
//| However, the standard devices provided by CircuitPython, `Device.KEYBOARD` and `Device.MOUSE`,
144147
//| describe reports that match the boot device reports, so you don't need to check this
145148
//| if you are using those devices.

shared-module/usb_hid/__init__.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
#include "py/gc.h"
3232
#include "py/runtime.h"
3333
#include "shared-bindings/usb_hid/__init__.h"
34-
#include "shared-module/usb_hid/Device.h"
34+
#include "shared-bindings/usb_hid/Device.h"
3535
#include "supervisor/memory.h"
3636
#include "supervisor/usb.h"
3737

38+
volatile float indicator = 0.1f;
39+
3840
static const uint8_t usb_hid_descriptor_template[] = {
3941
0x09, // 0 bLength
4042
0x04, // 1 bDescriptorType (Interface)
@@ -86,6 +88,7 @@ static mp_int_t num_hid_devices;
8688
// Which boot device is available 0: no boot devices, 1: boot keyboard, 2: boot mouse.
8789
// This value is set by usb_hid.enable(), and used to build the HID interface descriptor.
8890
// The value is remembered here from boot.py to code.py.
91+
8992
static uint8_t hid_boot_device;
9093

9194
// Whether a boot device was requested by a SET_PROTOCOL request from the host.
@@ -106,6 +109,38 @@ static mp_obj_tuple_t default_hid_devices_tuple = {
106109
},
107110
};
108111

112+
// These describe the standard descriptors used for boot keyboard and mouse, which don't use report IDs.
113+
// When the host requests a boot device, replace whatever HID devices were enabled with a tuple
114+
// containing just one of these, since the host is uninterested in other devices.
115+
// The driver code will then use the proper report length and send_report() will not send a report ID.
116+
static const usb_hid_device_obj_t boot_keyboard_obj = {
117+
.base = {
118+
.type = &usb_hid_device_type,
119+
},
120+
.report_descriptor = NULL,
121+
.report_descriptor_length = 0,
122+
.usage_page = 0x01,
123+
.usage = 0x06,
124+
.num_report_ids = 1,
125+
.report_ids = { 0, },
126+
.in_report_lengths = { 8, },
127+
.out_report_lengths = { 1, },
128+
};
129+
130+
static const usb_hid_device_obj_t boot_mouse_obj = {
131+
.base = {
132+
.type = &usb_hid_device_type,
133+
},
134+
.report_descriptor = NULL,
135+
.report_descriptor_length = 0,
136+
.usage_page = 0x01,
137+
.usage = 0x02,
138+
.num_report_ids = 1,
139+
.report_ids = { 0, },
140+
.in_report_lengths = { 4, },
141+
.out_report_lengths = { 0, },
142+
};
143+
109144
bool usb_hid_enabled(void) {
110145
return num_hid_devices > 0;
111146
}
@@ -121,6 +156,7 @@ uint8_t common_hal_usb_hid_get_boot_device(void) {
121156

122157
void usb_hid_set_defaults(void) {
123158
hid_boot_device = 0;
159+
hid_boot_device_requested = false;
124160
common_hal_usb_hid_enable(
125161
CIRCUITPY_USB_HID_ENABLED_DEFAULT ? &default_hid_devices_tuple : mp_const_empty_tuple, 0);
126162
}
@@ -209,7 +245,17 @@ bool common_hal_usb_hid_enable(const mp_obj_t devices, uint8_t boot_device) {
209245

210246
// Called when HID devices are ready to be used, when code.py or the REPL starts running.
211247
void usb_hid_setup_devices(void) {
212-
hid_boot_device_requested = false;
248+
249+
// If the host requested a boot device, replace the current list of devices
250+
// with a single-element tuple containing the proper boot device.
251+
if (hid_boot_device_requested) {
252+
memcpy(&hid_devices[0],
253+
// Will be 1 (keyboard) or 2 (mouse).
254+
hid_boot_device == 1 ? &boot_keyboard_obj : &boot_mouse_obj,
255+
sizeof(usb_hid_device_obj_t));
256+
num_hid_devices = 1;
257+
}
258+
213259
usb_hid_set_devices_from_hid_devices();
214260

215261
// Create report buffers on the heap.

0 commit comments

Comments
 (0)