- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathUSBCDC.h
144 lines (128 loc) · 3.89 KB
/
USBCDC.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include"soc/soc_caps.h"
#if SOC_USB_OTG_SUPPORTED
#include"sdkconfig.h"
#if CONFIG_TINYUSB_CDC_ENABLED
#include<inttypes.h>
#include"esp_event.h"
#include"freertos/FreeRTOS.h"
#include"freertos/queue.h"
#include"freertos/semphr.h"
#include"Stream.h"
ESP_EVENT_DECLARE_BASE(ARDUINO_USB_CDC_EVENTS);
typedefenum {
ARDUINO_USB_CDC_ANY_EVENT = ESP_EVENT_ANY_ID,
ARDUINO_USB_CDC_CONNECTED_EVENT = 0,
ARDUINO_USB_CDC_DISCONNECTED_EVENT,
ARDUINO_USB_CDC_LINE_STATE_EVENT,
ARDUINO_USB_CDC_LINE_CODING_EVENT,
ARDUINO_USB_CDC_RX_EVENT,
ARDUINO_USB_CDC_TX_EVENT,
ARDUINO_USB_CDC_RX_OVERFLOW_EVENT,
ARDUINO_USB_CDC_MAX_EVENT,
} arduino_usb_cdc_event_t;
typedefunion {
struct {
bool dtr;
bool rts;
} line_state;
struct {
uint32_t bit_rate;
uint8_t stop_bits; ///< 0: 1 stop bit - 1: 1.5 stop bits - 2: 2 stop bits
uint8_t parity; ///< 0: None - 1: Odd - 2: Even - 3: Mark - 4: Space
uint8_t data_bits; ///< can be 5, 6, 7, 8 or 16
} line_coding;
struct {
size_t len;
} rx;
struct {
size_t dropped_bytes;
} rx_overflow;
} arduino_usb_cdc_event_data_t;
classUSBCDC : publicStream {
public:
USBCDC(uint8_t itf = 0);
~USBCDC();
voidonEvent(esp_event_handler_t callback);
voidonEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback);
size_tsetRxBufferSize(size_t size);
voidsetTxTimeoutMs(uint32_t timeout);
voidbegin(unsignedlong baud = 0);
voidend();
intavailable(void);
intavailableForWrite(void);
intpeek(void);
intread(void);
size_tread(uint8_t *buffer, size_t size);
size_twrite(uint8_t);
size_twrite(constuint8_t *buffer, size_t size);
voidflush(void);
inlinesize_tread(char *buffer, size_t size) {
returnread((uint8_t *)buffer, size);
}
inlinesize_twrite(constchar *buffer, size_t size) {
returnwrite((uint8_t *)buffer, size);
}
inlinesize_twrite(constchar *s) {
returnwrite((uint8_t *)s, strlen(s));
}
inlinesize_twrite(unsignedlong n) {
returnwrite((uint8_t)n);
}
inlinesize_twrite(long n) {
returnwrite((uint8_t)n);
}
inlinesize_twrite(unsignedint n) {
returnwrite((uint8_t)n);
}
inlinesize_twrite(int n) {
returnwrite((uint8_t)n);
}
uint32_tbaudRate();
voidsetDebugOutput(bool);
operatorbool() const;
voidenableReboot(bool enable);
boolrebootEnabled(void);
//internal methods
void_onDFU(void);
void_onLineState(bool _dtr, bool _rts);
void_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _parity, uint8_t _data_bits);
void_onRX(void);
void_onTX(void);
void_onUnplugged(void);
protected:
uint8_t itf;
uint32_t bit_rate;
uint8_t stop_bits; ///< 0: 1 stop bit - 1: 1.5 stop bits - 2: 2 stop bits
uint8_t parity; ///< 0: None - 1: Odd - 2: Even - 3: Mark - 4: Space
uint8_t data_bits; ///< can be 5, 6, 7, 8 or 16
bool dtr;
bool rts;
bool connected;
bool reboot_enable;
QueueHandle_t rx_queue;
SemaphoreHandle_t tx_lock;
uint32_t tx_timeout_ms;
};
#if !ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT // Native USB CDC selected
#ifndef USB_SERIAL_IS_DEFINED
#defineUSB_SERIAL_IS_DEFINED1
#endif
// USBSerial is always available to be used
extern USBCDC USBSerial;
#endif
#endif/* CONFIG_TINYUSB_CDC_ENABLED */
#endif/* SOC_USB_OTG_SUPPORTED */