I am trying to implement a new Serial Port using pins A3 (18) and A4 (19) by following this guideline.
The reason I am wanting to use pins 18 and 19 is that I have an MKR Relay Proto Shield and wish to use the screw terminals for connecting.
However, I am having no luck. So I just want to check if my logic/code is correct.
So, the first thing I did was look at variant.cpp/variant.h to extract out the relevant detail for pins 18 and 19.
/*
+------------+------------------+--------+-----------------+--------+-----------------------+---------+---------+--------+--------+----------+----------+
| Pin number | MKR Board pin | PIN | Notes | Peri.A | Peripheral B | Perip.C | Perip.D | Peri.E | Peri.F | Periph.G | Periph.H |
| | | | | EIC | ADC | AC | PTC | DAC | SERCOMx | SERCOMx | TCCx | TCCx | COM | AC/GLCK |
| | | | |(EXTINT)|(AIN)|(AIN)| | | (x/PAD) | (x/PAD) | (x/WO) | (x/WO) | | |
+------------+------------------+--------+-----------------+--------+-----+-----+-----+-----+---------+---------+--------+--------+----------+----------+
| 15 | A0 / DAC0 | PA02 | | 02 | *00 | | Y00 | OUT | | | | | | |
| 16 | A1 | PB02 | | *02 | *10 | | Y08 | | | 5/00 | | | | |
| 17 | A2 | PB03 | | *03 | *11 | | Y09 | | | 5/01 | | | | |
| 18 | A3 | PA04 | | 04 | *04 | 00 | Y02 | | | 0/00 |*TCC0/0 | | | |
| 19 | A4 | PA05 | | 05 | *05 | 01 | Y03 | | | 0/01 |*TCC0/1 | | | |
| 20 | A5 | PA06 | | 06 | *06 | 02 | Y04 | | | 0/02 | TCC1/0 | | | |
| 21 | A6 | PA07 | | 07 | *07 | 03 | Y05 | | | 0/03 | TCC1/1 | | I2S/SD0 | |
+------------+------------------+--------+-----------------+--------+-----+-----+-----+-----+---------+---------+--------+--------+----------+----------+
*/
According to this table, Pins 18 & 19 use SERCOM 0 and Pads 0 and 1 respectively. I can now create a new Serial Instance using this data.
Uart mySerial (&sercom0, 19, 18, SERCOM_RX_PAD_1, UART_TX_PAD_0);
The interrupt routine is then handled by:
void SERCOM0_Handler() { mySerial.IrqHandler(); }
Then with the setup() routine I then begin mySerial and I assign the following
// Assign pins 18 & 19 SERCOM functionality
pinPeripheral(18, PIO_SERCOM);
pinPeripheral(19, PIO_SERCOM);
The code compiles but I am not seeing anything via the serial monitor.
Here is the code:
#include <Arduino.h> // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
Uart mySerial (&sercom0, 19, 18, SERCOM_RX_PAD_1, UART_TX_PAD_0);
void SERCOM0_Handler()
{
mySerial.IrqHandler();
}
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println(F("Serial Comms Test using MKR WAN 1300"));
mySerial.begin(9600);
// Assign pins 18 & 19 SERCOM functionality
pinPeripheral(18, PIO_SERCOM);
pinPeripheral(19, PIO_SERCOM);
}
void loop() {
if (mySerial.available()) {
char b = mySerial.read();
if (b) {
Serial.print(b);
}
}
}