tiny_usb: rename Kconfig name

1. Renamed Kconfig file of tinyusb (distinguish tinyusb stack from usb
   peripheral)
2. bugfix/typofix/doc update of tinyusb
This commit is contained in:
morris
2021-07-09 11:15:26 +08:00
parent 2218204aa7
commit 81448dcae8
25 changed files with 589 additions and 497 deletions

View File

@@ -27,9 +27,9 @@ extern "C" {
/* tinyusb uses buffers with type of uint8_t[] but in our driver we are reading them as a 32-bit word */
#if (CFG_TUD_ENDOINT0_SIZE < 4)
# define CFG_TUD_ENDOINT0_SIZE 4
# warning "CFG_TUD_ENDOINT0_SIZE was too low and was set to 4"
#if (CFG_TUD_ENDPOINT0_SIZE < 4)
# define CFG_TUD_ENDPOINT0_SIZE 4
# warning "CFG_TUD_ENDPOINT0_SIZE was too low and was set to 4"
#endif
#if TUSB_OPT_DEVICE_ENABLED
@@ -72,13 +72,30 @@ extern "C" {
*/
typedef struct {
tusb_desc_device_t *descriptor; /*!< Pointer to a device descriptor */
char **string_descriptor; /*!< Pointer to an array of string descriptors */
const char **string_descriptor; /*!< Pointer to an array of string descriptors */
bool external_phy; /*!< Should USB use an external PHY */
} tinyusb_config_t;
/**
* @brief This is an all-in-one helper function, including:
* 1. USB device driver initialization
* 2. Descriptors preparation
* 3. TinyUSB stack initialization
* 4. Creates and start a task to handle usb events
*
* @note Don't change Custom descriptor, but if it has to be done,
* Suggest to define as follows in order to match the Interface Association Descriptor (IAD):
* bDeviceClass = TUSB_CLASS_MISC,
* bDeviceSubClass = MISC_SUBCLASS_COMMON,
*
* @param config tinyusb stack specific configuration
* @retval ESP_ERR_INVALID_ARG Install driver and tinyusb stack failed because of invalid argument
* @retval ESP_FAIL Install driver and tinyusb stack failed because of internal error
* @retval ESP_OK Install driver and tinyusb stack successfully
*/
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config);
// TODO esp_err_t tinyusb_driver_uninstall(void); (IDF-1474)
// TODO esp_err_t tinyusb_driver_uninstall(void); (IDF-1474)
#ifdef __cplusplus
}

View File

@@ -25,7 +25,7 @@ typedef enum{
TINYUSB_USBDEV_0,
} tinyusb_usbdev_t;
typedef char *tusb_desc_strarray_device_t[USB_STRING_DESCRIPTOR_ARRAY_SIZE];
typedef const char *tusb_desc_strarray_device_t[USB_STRING_DESCRIPTOR_ARRAY_SIZE];
#ifdef __cplusplus
}

View File

