2022-02-10 10:57:20 +01:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
2022-02-15 13:01:30 +01:00
|
|
|
* SPDX-License-Identifier: CC0-1.0
|
2022-02-10 10:57:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "tinyusb.h"
|
|
|
|
#include "tusb_cdc_acm.h"
|
2022-09-08 16:28:36 +02:00
|
|
|
#include "esp_idf_version.h"
|
2022-02-10 10:57:20 +01:00
|
|
|
|
|
|
|
static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1];
|
|
|
|
void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event)
|
|
|
|
{
|
|
|
|
size_t rx_size = 0;
|
|
|
|
/* read and write back */
|
|
|
|
ESP_ERROR_CHECK(tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size));
|
|
|
|
tinyusb_cdcacm_write_queue(itf, buf, rx_size);
|
|
|
|
tinyusb_cdcacm_write_flush(itf, 0);
|
|
|
|
}
|
|
|
|
|
2022-09-08 16:28:36 +02:00
|
|
|
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
2022-02-10 11:16:53 +01:00
|
|
|
static const tusb_desc_device_t cdc_device_descriptor = {
|
|
|
|
.bLength = sizeof(cdc_device_descriptor),
|
|
|
|
.bDescriptorType = TUSB_DESC_DEVICE,
|
|
|
|
.bcdUSB = 0x0200,
|
|
|
|
.bDeviceClass = TUSB_CLASS_MISC,
|
|
|
|
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
|
|
|
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
|
|
|
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
|
|
|
.idVendor = USB_ESPRESSIF_VID,
|
|
|
|
.idProduct = 0x4002,
|
|
|
|
.bcdDevice = 0x0100,
|
|
|
|
.iManufacturer = 0x01,
|
|
|
|
.iProduct = 0x02,
|
|
|
|
.iSerialNumber = 0x03,
|
|
|
|
.bNumConfigurations = 0x01
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint16_t cdc_desc_config_len = TUD_CONFIG_DESC_LEN + 2 * TUD_CDC_DESC_LEN;
|
|
|
|
static const uint8_t cdc_desc_configuration[] = {
|
|
|
|
TUD_CONFIG_DESCRIPTOR(1, 4, 0, cdc_desc_config_len, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
|
|
|
|
TUD_CDC_DESCRIPTOR(0, 4, 0x81, 8, 0x02, 0x82, 64),
|
|
|
|
TUD_CDC_DESCRIPTOR(2, 4, 0x83, 8, 0x04, 0x84, 64),
|
|
|
|
};
|
2022-09-08 16:28:36 +02:00
|
|
|
#endif
|
2022-02-10 11:16:53 +01:00
|
|
|
|
2022-02-10 10:57:20 +01:00
|
|
|
void run_usb_dual_cdc_device(void)
|
|
|
|
{
|
2022-02-10 11:16:53 +01:00
|
|
|
const tinyusb_config_t tusb_cfg = {
|
2022-09-08 16:28:36 +02:00
|
|
|
.external_phy = false,
|
|
|
|
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
2022-02-10 11:16:53 +01:00
|
|
|
.device_descriptor = &cdc_device_descriptor,
|
|
|
|
.configuration_descriptor = cdc_desc_configuration
|
2022-09-08 16:28:36 +02:00
|
|
|
#endif
|
2022-02-10 11:16:53 +01:00
|
|
|
};
|
2022-02-10 10:57:20 +01:00
|
|
|
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
|
|
|
|
|
|
|
|
tinyusb_config_cdcacm_t amc_cfg = {
|
|
|
|
.usb_dev = TINYUSB_USBDEV_0,
|
|
|
|
.cdc_port = TINYUSB_CDC_ACM_0,
|
|
|
|
.rx_unread_buf_sz = 64,
|
2022-02-15 13:01:30 +01:00
|
|
|
.callback_rx = &tinyusb_cdc_rx_callback,
|
2022-02-10 10:57:20 +01:00
|
|
|
.callback_rx_wanted_char = NULL,
|
|
|
|
.callback_line_state_changed = NULL,
|
|
|
|
.callback_line_coding_changed = NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(tusb_cdc_acm_init(&amc_cfg));
|
|
|
|
#if (CONFIG_TINYUSB_CDC_COUNT > 1)
|
|
|
|
amc_cfg.cdc_port = TINYUSB_CDC_ACM_1;
|
|
|
|
ESP_ERROR_CHECK(tusb_cdc_acm_init(&amc_cfg));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("USB initialization DONE\n");
|
|
|
|
}
|