Skip to content

Commit 636b8a5

Browse files
SuGliderJAndrassy
authored andcommitted
Fixes uart attach/detach (espressif#8820)
* Fixes uart attach/detach * Fixes uart logging locks * enforces Serial0 setPins
1 parent a531804 commit 636b8a5

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ _eventTask(NULL)
107107
}
108108
}
109109
#endif
110-
// sets UART0 (default console) RX/TX pins as already configured in boot
111-
if (uart_nr == 0) {
112-
setPins(SOC_RX0, SOC_TX0);
113-
}
114110
// set deinit function in the Peripheral Manager
115111
uart_init_PeriMan();
116112
}

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
184184

185185
bool retCode = true;
186186
if (rxPin >= 0) {
187+
// forces a clean detaching from a previous peripheral
188+
if (perimanGetPinBusType(rxPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(rxPin, ESP32_BUS_TYPE_INIT, NULL);
187189
// connect RX Pad
188190
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
189191
if (ret) {
@@ -196,6 +198,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
196198
retCode &= ret;
197199
}
198200
if (txPin >= 0) {
201+
// forces a clean detaching from a previous peripheral
202+
if (perimanGetPinBusType(txPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(txPin, ESP32_BUS_TYPE_INIT, NULL);
199203
// connect TX Pad
200204
bool ret = ESP_OK == uart_set_pin(uart->num, txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
201205
if (ret) {
@@ -208,6 +212,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
208212
retCode &= ret;
209213
}
210214
if (ctsPin >= 0) {
215+
// forces a clean detaching from a previous peripheral
216+
if (perimanGetPinBusType(ctsPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(ctsPin, ESP32_BUS_TYPE_INIT, NULL);
211217
// connect CTS Pad
212218
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, ctsPin);
213219
if (ret) {
@@ -220,6 +226,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
220226
retCode &= ret;
221227
}
222228
if (rtsPin >= 0) {
229+
// forces a clean detaching from a previous peripheral
230+
if (perimanGetPinBusType(rtsPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(rtsPin, ESP32_BUS_TYPE_INIT, NULL);
223231
// connect RTS Pad
224232
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, rtsPin, UART_PIN_NO_CHANGE);
225233
if (ret) {
@@ -759,12 +767,14 @@ int log_printfv(const char *format, va_list arg)
759767
return 0;
760768
}
761769
}
770+
/*
771+
// This causes dead locks with logging in specific cases and also with C++ constructors that may send logs
762772
#if !CONFIG_DISABLE_HAL_LOCKS
763773
if(s_uart_debug_nr != -1 && _uart_bus_array[s_uart_debug_nr].lock){
764774
xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY);
765775
}
766776
#endif
767-
777+
*/
768778
#if CONFIG_IDF_TARGET_ESP32C3
769779
vsnprintf(temp, len+1, format, arg);
770780
ets_printf("%s", temp);
@@ -774,15 +784,19 @@ int log_printfv(const char *format, va_list arg)
774784
ets_write_char_uart(temp[i]);
775785
}
776786
#endif
777-
787+
/*
788+
// This causes dead locks with logging and also with constructors that may send logs
778789
#if !CONFIG_DISABLE_HAL_LOCKS
779790
if(s_uart_debug_nr != -1 && _uart_bus_array[s_uart_debug_nr].lock){
780791
xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock);
781792
}
782793
#endif
794+
*/
783795
if(len >= sizeof(loc_buf)){
784796
free(temp);
785797
}
798+
// flushes TX - make sure that the log message is completely sent.
799+
if(s_uart_debug_nr != -1) while(!uart_ll_is_tx_idle(UART_LL_GET_HW(s_uart_debug_nr)));
786800
return len;
787801
}
788802

Diff for: cores/esp32/main.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ __attribute__((weak)) bool shouldPrintChipDebugReport(void) {
4545

4646
void loopTask(void *pvParameters)
4747
{
48+
// sets UART0 (default console) RX/TX pins as already configured in boot or as defined in variants/pins_arduino.h
49+
Serial0.setPins(SOC_RX0, SOC_TX0);
4850
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
4951
printBeforeSetupInfo();
5052
#else

0 commit comments

Comments
 (0)