forked from espressif/arduino-esp32
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHWCDC.h
118 lines (103 loc) · 3.09 KB
/
HWCDC.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
// Copyright 2015-2024 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"sdkconfig.h"
#include"soc/soc_caps.h"
#if SOC_USB_SERIAL_JTAG_SUPPORTED
#include<inttypes.h>
#include"esp_event.h"
#include"Stream.h"
#include"driver/usb_serial_jtag.h"
ESP_EVENT_DECLARE_BASE(ARDUINO_HW_CDC_EVENTS);
typedefenum {
ARDUINO_HW_CDC_ANY_EVENT = ESP_EVENT_ANY_ID,
ARDUINO_HW_CDC_CONNECTED_EVENT = 0,
ARDUINO_HW_CDC_BUS_RESET_EVENT,
ARDUINO_HW_CDC_RX_EVENT,
ARDUINO_HW_CDC_TX_EVENT,
ARDUINO_HW_CDC_MAX_EVENT,
} arduino_hw_cdc_event_t;
typedefunion {
struct {
size_t len;
} rx;
struct {
size_t len;
} tx;
} arduino_hw_cdc_event_data_t;
classHWCDC : publicStream {
private:
staticbooldeinit(void *busptr);
staticboolisCDC_Connected();
public:
HWCDC();
~HWCDC();
voidonEvent(esp_event_handler_t callback);
voidonEvent(arduino_hw_cdc_event_t event, esp_event_handler_t callback);
size_tsetRxBufferSize(size_t);
size_tsetTxBufferSize(size_t);
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);
inlinestaticboolisPlugged(void) {
// SOF ISR is causing esptool to be unable to upload firmware to the board
// Using IDF 5.1 helper function because it is based on Timer check instead of ISR
returnusb_serial_jtag_is_connected();
}
inlinestaticboolisConnected(void) {
returnisCDC_Connected();
}
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);
}
operatorbool() const;
voidsetDebugOutput(bool);
uint32_tbaudRate() {
return115200;
}
};
#if ARDUINO_USB_MODE && ARDUINO_USB_CDC_ON_BOOT // Hardware JTAG CDC selected
#ifndef HWCDC_SERIAL_IS_DEFINED
#defineHWCDC_SERIAL_IS_DEFINED1
#endif
// HWCDCSerial is always available to be used
extern HWCDC HWCDCSerial;
#endif
#endif/* SOC_USB_SERIAL_JTAG_SUPPORTED */