Skip to content

Commit b98b52a

Browse files
authored
Reorganize RS485 Mode - FlowCtrl (espressif#8718)
1 parent 6b1933b commit b98b52a

File tree

4 files changed

+66
-82
lines changed

4 files changed

+66
-82
lines changed

cores/esp32/HardwareSerial.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -576,14 +576,20 @@ bool HardwareSerial::setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t r
576576
return uartSetPins(_uart_nr, rxPin, txPin, ctsPin, rtsPin);
577577
}
578578

579-
// Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
580-
bool HardwareSerial::setHwFlowCtrlMode(uint8_t mode, uint8_t threshold)
579+
// Enables or disables Hardware Flow Control using RTS and/or CTS pins
580+
// must use setAllPins() in order to set RTS/CTS pins
581+
// SerialHwFlowCtrl = UART_HW_FLOWCTRL_DISABLE, UART_HW_FLOWCTRL_RTS,
582+
// UART_HW_FLOWCTRL_CTS, UART_HW_FLOWCTRL_CTS_RTS
583+
bool HardwareSerial::setHwFlowCtrlMode(SerialHwFlowCtrl mode, uint8_t threshold)
581584
{
582585
return uartSetHwFlowCtrlMode(_uart, mode, threshold);
583586
}
584587

585-
// Sets the uart mode in the esp32 uart for use with RS485 modes (HwFlowCtrl must be disabled and RTS pin set)
586-
bool HardwareSerial::setMode(uint8_t mode)
588+
// Sets the uart mode in the esp32 uart for use with RS485 modes
589+
// HwFlowCtrl must be disabled and RTS pin set
590+
// SerialMode = UART_MODE_UART, UART_MODE_RS485_HALF_DUPLEX, UART_MODE_IRDA,
591+
// or testing mode: UART_MODE_RS485_COLLISION_DETECT, UART_MODE_RS485_APP_CTRL
592+
bool HardwareSerial::setMode(SerialMode mode)
587593
{
588594
return uartSetMode(_uart, mode);
589595
}

cores/esp32/HardwareSerial.h

+41-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@
5656
#include "freertos/task.h"
5757
#include "freertos/semphr.h"
5858

