I'm currently working with an Itsy Bitsy SAMD51 M4 board and facing a challenge with implementing a software Serial2 communication using SERCOM0. Despite following the typical setup procedures, I'm unable to transmit or receive any data on Serial2. I've wired RX to TX to simplify the problem, the exact same code works fine for the default hardware serial1. Below is my current code setup:
#include <Arduino.h>
#include "wiring_private.h"
// Define RX and TX pins
#define PIN_RX A5
#define PIN_TX A4
// Declare the second UART (Serial2) using SERCOM0
Uart Serial2(&sercom0, PIN_RX, PIN_TX, SERCOM_RX_PAD_1, UART_TX_PAD_0);
// SERCOM0 Handler
void SERCOM0_0_Handler() { Serial2.IrqHandler(); }
void SERCOM0_1_Handler() { Serial2.IrqHandler(); }
void SERCOM0_2_Handler() { Serial2.IrqHandler(); }
void SERCOM0_3_Handler() { Serial2.IrqHandler(); }
void setup() {
Serial.begin(115200); // Initialize the built-in serial port
Serial2.begin(19200); // Initialize Serial2 (19200 baud for RS-485)
pinPeripheral(PIN_RX, PIO_SERCOM); // Assign RX & TX pins to SERCOM functionality
pinPeripheral(PIN_TX, PIO_SERCOM);
}
void loop() {
char data[6] = {'H','E','L','L','O','\n'};
int i = 0;
i = (i + 1) % 6;
Serial2.write(data[i]); // Write data to Serial2
if (Serial2.available()) {
uint8_t data = Serial2.read(); // Read data from Serial2
Serial.print("Received from Serial2: 0x");
Serial.println(data, HEX);
}
delay(100);
}
You are clearly somewhat confused. SERCOM is a hardware feature of these chips which can be configured to act as a UART or an I2C or SPI bus. No software serial is involved. If you can figure out how to configure a SERCOM, it is a hardware UART.
Super helpful! This has gotten me way closer to solving my problem, thanks so much. But seriously, what would you call creating a UART object to talk to the hardware interface?
No need for sarcasm. Yes, in the Arduino world, hardware often has a software layer around it to make code more portable between Arduino models. You could argue that every UART port is software because your code does not directly interact with the hardware, but that's not a very useful definition because it doesn't distinguish between hardware UARTs and purely software UARTs.
I notice Adafruit have a page here that describes how to configure SERCOM to be additional hardware UART ports. But it only talks about SAMD21, not SAMD51: