Skip to content

Commit 001c3bd

Browse files
authored
Merge branch 'master' into ci/style_check
2 parents 1533703 + c17a688 commit 001c3bd

File tree

15 files changed

+2191
-348
lines changed

15 files changed

+2191
-348
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ set(CORE_SRCS
5454
cores/esp32/IPAddress.cpp
5555
cores/esp32/libb64/cdecode.c
5656
cores/esp32/libb64/cencode.c
57+
cores/esp32/MacAddress.cpp
5758
cores/esp32/main.cpp
5859
cores/esp32/MD5Builder.cpp
5960
cores/esp32/Print.cpp

boards.txt

+1,363-327
Large diffs are not rendered by default.

cores/esp32/HWCDC.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "esp_intr_alloc.h"
2525
#include "soc/periph_defs.h"
2626
#include "soc/io_mux_reg.h"
27+
#include "soc/usb_serial_jtag_struct.h"
2728
#pragma GCC diagnostic ignored "-Wvolatile"
2829
#include "hal/usb_serial_jtag_ll.h"
2930
#pragma GCC diagnostic warning "-Wvolatile"
@@ -86,7 +87,7 @@ static void hw_cdc_isr_handler(void *arg) {
8687
} else {
8788
connected = true;
8889
}
89-
if (usb_serial_jtag_ll_txfifo_writable() == 1) {
90+
if (tx_ring_buf != NULL && usb_serial_jtag_ll_txfifo_writable() == 1) {
9091
// We disable the interrupt here so that the interrupt won't be triggered if there is no data to send.
9192
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
9293
size_t queued_size;
@@ -164,6 +165,9 @@ bool HWCDC::isCDC_Connected()
164165
}
165166

166167
static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
168+
if(tx_ring_buf == NULL) {
169+
return;
170+
}
167171
uint32_t tx_timeout_ms = 0;
168172
if(HWCDC::isConnected()) {
169173
tx_timeout_ms = requested_tx_timeout_ms;
@@ -238,32 +242,33 @@ void HWCDC::begin(unsigned long baud)
238242
log_e("HW CDC TX Buffer error");
239243
}
240244
}
245+
246+
// the HW Serial pins needs to be first deinited in order to allow `if(Serial)` to work :-(
247+
deinit(NULL);
248+
delay(10); // USB Host has to enumerate it again
249+
250+
// Peripheral Manager setting for USB D+ D- pins
251+
uint8_t pin = USB_DM_GPIO_NUM;
252+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)) goto err;
253+
pin = USB_DP_GPIO_NUM;
254+
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)) goto err;
255+
256+
// Configure PHY
257+
// USB_Serial_JTAG use internal PHY
258+
USB_SERIAL_JTAG.conf0.phy_sel = 0;
259+
// Disable software control USB D+ D- pullup pulldown (Device FS: dp_pullup = 1)
260+
USB_SERIAL_JTAG.conf0.pad_pull_override = 0;
261+
// Enable USB D+ pullup
262+
USB_SERIAL_JTAG.conf0.dp_pullup = 1;
263+
// Enable USB pad function
264+
USB_SERIAL_JTAG.conf0.usb_pad_enable = 1;
241265
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_LL_INTR_MASK);
242266
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET);
243267
if(!intr_handle && esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0, hw_cdc_isr_handler, NULL, &intr_handle) != ESP_OK){
244268
isr_log_e("HW USB CDC failed to init interrupts");
245269
end();
246270
return;
247271
}
248-
// Setting USB D+ D- pins
249-
uint8_t pin = ESP32_BUS_TYPE_USB_DM;
250-
if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT){
251-
if(!perimanClearPinBus(pin)){
252-
goto err;
253-
}
254-
}
255-
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1)){
256-
goto err;
257-
}
258-
pin = ESP32_BUS_TYPE_USB_DP;
259-
if(perimanGetPinBusType(pin) != ESP32_BUS_TYPE_INIT){
260-
if(!perimanClearPinBus(pin)){
261-
goto err;
262-
}
263-
}
264-
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1)){
265-
goto err;
266-
}
267272
return;
268273

