From 1f5d06e4f8a20c77269fb8d063a7a5bdad94ade2 Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Sun, 4 Apr 2021 06:29:38 +0200 Subject: [PATCH] [tinyusb] Add better customization options --- ext/hathach/module.lb | 141 ++++++++++++++++++------------ ext/hathach/module.md | 28 ++++++ ext/hathach/tusb_config.h.in | 8 +- ext/hathach/tusb_descriptors.c.in | 12 ++- 4 files changed, 128 insertions(+), 61 deletions(-) diff --git a/ext/hathach/module.lb b/ext/hathach/module.lb index e16e3ffbd4..ef76b769a8 100644 --- a/ext/hathach/module.lb +++ b/ext/hathach/module.lb @@ -181,7 +181,14 @@ def build(env): class TinyUsbDeviceModule(Module): def init(self, module): module.name = ":tinyusb:device" - module.description = "TinyUSB in Device Mode" + module.description = """ +# TinyUSB in Device Mode + +Configuration options: + +- `CFG_TUD_ENDPOINT0_SIZE` = 64 +- `CFG_TUD_TASK_QUEUE_SZ` = 16 +""" def prepare(self, module, options): paths = {p.parent for p in Path(localpath("tinyusb/src/class/")).glob("*/*_device.h")} @@ -193,11 +200,6 @@ class TinyUsbDeviceModule(Module): env.outbasepath = "modm/ext/tinyusb" env.copy("tinyusb/src/device/", "device/") - """ Device config defaults: - - CFG_TUD_ENDPOINT0_SIZE = 64 - - CFG_TUD_TASK_QUEUE_SZ = 16 - """ - # ----------------------------------------------------------------------------- class TinyUsbHostModule(Module): @@ -206,7 +208,14 @@ class TinyUsbHostModule(Module): def init(self, module): module.name = ":tinyusb:host" - module.description = "TinyUSB in Host Mode" + module.description = """ +# TinyUSB in Host Mode + +Configuration options: + +- `CFG_TUH_EP_MAX` = 9 +- `CFG_TUH_TASK_QUEUE_SZ` = 16 +""" def prepare(self, module, options): # Only OTG has Host Mode @@ -223,12 +232,68 @@ class TinyUsbHostModule(Module): env.outbasepath = "modm/ext/tinyusb" env.copy("tinyusb/src/host/", "host/") - """ Host config defaults: - - CFG_TUH_TASK_QUEUE_SZ = 16 - """ - # ----------------------------------------------------------------------------- +tu_config_descr = {"device": { + "audio": """ +- `CFG_TUD_AUDIO_CTRL_BUF_SIZE` = [undef] +- `CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP` = 0 +- `CFG_TUD_AUDIO_EPSIZE_IN` = 0 +- `CFG_TUD_AUDIO_EPSIZE_OUT` = 0 +- `CFG_TUD_AUDIO_FORMAT_TYPE_I_RX` = `AUDIO_DATA_FORMAT_TYPE_I_PCM` +- `CFG_TUD_AUDIO_FORMAT_TYPE_I_TX` = `AUDIO_DATA_FORMAT_TYPE_I_PCM` +- `CFG_TUD_AUDIO_FORMAT_TYPE_RX` = `AUDIO_FORMAT_TYPE_UNDEFINED` +- `CFG_TUD_AUDIO_FORMAT_TYPE_TX` = `AUDIO_FORMAT_TYPE_UNDEFINED` +- `CFG_TUD_AUDIO_INT_CTR_BUFSIZE` = 6 +- `CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN` = 0 +- `CFG_TUD_AUDIO_N_AS_INT` = 0 +- `CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_RX` = 1 +- `CFG_TUD_AUDIO_N_BYTES_PER_SAMPLE_TX` = 1 +- `CFG_TUD_AUDIO_N_CHANNELS_RX` = 1 +- `CFG_TUD_AUDIO_N_CHANNELS_TX` = 1 +- `CFG_TUD_AUDIO_RX_FIFO_COUNT` = `CFG_TUD_AUDIO_N_CHANNELS_RX` +- `CFG_TUD_AUDIO_RX_FIFO_SIZE` = 0 +- `CFG_TUD_AUDIO_TX_FIFO_COUNT` = `CFG_TUD_AUDIO_N_CHANNELS_TX` +- `CFG_TUD_AUDIO_TX_FIFO_SIZE` = 0 +""", + "bth": """ +- `CFG_TUD_BTH_DATA_EPSIZE` = 64 +- `CFG_TUD_BTH_EVENT_EPSIZE` = 16 +- `CFG_TUD_BTH_ISO_ALT_COUNT` = 0 +""", + "cdc": """ +- `CFG_TUD_CDC_EP_BUFSIZE` = 64/512 (fs/hs) +- `CFG_TUD_CDC_RX_BUFSIZE` = [undef] modm default: 512 +- `CFG_TUD_CDC_TX_BUFSIZE` = [undef] modm default: 512 +""", + "hid": """ +- `CFG_TUD_HID_EP_BUFSIZE` = 16 +""", + "midi": """ +- `CFG_TUD_MIDI_EP_BUFSIZE` = 64/512 (fs/hs) +- `CFG_TUD_MIDI_RX_BUFSIZE` = [undef] modm default: 64/512 (fs/hs) +- `CFG_TUD_MIDI_TX_BUFSIZE` = [undef] modm default: 64/512 (fs/hs) +""", + "msc": """ +- `CFG_TUD_MSC_EP_BUFSIZE` = [undef] modm default: 512 +""", + "net": """ +- `CFG_TUD_NET_ENDPOINT_SIZE` = 64/512 (fs/hs) +- `CFG_TUD_NET_MTU` = 1514 +""", + "usbtmc": """ +- `CFG_TUD_USBTMC_ENABLE_488` = 1 +""", + "vendor": """ +- `CFG_TUD_VENDOR_EPSIZE` = 64 +- `CFG_TUD_VENDOR_RX_BUFSIZE` = [undef] modm default: 64/512 (fs/hs) +- `CFG_TUD_VENDOR_TX_BUFSIZE` = [undef] modm default: 64/512 (fs/hs) +""" +}, "host": { + "msc": """ +- `CFG_TUH_MSC_MAXLUN` = 4 +""" +}} class TinyUsbClassModule(Module): def __init__(self, path, mode): self.mode = mode @@ -236,20 +301,17 @@ class TinyUsbClassModule(Module): def init(self, module): module.name = ":tinyusb:{}:{}".format(self.mode, self.name) - module.description = "{} class {}".format(self.mode.capitalize(), self.name.upper()) + descr = "# {} class {}".format(self.mode.capitalize(), self.name.upper()) + conf = tu_config_descr[self.mode].get(self.name, "") + if conf: descr += "\n\nConfiguration options:\n" + conf + module.description = descr def prepare(self, module, options): if self.mode == "device": - # FIXME: Changing these defaults seems to crash TinyUSB in weird ways. - # Since all of the TinyUSB examples leave them at these default values - # I think they are not supposed to be changed? -Niklas - # if self.name in ["cdc", "midi", "vendor"]: - # module.add_option(NumericOption("buffer.rx", description="", minimum=64, default=64)) - # module.add_option(NumericOption("buffer.tx", description="", minimum=64, default=64)) - # if self.name in ["msc"]: - # module.add_option(NumericOption("buffer.ep", description="", minimum=512, default=512)) if self.name == "cdc": module.depends(":architecture:uart") + if self.name == "midi": + module.depends(":tinyusb:device:audio") module.depends(":tinyusb") return True @@ -267,40 +329,7 @@ class TinyUsbClassModule(Module): if self.mode == "device": # These are the defaults that don't crash TinyUSB. if self.name in ["cdc", "midi", "vendor"]: - tusb_config[cfg_name+"_RX_BUFSIZE"] = 512 if speed else 64 #env["buffer.rx"] - tusb_config[cfg_name+"_TX_BUFSIZE"] = 512 if speed else 64 #env["buffer.tx"] + tusb_config[cfg_name+"_RX_BUFSIZE"] = 512 if speed else 64 + tusb_config[cfg_name+"_TX_BUFSIZE"] = 512 if speed else 64 if self.name in ["msc"]: - tusb_config[cfg_name+"_EP_BUFSIZE"] = 512 #env["buffer.ep"] - - - """ Device class defaults: - - CFG_TUD_BTH_DATA_EPSIZE = 64 - - CFG_TUD_BTH_EVENT_EPSIZE = 16 - - CFG_TUD_BTH_ISO_ALT_COUNT = 0 - - - CFG_TUD_CDC_EP_BUFSIZE = 64 or 512 (hs) - - CFG_TUD_CDC_RX_BUFSIZE = [undef] - - CFG_TUD_CDC_TX_BUFSIZE = [undef] - - - CFG_TUD_HID_EP_BUFSIZE = 16 - - - CFG_TUD_MIDI_EP_BUFSIZE = 64 or 512 (hs) - - CFG_TUD_MIDI_RX_BUFSIZE = [undef] - - CFG_TUD_MIDI_TX_BUFSIZE = [undef] - - - CFG_TUD_MSC_EP_BUFSIZE = [undef] - - - CFG_TUD_NET_ENDPOINT_SIZE = 64 or 512 (hs) - - - CFG_TUD_USBTMC_ENABLE_488 = 1 - - - CFG_TUD_VENDOR_EPSIZE = 64 - - CFG_TUD_VENDOR_RX_BUFSIZE = [undef] - - CFG_TUD_VENDOR_TX_BUFSIZE = [undef] - """ - - """ Host config defaults: - - CFG_TUH_CDC_RNDIS - - CFG_TUH_HID_KEYBOARD - - CFG_TUH_HID_MOUSE - """ + tusb_config[cfg_name+"_EP_BUFSIZE"] = 512 diff --git a/ext/hathach/module.md b/ext/hathach/module.md index 12244165f4..0e96cb4a78 100644 --- a/ext/hathach/module.md +++ b/ext/hathach/module.md @@ -35,6 +35,30 @@ modm will generate the USB descriptors automatically for the set of device classes you've chosen. You can then implement your app via TinyUSB callbacks. +### Partial Customization + +You can overwrite or add configurations via a `` file, +which will be included at the very *beginning* of the modm-generated +`tusb_config.h` file: + +```c +// Overwrite the modm default +#define CFG_TUD_CDC_TX_BUFSIZE 1024 +// Overwrite the TinyUSB default +#define CFG_TUD_CDC_EP_BUFSIZE 1024 +``` + +You can also replace the following weakly linked descriptor functions and +objects in case you want to update only a small part of the autogenerated +descriptors: + +- `const uint8_t* tud_descriptor_device_cb(void)` to replace the autogenerated + `tusb_desc_device_t` descriptor. +- `const uint8_t* tud_descriptor_configuration_cb(uint8_t index)` to replace + the endpoint descriptions. +- `const char* string_desc_arr[]` to replace the string descriptors. + + ## Manual USB Descriptors If you leave the `modm:tinyusb:config` option empty, no descriptors are @@ -49,6 +73,10 @@ manually depend on the device classes you want to implement: modm:tinyusb:device:usbtmc ``` +Some of these classes require a lot of configuration that you must provide via +the `` file. See the class module descriptions for these +configurations and consult the TinyUSB documentation for their purpose. + ## Initializing USB diff --git a/ext/hathach/tusb_config.h.in b/ext/hathach/tusb_config.h.in index 8f07eb8ac6..c94ff4829c 100644 --- a/ext/hathach/tusb_config.h.in +++ b/ext/hathach/tusb_config.h.in @@ -13,8 +13,14 @@ #include +#if __has_include() +# include +#endif + %% for name, value in config.items() | sort -#define {{name}} {{value}} +#ifndef {{name}} +# define {{name}} {{value}} +#endif %% endfor diff --git a/ext/hathach/tusb_descriptors.c.in b/ext/hathach/tusb_descriptors.c.in index 43994e6196..4cb6e42efc 100644 --- a/ext/hathach/tusb_descriptors.c.in +++ b/ext/hathach/tusb_descriptors.c.in @@ -73,7 +73,8 @@ const tusb_desc_device_t desc_device = // Invoked when received GET DEVICE DESCRIPTOR // Application return pointer to descriptor -uint8_t const * tud_descriptor_device_cb(void) +modm_weak const uint8_t* +tud_descriptor_device_cb(void) { return (uint8_t const *) &desc_device; } @@ -133,7 +134,8 @@ const uint8_t desc_{{"hs" if hs else "fs"}}_configuration[] = // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete -const uint8_t* tud_descriptor_configuration_cb(uint8_t index) +modm_weak const uint8_t* +tud_descriptor_configuration_cb(uint8_t index) { (void)index; // for multiple configurations %% if port @@ -149,7 +151,8 @@ const uint8_t* tud_descriptor_configuration_cb(uint8_t index) //--------------------------------------------------------------------+ // array of pointer to string descriptors -const char* string_desc_arr[] = +modm_weak const char* +string_desc_arr[] = { NULL, // 0: Language "TinyUSB", // 1: Manufacturer @@ -165,7 +168,8 @@ static uint16_t _desc_str[32]; // Invoked when received GET STRING DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long // enough for transfer to complete -const uint16_t* tud_descriptor_string_cb(uint8_t index, uint16_t langid) +const uint16_t* +tud_descriptor_string_cb(uint8_t index, uint16_t langid) { (void)langid; uint8_t chr_count = 1;