Merge branch 'feature/usb_component' into 'master'

Feature/usb component

See merge request espressif/esp-idf!6897
This commit is contained in:
Michael (XIAO Xufeng)
2020-03-09 16:53:55 +08:00
24 changed files with 2779 additions and 3753 deletions

View File

@@ -0,0 +1,7 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(SUPPORTED_TARGETS esp32s2)
project(tusb_sample_descriptor)

View File

@@ -0,0 +1,70 @@
# TinyUSB Sample Descriptor
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example is demonstrating how to set up ESP32-S2 chip to work as a Generic USB Device with a user-defined descriptor. You can specify a manufacturer, device's name, ID and other USB-devices parameters responsible for identification by host.
As a USB stack, a TinyUSB component is used.
## How to use example
### Hardware Required
- Any board with the ESP32-S2 chip
### Configure the project
There are two ways to set up a descriptor - using Menuconfig tool and in-code
#### In-code setting up
For the manual descriptor's configuration use the default example's settings and modify `tusb_sample_descriptor.c` according to your needs
#### Menuconfig
If you want to set up the desctiptor using Menuconfig UI:
1. Execute in the terminal from the example's directory: `idf.py menuconfig`
2. Turn off `Set up a USB descriptor manually in code` parameter at `Example Configuration`
3. Folow to `Component config -> TinyUSB -> Descriptor configuration` for all available configurations.
### Build and Flash
Build the project and flash it to the board, then run monitor tool to view serial output:
```bash
idf.py -p PORT flash monitor
```
(Replace PORT with the name of the serial port to use.)
(To exit the serial monitor, type ``Ctrl-]``.)
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
## Example Output
After the flashing you should see the output:
```
I (314) example: USB initialization
I (314) TUSB:descriptors_control: Setting of a descriptor:
.bDeviceClass = 0
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = 64,
.idVendor = 0x0000303a,
.idProduct = 0x00004000,
.bcdDevice = 0x00000100,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,
.bNumConfigurations = 0x01
I (344) example: USB initialization DONE
I (354) example: USB task started
```

View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "tusb_sample_descriptor.c"
INCLUDE_DIRS . ${COMPONENT_DIR})

View File

@@ -0,0 +1,10 @@
menu "Example Configuration"
config EXAMPLE_MANUAL_DESC
bool "Set up a USB descriptor manually in code"
default y
help
You can set up a descriptor using Menuconfig or independently of
your project configuration - manually in code
endmenu

View File

@@ -0,0 +1,84 @@
/* USB Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdlib.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "tinyusb.h"
static const char *TAG = "example";
// USB Device Driver task
// This top level thread processes all usb events and invokes callbacks
static void usb_device_task(void *param) {
(void)param;
ESP_LOGI(TAG, "USB task started");
while (1) {
tud_task(); // RTOS forever loop
}
}
void app_main(void) {
ESP_LOGI(TAG, "USB initialization");
#if CONFIG_EXAMPLE_MANUAL_DESC
// Setting of descriptor. You can use descriptor_tinyusb and
// descriptor_str_tinyusb as a reference
tusb_desc_device_t my_descriptor = {
.bLength = sizeof(my_descriptor),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200, // USB version. 0x0200 means version 2.0
.bDeviceClass = TUSB_CLASS_UNSPECIFIED,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = 0x303A,
.idProduct = 0x3000,
.bcdDevice = 0x0101, // Device FW version
.iManufacturer = 0x01, // see string_descriptor[1] bellow
.iProduct = 0x02, // see string_descriptor[2] bellow
.iSerialNumber = 0x03, // see string_descriptor[3] bellow
.bNumConfigurations = 0x01};
tusb_desc_strarray_device_t my_string_descriptor = {
// array of pointer to string descriptors
(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
"I", // 1: Manufacturer
"My Custom Device", // 2: Product
"012-345", // 3: Serials, should use chip ID
};
tinyusb_config_t tusb_cfg = {
.descriptor = &my_descriptor,
.string_descriptor = my_string_descriptor,
.external_phy = false // In the most cases you need to use a `false` value
};
#else
tinyusb_config_t tusb_cfg = {
.descriptor = NULL,
.string_descriptor = NULL,
.external_phy = false // In the most cases you need to use a `false` value
};
#endif
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
ESP_LOGI(TAG, "USB initialization DONE");
// Create a task for tinyusb device stack:
xTaskCreate(usb_device_task, "usbd", 4096, NULL, 5, NULL);
return;
}

View File

@@ -0,0 +1,5 @@
CONFIG_USB_ENABLED=y
CONFIG_USB_DESC_USE_ESPRESSIF_VID=n
CONFIG_USB_DESC_CUSTOM_VID=0x303A
CONFIG_USB_DESC_USE_DEFAULT_PID=n
CONFIG_USB_DESC_CUSTOM_PID=0x3000