Skip to content

Commit

Permalink
[tinyusb] Add better customization options
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Apr 6, 2021
1 parent acd4168 commit 1f5d06e
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 61 deletions.
141 changes: 85 additions & 56 deletions ext/hathach/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -223,33 +232,86 @@ 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
self.name = str(path.name)

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

Expand All @@ -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
28 changes: 28 additions & 0 deletions ext/hathach/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<tusb_config_local.h>` 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
Expand All @@ -49,6 +73,10 @@ manually depend on the device classes you want to implement:
<module>modm:tinyusb:device:usbtmc</module>
```

Some of these classes require a lot of configuration that you must provide via
the `<tusb_config_local.h>` file. See the class module descriptions for these
configurations and consult the TinyUSB documentation for their purpose.


## Initializing USB

Expand Down
8 changes: 7 additions & 1 deletion ext/hathach/tusb_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

#include <modm/platform/device.hpp>

#if __has_include(<tusb_config_local.h>)
# include <tusb_config_local.h>
#endif

%% for name, value in config.items() | sort
#define {{name}} {{value}}
#ifndef {{name}}
# define {{name}} {{value}}
#endif

%% endfor

Expand Down
12 changes: 8 additions & 4 deletions ext/hathach/tusb_descriptors.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 1f5d06e

Please sign in to comment.