Page 1 of 1

[Solved] HardwareSerial 3 error on Maple Mini board

Posted: Sun Jan 26, 2020 2:38 am
by fredbox

Code: Select all

HardwareSerial Serial3(USART3); // compiles
HardwareSerial Serial3(PB_11, PB_10); // compiles
HardwareSerial Serial3(PB11, PB10); // error: call of overloaded 'HardwareSerial(int, int)' is ambiguous
All three examples work if the board type is changed to a blue pill.
Casting PB11 to an int lets it compile. PB11 is defined as 0 in variant.h.
digitalPinToPinName(PB11) and PB_11 both print as 27.
I've been looking through cores/arduino/HardwareSerial.cpp and HardwareSerial.h for the problem but haven't spotted it.

Code: Select all

HardwareSerial::HardwareSerial(uint32_t _rx, uint32_t _tx)
I can't see why HardwareSerial() doesn't accept 0 as the first argument.

Code: Select all

HardwareSerial Serial3(0,1); //same as PB11, PB10 - error
HardwareSerial Serial3(2,1); // wrong values but compiles without error

Re: HardwareSerial 3 error on Maple Mini board

Posted: Sun Jan 26, 2020 6:01 am
by fpiSTM
Right, seems strange.
Maybe a cache issue of the Arduino IDE. I will check if I can reproduce.

Re: HardwareSerial 3 error on Maple Mini board

Posted: Sun Jan 26, 2020 6:48 am
by fpiSTM
OK, I think, I found the issue.
Since the Half duplex was introduced, the error appears.

Code: Select all

    
- HardwareSerial(void *peripheral);
+ HardwareSerial(void *peripheral, bool halfDuplex = false);
So when 0 is used as first argument, the compiler doesn't know which constructor to use as 0 can be interpreted by NULL:

Code: Select all

    HardwareSerial(uint32_t _rx, uint32_t _tx);
    HardwareSerial(void *peripheral, bool halfDuplex = false);
as both prototype is valid.

One workaround is to use a cast:

Code: Select all

HardwareSerial Serial3((uint32_t)PB11, PB10);
Maybe this prototype can be added or changed the uint32_t to int:

Code: Select all

HardwareSerial(int _rx, int _tx);

Re: HardwareSerial 3 error on Maple Mini board

Posted: Sun Jan 26, 2020 3:38 pm
by fredbox
For "hello world" using the pin names gives the smallest code size:

Code: Select all

HardwareSerial Serial3((uint32_t)PB11,PB10);  // 8528 bytes flash - 676 bytes ram
HardwareSerial Serial3(USART3);          // 8452 bytes flash - 676 bytes ram
HardwareSerial Serial3(PB_11, PB_10);    // 8352 bytes flash - 676 bytes ram

Re: HardwareSerial 3 error on Maple Mini board

Posted: Sun Jan 26, 2020 4:03 pm
by fpiSTM
That is normal as PinName (PY_N) is the low level.
Using pin number PYN embeds the function to convert it to PinName.
Using peripheral name USARTx gave the PinName directly so it is an intermediate level.

Re: HardwareSerial 3 error on Maple Mini board

Posted: Mon Jan 27, 2020 4:41 pm
by fpiSTM
@fredbox

I've made a PR on GitHub to fix this issue:
https://github.com/stm32duino/Arduino_C ... 2/pull/893

I define explicitly an enum then it is no more ambiguous.

Re: HardwareSerial 3 error on Maple Mini board

Posted: Mon Jan 27, 2020 11:19 pm
by fredbox
It was more of a puzzle to me than an actual problem. I'm reviewing the fix on Github to understand how you solved it. Thanks.