@@ -25,6 +25,7 @@
*/
#pragma once
#include "tusb_option.h"
#include "sdkconfig.h"
@@ -32,11 +33,27 @@
extern "C" {
#endif
/* */
/* COMMON CONFIGURATION */
/* */
#ifndef CONFIG_TINYUSB_CDC_ENABLED
# define CONFIG_TINYUSB_CDC_ENABLED 0
#endif
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#ifndef CONFIG_TINYUSB_MSC_ENABLED
# define CONFIG_TINYUSB_MSC_ENABLED 0
#endif
#ifndef CONFIG_TINYUSB_HID_ENABLED
# define CONFIG_TINYUSB_HID_ENABLED 0
#endif
#ifndef CONFIG_TINYUSB_MIDI_ENABLED
# define CONFIG_TINYUSB_MIDI_ENABLED 0
#endif
#ifndef CONFIG_TINYUSB_CUSTOM_CLASS_ENABLED
# define CONFIG_TINYUSB_CUSTOM_CLASS_ENABLED 0
#endif
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED
#define CFG_TUSB_OS OPT_OS_FREERTOS
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
@@ -51,55 +68,29 @@ extern "C" {
#endif
#ifndef CFG_TUSB_MEM_ALIGN
# define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
# define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
#endif
/* */
/* DRIVER CONFIGURATION */
/* */
#define CFG_TUD_MAINTASK_SIZE 4096
#define CFG_TUD_ENDOINT0_SIZE 64
#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif
// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE CONFIG_USB_CDC_RX_BUFSIZE
#define CFG_TUD_CDC_TX_BUFSIZE CONFIG_USB_CDC_TX_BUFSIZE
#define CFG_TUD_CDC_RX_BUFSIZE CONFIG_TINYUSB_CDC_RX_BUFSIZE
#define CFG_TUD_CDC_TX_BUFSIZE CONFIG_TINYUSB_CDC_TX_BUFSIZE
// MSC Buffer size of Device Mass storage:
#define CFG_TUD_MSC_BUFSIZE CONFIG_USB_MSC_BUFSIZE
// MSC Buffer size of Device Mass storage
#define CFG_TUD_MSC_BUFSIZE CONFIG_TINYUSB_MSC_BUFSIZE
// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_BUFSIZE CONFIG_USB_HID_BUFSIZE
#define CFG_TUD_HID_BUFSIZE CONFIG_TINYUSB_HID_BUFSIZE
#define CFG_TUD_CDC CONFIG_USB_CDC_ENABLED
#define CFG_TUD_MSC CONFIG_USB_MSC_ENABLED
#define CFG_TUD_HID CONFIG_USB_HID_ENABLED
#define CFG_TUD_MIDI CONFIG_USB_MIDI_ENABLED
#define CFG_TUD_CUSTOM_CLASS CONFIG_USB_CUSTOM_CLASS_ENABLED
/* */
/* KCONFIG */
/* */
#ifndef CONFIG_USB_CDC_ENABLED
# define CONFIG_USB_CDC_ENABLED 0
#endif
#ifndef CONFIG_USB_MSC_ENABLED
# define CONFIG_USB_MSC_ENABLED 0
#endif
#ifndef CONFIG_USB_HID_ENABLED
# define CONFIG_USB_HID_ENABLED 0
#endif
#ifndef CONFIG_USB_MIDI_ENABLED
# define CONFIG_USB_MIDI_ENABLED 0
#endif
#ifndef CONFIG_USB_CUSTOM_CLASS_ENABLED
# define CONFIG_USB_CUSTOM_CLASS_ENABLED 0
#endif
// Enabled device class driver
#define CFG_TUD_CDC CONFIG_TINYUSB_CDC_ENABLED
#define CFG_TUD_MSC CONFIG_TINYUSB_MSC_ENABLED
#define CFG_TUD_HID CONFIG_TINYUSB_HID_ENABLED
#define CFG_TUD_MIDI CONFIG_TINYUSB_MIDI_ENABLED
#define CFG_TUD_CUSTOM_CLASS CONFIG_TINYUSB_CUSTOM_CLASS_ENABLED
#ifdef __cplusplus
}

View File

@@ -16,26 +16,28 @@
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief This API starts a task with a wrapper function of tud_task and default task parameters.
* @brief This helper function creates and starts a task which wraps `tud_task()`.
*
* The wrapper function basically wraps tud_task and some log. Default parameters: stack size and priority as configured, argument = NULL,
* not pinned to any core.
* The wrapper function basically wraps tud_task and some log.
* Default parameters: stack size and priority as configured, argument = NULL, not pinned to any core.
* If you have more requirements for this task, you can create your own task which calls tud_task as the last step.
*
* @return ESP_OK or ESP_FAIL
* @retval ESP_OK run tinyusb main task successfully
* @retval ESP_FAIL run tinyusb main task failed of internal error
* @retval ESP_ERR_INVALID_STATE tinyusb main task has been created before
*/
esp_err_t tusb_run_task(void);
/**
* @brief Stops a FreeRTOS task
* @brief This helper function stops and destroys the task created by `tusb_run_task()`
*
* @return ESP_OK or ESP_FAIL
* @retval ESP_OK stop and destory tinyusb main task successfully
* @retval ESP_ERR_INVALID_STATE tinyusb main task hasn't been created yet
*/
esp_err_t tusb_stop_task(void);

View File

@@ -61,7 +61,7 @@ enum {
};
bool tusb_desc_set;
void tusb_set_descriptor(tusb_desc_device_t *desc, char **str_desc);
void tusb_set_descriptor(tusb_desc_device_t *desc, const char **str_desc);
tusb_desc_device_t *tusb_get_active_desc(void);
char **tusb_get_active_str_desc(void);
void tusb_clear_descriptor(void);

View File

@@ -130,26 +130,40 @@ uint8_t const *tud_hid_descriptor_report_cb(void)
// Driver functions
// =============================================================================
void tusb_set_descriptor(tusb_desc_device_t *desc, char **str_desc)
void tusb_set_descriptor(tusb_desc_device_t *dev_desc, const char **str_desc)
{
ESP_LOGI(TAG, "Setting of a descriptor: \n"
".bDeviceClass = %u\n"
".bDeviceSubClass = %u,\n"
".bDeviceProtocol = %u,\n"
".bMaxPacketSize0 = %u,\n"
".idVendor = 0x%08x,\n"
".idProduct = 0x%08x,\n"
".bcdDevice = 0x%08x,\n"
".iManufacturer = 0x%02x,\n"
".iProduct = 0x%02x,\n"
".iSerialNumber = 0x%02x,\n"
".bNumConfigurations = 0x%02x\n",
desc->bDeviceClass, desc->bDeviceSubClass,
desc->bDeviceProtocol, desc->bMaxPacketSize0,
desc->idVendor, desc->idProduct, desc->bcdDevice,
desc->iManufacturer, desc->iProduct, desc->iSerialNumber,
desc->bNumConfigurations);
s_descriptor = *desc;
ESP_LOGI(TAG, "\n"
"┌─────────────────────────────────┐\n"
"│ USB Device Descriptor Summary │\n"
"├───────────────────┬─────────────┤\n"
"│bDeviceClass │ %-4u │\n"
"├───────────────────┼─────────────┤\n"
"│bDeviceSubClass │ %-4u │\n"
"├───────────────────┼─────────────┤\n"
"│bDeviceProtocol │ %-4u │\n"
"├───────────────────┼─────────────┤\n"
"│bMaxPacketSize0 │ %-4u │\n"
"├───────────────────┼─────────────┤\n"
"│idVendor │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│idProduct │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│bcdDevice │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│iManufacturer │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│iProduct │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│iSerialNumber │ %-#10x │\n"
"├───────────────────┼─────────────┤\n"
"│bNumConfigurations │ %-#10x │\n"
"└───────────────────┴─────────────┘",
dev_desc->bDeviceClass, dev_desc->bDeviceSubClass,
dev_desc->bDeviceProtocol, dev_desc->bMaxPacketSize0,
dev_desc->idVendor, dev_desc->idProduct, dev_desc->bcdDevice,
dev_desc->iManufacturer, dev_desc->iProduct, dev_desc->iSerialNumber,
dev_desc->bNumConfigurations);
s_descriptor = *dev_desc;
if (str_desc != NULL) {
memcpy(s_str_descriptor, str_desc,

View File

@@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp_log.h"
#include "esp_rom_gpio.h"
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "driver/periph_ctrl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_rom_gpio.h"
#include "hal/gpio_ll.h"
#include "hal/usb_hal.h"
#include "soc/gpio_periph.h"
@@ -26,8 +26,6 @@
#include "descriptors_control.h"
#include "tusb.h"
#include "tusb_tasks.h"
#include "sdkconfig.h"
#include "esp_rom_gpio.h"
const static char *TAG = "TinyUSB";
@@ -57,62 +55,30 @@ static void configure_pins(usb_hal_context_t *usb)
}
}
/**
* @brief Initializes the tinyUSB driver.
*
* Note: Do not change any Custom descriptor, but
* if it used it is recomended to define: bDeviceClass = TUSB_CLASS_MISC,
* bDeviceSubClass = MISC_SUBCLASS_COMMON and bDeviceClass = TUSB_CLASS_MISC
* to match with Interface Association Descriptor (IAD) for CDC
*
* @param config if equal to NULL the default descriptor will be used
* @return esp_err_t Errors during the initialization
*/
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
{
tusb_desc_device_t *descriptor;
int res;
char **string_descriptor;
ESP_LOGI(TAG, "Driver installation...");
periph_module_reset(PERIPH_USB_MODULE);
tusb_desc_device_t *dev_descriptor;
const char **string_descriptor;
ESP_RETURN_ON_FALSE(config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
// Enable APB CLK to USB peripheral
periph_module_enable(PERIPH_USB_MODULE);
// Hal init
periph_module_reset(PERIPH_USB_MODULE);
// Initialize HAL layer
usb_hal_context_t hal = {
.use_external_phy = config->external_phy
};
usb_hal_init(&hal);
configure_pins(&hal);
if (config->descriptor == NULL) {
descriptor = &descriptor_kconfig;
} else {
descriptor = config->descriptor;
}
dev_descriptor = config->descriptor ? config->descriptor : &descriptor_kconfig;
string_descriptor = config->string_descriptor ? config->string_descriptor : descriptor_str_kconfig;
tusb_set_descriptor(dev_descriptor, string_descriptor);
if (config->string_descriptor == NULL) {
string_descriptor = descriptor_str_kconfig;
} else {
string_descriptor = config->string_descriptor;
}
tusb_set_descriptor(descriptor,
string_descriptor);
if (!tusb_init()) {
ESP_LOGE(TAG, "Can't initialize the TinyUSB stack.");
return ESP_FAIL;
}
#if !CONFIG_USB_DO_NOT_CREATE_TASK
res = tusb_run_task();
if (res != ESP_OK) {
ESP_LOGE(TAG, "Can't create the TinyUSB task.");
return res;
}
ESP_RETURN_ON_FALSE(tusb_init(), ESP_FAIL, TAG, "Init TinyUSB stack failed");
#if !CONFIG_TINYUSB_NO_DEFAULT_TASK
ESP_RETURN_ON_ERROR(tusb_run_task(), TAG, "Run TinyUSB task failed");
#endif
ESP_LOGI(TAG, "Driver installed");
ESP_LOGI(TAG, "TinyUSB Driver installed");
return ESP_OK;
}

View File

@@ -103,7 +103,7 @@ void tud_cdc_rx_cb(uint8_t itf)
while (tud_cdc_n_available(itf)) {
int read_res = tud_cdc_n_read( itf,
acm->rx_tfbuf,
CONFIG_USB_CDC_RX_BUFSIZE );
CONFIG_TINYUSB_CDC_RX_BUFSIZE );
int res = xRingbufferSend(acm->rx_unread_buf,
acm->rx_tfbuf,
read_res, 0);
@@ -312,7 +312,7 @@ esp_err_t tinyusb_cdcacm_write_flush(tinyusb_cdcacm_itf_t itf, uint32_t timeout_
if (!timeout_ticks) { // if no timeout - nonblocking mode
int res = tud_cdc_n_write_flush(itf);
if (!res) {
ESP_LOGW(TAG, "flush fauled (res: %d)", res);
ESP_LOGW(TAG, "flush failed (res: %d)", res);
return ESP_FAIL;
} else {
if (tud_cdc_n_write_occupied(itf)) {
@@ -396,7 +396,7 @@ esp_err_t tusb_cdc_acm_init(const tinyusb_config_cdcacm_t *cfg)
return ESP_ERR_NO_MEM;
}
acm->rx_tfbuf = malloc(CONFIG_USB_CDC_RX_BUFSIZE);
acm->rx_tfbuf = malloc(CONFIG_TINYUSB_CDC_RX_BUFSIZE);
if (!acm->rx_tfbuf) {
ESP_LOGE(TAG, "Creation buffer error");
free_obj(itf);

View File

@@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp_log.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hal/usb_hal.h"
#include "esp_log.h"
#include "esp_check.h"
#include "tinyusb.h"
#include "tusb_tasks.h"
#include "sdkconfig.h"
const static char *TAG = "tusb_tsk";
static TaskHandle_t s_tusb_tskh;
@@ -34,29 +34,21 @@ static void tusb_device_task(void *arg)
}
}
esp_err_t tusb_run_task(void)
{
// This function is not garanteed to be thread safe, if invoked multiple times without calling `tusb_stop_task`, will cause memory leak
// doing a sanity check anyway
ESP_RETURN_ON_FALSE(!s_tusb_tskh, ESP_ERR_INVALID_STATE, TAG, "TinyUSB main task already started");
// Create a task for tinyusb device stack:
xTaskCreate(tusb_device_task, "tinyUSB: main task", CFG_TUD_MAINTASK_SIZE, NULL, CONFIG_USB_TASK_PRIORITY, &s_tusb_tskh);
if (!s_tusb_tskh) {
return ESP_FAIL;
} else {
return ESP_OK;
}
xTaskCreate(tusb_device_task, "TinyUSB", CONFIG_TINYUSB_TASK_STACK_SIZE, NULL, CONFIG_TINYUSB_TASK_PRIORITY, &s_tusb_tskh);
ESP_RETURN_ON_FALSE(s_tusb_tskh, ESP_FAIL, TAG, "create TinyUSB main task failed");
return ESP_OK;
}
esp_err_t tusb_stop_task(void)
{
if ( s_tusb_tskh != NULL ) {
vTaskDelete(s_tusb_tskh);
} else {
ESP_LOGE(TAG, "tinyusb task is not started");
return ESP_FAIL;
}
if (s_tusb_tskh) {
return ESP_FAIL;
} else {
return ESP_OK;
}
ESP_RETURN_ON_FALSE(s_tusb_tskh, ESP_ERR_INVALID_STATE, TAG, "TinyUSB main task not started yet");
vTaskDelete(s_tusb_tskh);
s_tusb_tskh = NULL;
return ESP_OK;
}

View File

@@ -35,7 +35,7 @@ tusb_desc_device_t descriptor_tinyusb = {
.bDeviceProtocol = 0x00,
#endif
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = 0xCafe,
.idProduct = USB_TUSB_PID,
@@ -78,21 +78,21 @@ tusb_desc_device_t descriptor_kconfig = {
.bDeviceProtocol = 0x00,
#endif
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
#if CONFIG_USB_DESC_USE_ESPRESSIF_VID
#if CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID
.idVendor = USB_ESPRESSIF_VID,
#else
.idVendor = CONFIG_USB_DESC_CUSTOM_VID,
.idVendor = CONFIG_TINYUSB_DESC_CUSTOM_VID,
#endif
#if CONFIG_USB_DESC_USE_DEFAULT_PID
#if CONFIG_TINYUSB_DESC_USE_DEFAULT_PID
.idProduct = USB_TUSB_PID,
#else
.idProduct = CONFIG_USB_DESC_CUSTOM_PID,
.idProduct = CONFIG_TINYUSB_DESC_CUSTOM_PID,
#endif
.bcdDevice = CONFIG_USB_DESC_BCDDEVICE,
.bcdDevice = CONFIG_TINYUSB_DESC_BCD_DEVICE,
.iManufacturer = 0x01,
.iProduct = 0x02,
@@ -104,24 +104,24 @@ tusb_desc_device_t descriptor_kconfig = {
tusb_desc_strarray_device_t descriptor_str_kconfig = {
// array of pointer to string descriptors
(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
CONFIG_USB_DESC_MANUFACTURER_STRING, // 1: Manufacturer
CONFIG_USB_DESC_PRODUCT_STRING, // 2: Product
CONFIG_USB_DESC_SERIAL_STRING, // 3: Serials, should use chip ID
CONFIG_TINYUSB_DESC_MANUFACTURER_STRING, // 1: Manufacturer
CONFIG_TINYUSB_DESC_PRODUCT_STRING, // 2: Product
CONFIG_TINYUSB_DESC_SERIAL_STRING, // 3: Serials, should use chip ID
#if CONFIG_USB_CDC_ENABLED
CONFIG_USB_DESC_CDC_STRING, // 4: CDC Interface
#if CONFIG_TINYUSB_CDC_ENABLED
CONFIG_TINYUSB_DESC_CDC_STRING, // 4: CDC Interface
#else
"",
#endif
#if CONFIG_USB_MSC_ENABLED
CONFIG_USB_DESC_MSC_STRING, // 5: MSC Interface
#if CONFIG_TINYUSB_MSC_ENABLED
CONFIG_TINYUSB_DESC_MSC_STRING, // 5: MSC Interface
#else
"",
#endif
#if CONFIG_USB_HID_ENABLED
CONFIG_USB_DESC_HID_STRING // 6: HIDs
#if CONFIG_TINYUSB_HID_ENABLED
CONFIG_TINYUSB_DESC_HID_STRING // 6: HIDs
#else
"",
#endif