circuitpython/supervisor/shared/usb/usb_desc.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

366 lines
14 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 hathach for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2019-07-24 05:46:31 -04:00
#include "lib/tinyusb/src/tusb.h"
2021-04-27 14:37:36 -04:00
#include "py/objstr.h"
#include "py/runtime.h"
2021-04-28 13:00:44 -04:00
#include "supervisor/memory.h"
2021-05-05 12:35:12 -04:00
#include "supervisor/shared/safe_mode.h"
2021-04-26 23:54:01 -04:00
#include "supervisor/usb.h"
#if CIRCUITPY_USB_CDC
2021-04-26 23:54:01 -04:00
#include "shared-bindings/usb_cdc/__init__.h"
#endif
#if CIRCUITPY_USB_HID
2021-04-26 23:54:01 -04:00
#include "shared-bindings/usb_hid/__init__.h"
#endif
#if CIRCUITPY_USB_MIDI
2021-04-26 23:54:01 -04:00
#include "shared-bindings/usb_midi/__init__.h"
#endif
2021-04-26 23:54:01 -04:00
#if CIRCUITPY_USB_MSC && CIRCUITPY_STORAGE
#include "shared-bindings/storage/__init__.h"
#endif
2021-04-27 14:37:36 -04:00
#include "shared-bindings/microcontroller/Processor.h"
2021-04-20 22:20:34 -04:00
2021-04-21 23:25:36 -04:00
// Table for collecting interface strings (interface names) as descriptor is built.
2021-04-28 13:00:44 -04:00
// We reuse the same table after collection, replacing the char string pointers with le16 string pointers.
2021-04-21 23:25:36 -04:00
#define MAX_INTERFACE_STRINGS 16
2021-04-27 23:53:23 -04:00
// slot 0 is always the Language ID
2021-04-28 13:00:44 -04:00
typedef union {
const char *char_str;
const uint16_t *descriptor;
} interface_string_t;
static interface_string_t collected_interface_strings[MAX_INTERFACE_STRINGS];
static size_t collected_interface_strings_length;
2021-04-27 14:37:36 -04:00
static uint8_t current_interface_string;
2021-04-21 23:25:36 -04:00
static supervisor_allocation *device_descriptor_allocation;
static supervisor_allocation *configuration_descriptor_allocation;
static supervisor_allocation *string_descriptors_allocation;
2021-04-28 13:00:44 -04:00
2021-04-27 14:37:36 -04:00
static const char manufacturer_name[] = USB_MANUFACTURER;
static const char product_name[] = USB_PRODUCT;
2021-04-21 23:25:36 -04:00
// Serial number string is UID length * 2 (2 nibbles per byte) + 1 byte for null termination.
static char serial_number_hex_string[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH * 2 + 1];
2021-04-28 13:00:44 -04:00
2021-04-20 22:20:34 -04:00
static const uint8_t device_descriptor_template[] = {
0x12, // 0 bLength
0x01, // 1 bDescriptorType (Device)
0x00, 0x02, // 2,3 bcdUSB 2.00
0x00, // 4 bDeviceClass (Use class information in the Interface Descriptors)
0x00, // 5 bDeviceSubClass
0x00, // 6 bDeviceProtocol
0x40, // 7 bMaxPacketSize0 64
2021-04-26 23:54:01 -04:00
0xFF, 0xFF, // 8,9 idVendor [SET AT RUNTIME: lo,hi]
2021-04-23 00:18:05 -04:00
#define DEVICE_VID_LO_INDEX (8)
#define DEVICE_VID_HI_INDEX (9)
2021-04-26 23:54:01 -04:00
0xFF, 0xFF, // 10,11 idProduct [SET AT RUNTIME: lo,hi]
#define DEVICE_PID_LO_INDEX (10)
#define DEVICE_PID_HI_INDEX (11)
2021-04-20 22:20:34 -04:00
0x00, 0x01, // 12,13 bcdDevice 2.00
2021-04-27 23:53:23 -04:00
0xFF, // 14 iManufacturer (String Index) [SET AT RUNTIME]
2021-04-23 00:18:05 -04:00
#define DEVICE_MANUFACTURER_STRING_INDEX (14)
2021-04-27 23:53:23 -04:00
0xFF, // 15 iProduct (String Index) [SET AT RUNTIME]
2021-04-23 00:18:05 -04:00
#define DEVICE_PRODUCT_STRING_INDEX (15)
2021-04-27 23:53:23 -04:00
0xFF, // 16 iSerialNumber (String Index) [SET AT RUNTIME]
2021-04-23 00:18:05 -04:00
#define DEVICE_SERIAL_NUMBER_STRING_INDEX (16)
2021-04-20 22:20:34 -04:00
0x01, // 17 bNumConfigurations 1
};
static const uint8_t configuration_descriptor_template[] = {
0x09, // 0 bLength
0x02, // 1 bDescriptorType (Configuration)
0xFF, 0xFF, // 2,3 wTotalLength [SET AT RUNTIME: lo, hi]
2021-04-23 00:18:05 -04:00
#define CONFIG_TOTAL_LENGTH_LO_INDEX (2)
#define CONFIG_TOTAL_LENGTH_HI_INDEX (3)
2021-04-20 22:20:34 -04:00
0xFF, // 4 bNumInterfaces [SET AT RUNTIME]
2021-04-23 00:18:05 -04:00
#define CONFIG_NUM_INTERFACES_INDEX (4)
2021-04-20 22:20:34 -04:00
0x01, // 5 bConfigurationValue
0x00, // 6 iConfiguration (String Index)
0x80, // 7 bmAttributes
0x32, // 8 bMaxPower 100mA
};
2021-04-26 23:54:01 -04:00
static void usb_build_device_descriptor(uint16_t vid, uint16_t pid) {
2021-04-28 13:00:44 -04:00
device_descriptor_allocation =
allocate_memory(align32_size(sizeof(device_descriptor_template)),
2021-05-03 20:59:50 -04:00
/*high_address*/ false, /*movable*/ false);
uint8_t *device_descriptor = (uint8_t *)device_descriptor_allocation->ptr;
2021-04-20 22:20:34 -04:00
memcpy(device_descriptor, device_descriptor_template, sizeof(device_descriptor_template));
device_descriptor[DEVICE_VID_LO_INDEX] = vid & 0xFF;
device_descriptor[DEVICE_VID_HI_INDEX] = vid >> 8;
device_descriptor[DEVICE_PID_LO_INDEX] = pid & 0xFF;
device_descriptor[DEVICE_PID_HI_INDEX] = pid >> 8;
2021-04-21 23:25:36 -04:00
2021-04-27 14:37:36 -04:00
usb_add_interface_string(current_interface_string, manufacturer_name);
device_descriptor[DEVICE_MANUFACTURER_STRING_INDEX] = current_interface_string;
current_interface_string++;
2021-04-21 23:25:36 -04:00
2021-04-27 14:37:36 -04:00
usb_add_interface_string(current_interface_string, product_name);
device_descriptor[DEVICE_PRODUCT_STRING_INDEX] = current_interface_string;
current_interface_string++;
2021-04-21 23:25:36 -04:00
2021-04-27 14:37:36 -04:00
usb_add_interface_string(current_interface_string, serial_number_hex_string);
device_descriptor[DEVICE_SERIAL_NUMBER_STRING_INDEX] = current_interface_string;
current_interface_string++;
2019-07-24 05:46:31 -04:00
}
2021-04-27 14:37:36 -04:00
static void usb_build_configuration_descriptor(void) {
2021-04-20 22:20:34 -04:00
size_t total_descriptor_length = sizeof(configuration_descriptor_template);
// CDC should be first, for compatibility with Adafruit Windows 7 drivers.
2021-04-27 14:37:36 -04:00
// In the past, the order has been CDC, MSC, MIDI, HID, so preserve that order.
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_CDC
if (usb_cdc_console_enabled()) {
2021-04-20 22:20:34 -04:00
total_descriptor_length += usb_cdc_descriptor_length();
}
2021-04-27 14:37:36 -04:00
if (usb_cdc_data_enabled()) {
2021-04-20 22:20:34 -04:00
total_descriptor_length += usb_cdc_descriptor_length();
}
2021-05-03 20:59:50 -04:00
#endif
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_MSC
2021-04-27 14:37:36 -04:00
if (storage_usb_enabled()) {
2021-04-20 22:20:34 -04:00
total_descriptor_length += storage_usb_descriptor_length();
}
2021-05-03 20:59:50 -04:00
#endif
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_HID
2021-04-27 14:37:36 -04:00
if (usb_hid_enabled()) {
2021-04-20 22:20:34 -04:00
total_descriptor_length += usb_hid_descriptor_length();
}
2021-05-03 20:59:50 -04:00
#endif
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_MIDI
2021-04-27 23:53:23 -04:00
if (usb_midi_enabled()) {
total_descriptor_length += usb_midi_descriptor_length();
}
2021-05-03 20:59:50 -04:00
#endif
2021-04-27 23:53:23 -04:00
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#if CIRCUITPY_USB_VENDOR
if (usb_vendor_enabled()) {
total_descriptor_length += usb_vendor_descriptor_length();
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
}
#endif
2021-04-28 13:00:44 -04:00
// Now we now how big the configuration descriptor will be, so we can allocate space for it.
configuration_descriptor_allocation =
allocate_memory(align32_size(total_descriptor_length),
2021-05-03 20:59:50 -04:00
/*high_address*/ false, /*movable*/ false);
uint8_t *configuration_descriptor = (uint8_t *)configuration_descriptor_allocation->ptr;
2021-04-28 13:00:44 -04:00
2021-04-27 23:53:23 -04:00
// Copy the template, which is the first part of the descriptor, and fix up its length.
2021-04-28 13:00:44 -04:00
2021-04-27 23:53:23 -04:00
memcpy(configuration_descriptor, configuration_descriptor_template, sizeof(configuration_descriptor_template));
2021-04-20 22:20:34 -04:00
configuration_descriptor[CONFIG_TOTAL_LENGTH_LO_INDEX] = total_descriptor_length & 0xFF;
configuration_descriptor[CONFIG_TOTAL_LENGTH_HI_INDEX] = (total_descriptor_length >> 8) & 0xFF;
// Number interfaces and endpoints.
// Endpoint 0 is already used for USB control,
// so start with 1 for the current endpoint and for the number of in and out endpoints
// already in use.
descriptor_counts_t descriptor_counts = {
.current_interface = 0,
.current_endpoint = 1,
.num_in_endpoints = 1,
.num_out_endpoints = 1,
};
2021-04-20 22:20:34 -04:00
uint8_t *descriptor_buf_remaining = configuration_descriptor + sizeof(configuration_descriptor_template);
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_CDC
if (usb_cdc_console_enabled()) {
// Concatenate and fix up the CDC REPL descriptor.
2021-04-21 23:25:36 -04:00
descriptor_buf_remaining += usb_cdc_add_descriptor(
descriptor_buf_remaining, &descriptor_counts, &current_interface_string, true /*console*/);
}
2021-04-27 14:37:36 -04:00
if (usb_cdc_data_enabled()) {
// Concatenate and fix up the CDC data descriptor.
2021-04-21 23:25:36 -04:00
descriptor_buf_remaining += usb_cdc_add_descriptor(
descriptor_buf_remaining, &descriptor_counts, &current_interface_string, false /*console*/);
}
2021-05-03 20:59:50 -04:00
#endif
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_MSC
2021-04-27 14:37:36 -04:00
if (storage_usb_enabled()) {
// Concatenate and fix up the MSC descriptor.
2021-04-20 22:20:34 -04:00
descriptor_buf_remaining += storage_usb_add_descriptor(
descriptor_buf_remaining, &descriptor_counts, &current_interface_string);
}
2021-05-03 20:59:50 -04:00
#endif
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_HID
2021-04-27 14:37:36 -04:00
if (usb_hid_enabled()) {
if (usb_hid_boot_device() > 0 && descriptor_counts.current_interface > 0) {
// Hosts using boot devices generally to expect them to be at interface zero,
// and will not work properly otherwise.
reset_into_safe_mode(USB_BOOT_DEVICE_NOT_INTERFACE_ZERO);
}
2021-04-21 23:25:36 -04:00
descriptor_buf_remaining += usb_hid_add_descriptor(
descriptor_buf_remaining, &descriptor_counts, &current_interface_string,
usb_hid_report_descriptor_length(), usb_hid_boot_device());
}
2021-05-03 20:59:50 -04:00
#endif
2021-05-03 20:59:50 -04:00
#if CIRCUITPY_USB_MIDI
2021-04-27 23:53:23 -04:00
if (usb_midi_enabled()) {
// Concatenate and fix up the MIDI descriptor.
descriptor_buf_remaining += usb_midi_add_descriptor(
descriptor_buf_remaining, &descriptor_counts, &current_interface_string);
2021-04-27 23:53:23 -04:00
}
2021-05-03 20:59:50 -04:00
#endif
2021-04-27 23:53:23 -04:00
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#if CIRCUITPY_USB_VENDOR
if (usb_vendor_enabled()) {
descriptor_buf_remaining += usb_vendor_add_descriptor(
descriptor_buf_remaining, &descriptor_counts, &current_interface_string);
}
#endif
2021-04-20 22:20:34 -04:00
// Now we know how many interfaces have been used.
configuration_descriptor[CONFIG_NUM_INTERFACES_INDEX] = descriptor_counts.current_interface;
2021-04-20 22:20:34 -04:00
// Did we run out of endpoints?
if (descriptor_counts.current_endpoint > USB_NUM_ENDPOINT_PAIRS ||
descriptor_counts.num_in_endpoints > USB_NUM_IN_ENDPOINTS ||
descriptor_counts.num_out_endpoints > USB_NUM_OUT_ENDPOINTS) {
2021-05-05 12:35:12 -04:00
reset_into_safe_mode(USB_TOO_MANY_ENDPOINTS);
2021-04-20 22:20:34 -04:00
}
}
2021-04-28 13:00:44 -04:00
// str must not be on the heap.
2021-04-27 14:37:36 -04:00
void usb_add_interface_string(uint8_t interface_string_index, const char str[]) {
2021-04-21 23:25:36 -04:00
if (interface_string_index > MAX_INTERFACE_STRINGS) {
2021-05-05 12:35:12 -04:00
reset_into_safe_mode(USB_TOO_MANY_INTERFACE_NAMES);
2021-04-21 23:25:36 -04:00
}
2021-04-28 13:00:44 -04:00
collected_interface_strings[interface_string_index].char_str = str;
collected_interface_strings_length += strlen(str);
2021-04-21 23:25:36 -04:00
}
static const uint16_t language_id[] = {
0x0304,
0x0409,
};
2021-04-28 13:00:44 -04:00
static void usb_build_interface_string_table(void) {
// Allocate space for the le16 String descriptors.
// Space needed is 2 bytes for String Descriptor header, then 2 bytes for each character
string_descriptors_allocation =
allocate_memory(align32_size(current_interface_string * 2 + collected_interface_strings_length * 2),
2021-05-03 20:59:50 -04:00
/*high_address*/ false, /*movable*/ false);
uint16_t *string_descriptors = (uint16_t *)string_descriptors_allocation->ptr;
2021-04-21 23:25:36 -04:00
2021-04-25 23:17:41 -04:00
2021-04-28 13:00:44 -04:00
uint16_t *string_descriptor = string_descriptors;
2021-04-27 23:53:23 -04:00
// Language ID is always the 0th string descriptor.
collected_interface_strings[0].descriptor = language_id;
2021-04-27 23:53:23 -04:00
2021-04-28 13:00:44 -04:00
// Build the le16 versions of all the descriptor strings.
// Start at 1 to skip the Language ID.
for (uint8_t string_index = 1; string_index < current_interface_string; string_index++) {
const char *str = collected_interface_strings[string_index].char_str;
const size_t str_len = strlen(str);
// 1 word for descriptor type and length, 1 word for each character.
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;
2021-04-28 13:00:44 -04:00
// Convert to le16.
for (size_t i = 0; i < str_len; i++) {
2021-04-28 13:00:44 -04:00
string_descriptor[i + 1] = str[i];
}
// Save ptr to string descriptor with le16 str.
collected_interface_strings[string_index].descriptor = string_descriptor;
// Move to next descriptor slot.
string_descriptor += descriptor_size_words;
2021-04-28 13:00:44 -04:00
}
}
// After boot.py runs, the USB devices to be used have been chosen, and the descriptors can be set up.
2021-04-28 23:48:26 -04:00
// This is called after the VM is finished, because it uses storage_allocations.
void usb_build_descriptors(void) {
2021-04-25 23:17:41 -04:00
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
common_hal_mcu_processor_get_uid(raw_id);
for (int i = 0; i < COMMON_HAL_MCU_PROCESSOR_UID_LENGTH; i++) {
for (int j = 0; j < 2; j++) {
uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf;
serial_number_hex_string[i * 2 + (1 - j)] = nibble_to_hex_upper[nibble];
}
}
// Null-terminate the string.
2021-04-27 23:53:23 -04:00
serial_number_hex_string[sizeof(serial_number_hex_string) - 1] = '\0';
2021-04-25 23:17:41 -04:00
current_interface_string = 1;
2021-04-28 13:00:44 -04:00
collected_interface_strings_length = 0;
2021-04-25 23:17:41 -04:00
2021-04-26 23:54:01 -04:00
usb_build_device_descriptor(USB_VID, USB_PID);
2021-04-25 23:17:41 -04:00
usb_build_configuration_descriptor();
2021-04-28 13:00:44 -04:00
usb_build_interface_string_table();
2021-04-25 23:17:41 -04:00
}
2021-04-21 23:25:36 -04:00
// Invoked when GET DEVICE DESCRIPTOR is received.
2021-04-20 22:20:34 -04:00
// Application return pointer to descriptor
uint8_t const *tud_descriptor_device_cb(void) {
2021-05-03 20:59:50 -04:00
return (uint8_t *)device_descriptor_allocation->ptr;
2021-04-20 22:20:34 -04:00
}
2021-04-21 23:25:36 -04:00
// Invoked when GET CONFIGURATION DESCRIPTOR is received.
2021-04-20 22:20:34 -04:00
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
(void)index; // for multiple configurations
2021-05-03 20:59:50 -04:00
return (uint8_t *)configuration_descriptor_allocation->ptr;
2019-07-24 05:46:31 -04:00
}
2021-04-21 23:25:36 -04:00
// Invoked when GET STRING DESCRIPTOR request is received.
2019-07-24 05:46:31 -04:00
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
2021-03-15 09:57:36 -04:00
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
2021-04-21 23:25:36 -04:00
if (index > MAX_INTERFACE_STRINGS) {
return NULL;
}
2021-04-28 13:00:44 -04:00
return collected_interface_strings[index].descriptor;
2019-07-24 05:46:31 -04:00
}