HardwareSerial on Zero board

Hi Guys,

This code (written by @Blackfin) originally worked on a Mega2560, supporting both HW-Serial and SW-Serial. I have now migrated to a SAMD board using Zero bootloader.
The IDE 1.8.8 won't compile the code (Zero Native USB), I've tried commenting away everything that has to do with SW-serial and the code now compiles, but HW-Serial doesn't seem to start.. Earlier I could see all of the HEX values the board tried to transmit on HW-serial, printed on SerialUSB. Now I only get "Message timed out"
Somehow, I think the board interpret it doesn't have the HW-serial? hence doing nothing.
(This board doesn't have the EDBG/porgramming port)

Full code is attached, part that I think would be relevant are in this post.

Had the problems before I added up the Sercom handler, but that didn't solve my issue, however I would need it anyway. :slight_smile:

#include <REGO637.h>
#include <Arduino.h>   // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
.......................
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
void SERCOM1_Handler()
{
  Serial2.IrqHandler();
}

//heatpump is on Serial2
Rego637 Heatpump( Serial2 );
.........................
void setup( void )
{
  
    SerialUSB.begin( 9600 );         //serial monitor
    Heatpump.start( 19200 );      //Rego637 heatpump
    
   pinPeripheral(10, PIO_SERCOM);
   pinPeripheral(11, PIO_SERCOM);
#include <HardwareSerial.h>
//#include <SoftwareSerial.h>
....................
class Rego637
{

public:
    Rego637( HardwareSerial &sp ) {_hwRegoPort = &sp;}
    //Rego637( SoftwareSerial &sp ) {_swRegoPort = &sp;}
 
   ~Rego637(){;}

    void start( uint32_t baudrate );
...................
private:

    Stream          *_RegoStream;
    HardwareSerial  *_hwRegoPort;
    //SoftwareSerial  *_swRegoPort;
void Rego637::start( uint32_t baudrate )
{
    if( _hwRegoPort )
    {
        _hwRegoPort->begin( baudrate );
        _RegoStream = (Stream *)_hwRegoPort;

    }//if
    //else
    //{
        //_swRegoPort->begin( baudrate );
        //_RegoStream = (Stream *)_swRegoPort;

    //}//else

    ClrMsgStatus();

}//start

RegoClass.ino (9.08 KB)

REGO637.cpp (8.14 KB)

REGO637.h (5.45 KB)

keywords.txt (2.63 KB)

which type of SAMD board? If I remember well, they not always have all the same naming convention of Serial stuff (Zero versus Feather M0). Long time I did not play with it though

It's a SAMD21G18A with Zero bootloader, should be equal to a Zero (except for the EDBG chipset that this board lack)

With that boot loader The USB port in use is the USB Native port and the USART is on pins D0/RX & D1/TX

that being said, various functions are assigned to each pin depending on the variant.cpp file that you pick for your board. have you checked ?

Yes, I have checked and I think I have understood it but I can't be sure.
SerialUSB = Native USB port
Serial = D0/D1

As for now, I won't use the Serial on D0/1, I will use it later when I gotten this sketch to compile as it is.
I have with Sercom created/mapped Serial2 to pin 10(RX) and 11(TX), I can't say for sure this will work in my sketch yet.

The thing is, when I ran this sketch on the MEGA2560, the sketch printed on the monitor-Serial what it just transmitted on the "real" serial, even if the real serial was disconnected it tried to transmit. And the serial monitor showed

Tx Bufferz: 81 02 00 04 09 00 00 00 0D

As for now, the Zero seem to not initialize the HW-serial at all.
And the serial monitor just gives me

Message error.....: Message timeout

I just tried comment away the Sercom handler and assigning the Heatpump to "Serial" on 0/1, but I still get the "Message error.....: Message timeout" and nothing about what it's trying to transmit on the Serial.
The only difference is with 'Serial' it loops the message in infinity, with my Sercom, it only loops the message 6 times, then stops.

And the serial monitor just gives me

Do I get right that the Serial monitor is connected via the USB port ?how do you print with SerialUSB.print() ??

also you might want to give it a bit of time at start for the USB to do its stuff so add

while(!SerialUSB);

after     SerialUSB.begin( 9600 );        //serial monitor(any reason to go this slow?)

Yes, Serial monitor is connected to the Native USB port, printing is done with:

            SerialUSB.print( "Message error.....: " );
            SerialUSB.println( RegoErrorMsgs[chkRtn] );

However, my problem isn't within the USB-Serial, it's the HW-serial that communicates with the Heat-pump that doesn't seem to initialize. Thats probably why it's not outputting what it's trying to transmit to the heatpump, on the SerialUSB(monitor) but instead throws error message on the monitor.

No real reason for the speed, the monitor is just used for debugging at this time.. The speed was enough on the Mega2560.

Will try your suggestion.

RX/TX pins are Serial1

Tried you suggestion @J-M-L, still the same.
It's just like the compiler seeing Serial/Serial1/Serial2 (doesn't matter if I use Sercom or not) and interpreting is as it would not be a hardwareserial.

@Juraj, tried assigning it to Serial1 aswell, still doesn't seem to initialize HW-serial.

sx3000:
Tried you suggestion @J-M-L, still the same.
It's just like the compiler seeing Serial/Serial1/Serial2 (doesn't matter if I use Sercom or not) and interpreting is as it would not be a hardwareserial.

@Juraj, tried assigning it to Serial1 aswell, still doesn't seem to initialize HW-serial.

do you have the physical pin mapping and core board variant same as Zero?

Yes, I beleive so!

I have attached Tx to Zero-pin 10 and Rx to Zero-Pin 11.
That would be pin 21/23 on this image. I have triple checked the routes on PCB.

But the thing is, with Mega2560.. When I had the serial-monitor open, but the heatpump wasn't connected I got following message on serial montor:

Tx Bufferz: 81 02 00 04 09 00 00 00 0D
Message error.....: Message timeout

Since the Heatpump wasn't connected, I got the 'Message timeout' after a second or two, and so it looped until I connected the heatpump.

But now, with Zero/SAMD it never plots the Tx buffer on the serial monitor. Doesn't matter if it's pluggen into heatpump or unplugged. Just throwing the Message timeout.
The problem should be withing the code, probably how the stream class is called/used.. I think..