Using SERCOM on Grand Central M4

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
MarcelPfuetzner
 
Posts: 2
Joined: Tue Feb 07, 2023 3:11 pm

Using SERCOM on Grand Central M4

Post by MarcelPfuetzner »

Hi, I am trying to switch an existing project from Arduino Mega 2560 R3 to Adafruit Grand Central M4 Express. I am struggling in getting the Serial Communication transformed.

The current project (Mega) uses Pins 0/1 (RX/TX) for debugging and communication with an external PC via USB, Pins 65/66 via SoftwareSerial as RX/TX to communicate with an Adafruit Metro Mini, and Pins 67/68 via SoftwareSerial as RX/TX to communicate with HC06 BT module. Everything works perfectly. So far so good...

When switching to GCM4E, I am not getting the serial communication between the GCM4E and the Metro Mini / HC06 to work. Following the GCM4E pin out diagram, the pins used for Serial Communication with the Mega can also be used for Serial Communication with the GCM4E:
Pin 57 --> SERCOM_ALT 4 Pad 0 --> Sercom4 TX
Pin 58 --> SERCOM_ALT 4 Pad 2 --> Sercom4 RX
Pin 59 --> SERCOM_ALT 0 Pad 0 --> Sercom0 TX
Pin 60 --> SERCOM_ALT 0 Pad 2 --> Sercom0 RX

As the GCM4E uses a 3.3V logic, I also switched the Metro Mini to 3.3V. But this does not help. I use the following test-sketch to establish a basic serial communication (in this case via Pins 57/58), but it does not work:

Code: Select all

```cpp
/////////////////////////////////////////////
// Master Adafruit Grand Central M4 ATSAMD51
/////////////////////////////////////////////

#include <Arduino.h>   
#include "wiring_private.h" // pinPeripheral() function


Uart BTSerial (&sercom4, 58, 57, SERCOM_RX_PAD_1, UART_TX_PAD_0); // BT Serial connection

void SERCOM4_Handler()
{
  BTSerial.IrqHandler();
}

void setup() {
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect
  }

  Serial.println("Serial Communication Test");

  BTSerial.begin(9600);
  
  // Assign pins 10 & 11 SERCOM functionality
  pinPeripheral(57, PIO_SERCOM_ALT);
  pinPeripheral(58, PIO_SERCOM_ALT);

  while (!BTSerial) {
  Serial.println("Waiting for BTSerial to connect"); // wait for BTserial port to connect
  }

  Serial.println("BTSerial connected");

  BTSerial.println("Slave Test");
}


void loop() {

  if (Serial.available())
  { // If anything comes in Serial (USB),
    BTSerial.write(Serial.read());
  }
  
  if (BTSerial.available())
  { // If anything comes in SoftwareSerial,
    Serial.write(BTSerial.read());   // read it and send it out Serial1 (pins 0 & 1)
  }
  
}
```

What am I doing wrong - any help?

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Using SERCOM on Grand Central M4

Post by adafruit_support_mike »

The code above looks good.

As a sanity check, do you have access to an oscilloscope or a logic analyzer? It would be useful to see what the pins are doing at the signal level. If you don't, try connecting another microcontroller's GPIO pin to TX, configure it as an input, and run a fast loop to see if the Grand Central pin voltage goes high and low when you try to send data.

User avatar
westfw
 
Posts: 2006
Joined: Fri Apr 27, 2007 1:01 pm

Re: Using SERCOM on Grand Central M4

Post by westfw »

void SERCOM4_Handler()
{
BTSerial.IrqHandler();
}
The SAMD51 on the GC M4 has separate interrupt vectors for different UART "events" (rx data vs tx data, for example), and needs you to define four separate handlers.
See https://github.com/WestfW/WestfW_SerComLib

User avatar
MarcelPfuetzner
 
Posts: 2
Joined: Tue Feb 07, 2023 3:11 pm

Re: Using SERCOM on Grand Central M4

Post by MarcelPfuetzner »

westfw wrote: Sat Apr 29, 2023 3:10 pm
void SERCOM4_Handler()
{
BTSerial.IrqHandler();
}
The SAMD51 on the GC M4 has separate interrupt vectors for different UART "events" (rx data vs tx data, for example), and needs you to define four separate handlers.
See https://github.com/WestfW/WestfW_SerComLib
Thank you for the answer, According to the description of your library, i would add the library and go with:

"SERCOMLIB_MakeSerialPinsPads(sercomNumber, SerialNumber, rxPin, txPin, rxPad, txPad)"

"SERCOMLIB_MakeSerialPinsPads(4 , 2 , 58 , 57 , 1 , 0)"

The only think I wonder about is, if this will also work with the pins I would like to use: Pins 57 ... 60 are available als SERCOM_ALT only...

User avatar
westfw
 
Posts: 2006
Joined: Fri Apr 27, 2007 1:01 pm

Re: Using SERCOM on Grand Central M4

Post by westfw »

Pins 57 ... 60 are available als SERCOM_ALT only
The westfw_SerComLib stuff just sets up the data structures and ISRs; I haven't figured out a way to get it to intercept the actual pin configuration stuff.

For pins that are not tagged in variant.cpp as being on PIO_SERCOM_ALT (I guess NONE of the SAMD51 pins on GCM4 use it), you will have to explicitly call pinPeripheral() on the pins. (this might apply to pins not tagged with PIO_SERCOM too, since the default Uart::begin code does "pinPeripheral(uc_pinRX, g_APinDescription[uc_pinRX].ulPinType);" (I see you are already doing this, since those pins are only tagged with PIO_ANALOG)

There's an example of this happening in the SparkFun 6 UART example

I'm not sure what will happen when the pinPeripheral() call in Uart::begin() sets the pins to analog immediately before they get reset. ulPinType is really starting to BANNED me off.

Since you look like you have a good handle on how things work, you probably don't need to use SerComLib at all; I was just offering it as an example.

Locked
Please be positive and constructive with your questions and comments.

Return to “Metro, Metro Express, and Grand Central Boards”