Skip to content

Commit 81f27f4

Browse files
authored
Update esp32-hal-uart.c
1 parent 6b257a1 commit 81f27f4

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

cores/esp32/esp32-hal-uart.c

+38-30
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static uart_t _uart_bus_array[] = {
7373

7474
// solves issue https://github.com/espressif/arduino-esp32/issues/6032
7575
// baudrate must be multiplied when CPU Frequency is lower than APB 80MHz
76-
uint32_t _get_effective_baudrate(uint32_t baudrate)
76+
uint32_t _get_effective_baudrate(uint32_t baudrate)
7777
{
7878
uint32_t Freq = getApbFrequency()/1000000;
7979
if (Freq < 80) {
@@ -143,7 +143,7 @@ static void uart_event_task(void *args)
143143
}
144144

145145

146-
bool uartIsDriverInstalled(uart_t* uart)
146+
bool uartIsDriverInstalled(uart_t* uart)
147147
{
148148
if(uart == NULL) {
149149
return 0;
@@ -155,15 +155,27 @@ bool uartIsDriverInstalled(uart_t* uart)
155155
return false;
156156
}
157157

158-
void uartSetPins(uart_t* uart, uint8_t rxPin, uint8_t txPin)
158+
// Valid pin UART_PIN_NO_CHANGE is defined to (-1)
159+
// Negative Pin Number will keep it unmodified, thus this function can set individual pins
160+
void uartSetPins(uart_t* uart, int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin)
159161
{
160-
if(uart == NULL || rxPin >= SOC_GPIO_PIN_COUNT || txPin >= SOC_GPIO_PIN_COUNT) {
162+
if(uart == NULL) {
161163
return;
162164
}
163165
UART_MUTEX_LOCK();
164-
ESP_ERROR_CHECK(uart_set_pin(uart->num, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
165-
UART_MUTEX_UNLOCK();
166+
// IDF uart_set_pin() will issue necessary Error Message and take care of all GPIO Number validation.
167+
uart_set_pin(uart->num, txPin, rxPin, ctsPin, rtsPin);
168+
UART_MUTEX_UNLOCK();
169+
}
166170

171+
//
172+
void uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold) {
173+
if(uart == NULL) {
174+
return;
175+
}
176+
// IDF will issue corresponding error message when mode or threshold are wrong and prevent crashing
177+
// IDF will check (mode > HW_FLOWCTRL_CTS_RTS || threshold >= SOC_UART_FIFO_LEN)
178+
uart_set_hw_flow_ctrl(uart->num, (uart_hw_flowcontrol_t) mode, threshold);
167179
}
168180

169181

@@ -173,10 +185,6 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
173185
return NULL;
174186
}
175187

176-
if(rxPin == -1 && txPin == -1) {
177-
return NULL;
178-
}
179-
180188
uart_t* uart = &_uart_bus_array[uart_nr];
181189

182190
if (uart_is_driver_installed(uart_nr)) {
@@ -208,10 +216,10 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
208216
ESP_ERROR_CHECK(uart_param_config(uart_nr, &uart_config));
209217
ESP_ERROR_CHECK(uart_set_pin(uart_nr, txPin, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
210218

211-
// Is it right or the idea is to swap rx and tx pins?
219+
// Is it right or the idea is to swap rx and tx pins?
212220
if (inverted) {
213221
// invert signal for both Rx and Tx
214-
ESP_ERROR_CHECK(uart_set_line_inverse(uart_nr, UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV));
222+
ESP_ERROR_CHECK(uart_set_line_inverse(uart_nr, UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV));
215223
}
216224

217225
// Creating UART event Task
@@ -231,7 +239,7 @@ void uartEnd(uart_t* uart)
231239
if(uart == NULL) {
232240
return;
233241
}
234-
242+
235243
UART_MUTEX_LOCK();
236244
uart_driver_delete(uart->num);
237245
if (uart->envent_task) {
@@ -248,13 +256,13 @@ void uartSetRxInvert(uart_t* uart, bool invert)
248256
if (uart == NULL)
249257
return;
250258
#if 0
251-
// POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
259+
// POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
252260
// IDF or LL set/reset the whole inv_mask!
253261
if (invert)
254262
ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_RXD_INV));
255263
else
256264
ESP_ERROR_CHECK(uart_set_line_inverse(uart->num, UART_SIGNAL_INV_DISABLE));
257-
265+
258266
#else
259267
// this implementation is better over IDF API because it only affects RXD
260268
// this is supported in ESP32, ESP32-S2 and ESP32-C3
@@ -263,7 +271,7 @@ void uartSetRxInvert(uart_t* uart, bool invert)
263271
hw->conf0.rxd_inv = 1;
264272
else
265273
hw->conf0.rxd_inv = 0;
266-
#endif
274+
#endif
267275
}
268276

269277

@@ -289,7 +297,7 @@ uint32_t uartAvailableForWrite(uart_t* uart)
289297
return 0;
290298
}
291299
UART_MUTEX_LOCK();
292-
uint32_t available = uart_ll_get_txfifo_len(UART_LL_GET_HW(uart->num));
300+
uint32_t available = uart_ll_get_txfifo_len(UART_LL_GET_HW(uart->num));
293301
UART_MUTEX_UNLOCK();
294302
return available;
295303
}
@@ -373,7 +381,7 @@ void uartFlushTxOnly(uart_t* uart, bool txOnly)
373381
if(uart == NULL) {
374382
return;
375383
}
376-
384+
377385
UART_MUTEX_LOCK();
378386
while(!uart_ll_is_tx_idle(UART_LL_GET_HW(uart->num)));
379387

@@ -486,7 +494,7 @@ int log_printf(const char *format, ...)
486494
xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY);
487495
}
488496
#endif
489-
497+
490498
vsnprintf(temp, len+1, format, arg);
491499
ets_printf("%s", temp);
492500

@@ -563,22 +571,22 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
563571
* To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
564572
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
565573
* rounded to the closed real baudrate.
566-
*
574+
*
567575
* ESP32-C3 reports wrong baud rate detection as shown below:
568-
*
576+
*
569577
* This will help in a future recall for the C3.
570578
* Baud Sent: Baud Read:
571579
* 300 --> 19536
572580
* 2400 --> 19536
573-
* 4800 --> 19536
574-
* 9600 --> 28818
581+
* 4800 --> 19536
582+
* 9600 --> 28818
575583
* 19200 --> 57678
576584
* 38400 --> 115440
577585
* 57600 --> 173535
578586
* 115200 --> 347826
579587
* 230400 --> 701754
580-
*
581-
*
588+
*
589+
*
582590
*/
583591
void uartStartDetectBaudrate(uart_t *uart) {
584592
if(uart == NULL) {
@@ -588,10 +596,10 @@ void uartStartDetectBaudrate(uart_t *uart) {
588596
uart_dev_t *hw = UART_LL_GET_HW(uart->num);
589597

590598
#ifdef CONFIG_IDF_TARGET_ESP32C3
591-
599+
592600
// ESP32-C3 requires further testing
593-
// Baud rate detection returns wrong values
594-
601+
// Baud rate detection returns wrong values
602+
595603
log_e("ESP32-C3 baud rate detection is not supported.");
596604
return;
597605

@@ -607,7 +615,7 @@ void uartStartDetectBaudrate(uart_t *uart) {
607615
hw->auto_baud.en = 1;
608616
#endif
609617
}
610-
618+
611619
unsigned long
612620
uartDetectBaudrate(uart_t *uart)
613621
{
@@ -664,4 +672,4 @@ uartDetectBaudrate(uart_t *uart)
664672
log_e("ESP32-C3 baud rate detection is not supported.");
665673
return 0;
666674
#endif
667-
}
675+
}

0 commit comments

Comments
 (0)