In this tutorial we explain how to add further serial interfaces to your SAMD based board. These interfaces are hardware based and can be of I2C, UART, or SPI type. This is possible because the SAMD microcontroller has six internal serial modules that can be configured individually and just four of them are already configured. The other two are available for mapping onto specific pins. In this tutorial we explain how you can do that.
Only your Arduino Board is needed for this example.
One of the advantages of the Arduino platform is the simplification of the hardware, assigning to each microcontroller pin one of the many possible functions. You can find the various functions assigned to each pin in the variant.cpp file of each board. Let's see, for example, the variant.cpp for the MKR1000.
Focusing our attention on the SERCOM related pins, we can extract the following information:
1/*2
3 +------------+------------------+---------+---------+----------+4
5 | Pin number | MKR Board pin | Perip.C | Perip.D | Periph.G |6
7 | | | SERCOMx | SERCOMx | COM |8
9 | | | (x/PAD) | (x/PAD) | |10
11 +------------+------------------+---------+---------+----------+12
13 | 00 | D0 | 3/00 | 5/00 | |14
15 | 01 | D1 | 3/01 | 5/01 | USB/SOF |16
17 | 02 | D2 | 0/02 | 2/02 | I2S/SCK0 |18
19 | 03 | D3 | 0/03 | 2/03 | I2S/FS0 |20
21 | 04 | D4 | | 4/02 | I2S/MCK1 |22
23 | 05 | D5 | | 4/03 | I2S/SCK1 |24
25 | 06 | D6 | 5/02 | 3/02 | I2S/SCK0 |26
27 | 07 | D7 | 5/03 | 3/03 | I2S/FS0 |28
29 +------------+------------------+---------+---------+----------+30
31 | | SPI | | | |32
33 | 08 | MOSI | *1/00 | 3/00 | |34
35 | 09 | SCK | *1/01 | 3/01 | |36
37 | 10 | MISO | *1/03 | 3/03 | I2S/SD0 |38
39 +------------+------------------+---------+---------+----------+40
41 | | Wire | | | |42
43 | 11 | SDA | *0/00 | 2/00 | I2S/SD1 |44
45 | 12 | SCL | *0/01 | 2/01 | I2S/MCK0 |46
47 +------------+------------------+---------+---------+----------+48
49 | | Serial1 | | | |50
51 | 13 | RX | | *5/03 | |52
53 | 14 | TX | | *5/02 | |54
55 +------------+------------------+---------+---------+----------+56
57 | 16 | A1 | | 5/00 | |58
59 | 17 | A2 | | 5/01 | |60
61 | 18 | A3 | | 0/00 | |62
63 | 19 | A4 | | 0/01 | |64
65 | 20 | A5 | | 0/02 | |66
67 | 21 | A6 | | 0/03 | I2S/SD0 |68
69 +------------+------------------+---------+---------+----------+70
71 | | ATWINC1501B SPI | | | |72
73 | 26 | WINC MOSI | *2/00 | 4/00 | |74
75 | 27 | WINC SCK | *2/01 | 4/01 | |76
77 | 28 | WINC SSN | 2/02 | 4/02 | |78
79 | 29 | WINC MISO | *2/03 | 4/03 | |80
81 +------------+------------------+---------+---------+----------+82
83 | | ATWINC1501B PINS | | | |84
85 | 32 | WINC WAKE | | 4/00 | |86
87 | 33 | WINC IRQN | | 4/01 | |88
89 +------------+------------------+---------+---------+----------+90
91 */
As you can see, SERCOMs can be routed almost everywhere, having more than one SERCOM routable on more than one pin.
On the header of the MKR1000 boards, you can find an SPI, I2C and UART interface positioned as follows:
SPI / SERCOM 1:
MOSI on pin 8;
SCK on pin 9;
MISO on pin 10;
I2C / SERCOM 0:
SDA on pin 11;
SCL on pin 12;
UART / SERCOM 5:
RX on pin 13;
TX on pin 14;
Additionally there is another SPI interface internally connected to the WINC1500 module, wired as follows:
WINC1500 SPI / SERCOM 2:
MOSI on pin 26;
SCK on pin 27;
MISO on pin 29;
So removing from our table the pre-existing interfaces, since our aim is to add new interfaces instead of changing the pre-defined ones, we obtain what follows:
1/*2
3 +------------+------------------+---------+---------+----------+4
5 | Pin number | MKR Board pin | Perip.C | Perip.D | Periph.G |6
7 | | | SERCOMx | SERCOMx | COM |8
9 | | | (x/PAD) | (x/PAD) | |10
11 +------------+------------------+---------+---------+----------+12
13 | 00 | D0 | 3/00 | 5/00 | |14
15 | 01 | D1 | 3/01 | 5/01 | USB/SOF |16
17 | 02 | D2 | 0/02 | 2/02 | I2S/SCK0 |18
19 | 03 | D3 | 0/03 | 2/03 | I2S/FS0 |20
21 | 04 | D4 | | 4/02 | I2S/MCK1 |22
23 | 05 | D5 | | 4/03 | I2S/SCK1 |24
25 | 06 | D6 | 5/02 | 3/02 | I2S/SCK0 |26
27 | 07 | D7 | 5/03 | 3/03 | I2S/FS0 |28
29 +------------+------------------+---------+---------+----------+30
31 | 16 | A1 | | 5/00 | |32
33 | 17 | A2 | | 5/01 | |34
35 | 18 | A3 | | 0/00 | |36
37 | 19 | A4 | | 0/01 | |38
39 | 20 | A5 | | 0/02 | |40
41 | 21 | A6 | | 0/03 | I2S/SD0 |42
43 +------------+------------------+---------+---------+----------+44
45 */
Let's now try to use the table above to add a new interface to our MKR1000 board.
As we can see, pin 0 and pin 1 can be driven by two SERCOMs. In particular by SERCOM3 and SERCOM5. Looking at the SAMD21 datasheet, we can figure out that the SERCOM PAD0 can be used as SDA and the SERCOM PAD1 as SCL. So we can do this using the example below.
1/* Wire Slave Sender on pins 0 and 1 on MKR10002
3 Demonstrates use of the Wire library and how to instantiate another Wire4
5 Sends data as an I2C/TWI slave device6
7 Refer to the "Wire Master Reader" example for use with this8
9 Created 20 Jun 201610
11 by12
13 Arturo Guadalupi <a.guadalupi@arduino.cc>14
15 Sandeep Mistry <s.mistry@arduino.cc>16
17*/18
19#include <Wire.h>20#include "wiring_private.h"21
22TwoWire myWire(&sercom3, 0, 1); // Create the new wire instance assigning it to pin 0 and 123
24void setup()25{26
27 myWire.begin(2); // join i2c bus with address #228
29 pinPeripheral(0, PIO_SERCOM); //Assign SDA function to pin 030
31 pinPeripheral(1, PIO_SERCOM); //Assign SCL function to pin 132
33 myWire.onRequest(requestEvent); // register event34}35
36void loop()37{38
39 delay(100);40}41
42// function that executes whenever data is requested by master43// this function is registered as an event, see setup()44void requestEvent()45{46
47 myWire.write("hello "); // respond with message of 6 bytes48
49 // as expected by master50}51
52// Attach the interrupt handler to the SERCOM53
54extern "C" {55
56 void SERCOM3_Handler(void);57
58 void SERCOM3_Handler(void) {59
60 myWire.onService();61
62 }63}
the two instructions use the internal function
pinPeripheral (pinnumber, function)
that reassigns the pins1pinPeripheral(0, PIO_SERCOM); //Assign SDA function to pin 02
3pinPeripheral(1, PIO_SERCOM); //Assign SCL function to pin 1
must be put in the
setup()
in order to override the standard Arduino pin assignment for this board (digital I/O) and to allow the SERCOM to drive them.The callback
1void SERCOM3_Handler(void) {2
3 myWire.onService();4}
is used to allow the real I2C communication, since the Wire library relies on interrupts.
1/*2
3 AnalogReadSerial on new UART placed on pins 1 and 04
5 Reads an analog input on pin A0, prints the result to the serial monitor.6
7 Graphical representation is available using serial plotter (Tools > Serial Plotter menu)8
9 Attach the center pin of a potentiometer to pin A0, and the outside pins to +3.3V and ground.10
11 Short together pin 1 and pin 0 with a wire jumper12
13 Created 20 Jun 201614
15 by16
17 Arturo Guadalupi <a.guadalupi@arduino.cc>18
19 This example code is in the public domain.20
21*/22
23#include <Arduino.h>24#include "wiring_private.h"25
26Uart mySerial (&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); // Create the new UART instance assigning it to pin 1 and 027
28// the setup routine runs once when you press reset:29void setup() {30
31 // initialize serial communication at 9600 bits per second:32
33 Serial.begin(9600);34
35 mySerial.begin(9600);36
37 pinPeripheral(1, PIO_SERCOM); //Assign RX function to pin 138
39 pinPeripheral(0, PIO_SERCOM); //Assign TX function to pin 040}41
42// the loop routine runs over and over again forever:43void loop() {44
45 // read the input on analog pin 0:46
47 int sensorValue = analogRead(A0);48
49 // print out the value you read on mySerial wired in loopback:50
51 mySerial.write(sensorValue);52
53 while (mySerial.available()) {54
55 Serial.print(mySerial.read());56
57 }58
59 Serial.println();60
61 delay(1); // delay in between reads for stability62}63
64// Attach the interrupt handler to the SERCOM65void SERCOM3_Handler()66{67
68 mySerial.IrqHandler();69}
as we did for Wire, the two instructions
1pinPeripheral(1, PIO_SERCOM); //Assign RX function to pin 12
3pinPeripheral(0, PIO_SERCOM); //Assign TX function to pin 0
must be placed in order to override the standard Arduino pin assignment for this board (digital I/O) and to allow the SERCOM to drive them.
The callback
1void SERCOM3_Handler()2{3
4 mySerial.IrqHandler();5}
is used to allow the real Serial communication, since the Serial library relies on interrupts too.