Skip to content

Commit b8b32ea

Browse files
feat(uart): refactor PeriMan detaching function init (#11896)
* feat(uart): refactor PeriMan detaching function init Removed uart_init_PeriMan function and related comments. * feat(uart): remove uart_init_PeriMan function declaration Removed unused uart_init_PeriMan function declaration. * feat(uart): remove uart_init_PeriMan call in constructor Removed call to uart_init_PeriMan in HardwareSerial destructor. * feat(uart): reorganize Serial0 setup call to show in log_v within CDC * feat(hwcdc): Refactor HWCDC constructor for bus deinitialization Refactor HWCDC constructor to conditionally set bus deinitialization for USB DM and DP. * fix(hwcdc): fixes problem with log message * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent c9a5d27 commit b8b32ea

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

cores/esp32/HWCDC.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,6 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
253253
}
254254

255255
HWCDC::HWCDC() {
256-
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit);
257-
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit);
258256
// SOF in ISR causes problems for uploading firmware
259257
// lastSOF_ms = 0;
260258
// SOF_TIMEOUT = 5;
@@ -324,14 +322,19 @@ void HWCDC::begin(unsigned long baud) {
324322

325323
// Peripheral Manager setting for USB D+ D- pins
326324
uint8_t pin = USB_INT_PHY0_DM_GPIO_NUM;
325+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DM) == NULL) {
326+
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit);
327+
}
327328
if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) {
328329
goto err;
329330
}
330331
pin = USB_INT_PHY0_DP_GPIO_NUM;
332+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DP) == NULL) {
333+
perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit);
334+
}
331335
if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) {
332336
goto err;
333337
}
334-
335338
// Configure PHY
336339
// USB_Serial_JTAG use internal PHY
337340
USB_SERIAL_JTAG.conf0.phy_sel = 0;

cores/esp32/HardwareSerial.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ HardwareSerial::HardwareSerial(uint8_t uart_nr)
136136
}
137137
}
138138
#endif
139-
// set deinit function in the Peripheral Manager
140-
uart_init_PeriMan();
141139
}
142140

143141
HardwareSerial::~HardwareSerial() {

cores/esp32/esp32-hal-uart.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
479479
ret &= perimanSetPinBus(rxPin, ESP32_BUS_TYPE_UART_RX, (void *)uart, uart_num, -1);
480480
if (ret) {
481481
uart->_rxPin = rxPin;
482+
// set Peripheral Manager deInit Callback for this UART pin
483+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_RX) == NULL) {
484+
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RX, _uartDetachBus_RX);
485+
}
482486
}
483487
}
484488
if (!ret) {
@@ -502,6 +506,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
502506
ret &= perimanSetPinBus(txPin, ESP32_BUS_TYPE_UART_TX, (void *)uart, uart_num, -1);
503507
if (ret) {
504508
uart->_txPin = txPin;
509+
// set Peripheral Manager deInit Callback for this UART pin
510+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_TX) == NULL) {
511+
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_TX, _uartDetachBus_TX);
512+
}
505513
}
506514
}
507515
if (!ret) {
@@ -525,6 +533,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
525533
ret &= perimanSetPinBus(ctsPin, ESP32_BUS_TYPE_UART_CTS, (void *)uart, uart_num, -1);
526534
if (ret) {
527535
uart->_ctsPin = ctsPin;
536+
// set Peripheral Manager deInit Callback for this UART pin
537+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_CTS) == NULL) {
538+
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_CTS, _uartDetachBus_CTS);
539+
}
528540
}
529541
}
530542
if (!ret) {
@@ -548,6 +560,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
548560
ret &= perimanSetPinBus(rtsPin, ESP32_BUS_TYPE_UART_RTS, (void *)uart, uart_num, -1);
549561
if (ret) {
550562
uart->_rtsPin = rtsPin;
563+
// set Peripheral Manager deInit Callback for this UART pin
564+
if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_RTS) == NULL) {
565+
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RTS, _uartDetachBus_RTS);
566+
}
551567
}
552568
}
553569
if (!ret) {
@@ -567,14 +583,6 @@ int8_t uart_get_TxPin(uint8_t uart_num) {
567583
return _uart_bus_array[uart_num]._txPin;
568584
}
569585

570-
void uart_init_PeriMan(void) {
571-
// set Peripheral Manager deInit Callback for each UART pin
572-
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RX, _uartDetachBus_RX);
573-
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_TX, _uartDetachBus_TX);
574-
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_CTS, _uartDetachBus_CTS);
575-
perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RTS, _uartDetachBus_RTS);
576-
}
577-
578586
// Routines that take care of UART events will be in the HardwareSerial Class code
579587
void uartGetEventQueue(uart_t *uart, QueueHandle_t *q) {
580588
// passing back NULL for the Queue pointer when UART is not initialized yet

cores/esp32/esp32-hal-uart.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, in
7979
// helper functions
8080
int8_t uart_get_RxPin(uint8_t uart_num);
8181
int8_t uart_get_TxPin(uint8_t uart_num);
82-
void uart_init_PeriMan(void);
8382

8483
// Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins
8584
// UART_HW_FLOWCTRL_DISABLE = 0x0 disable hardware flow control

cores/esp32/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ __attribute__((weak)) uint64_t getArduinoSetupWaitTime_ms(void) {
5050
}
5151

5252
void loopTask(void *pvParameters) {
53-
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
54-
// sets UART0 (default console) RX/TX pins as already configured in boot or as defined in variants/pins_arduino.h
55-
Serial0.setPins(gpioNumberToDigitalPin(SOC_RX0), gpioNumberToDigitalPin(SOC_TX0));
56-
// time in ms that the sketch may wait before starting its execution - default is zero
57-
// usually done for opening the Serial Monitor and seeing all debug messages
5853
delay(getArduinoSetupWaitTime_ms());
59-
#endif
6054
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
6155
printBeforeSetupInfo();
6256
#else
6357
if (shouldPrintChipDebugReport()) {
6458
printBeforeSetupInfo();
6559
}
60+
#endif
61+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
62+
// sets UART0 (default console) RX/TX pins as already configured in boot or as defined in variants/pins_arduino.h
63+
Serial0.setPins(gpioNumberToDigitalPin(SOC_RX0), gpioNumberToDigitalPin(SOC_TX0));
64+
// time in ms that the sketch may wait before starting its execution - default is zero
65+
// usually done for opening the Serial Monitor and seeing all debug messages
6666
#endif
6767
setup();
6868
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG

0 commit comments

Comments
 (0)