Hi I am currently using the SAMD21 Xplained Pro with the Arduino IDE and I am able to communicate with my PC using the EDGB but I can't seem to access the other serial ports on the SAMD21.
I want to be able to connect the SAMD21 to a sensor and send a command through serial to the sensor and receive a response and read it back to console.
I have tried to see if the other port is even available, but it seems that it won't even communicate with it. I am using this code below.
#define PIN_SERIAL2_RX (34ul) // Pin description number for PIO_SERCOM on D12
#define PIN_SERIAL2_TX (36ul) // Pin description number for PIO_SERCOM on D10
#define PAD_SERIAL2_TX (UART_TX_PAD_2) // SERCOM pad 2
#define PAD_SERIAL2_RX (SERCOM_RX_PAD_3) // SERCOM pad 3
// Instantiate the Serial2 class
Uart Serial2(&sercom1, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);
void setup() {
//test to get digital com working
Serial.begin(115200);
Serial2.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.print("Hello");
String readString;
String Q;
while(Serial.available()){
delay(1);
if(Serial.available() > 0){
char c = Serial.read();
//Serial.println("I've reached here");
if(isControl(c)){
break;
}
readString += c;
}
}
Q = readString;
//Checking Serial Reading
if(Q == "INIT"){
Serial2.write("INIT");
Serial.println("Sent:Radar");
}
while(Serial2.available()){
Serial.print("Hello");
delay(1);
if(Serial2.available() > 0){
char c = Serial2.read();
Serial.print(c);
if(isControl(c)){
break;
}
}
}
}
void SERCOM1_Handler() // Interrupt handler for SERCOM1
{
Serial2.IrqHandler();
}
I don't see anything obviously wrong with the code, except that you may need the "pinPeripheral" assignment (see below).
But I have no idea what you mean by the quoted statement above. Please explain in sufficient detail how you wired and tested the new serial port, and what other device was used in the test.
If you are testing it with a PC, you need a USB to UART serial converter with 3.3V I/O.
I have had no problem implemented new serial ports with the SAMD21. Here is the code I use to test Serial2 function with a PC, running on the Adafruit Feather M0:
#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();
}
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.println("Serial2 test");
Serial2.begin(115200);
// Assign pins 10 & 11 SERCOM functionality
pinPeripheral(10, PIO_SERCOM);
pinPeripheral(11, PIO_SERCOM);
}
// echo back and forth
void loop() {
if (Serial.available()) Serial2.write(Serial.read());
if (Serial2.available()) Serial.write(Serial2.read());
}
Thank you ! I tested your code out and it echo's back "Serial2 test".
I am trying to connect the SAMD21 MCU to a KMD7 Radar. I want to be able to send commands through serial and receive data back from the Radar. I have connected the Radar to the SAMD21 pins described in my original code (D12 and D10) from the picture attached. I will now try the pins described in your example and will reach back if not working.
The "Ser2" pins you highlight in your diagram (PB10 and PB11) are NOT
"Arduino Pins" 10 and 11 (Not pins 34 and 36, either (where did THOSE come from?))... (The mapping between Arduino Pin Numbers and Chip/Port Pin numbers is entirely arbitrary and controlled by variants/boardname/variant.cpp
If you want to use PB10/PB11 for a Serial Port, arduino-like functions will need to use pin23 and pin24 (assuming the board type is "Arduino Zero." If you're using a different board type, this will change (mzero is 21 and 20.))
What "board" setting ARE you using?
I am using Atmel SAMD21 Xplained Pro (via EDBG) board setting. Is the not the correct board setting I should be using? I am also a little confused on the pinouts now. Which pins would I use for that setting?
Okay I see what you mean. Yes, I am using that board package from the link you have sent. So according to that documentation, I can instantiate the Serial2 class from the comments and then define them in the pinPeripheral function?
Remember these lines I referred to back in Post #6?
I believe these lines are in error (a BUG in the core!)
B10 and B11 do not have "SERCOM" functionality, they have "SERCOM_ALT" functionality. Try changing your code to:
(although, the code you posted seems to have the sort of mistakes common to new users of Serial interfaces, like "reading the port faster than data becomes available." See the "Serial Input Basics" tutorial
I've created an "issue" on their github, a patch, and a "pull request."
I don't know how actively that core is being maintained, though - the last code change was 7 years ago.
(with the patched core, I think it should work without needing pinPeripheral() calls.)