From b06c3d5f6d0977c91d4e05b72e6ba25cbe3e0a2b Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 27 Jan 2020 17:37:41 +0100 Subject: [PATCH] [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 --- cores/arduino/HardwareSerial.cpp | 4 ++-- cores/arduino/HardwareSerial.h | 11 ++++++++++- keywords.txt | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 1b1d8fd634..57975975b9 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -116,7 +116,7 @@ HardwareSerial::HardwareSerial(PinName _rx, PinName _tx) init(_rx, _tx); } -HardwareSerial::HardwareSerial(void *peripheral, bool halfDuplex) +HardwareSerial::HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex) { // If PIN_SERIALy_RX is not defined assume half-duplex _serial.pin_rx = NC; @@ -247,7 +247,7 @@ HardwareSerial::HardwareSerial(void *peripheral, bool halfDuplex) _serial.pin_rx = pinmap_pin(peripheral, PinMap_UART_RX); _serial.pin_tx = pinmap_pin(peripheral, PinMap_UART_TX); } - if (halfDuplex) { + if (halfDuplex == HALF_DUPLEX_ENABLED) { _serial.pin_rx = NC; } init(_serial.pin_rx, _serial.pin_tx); diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index a2a5644776..b9325858f5 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -57,6 +57,15 @@ typedef uint16_t rx_buffer_index_t; typedef uint8_t rx_buffer_index_t; #endif +// A bool should be enough for this +// But it brings an build error due to ambiguous +// call of overloaded HardwareSerial(int, int) +// So defining a dedicated type +typedef enum { + HALF_DUPLEX_DISABLED, + HALF_DUPLEX_ENABLED +} HalfDuplexMode_t; + // Define config for Serial.begin(baud, config); // below configs are not supported by STM32 //#define SERIAL_5N1 0x00 @@ -103,7 +112,7 @@ class HardwareSerial : public Stream { public: HardwareSerial(uint32_t _rx, uint32_t _tx); HardwareSerial(PinName _rx, PinName _tx); - HardwareSerial(void *peripheral, bool halfDuplex = false); + HardwareSerial(void *peripheral, HalfDuplexMode_t halfDuplex = HALF_DUPLEX_DISABLED); HardwareSerial(uint32_t _rxtx); HardwareSerial(PinName _rxtx); void begin(unsigned long baud) diff --git a/keywords.txt b/keywords.txt index 46f16f7a21..019b437912 100644 --- a/keywords.txt +++ b/keywords.txt @@ -270,6 +270,8 @@ PIN_WIRE_SCL LITERAL1 HardwareSerial KEYWORD2 setRx KEYWORD2 setTx KEYWORD2 +HALF_DUPLEX_DISABLED LITERAL1 +HALF_DUPLEX_ENABLED LITERAL1 setHalfDuplex KEYWORD2 isHalfDuplex KEYWORD2 enableHalfDuplexRx KEYWORD2