59+
enum SerialConfig {
60+
SERIAL_5N1 = 0x8000010,
61+
SERIAL_6N1 = 0x8000014,
62+
SERIAL_7N1 = 0x8000018,
63+
SERIAL_8N1 = 0x800001c,
64+
SERIAL_5N2 = 0x8000030,
65+
SERIAL_6N2 = 0x8000034,
66+
SERIAL_7N2 = 0x8000038,
67+
SERIAL_8N2 = 0x800003c,
68+
SERIAL_5E1 = 0x8000012,
69+
SERIAL_6E1 = 0x8000016,
70+
SERIAL_7E1 = 0x800001a,
71+
SERIAL_8E1 = 0x800001e,
72+
SERIAL_5E2 = 0x8000032,
73+
SERIAL_6E2 = 0x8000036,
74+
SERIAL_7E2 = 0x800003a,
75+
SERIAL_8E2 = 0x800003e,
76+
SERIAL_5O1 = 0x8000013,
77+
SERIAL_6O1 = 0x8000017,
78+
SERIAL_7O1 = 0x800001b,
79+
SERIAL_8O1 = 0x800001f,
80+
SERIAL_5O2 = 0x8000033,
81+
SERIAL_6O2 = 0x8000037,
82+
SERIAL_7O2 = 0x800003b,
83+
SERIAL_8O2 = 0x800003f
84+
};
85+
86+
typedef uart_mode_t SerialMode;
87+
typedef uart_hw_flowcontrol_t SerialHwFlowCtrl;
88+
5989
typedef enum {
6090
UART_NO_ERROR,
6191
UART_BREAK_ERROR,
@@ -169,9 +199,18 @@ class HardwareSerial: public Stream
169199
// When pins are changed, it will detach the previous ones
170200
bool setPins(int8_t rxPin, int8_t txPin, int8_t ctsPin = -1, int8_t rtsPin = -1);
171201
// Enables or disables Hardware Flow Control using RTS and/or CTS pins (must use setAllPins() before)
172-
bool setHwFlowCtrlMode(uint8_t mode = HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length
202+
// UART_HW_FLOWCTRL_DISABLE = 0x0 disable hardware flow control
203+
// UART_HW_FLOWCTRL_RTS = 0x1 enable RX hardware flow control (rts)
204+
// UART_HW_FLOWCTRL_CTS = 0x2 enable TX hardware flow control (cts)
205+
// UART_HW_FLOWCTRL_CTS_RTS = 0x3 enable hardware flow control
206+
bool setHwFlowCtrlMode(SerialHwFlowCtrl mode = UART_HW_FLOWCTRL_CTS_RTS, uint8_t threshold = 64); // 64 is half FIFO Length
173207
// Used to set RS485 modes such as UART_MODE_RS485_HALF_DUPLEX for Auto RTS function on ESP32
174-
bool setMode(uint8_t mode);
208+
// UART_MODE_UART = 0x00 mode: regular UART mode
209+
// UART_MODE_RS485_HALF_DUPLEX = 0x01 mode: half duplex RS485 UART mode control by RTS pin
210+
// UART_MODE_IRDA = 0x02 mode: IRDA UART mode
211+
// UART_MODE_RS485_COLLISION_DETECT = 0x03 mode: RS485 collision detection UART mode (used for test purposes)
212+
// UART_MODE_RS485_APP_CTRL = 0x04 mode: application control RS485 UART mode (used for test purposes)
213+
bool setMode(SerialMode mode);
175214
size_t setRxBufferSize(size_t new_size);
176215
size_t setTxBufferSize(size_t new_size);
177216

cores/esp32/esp32-hal-uart.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, in
335335
}
336336

337337
//
338-
bool uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold) {
338+
bool uartSetHwFlowCtrlMode(uart_t *uart, uart_hw_flowcontrol_t mode, uint8_t threshold) {
339339
if(uart == NULL) {
340340
return false;
341341
}
342342
// IDF will issue corresponding error message when mode or threshold are wrong and prevent crashing
343343
// IDF will check (mode > HW_FLOWCTRL_CTS_RTS || threshold >= SOC_UART_FIFO_LEN)
344344
UART_MUTEX_LOCK();
345-
bool retCode = (ESP_OK == uart_set_hw_flow_ctrl(uart->num, (uart_hw_flowcontrol_t) mode, threshold));
345+
bool retCode = (ESP_OK == uart_set_hw_flow_ctrl(uart->num, mode, threshold));
346346
UART_MUTEX_UNLOCK();
347347
return retCode;
348348
}
@@ -716,7 +716,7 @@ void uart_install_putc()
716716

717717
// Routines that take care of UART mode in the HardwareSerial Class code
718718
// used to set UART_MODE_RS485_HALF_DUPLEX auto RTS for TXD for ESP32 chips
719-
bool uartSetMode(uart_t *uart, uint8_t mode)
719+
bool uartSetMode(uart_t *uart, uart_mode_t mode)
720720
{
721721
if (uart == NULL || uart->num >= SOC_UART_NUM)
722722
{

cores/esp32/esp32-hal-uart.h

+12-73
Original file line numberDiff line numberDiff line change
@@ -27,77 +27,7 @@ extern "C" {
2727
#include <stdlib.h>
2828
#include "freertos/FreeRTOS.h"
2929
#include "freertos/queue.h"
30-
31-
#ifdef __cplusplus
32-
enum SerialConfig {
33-
SERIAL_5N1 = 0x8000010,
34-
SERIAL_6N1 = 0x8000014,
35-
SERIAL_7N1 = 0x8000018,
36-
SERIAL_8N1 = 0x800001c,
37-
SERIAL_5N2 = 0x8000030,
38-
SERIAL_6N2 = 0x8000034,
39-
SERIAL_7N2 = 0x8000038,
40-
SERIAL_8N2 = 0x800003c,
41-
SERIAL_5E1 = 0x8000012,
42-
SERIAL_6E1 = 0x8000016,
43-
SERIAL_7E1 = 0x800001a,
44-
SERIAL_8E1 = 0x800001e,
45-
SERIAL_5E2 = 0x8000032,
46-
SERIAL_6E2 = 0x8000036,
47-
SERIAL_7E2 = 0x800003a,
48-
SERIAL_8E2 = 0x800003e,
49-
SERIAL_5O1 = 0x8000013,
50-
SERIAL_6O1 = 0x8000017,
51-
SERIAL_7O1 = 0x800001b,
52-
SERIAL_8O1 = 0x800001f,
53-
SERIAL_5O2 = 0x8000033,
54-
SERIAL_6O2 = 0x8000037,
55-
SERIAL_7O2 = 0x800003b,
56-
SERIAL_8O2 = 0x800003f
57-
};
58-
#else
59-
#define SERIAL_5N1 0x8000010
60-
#define SERIAL_6N1 0x8000014
61-
#define SERIAL_7N1 0x8000018
62-
#define SERIAL_8N1 0x800001c
63-
#define SERIAL_5N2 0x8000030
64-
#define SERIAL_6N2 0x8000034
65-
#define SERIAL_7N2 0x8000038
66-
#define SERIAL_8N2 0x800003c
67-
#define SERIAL_5E1 0x8000012
68-
#define SERIAL_6E1 0x8000016
69-
#define SERIAL_7E1 0x800001a
70-
#define SERIAL_8E1 0x800001e
71-
#define SERIAL_5E2 0x8000032
72-
#define SERIAL_6E2 0x8000036
73-
#define SERIAL_7E2 0x800003a
74-
#define SERIAL_8E2 0x800003e
75-
#define SERIAL_5O1 0x8000013
76-
#define SERIAL_6O1 0x8000017
77-
#define SERIAL_7O1 0x800001b
78-
#define SERIAL_8O1 0x800001f
79-
#define SERIAL_5O2 0x8000033
80-
#define SERIAL_6O2 0x8000037
81-
#define SERIAL_7O2 0x800003b
82-
#define SERIAL_8O2 0x800003f
83-
#endif // __cplusplus
84-
85-
// These are Hardware Flow Contol possible usage
86-
// equivalent to UDF enum uart_hw_flowcontrol_t from
87-
// https://github.com/espressif/esp-idf/blob/master/components/hal/include/hal/uart_types.h#L75-L81
88-
#define HW_FLOWCTRL_DISABLE 0x0 // disable HW Flow Control
89-
#define HW_FLOWCTRL_RTS 0x1 // use only RTS PIN for HW Flow Control
90-
#define HW_FLOWCTRL_CTS 0x2 // use only CTS PIN for HW Flow Control
91-
#define HW_FLOWCTRL_CTS_RTS 0x3 // use both CTS and RTS PIN for HW Flow Control
92-
93-
// These are Hardware Uart Modes possible usage
94-
// equivalent to UDF enum uart_mode_t from
95-
// https://github.com/espressif/esp-idf/blob/master/components/hal/include/hal/uart_types.h#L34-L40
96-
#define MODE_UART 0x00 // mode: regular UART mode
97-
#define MODE_RS485_HALF_DUPLEX 0x01 // mode: half duplex RS485 UART mode control by RTS pin
98-
#define MODE_IRDA 0x02 // mode: IRDA UART mode
99-
#define MODE_RS485_COLLISION_DETECT 0x03 // mode: RS485 collision detection UART mode (used for test purposes)
100-
#define MODE_RS485_APP_CTRL 0x04
30+
#include "hal/uart_types.h"
10131

10232
struct uart_struct_t;
10333
typedef struct uart_struct_t uart_t;
@@ -145,11 +75,20 @@ void uart_init_PeriMan(void);
14575

14676

14777
// Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins
148-
bool uartSetHwFlowCtrlMode(uart_t *uart, uint8_t mode, uint8_t threshold);
78+
// UART_HW_FLOWCTRL_DISABLE = 0x0 disable hardware flow control
79+
// UART_HW_FLOWCTRL_RTS = 0x1 enable RX hardware flow control (rts)
80+
// UART_HW_FLOWCTRL_CTS = 0x2 enable TX hardware flow control (cts)
81+
// UART_HW_FLOWCTRL_CTS_RTS = 0x3 enable hardware flow control
82+
bool uartSetHwFlowCtrlMode(uart_t *uart, uart_hw_flowcontrol_t mode, uint8_t threshold);
14983

15084
// Used to set RS485 function -- needs to disable HW Flow Control and set RTS pin to use
15185
// RTS pin becomes RS485 half duplex RE/DE
152-
bool uartSetMode(uart_t *uart, uint8_t mode);
86+
// UART_MODE_UART = 0x00 mode: regular UART mode
87+
// UART_MODE_RS485_HALF_DUPLEX = 0x01 mode: half duplex RS485 UART mode control by RTS pin
88+
// UART_MODE_IRDA = 0x02 mode: IRDA UART mode
89+
// UART_MODE_RS485_COLLISION_DETECT = 0x03 mode: RS485 collision detection UART mode (used for test purposes)
90+
// UART_MODE_RS485_APP_CTRL = 0x04 mode: application control RS485 UART mode (used for test purposes)
91+
bool uartSetMode(uart_t *uart, uart_mode_t mode);
15392

15493
void uartStartDetectBaudrate(uart_t *uart);
15594
unsigned long uartDetectBaudrate(uart_t *uart);

0 commit comments

Comments
 (0)