269274
err:
@@ -289,6 +294,7 @@ void HWCDC::end()
289294
arduino_hw_cdc_event_loop_handle = NULL;
290295
}
291296
HWCDC::deinit(this);
297+
setDebugOutput(false);
292298
connected = false;
293299
}
294300

cores/esp32/MacAddress.cpp

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#include <MacAddress.h>
2+
#include <stdio.h>
3+
#include <Print.h>
4+
5+
//Default constructor, blank mac address.
6+
MacAddress::MacAddress() : MacAddress(MAC6){}
7+
8+
MacAddress::MacAddress(MACType mac_type){
9+
_type = mac_type;
10+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
11+
}
12+
MacAddress::MacAddress(MACType mac_type, uint64_t mac) {
13+
_type = mac_type;
14+
_mac.val = mac;
15+
}
16+
17+
MacAddress::MacAddress(MACType mac_type, const uint8_t *macbytearray) {
18+
_type = mac_type;
19+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
20+
if(_type == MAC6) {
21+
memcpy(_mac.bytes, macbytearray, 6);
22+
} else {
23+
memcpy(_mac.bytes, macbytearray, 8);
24+
}
25+
}
26+
27+
MacAddress::MacAddress(const char *macstr){
28+
fromString(macstr);
29+
}
30+
31+
MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6) {
32+
_type = MAC6;
33+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
34+
_mac.bytes[0] = b1;
35+
_mac.bytes[1] = b2;
36+
_mac.bytes[2] = b3;
37+
_mac.bytes[3] = b4;
38+
_mac.bytes[4] = b5;
39+
_mac.bytes[5] = b6;
40+
}
41+
42+
MacAddress::MacAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7, uint8_t b8) {
43+
_type = MAC8;
44+
_mac.bytes[0] = b1;
45+
_mac.bytes[1] = b2;
46+
_mac.bytes[2] = b3;
47+
_mac.bytes[3] = b4;
48+
_mac.bytes[4] = b5;
49+
_mac.bytes[5] = b6;
50+
_mac.bytes[6] = b7;
51+
_mac.bytes[7] = b8;
52+
}
53+
54+
//Parse user entered string into MAC address
55+
bool MacAddress::fromString(const char *buf) {
56+
if(strlen(buf) == 17) {
57+
return fromString6(buf);
58+
} else if(strlen(buf) == 23) {
59+
return fromString8(buf);
60+
}
61+
return false;
62+
}
63+
64+
//Parse user entered string into MAC address
65+
bool MacAddress::fromString6(const char *buf) {
66+
char cs[18];
67+
char *token;
68+
char *next; //Unused but required
69+
int i;
70+
71+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
72+
73+
for(i = 0; i < 6; i++) {
74+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
75+
if(!token) { //No more tokens found
76+
return false;
77+
}
78+
_mac.bytes[i] = strtol(token, &next, 16);
79+
}
80+
_type = MAC6;
81+
return true;
82+
}
83+
84+
bool MacAddress::fromString8(const char *buf) {
85+
char cs[24];
86+
char *token;
87+
char *next; //Unused but required
88+
int i;
89+
90+
strncpy(cs, buf, sizeof(cs)); //strtok modifies the buffer: copy to working buffer.
91+
92+
for(i = 0; i < 8; i++) {
93+
token = strtok((i==0) ? cs : NULL, ":"); //Find first or next token
94+
if(!token) { //No more tokens found
95+
return false;
96+
}
97+
_mac.bytes[i] = strtol(token, &next, 16);
98+
}
99+
_type = MAC8;
100+
return true;
101+
}
102+
103+
//Copy MAC into byte array
104+
void MacAddress::toBytes(uint8_t *buf) {
105+
if(_type == MAC6) {
106+
memcpy(buf, _mac.bytes, 6);
107+
} else {
108+
memcpy(buf, _mac.bytes, sizeof(_mac.bytes));
109+
}
110+
}
111+
112+
//Print MAC address into a C string.
113+
//MAC: Buffer must be at least 18 chars
114+
int MacAddress::toString(char *buf) {
115+
if(_type == MAC6) {
116+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
117+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
118+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
119+
} else {
120+
return sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
121+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
122+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5],
123+
_mac.bytes[6], _mac.bytes[7]);
124+
}
125+
}
126+
127+
String MacAddress::toString() const {
128+
uint8_t bytes = (_type == MAC6) ? 18 : 24;
129+
char buf[bytes];
130+
if(_type == MAC6) {
131+
snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
132+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
133+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
134+
} else {
135+
snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
136+
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
137+
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5],
138+
_mac.bytes[6], _mac.bytes[7]);
139+
}
140+
return String(buf);
141+
}
142+
143+
uint64_t MacAddress::Value() {
144+
return _mac.val;
145+
}
146+
147+
//Allow getting individual octets of the address. e.g. uint8_t b0 = ma[0];
148+
uint8_t MacAddress::operator[](int index) const {
149+
index = EnforceIndexBounds(index);
150+
return _mac.bytes[index];
151+
}
152+
153+
//Allow setting individual octets of the address. e.g. ma[2] = 255;
154+
uint8_t& MacAddress::operator[](int index) {
155+
index = EnforceIndexBounds(index);
156+
return _mac.bytes[index];
157+
}
158+
159+
//Overloaded copy operator: init MacAddress object from byte array
160+
MacAddress& MacAddress::operator=(const uint8_t *macbytearray) {
161+
// 6-bytes MacAddress only
162+
_type = MAC6;
163+
memset(_mac.bytes, 0, sizeof(_mac.bytes));
164+
memcpy(_mac.bytes, macbytearray, 6);
165+
return *this;
166+
}
167+
168+
//Overloaded copy operator: init MacAddress object from uint64_t
169+
MacAddress& MacAddress::operator=(uint64_t macval) {
170+
// 6-bytes MacAddress only
171+
_type = MAC6;
172+
_mac.val = macval;
173+
return *this;
174+
}
175+
176+
//Compare class to byte array
177+
bool MacAddress::operator==(const uint8_t *macbytearray) const {
178+
return !memcmp(_mac.bytes, macbytearray, 6);
179+
}
180+
181+
//Allow comparing value of two classes
182+
bool MacAddress::operator==(const MacAddress& mac2) const {
183+
return _mac.val == mac2._mac.val;
184+
}
185+
186+
//Type converter object to uint64_t [same as .Value()]
187+
MacAddress::operator uint64_t() const {
188+
return _mac.val;
189+
}
190+
191+
//Type converter object to read only pointer to mac bytes. e.g. const uint8_t *ip_8 = ma;
192+
MacAddress::operator const uint8_t*() const {
193+
return _mac.bytes;
194+
}
195+
196+
//Type converter object to read only pointer to mac value. e.g. const uint32_t *ip_64 = ma;
197+
MacAddress::operator const uint64_t*() const {
198+
return &_mac.val;
199+
}
200+
201+
size_t MacAddress::printTo(Print& p) const
202+
{
203+
uint8_t bytes = (_type == MAC6) ? 6 : 8;
204+
size_t n = 0;
205+
for(int i = 0; i < bytes; i++) {
206+
if(i){
207+
n += p.print(':');
208+
}
209+
n += p.printf("%02X", _mac.bytes[i]);
210+
}
211+
return n;
212+
}
213+
214+
//Bounds checking
215+
int MacAddress::EnforceIndexBounds(int i) const {
216+
if(i < 0) {
217+
return 0;
218+
}
219+
if(_type == MAC6) {
220+
if(i >= 6) {
221+
return 5;
222+
}
223+
} else {
224+
if(i >= 8) {
225+
return 7;
226+
}
227+
}
228+
return i;
229+
}

0 commit comments

Comments
 (0)