Go Down

Topic: Software Serial library for MKR1000 (Read 2893 times) previous topic - next topic

tkuehn

Hi all,

Love the MKR1000 so far. I'm finding it quite a nice little board.

The roadblock I hit with my project was that the MKR1000 only has one serial port. As there was no official software serial library yet, I started to modify the NewSoftSerial library for the MKR1000. So far I have successfully been able to receive serial data up to 57600 baud, however the TX side does not seem to work. I feel like it's something simple I'm overlooking for the TX, so suggestions would be welcome.

Hopefully this helps someone until a more official library is worked out.

https://github.com/guywithaview/MKRSoftSerial

Cheers

ballscrewbob

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

MartinL

#2
May 17, 2016, 06:21 pm Last Edit: May 19, 2016, 05:03 pm by MartinL
There isn't a software serial library, but fortunately you can configure the SAMD21's spare serial communciation (SERCOM) modules to obtain multiple hardware serial ports. The SAMD21 has 6 (0..5) flexible SERCOM modules that can be configured either as an I2C, SPI or Serial port. Looking at the MKR1000's "variant.cpp" file it looks as though 3 are currently in use, that's:

Sercom 0 - I2C
Sercom 1 - SPI
Sercom 5 - Serial1

That leaves Sercom 2, 3 and 4 that are free to be configured how you wish.

Here's an Adafruit article, (it's for their Adafruit Feather M0, but it uses the same SAMD21G18A processor): https://learn.adafruit.com/using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports/creating-a-new-serial

The article also tells you how to configure the SERCOM modules to add additional I2C and SPI ports.

The code to set-up MKR1000's Serial2 (using SERCOM3) on digital pins 0 (TX) and 1 (RX) should go something like this:

Code: [Select]
#include <Arduino.h>                              // required before wiring_private.h
#include <wiring_private.h>

// Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL2_RX       (1ul)                // Pin description number for PIO_SERCOM on D1
#define PIN_SERIAL2_TX       (0ul)                // Pin description number for PIO_SERCOM on D0
#define PAD_SERIAL2_TX       (UART_TX_PAD_0)      // SERCOM pad 0 TX
#define PAD_SERIAL2_RX       (SERCOM_RX_PAD_1)    // SERCOM pad 1 RX

// Instantiate the Serial2 class
Uart Serial2(&sercom3, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);

void setup()
{
  Serial2.begin(115200);          // Begin Serial2
  pinPeripheral(0, PIO_SERCOM);   // Assign pins 0 & 1 SERCOM functionality
  pinPeripheral(1, PIO_SERCOM);
}

void loop()
{
  if (Serial2.available())        // Check if incoming data is available
  {
    byte byteRead = Serial2.read();    // Read the most recent byte
    Serial2.write(byteRead);           // Echo the byte back out on the serial port
  }
}

void SERCOM3_Handler()    // Interrupt handler for SERCOM3
{
  Serial2.IrqHandler();
}

According to Adafruit you need to include "Arduino.h". I got the code to compile without it, so it's probably unnecessary, but as I don't own a MKR1000 can't confirm whether this include can be omitted.

tkuehn

Awesome, thanks! Far better than using software serial. I will try this out soon.

tkuehn

#4
Jun 26, 2016, 12:49 pm Last Edit: Jun 26, 2016, 02:07 pm by tkuehn
Tested and working! Thanks MartinL!

I created a test sketch with two extra serial ports if anyone needs to get started. This gives Serial (USB), Serial 1, Serial 2 and Serial 3.

https://github.com/guywithaview/Arduino-Test/blob/master/sercom/sercom.ino

Pretty cool that the MKR1000 can do this.

toqduj


AndreaRichetta

#6
Sep 07, 2016, 12:34 pm Last Edit: Sep 07, 2016, 12:43 pm by AndreaRichetta
Hello,
You can check also this tutorial we've created:
https://www.arduino.cc/en/Tutorial/SamdSercom

abdallaziz92

Awesome, thanks! Far better than using software serial. I will try this out soon.
I would like to know why you got multiple Tx and Rx defined?

MartinL

Hi abdallaziz92,

The definitions are for both the TX and RX pads and pins.

The SERCOM peripheral IO is generated on 4 internal pads. These pads can then be connected to physical pins of the device, by setting the TXPO (Transmit Data Pinout) and RXPO (Receive Data Pinout) bitfields in the SERCOM's CTRLA (Control A) register.

The defines just determine what pad is connected to what pin.

Go Up