Skip to content

Commit be074ee

Browse files
committed
Add TinyUSB HAL and CDC
1 parent bd96f7f commit be074ee

File tree

10 files changed

+1758
-0
lines changed

10 files changed

+1758
-0
lines changed

cores/esp32/esp32-hal-tinyusb.c

Lines changed: 682 additions & 0 deletions
Large diffs are not rendered by default.

cores/esp32/esp32-hal-tinyusb.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#include "esp32-hal.h"
17+
18+
#if CONFIG_IDF_TARGET_ESP32S2
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
#include "tinyusb.h"
25+
26+
typedef struct {
27+
uint16_t vid;
28+
uint16_t pid;
29+
const char * product_name;
30+
const char * manufacturer_name;
31+
const char * serial_number;
32+
uint16_t fw_version;
33+
34+
uint16_t usb_version;
35+
uint8_t usb_class;
36+
uint8_t usb_subclass;
37+
uint8_t usb_protocol;
38+
uint8_t usb_attributes;
39+
uint16_t usb_power_ma;
40+
41+
bool webusb_enabled;
42+
const char * webusb_url;
43+
} tinyusb_device_config_t;
44+
45+
#define TINYUSB_CONFIG_DEFAULT() { \
46+
.vid = USB_ESPRESSIF_VID, \
47+
.pid = 0x0002, \
48+
.product_name = CONFIG_USB_DESC_PRODUCT_STRING, \
49+
.manufacturer_name = CONFIG_USB_DESC_MANUFACTURER_STRING, \
50+
.serial_number = CONFIG_USB_DESC_SERIAL_STRING, \
51+
.fw_version = CONFIG_USB_DESC_BCDDEVICE, \
52+
.usb_version = 0x0200, \
53+
.usb_class = TUSB_CLASS_MISC, \
54+
.usb_subclass = MISC_SUBCLASS_COMMON, \
55+
.usb_protocol = MISC_PROTOCOL_IAD, \
56+
.usb_attributes = TUSB_DESC_CONFIG_ATT_SELF_POWERED, \
57+
.usb_power_ma = 500, \
58+
.webusb_enabled = false, \
59+
.webusb_url = "espressif.github.io/arduino-esp32/webusb.html" \
60+
}
61+
62+
esp_err_t tinyusb_init(tinyusb_device_config_t *config);
63+
64+
// The following definitions and functions are to be used only by the drivers
65+
typedef enum {
66+
USB_INTERFACE_CDC,
67+
USB_INTERFACE_DFU,
68+
USB_INTERFACE_HID,
69+
USB_INTERFACE_VENDOR,
70+
USB_INTERFACE_MSC,
71+
USB_INTERFACE_MIDI,
72+
USB_INTERFACE_CUSTOM,
73+
USB_INTERFACE_MAX
74+
} tinyusb_interface_t;
75+
76+
esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface);
77+
uint8_t tinyusb_add_string_descriptor(const char * str);
78+
uint8_t tinyusb_get_free_duplex_endpoint(void);
79+
uint8_t tinyusb_get_free_in_endpoint(void);
80+
uint8_t tinyusb_get_free_out_endpoint(void);
81+
82+
#ifdef __cplusplus
83+
}
84+
#endif
85+
86+
#endif /* CONFIG_IDF_TARGET_ESP32S2 */

libraries/USB/examples/USBSerial/.skip.esp32

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "USB.h"
2+
USBCDC USBSerial;
3+
4+
static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
5+
if(event_base == ARDUINO_USB_EVENTS){
6+
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
7+
switch (event_id){
8+
case ARDUINO_USB_STARTED_EVENT:
9+
Serial.println("USB PLUGGED");
10+
break;
11+
case ARDUINO_USB_STOPPED_EVENT:
12+
Serial.println("USB UNPLUGGED");
13+
break;
14+
case ARDUINO_USB_SUSPEND_EVENT:
15+
Serial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
16+
break;
17+
case ARDUINO_USB_RESUME_EVENT:
18+
Serial.println("USB RESUMED");
19+
break;
20+
21+
default:
22+
break;
23+
}
24+
} else if(event_base == ARDUINO_USB_CDC_EVENTS){
25+
arduino_usb_cdc_event_data_t * data = (arduino_usb_cdc_event_data_t*)event_data;
26+
switch (event_id){
27+
case ARDUINO_USB_CDC_CONNECTED_EVENT:
28+
Serial.println("CDC CONNECTED");
29+
break;
30+
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
31+
Serial.println("CDC DISCONNECTED");
32+
break;
33+
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
34+
Serial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
35+
break;
36+
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
37+
Serial.printf("CDC LINE CODING: bit_rate: %u, data_bits: %u, stop_bits: %u, parity: %u\n", data->line_coding.bit_rate, data->line_coding.data_bits, data->line_coding.stop_bits, data->line_coding.parity);
38+
break;
39+
case ARDUINO_USB_CDC_RX_EVENT:
40+
Serial.printf("CDC RX: %u\n", data->rx.len);
41+
{
42+
uint8_t buf[data->rx.len];
43+
size_t len = USBSerial.read(buf, data->rx.len);
44+
Serial.write(buf, len);
45+
}
46+
break;
47+
48+
default:
49+
break;
50+
}
51+
}
52+
}
53+
54+
55+
void setup() {
56+
Serial.begin(115200);
57+
Serial.setDebugOutput(true);
58+
59+
USB.onEvent(usbEventCallback);
60+
USB.productName("ESP32S2-USB");
61+
USB.begin();
62+
63+
USBSerial.onEvent(usbEventCallback);
64+
USBSerial.begin();
65+
}
66+
67+
void loop() {
68+
while(Serial.available()){
69+
size_t l = Serial.available();
70+
uint8_t b[l];
71+
l = Serial.read(b, l);
72+
USBSerial.write(b, l);
73+
}
74+
}

libraries/USB/keywords.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
USB KEYWORD1
10+
USBCDC KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
16+
begin KEYWORD2
17+
end KEYWORD2
18+
onEvent KEYWORD2
19+
enableReset KEYWORD2
20+
21+
#######################################
22+
# Constants (LITERAL1)
23+
#######################################
24+

libraries/USB/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=USB
2+
version=1.0
3+
author=Hristo Gochkov
4+
maintainer=Hristo Gochkov <[email protected]>
5+
sentence=ESP32S2 USB Library
6+
paragraph=
7+
category=Communication
8+
url=
9+
architectures=esp32

0 commit comments

Comments
 (0)