Skip to content

Commit 45dbadb

Browse files
authored
Merge pull request #120 from tasmota/s3_tasmota_work
S3 tasmota work
2 parents 6a1ee9c + 1e9112d commit 45dbadb

File tree

259 files changed

+3883
-2629
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

259 files changed

+3883
-2629
lines changed

Diff for: boards.txt

+422-3
Large diffs are not rendered by default.

Diff for: cores/esp32/HardwareSerial.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void serialEvent(void) {}
3737
#ifndef RX1
3838
#if CONFIG_IDF_TARGET_ESP32
3939
#define RX1 9
40-
#elif CONFIG_IDF_TARGET_ESP32S2
40+
#elif CONFIG_IDF_TARGET_ESP32S2
4141
#define RX1 18
4242
#elif CONFIG_IDF_TARGET_ESP32C3
4343
#define RX1 18
@@ -86,8 +86,6 @@ void serialEvent2(void) {}
8686
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
8787
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
8888
HardwareSerial Serial0(0);
89-
#elif ARDUINO_HW_CDC_ON_BOOT
90-
HardwareSerial Serial0(0);
9189
#else
9290
HardwareSerial Serial(0);
9391
#endif
@@ -102,8 +100,6 @@ void serialEventRun(void)
102100
{
103101
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
104102
if(Serial0.available()) serialEvent();
105-
#elif ARDUINO_HW_CDC_ON_BOOT
106-
if(Serial0.available()) serialEvent();
107103
#else
108104
if(Serial.available()) serialEvent();
109105
#endif
@@ -127,7 +123,8 @@ void serialEventRun(void)
127123
HardwareSerial::HardwareSerial(int uart_nr) :
128124
_uart_nr(uart_nr),
129125
_uart(NULL),
130-
_rxBufferSize(256),
126+
_rxBufferSize(256),
127+
_txBufferSize(0),
131128
_onReceiveCB(NULL),
132129
_onReceiveErrorCB(NULL),
133130
_eventTask(NULL)
@@ -295,7 +292,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
295292
}
296293

297294
// IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
298-
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
295+
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
299296
if (!baud) {
300297
// using baud rate as zero, forces it to try to detect the current baud rate in place
301298
uartStartDetectBaudrate(_uart);
@@ -309,7 +306,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
309306

310307
if(detectedBaudRate) {
311308
delay(100); // Give some time...
312-
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, invert, rxfifo_full_thrhd);
309+
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, _rxBufferSize, _txBufferSize, invert, rxfifo_full_thrhd);
313310
} else {
314311
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
315312
_uart = NULL;
@@ -458,10 +455,26 @@ size_t HardwareSerial::setRxBufferSize(size_t new_size) {
458455
}
459456

460457
if (new_size <= SOC_UART_FIFO_LEN) {
461-
log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN);
458+
log_e("RX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
462459
return 0;
463460
}
464461

465462
_rxBufferSize = new_size;
466463
return _rxBufferSize;
467464
}
465+
466+
size_t HardwareSerial::setTxBufferSize(size_t new_size) {
467+
468+
if (_uart) {
469+
log_e("TX Buffer can't be resized when Serial is already running.\n");
470+
return 0;
471+
}
472+
473+
if (new_size <= SOC_UART_FIFO_LEN) {
474+
log_e("TX Buffer must be higher than %d.\n", SOC_UART_FIFO_LEN); // ESP32, S2, S3 and C3 means higher than 128
475+
return 0;
476+
}
477+
478+
_txBufferSize = new_size;
479+
return _txBufferSize;
480+
}

Diff for: cores/esp32/HardwareSerial.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ class HardwareSerial: public Stream
132132
void setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length
133133

134134
size_t setRxBufferSize(size_t new_size);
135+
size_t setTxBufferSize(size_t new_size);
135136

136137
protected:
137138
int _uart_nr;
138139
uart_t* _uart;
139140
size_t _rxBufferSize;
141+
size_t _txBufferSize;
140142
OnReceiveCb _onReceiveCB;
141143
OnReceiveErrorCb _onReceiveErrorCB;
142144
TaskHandle_t _eventTask;
@@ -156,10 +158,10 @@ extern void serialEventRun(void) __attribute__((weak));
156158
#define ARDUINO_USB_CDC_ON_BOOT 0
157159
#endif
158160
#if ARDUINO_USB_CDC_ON_BOOT //Serial used for USB CDC
161+
#if !ARDUINO_USB_MODE
159162
#include "USB.h"
160163
#include "USBCDC.h"
161-
extern HardwareSerial Serial0;
162-
#elif ARDUINO_HW_CDC_ON_BOOT
164+
#endif
163165
extern HardwareSerial Serial0;
164166
#else
165167
extern HardwareSerial Serial;

Diff for: cores/esp32/USBCDC.cpp

