robotics-masters / mm1-hat-arduino

Repository for storing Arduino related files and custom board definition.
GNU General Public License v3.0
8 stars 6 forks source link

Application freezes on SerialGPS RX #14

Closed civcode closed 4 years ago

civcode commented 4 years ago

The application freezes as soon as data is received on SerialGPS RX pin GPS_RX/PB03.

I tried several setups and got the same result (RoboHatMM1 + JetsonNano / RasPi 3 / USB2Serial FTDI).

Sending data from SAMD51 to JetsonNano on SerialGPS TX pin GPS_TX/PB02 works fine.

wallarug commented 4 years ago

Hi @civcode ,

We are looking into your issue and will get back to you if we find something. We think we are on the right track with finding a resolution soon.

Could you elaborate on your setup a little and maybe provide a quick schematic drawing of your circuit?

civcode commented 4 years ago

Hi @wallarug,

thanks for your reply. In my main setup the RoboHat is connected to a JetsonNano over the 40 pin header, and to the PC over microUSB for flashing and Arduino serial monitor. To eliminate errors caused by JetsonNano i removed it and used a USB2Serial interface plugged to the GPS header pins.

Arduino code:

#define TIME_OVER(target,time) ((unsigned long)((time) - (target)) < 0x8000000U)

const int delay_ms = 10000;
unsigned long time_ms;
int byte_rx = 0;
bool print_newline;

int target = 0;
int cnt = 0;

void setup() {
  Serial.begin(115200, SERIAL_8N1);
  SerialGPS.begin(115200, SERIAL_8N1);

  while (!Serial || !SerialGPS) {
      delay(1);
  }

  Serial.println("Let's go!");
  SerialGPS.println("Let's go!");
}

void loop() {
  print_newline = false;
  //read from Serial and write to SerialGPS
  while ((byte_rx = Serial.read()) > 0) {
    SerialGPS.write(byte_rx); //SerialGPS.print(" ");
    print_newline = true;
  }
  if (print_newline)
    SerialGPS.println(" ");

  print_newline = false;
  //read from SerialGPS and write to Serial
  while ((byte_rx = SerialGPS.read()) > 0) {
    Serial.write(byte_rx); //Serial.print(" ");
    print_newline = true;
  }
  if (print_newline)
    Serial.println(" ");

  //print periodically
  time_ms = millis();
  //if (time_ms > target){
  if (TIME_OVER(target, time_ms)) {
    Serial.print("step "); Serial.println(cnt);  
    SerialGPS.print("step "); SerialGPS.println(cnt);
    cnt++;
    target = time_ms + delay_ms;  
  }

  delay(10); 
}

Schematics: RoboHat + USB2Serial Serial-Wiring-01

RoboHat + JetsonNano Serial-Wiring-02

The app freezes in both cases. When i use the USB2Serial interface it looks like this: 2020-05-01_12h37_35

I think there is also a serial connection wired on the pcb from SAMD51 to the pins 8 an 10 of the 40 pin header. There is a section in the variants.h file of the robohat Arduino package but it's commented out. Changing the code in the package does not have an effect. I guess that the package has to be built again, but i could not figure out how to do that.

//AppData\Local\Arduino15\packages\robohat\hardware\samd\0.0.25\variants\robohatmm1_m4\variant.h

/*
 * Serial interfaces
 */
// Serial1 (SERCOM1 - RPi)
// #define PIN_SERIAL1_RX       (0ul)
// #define PIN_SERIAL1_TX       (1ul)
// #define PAD_SERIAL1_RX       (SERCOM_RX_PAD_1)
// #define PAD_SERIAL1_TX       (UART_TX_PAD_0)

// SerialGPS (SERCOM5 - GPS)
#define PIN_SERIALGPS_RX       (39ul)
#define PIN_SERIALGPS_TX       (40ul)
#define PAD_SERIALGPS_RX       (SERCOM_RX_PAD_1)
#define PAD_SERIALGPS_TX      (UART_TX_PAD_0)

// // SerialGrove (SERCOM0 - Grove)
// #define PIN_SERIALGROVE_RX     (44ul)
// #define PIN_SERIALGROVE_TX     (45ul)
// #define PAD_SERIALGROVE_RX     (SERCOM_RX_PAD_1)
// #define PAD_SERIALGROVE_TX     (UART_TX_PAD_0)
wallarug commented 4 years ago

Hey @civcode .

Thank you for all the details.

In the second image I don't believe the GND pins connected together help in anyway.

If you are interested in trying out playing with the variants file(s) you are more than welcome while we look into it further.

Here is the tutorial on how to do that: https://www.hackster.io/wallarug/arduino-ide-creating-custom-boards-89f7a6

peterpanstechland commented 4 years ago

@civcode Hi, we had closer look at this issue these two days, and we found out that its the USB-to-Serial converter could causing the issue, as we tested the serial pins with both a Chinese cheap one with CH340 chip and a one with cp2102, seems like your problem only can be reproduced with the one come with CH340 chip...so if you have different serial converter laying around please give it a try. Also please let us know what is the chip used on your USB-to-Serial converter thanks. BTW for the pinout label, we labeled the serial port based from our board perspective which means the connection is PI_RX(Robohat MM1 PA17) to TXD (PI GPIO14) and PI_TX(Robohat MM1 PA16) to RXD(PI GPIO14), but with software serial setup you could switch the RX/TX around from Robohat MM1 which means both PA17 and PA16 could works either as RX or TX depends on your setup with software serial.

civcode commented 4 years ago

Hi @peterpanstechland,

ok, i understand what you say about the PI_RX/TX labels.

I am using an FTDI Chip tty-232r-3v3 USB2Seial converter. To test the internal PI_RX/TX connection i wrote a similar program like the one above in CircuitPython. The serial connection works fine with both, the JetsonNano and the USB2Serial converter.

uart = busio.UART(board.PI_TX, board.PI_RX, baudrate = 115200, timeout = 0.001)

The GPS_RX/TX connection, however, shows the same issue. RoboHat seems not to receive anything, but the program does not freeze completely like in the case of Arduino code.

uartGPS = busio.UART(board.GPS_TX, board.GPS_RX, baudrate = 115200, timeout = 0.001)

Maybe this only happens on my board. But that's not really a problem if i can use the PI_RX/TX connection instead. So i will try to enable it in the Arduino board package. Can you tell me if uncommenting the respective lines in the variant.h file is enough, or does it need other changes?

wallarug commented 4 years ago

@civcode The PI RX/TX are D0 and D1 in Arduino IDE. I don't know/think you need to do anything.

I want to take another look at the handlers. The GPS serial line has handlers defined while the Pi one doesn't. The handlers in SAMD51 act differently and you need to define all 4 of the IRQ Handlers for it to work. This may be the problem too and explain why it freezes in Arduino but not CircuitPython.

The other thing we looked a @civcode was a potential Ground Loop with those pins. We couldn't find any in the hardware design but maybe an external circuit is triggering it.

wallarug commented 4 years ago

Hey @civcode ,

Please go and update to Version 0.0.26 for the board and let us know how this goes. Potential fix in PR #16 .

Thanks.

civcode commented 4 years ago

Hi @wallarug ,

looks very good! I tested Serial1(RPI), Serial2(GPS) and Serial3(Grove) with the FTDI Chip USB2Serial converter and communication works fine in both directions. Serial1(RPI) and JetsonNano work together nicely as well. Also, with version 0.0.25 the program froze if you removed one of the wires from the serial connection pins. Now the communication stops temporarily but continues right away when you reconnect the wire.

Great job! :smiley:

wallarug commented 4 years ago

Thanks @civcode ! Let us know if there is anything else.