Skip to content

Commit e013b67

Browse files
committed
[HardwareSerial] Fix ambiguous call of overloaded function
``` error: call of overloaded 'HardwareSerial(int, int)' is ambiguous` 1 | HardwareSerial Serialx(0, 1); | ^ In file included from cores/arduino/WSerial.h:5, from cores/arduino/wiring.h:47, from cores/arduino/Arduino.h:32, from sketch_jan27b.ino.cpp:1: cores/arduino/HardwareSerial.h:106:5: note: candidate: 'HardwareSerial::HardwareSerial(void*, bool)' 106 | HardwareSerial(void *peripheral, bool halfDuplex = false); | ^~~~~~~~~~~~~~ cores/arduino/HardwareSerial.h:104:5: note: candidate: 'HardwareSerial::HardwareSerial(uint32_t, uint32_t)' 104 | HardwareSerial(uint32_t _rx, uint32_t _tx); | ^~~~~~~~~~~~~~ ``` When the Rx Pin is 0 then the compiler doesn't know which constructor to use as 0 can be interpreted like NULL. So both of them are valid: HardwareSerial(uint32_t _rx, uint32_t _tx); HardwareSerial(void *peripheral, bool halfDuplex = false); Defining an explicit type avoid this issue. Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 7193ece commit e013b67

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

cores/arduino/HardwareSerial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ HardwareSerial::HardwareSerial(PinName _rx, PinName _tx)
116116
init(_rx, _tx);
117117
}
118118

119-
HardwareSerial::HardwareSerial(void *peripheral, bool halfDuplex)
119+
HardwareSerial::HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex)
120120
{
121121
// If PIN_SERIALy_RX is not defined assume half-duplex
122122
_serial.pin_rx = NC;
@@ -247,7 +247,7 @@ HardwareSerial::HardwareSerial(void *peripheral, bool halfDuplex)
247247
_serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX);
248248
_serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX);
249249
}
250-
if (halfDuplex) {
250+
if (halfDuplex == HALF_DUPLEX_ENABLED) {
251251
_serial.pin_rx = NC;
252252
}
253253
init(_serial.pin_rx, _serial.pin_tx);

cores/arduino/HardwareSerial.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ typedef uint16_t rx_buffer_index_t;
5757
typedef uint8_t rx_buffer_index_t;
5858
#endif
5959

60+
// A bool should be enough for this
61+
// But it brings an build error due to ambiguous
62+
// call of overloaded HardwareSerial(int, int)
63+
// So defining a dedicated type
64+
typedef enum {
65+
HALF_DUPLEX_DISABLED,
66+
HALF_DUPLEX_ENABLED
67+
} HalfDuplexMode_t;
68+
6069
// Define config for Serial.begin(baud, config);
6170
// below configs are not supported by STM32
6271
//#define SERIAL_5N1 0x00
@@ -103,7 +112,7 @@ class HardwareSerial : public Stream {
103112
public:
104113
HardwareSerial(uint32_t _rx, uint32_t _tx);
105114
HardwareSerial(PinName _rx, PinName _tx);
106-
HardwareSerial(void *peripheral, bool halfDuplex = false);
115+
HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex = HALF_DUPLEX_DISABLED);
107116
HardwareSerial(uint32_t _rxtx);
108117
HardwareSerial(PinName _rxtx);
109118
void begin(unsigned long baud)

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ PIN_WIRE_SCL LITERAL1
270270
HardwareSerial KEYWORD2
271271
setRx KEYWORD2
272272
setTx KEYWORD2
273+
HALF_DUPLEX_DISABLED LITERAL1
274+
HALF_DUPLEX_ENABLED LITERAL1
273275
setHalfDuplex KEYWORD2
274276
isHalfDuplex KEYWORD2
275277
enableHalfDuplexRx KEYWORD2

0 commit comments

Comments
 (0)