+38-22
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,39 @@ size_t USBCDC::setRxBufferSize(size_t rx_queue_len){
117117
size_t currentQueueSize = rx_queue ?
118118
uxQueueSpacesAvailable(rx_queue) + uxQueueMessagesWaiting(rx_queue) : 0;
119119

120-
if (rx_queue && (!rx_queue_len || rx_queue_len != currentQueueSize)) {
121-
vQueueDelete(rx_queue);
122-
rx_queue = NULL;
123-
}
124-
if(!rx_queue_len || rx_queue_len == currentQueueSize){
125-
return 0;
126-
}
127-
rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
128-
if(!rx_queue){
129-
return 0;
120+
if (rx_queue_len != currentQueueSize) {
121+
xQueueHandle new_rx_queue = NULL;
122+
if (rx_queue_len) {
123+
new_rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
124+
if(!new_rx_queue){
125+
log_e("CDC Queue creation failed.");
126+
return 0;
127+
}
128+
if (rx_queue) {
129+
size_t copySize = uxQueueMessagesWaiting(rx_queue);
130+
if (copySize > 0) {
131+
for(size_t i = 0; i < copySize; i++) {
132+
uint8_t ch = 0;
133+
xQueueReceive(rx_queue, &ch, 0);
134+
if (!xQueueSend(new_rx_queue, &ch, 0)) {
135+
arduino_usb_cdc_event_data_t p;
136+
p.rx_overflow.dropped_bytes = copySize - i;
137+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_OVERFLOW_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
138+
log_e("CDC RX Overflow.");
139+
break;
140+
}
141+
}
142+
}
143+
vQueueDelete(rx_queue);
144+
}
145+
rx_queue = new_rx_queue;
146+
return rx_queue_len;
147+
} else {
148+
if (rx_queue) {
149+
vQueueDelete(rx_queue);
150+
rx_queue = NULL;
151+
}
152+
}
130153
}
131154
return rx_queue_len;
132155
}
@@ -249,26 +272,19 @@ void USBCDC::_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _pari
249272
}
250273

251274
void USBCDC::_onRX(){
275+
arduino_usb_cdc_event_data_t p;
252276
uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE+1];
253277
uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE);
254-
255-
if(rx_queue == NULL) {
256-
return;
257-
}
258-
if (uxQueueSpacesAvailable(rx_queue) < count) {
259-
//this VTaskDelay gives, to Arduino's task, time to the CPU do its processing
260-
//without it, data may be lost when the number of bytes received is higher than CDC buffer size
261-
vTaskDelay(10);
262-
}
263278
for(uint32_t i=0; i<count; i++){
264-
if(!xQueueSend(rx_queue, buf+i, 0)){
265-
// rx_queue overflow - data will be lost
279+
if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 10)) {
280+
p.rx_overflow.dropped_bytes = count - i;
281+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_OVERFLOW_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
282+
log_e("CDC RX Overflow.");
266283
count = i;
267284
break;
268285
}
269286
}
270287
if (count) {
271-
arduino_usb_cdc_event_data_t p;
272288
p.rx.len = count;
273289
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
274290
}

Diff for: cores/esp32/USBCDC.h

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef enum {
3333
ARDUINO_USB_CDC_LINE_CODING_EVENT,
3434
ARDUINO_USB_CDC_RX_EVENT,
3535
ARDUINO_USB_CDC_TX_EVENT,
36+
ARDUINO_USB_CDC_RX_OVERFLOW_EVENT,
3637
ARDUINO_USB_CDC_MAX_EVENT,
3738
} arduino_usb_cdc_event_t;
3839

@@ -50,6 +51,9 @@ typedef union {
5051
struct {
5152
size_t len;
5253
} rx;
54+
struct {
55+
size_t dropped_bytes;
56+
} rx_overflow;
5357
} arduino_usb_cdc_event_data_t;
5458

5559
class USBCDC: public Stream

Diff for: cores/esp32/esp32-hal-uart.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold) {
130130
}
131131

132132

133-
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd)
133+
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t rx_buffer_size, uint16_t tx_buffer_size, bool inverted, uint8_t rxfifo_full_thrhd)
134134
{
135135
if(uart_nr >= SOC_UART_NUM) {
136136
return NULL;
@@ -163,7 +163,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
163163
uart_config.source_clk = UART_SCLK_APB;
164164

165165

166-
ESP_ERROR_CHECK(uart_driver_install(uart_nr, 2*queueLen, 0, 20, &(uart->uart_event_queue), 0));
166+
ESP_ERROR_CHECK(uart_driver_install(uart_nr, rx_buffer_size, tx_buffer_size, 20, &(uart->uart_event_queue), 0));
167167
ESP_ERROR_CHECK(uart_param_config(uart_nr, &uart_config));
168168
ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
169169

Diff for: cores/esp32/esp32-hal-uart.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extern "C" {
6161
struct uart_struct_t;
6262
typedef struct uart_struct_t uart_t;
6363

64-
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted, uint8_t rxfifo_full_thrhd);
64+
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t rx_buffer_size, uint16_t tx_buffer_size, bool inverted, uint8_t rxfifo_full_thrhd);
6565
void uartEnd(uart_t* uart);
6666

6767
// This is used to retrieve the Event Queue pointer from a UART IDF Driver in order to allow user to deal with its events

0 commit comments

Comments
 (0)