Merge pull request #4734 from dhalbert/dynamic-usb-fixes

fix HID; fix interface name table creation
This commit is contained in:
Scott Shawcroft 2021-05-10 12:55:29 -07:00 committed by GitHub
commit 4eb4f14840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 14 deletions

View File

@ -16,6 +16,7 @@ CIRCUITPY_FULL_BUILD = 0
CIRCUITPY_ANALOGIO = 0 CIRCUITPY_ANALOGIO = 0
CIRCUITPY_MATH = 0 CIRCUITPY_MATH = 0
CIRCUITPY_NEOPIXEL_WRITE = 0 CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_PULSEIO = 1
CIRCUITPY_ROTARYIO = 0 CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0 CIRCUITPY_RTC = 0
CIRCUITPY_USB_MIDI = 1 CIRCUITPY_USB_MIDI = 1

View File

@ -97,7 +97,7 @@ static mp_obj_tuple_t default_hid_devices_tuple = {
}; };
bool usb_hid_enabled(void) { bool usb_hid_enabled(void) {
return hid_devices_num == 0; return hid_devices_num > 0;
} }
void usb_hid_set_defaults(void) { void usb_hid_set_defaults(void) {
@ -119,7 +119,7 @@ size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interfac
descriptor_buf[HID_DESCRIPTOR_INTERFACE_INDEX] = *current_interface; descriptor_buf[HID_DESCRIPTOR_INTERFACE_INDEX] = *current_interface;
(*current_interface)++; (*current_interface)++;
usb_add_interface_string(*current_interface, usb_hid_interface_name); usb_add_interface_string(*current_interface_string, usb_hid_interface_name);
descriptor_buf[HID_DESCRIPTOR_INTERFACE_STRING_INDEX] = *current_interface_string; descriptor_buf[HID_DESCRIPTOR_INTERFACE_STRING_INDEX] = *current_interface_string;
(*current_interface_string)++; (*current_interface_string)++;

View File

@ -191,19 +191,19 @@ size_t usb_midi_add_descriptor(uint8_t *descriptor_buf, uint8_t *current_interfa
descriptor_buf[MIDI_STREAMING_INTERFACE_NUMBER_INDEX_2] = *current_interface; descriptor_buf[MIDI_STREAMING_INTERFACE_NUMBER_INDEX_2] = *current_interface;
(*current_interface)++; (*current_interface)++;
usb_add_interface_string(*current_interface, midi_streaming_interface_name); usb_add_interface_string(*current_interface_string, midi_streaming_interface_name);
descriptor_buf[MIDI_STREAMING_INTERFACE_STRING_INDEX] = *current_interface; descriptor_buf[MIDI_STREAMING_INTERFACE_STRING_INDEX] = *current_interface;
(*current_interface_string)++; (*current_interface_string)++;
usb_add_interface_string(*current_interface, midi_audio_control_interface_name); usb_add_interface_string(*current_interface_string, midi_audio_control_interface_name);
descriptor_buf[MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX] = *current_interface; descriptor_buf[MIDI_AUDIO_CONTROL_INTERFACE_STRING_INDEX] = *current_interface;
(*current_interface_string)++; (*current_interface_string)++;
usb_add_interface_string(*current_interface, midi_in_jack_name); usb_add_interface_string(*current_interface_string, midi_in_jack_name);
descriptor_buf[MIDI_IN_JACK_STRING_INDEX] = *current_interface; descriptor_buf[MIDI_IN_JACK_STRING_INDEX] = *current_interface;
(*current_interface_string)++; (*current_interface_string)++;
usb_add_interface_string(*current_interface, midi_out_jack_name); usb_add_interface_string(*current_interface_string, midi_out_jack_name);
descriptor_buf[MIDI_OUT_JACK_STRING_INDEX] = *current_interface; descriptor_buf[MIDI_OUT_JACK_STRING_INDEX] = *current_interface;
(*current_interface_string)++; (*current_interface_string)++;

View File

@ -247,6 +247,11 @@ void usb_add_interface_string(uint8_t interface_string_index, const char str[])
collected_interface_strings_length += strlen(str); collected_interface_strings_length += strlen(str);
} }
static const uint16_t language_id[] = {
0x0304,
0x0409,
};
static void usb_build_interface_string_table(void) { static void usb_build_interface_string_table(void) {
// Allocate space for the le16 String descriptors. // Allocate space for the le16 String descriptors.
// Space needed is 2 bytes for String Descriptor header, then 2 bytes for each character // Space needed is 2 bytes for String Descriptor header, then 2 bytes for each character
@ -259,21 +264,20 @@ static void usb_build_interface_string_table(void) {
uint16_t *string_descriptor = string_descriptors; uint16_t *string_descriptor = string_descriptors;
// Language ID is always the 0th string descriptor. // Language ID is always the 0th string descriptor.
collected_interface_strings[0].descriptor = (uint16_t[]) { collected_interface_strings[0].descriptor = language_id;
0x0304,
0x0409,
};
// Build the le16 versions of all the descriptor strings. // Build the le16 versions of all the descriptor strings.
// Start at 1 to skip the Language ID. // Start at 1 to skip the Language ID.
for (uint8_t string_index = 1; string_index < current_interface_string; string_index++) { for (uint8_t string_index = 1; string_index < current_interface_string; string_index++) {
const char *str = collected_interface_strings[string_index].char_str; const char *str = collected_interface_strings[string_index].char_str;
const size_t str_len = strlen(str); const size_t str_len = strlen(str);
uint8_t descriptor_size = 2 + (str_len * 2); // 1 word for descriptor type and length, 1 word for each character.
string_descriptor[0] = 0x0300 | descriptor_size; const uint8_t descriptor_size_words = 1 + str_len;
const uint8_t descriptor_size_bytes = descriptor_size_words * 2;
string_descriptor[0] = 0x0300 | descriptor_size_bytes;
// Convert to le16. // Convert to le16.
for (size_t i = 0; i <= str_len; i++) { for (size_t i = 0; i < str_len; i++) {
string_descriptor[i + 1] = str[i]; string_descriptor[i + 1] = str[i];
} }
@ -281,7 +285,7 @@ static void usb_build_interface_string_table(void) {
collected_interface_strings[string_index].descriptor = string_descriptor; collected_interface_strings[string_index].descriptor = string_descriptor;
// Move to next descriptor slot. // Move to next descriptor slot.
string_descriptor += descriptor_size; string_descriptor += descriptor_size_words;
} }
} }