arduino / nicla-sense-me-fw

Arduino Nicla Sense ME resources (libraries, bootloader, host pc utilities)
GNU Affero General Public License v3.0
45 stars 27 forks source link

Bug with Serial + Hardware Serial when using different baud rates #55

Closed CiprianFlorin-Ifrim closed 2 years ago

CiprianFlorin-Ifrim commented 2 years ago

Description of defect

If I intitialize Serial.begin(115200) and hardware Serial1.begin(115200), both work. If I initialize Serial.begin(9600) and hardware Serial1.begin(9600), both work. If, however, I initialize Serial.begin(115200) and hardware Serial1.begin(9600), the whole code freezes.

Moreover, the battery bug fix solved in 3.0.0 is still persisting with UART.

Target(s) affected by this defect ?

Nicla Sense ME

Toolchain(s) (name and version) displaying this defect ?

Mbed OS Nicla Boards 3.0.1 Latest bootloader Arduino 1.8.16

What version of Mbed-os are you using (tag or sha) ?

mbed-os-3.0.1

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

Mbed OS Nicla Boards 3.0.1 Latest bootloader available in the Nicla FW repo Arduino 1.8.16

How is this defect reproduced ?

Simple example code:

#include <Arduino.h>
#include "Nicla_System.h"

void setup()
{
  nicla::begin();
  Serial.begin(115200);
  Serial1.begin(9600);

  delay(1000);
}

void loop() {
  while(Serial.available()) {
    char incomingCharacter = Serial.read();
    if (incomingCharacter == '1') {
      Serial.println("Command activated");
    }
    delay(100);                   
  }
}
facchinm commented 2 years ago

Hi @CiprianFlorin-Ifrim , in Nicla Sense Serial and Serial1 are indeed the same object (and the same couple of pins) due to how serial ports are defined in the core (see here https://github.com/arduino/ArduinoCore-mbed/blob/master/cores/arduino/Arduino.h#L108-L110 ). Moreover, we can't instantiate two different serials since the onboard nRF52832 only has one hardware UART instance.

Maybe it would be less confusing for the end user to undefine Serial1 object if only one UART is available. @per1234 @sebromero what do you think about that?

per1234 commented 2 years ago

Maybe it would be less confusing for the end user to undefine Serial1 object if only one UART is available.

Yes. I said the same back during the discussion that somehow resulted in that code being added:

https://github.com/arduino/ArduinoCore-mbed/pull/54#issuecomment-691636008

CiprianFlorin-Ifrim commented 2 years ago

Thank you for the replies. I was not aware it was the same hardware serial and spent a few good hours on my main code thinking I had some issues there.

I think it would be good to add in the Cheat Sheet that they are the same and same baur rate should be set for both: https://docs.arduino.cc/tutorials/nicla-sense-me/cheat-sheet#uart

Leaving that aside, have you checked "Moreover, the battery bug fix solved in 3.0.0 is still persisting with UART.". I still cannot run the code off the battery without having the USB first. I do not know if it is an I2C issue or UART issue. But it works with a simple code, and doesn't with the protocols started.

sebromero commented 2 years ago

Maybe it would be less confusing for the end user to undefine Serial1 object if only one UART is available. @per1234 @sebromero what do you think about that?

@per1234 @facchinm Agree, I think it would be less confusing. Let's do it 👍