Arduino SPI, SAM R21E Sercom4 and RFCTRL

172 views
Skip to first unread message

Alex Albino

unread,
Apr 18, 2016, 1:00:51 AM4/18/16
to devel...@arduino.cc

No matter what I do, I can't seem to get SPI on sercom4 to respond. I'm using the Atmel SAM R21E (ATSAMR21E18A).

Does this pic of variant.cpp config look right?

IMG_20160417_214903.jpg

Daniel De Kock

unread,
Apr 19, 2016, 3:13:12 AM4/19/16
to Developers

You might also have to set the pin/ PAD mux in "variant.h" to correspond with your pin naming in variant.cpp
like this example from the MKR1000 :

#define PIN_SPI_MISO  (10u)
#define PIN_SPI_MOSI  (8u)
#define PIN_SPI_SCK   (9u)
#define PIN_SPI_SS    (24u)
#define PERIPH_SPI    sercom1
#define PAD_SPI_TX    SPI_PAD_0_SCK_1
#define PAD_SPI_RX    SERCOM_RX_PAD_3
static const uint8_t SS   = PIN_SPI_SS;   // SPI Slave SS not used. Set here only for reference.
static const uint8_t MOSI = PIN_SPI_MOSI;
static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK  = PIN_SPI_SCK;

// SPI1: Connected to WINC1501B
#define PIN_SPI1_MISO (29u)
#define PIN_SPI1_MOSI (26u)
#define PIN_SPI1_SCK  (27u)
#define PIN_SPI1_SS   (28u)
#define PERIPH_SPI1   sercom2
#define PAD_SPI1_TX   SPI_PAD_0_SCK_1
#define PAD_SPI1_RX   SERCOM_RX_PAD_3
static const uint8_t SS1   = PIN_SPI1_SS;
static const uint8_t MOSI1 = PIN_SPI1_MOSI;
static const uint8_t MISO1 = PIN_SPI1_MISO;
static const uint8_t SCK1  = PIN_SPI1_SCK;

Daniel De Kock

unread,
Apr 19, 2016, 3:21:14 AM4/19/16
to Developers
I used Atmel start to get the Sercom configuration to enable another SPI port on the MKR1000 : http://start.atmel.com/

You might aso have to set the default mode for those pins to "PIO_SERCOM_ALT" or "PIO_SERCOM" in variant.cpp - I'm not entirely sure if that is required or if it only sets the default initialization mode


also make sure you have a Sercom instance - If you copied the Arduino Zero/ mkr1000 variant files it should already be there:
// Multi-serial objects instantiation
SERCOM sercom0
(SERCOM0);
SERCOM sercom1
(SERCOM1);
SERCOM sercom2
(SERCOM2);
SERCOM sercom3
(SERCOM3);
SERCOM sercom4
(SERCOM4);
SERCOM sercom5
(SERCOM5);

also make sure you have the correct amount of SPI interfaces defined in variant.h:
// SPI Interfaces
// --------------
#define SPI_INTERFACES_COUNT 2




MKR1000  has a sercom on pin 0 and 1 That I want to use for LED control so it needed to be changed to SPI mode.


On Monday, 18 April 2016 07:00:51 UTC+2, Alex Albino wrote:

Alex Albino

unread,
Apr 19, 2016, 6:50:21 AM4/19/16
to devel...@arduino.cc
Thanks, but I still can't seem to get it working. 

I've scanned the data sheet for the SAM R21, the LwMesh Peer2Peer example, the atsamr21e18a variant, cores/arduino_osc32k. Reading through the few google search results that relate to SPI on SERCOM4 (SAM R21) yields nothing of use.

The example sketch is in the libraries/FemtoBeacon_Rf/FemtoBeacon_Rf_Wireless_Data/ folder

I've checked my definitions for the SPI pins on SERCOM4 (peripheral F) in variants/atsamr21e18a/variant.cpp

/* +------------+------------------+--------+-----------------+--------------------------------------------------------------------------------------------------------
 * | Pin number | ATRF233          |  PIN   | Label/Name      | Comments (* is for default peripheral in use)
 * +------------+------------------+--------+-----------------+--------------------------------------------------------------------------------------------------------
 * | 9          |                  |  PB30  | MOSI            | SERCOM4/PAD[2]   peripheral F
 * | 10         |                  |  PC19  | MISO            | SERCOM4/PAD[0]   peripheral F
 * | 11         |                  |  PC18  | SCK             | SERCOM4/PAD[3]   peripheral F
 * | 12         |                  |  PB31  | SS (/SEL, CS)   | SERCOM4/PAD[1]   peripheral F
 * |            |                  |        |                 |  
 * | 13         |                  |  PB00  | IRQ             | EXTINT[0]
 * | 14         |                  |  PA20  | SLP_TR          | 
 * | 15         |                  |  PB15  | /RST            | 
 * +------------+------------------+--------+-----------------+--------------------------------------------------------------------------------------------------------
 */
  { PORTB, 30, PIO_TIMER_ALT, (PIN_ATTR_DIGITAL|PIN_ATTR_TIMER_ALT), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // SERCOM4/PAD[2]
  { PORTC, 19, PIO_TIMER_ALT, (PIN_ATTR_DIGITAL|PIN_ATTR_TIMER_ALT), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // SERCOM4/PAD[0]
  { PORTC, 18, PIO_TIMER_ALT, (PIN_ATTR_DIGITAL|PIN_ATTR_TIMER_ALT), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // SERCOM4/PAD[3]
  { PORTB, 31, PIO_TIMER_ALT, (PIN_ATTR_DIGITAL|PIN_ATTR_TIMER_ALT), No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // SERCOM4/PAD[1]
  { PORTB,  0, PIO_DIGITAL, PIN_ATTR_DIGITAL, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_0 }, // EXTINT[0]
  { PORTA, 20, PIO_DIGITAL, PIN_ATTR_DIGITAL, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // 
  { PORTB, 15, PIO_DIGITAL, PIN_ATTR_DIGITAL, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE }, // 


I've checked my setup for SPI on SERCOM4, in variants/atsamr21e18a/variant.h
/*
 * SPI Interfaces
 */
#define SPI_INTERFACES_COUNT 1
#define PIN_SPI_MOSI         ( 9u) // Tx, PAD 2
#define PIN_SPI_MISO         (10u) // Rx, PAD 0
#define PIN_SPI_SCK          (11u) //     PAD 3
#define PIN_SPI_SS           (12u) //     PAD 1
#define PERIPH_SPI           sercom4
#define PAD_SPI_TX           SPI_PAD_2_SCK_3
#define PAD_SPI_RX           SERCOM_RX_PAD_0

static const uint8_t SS   = PIN_SPI_SS  ;
static const uint8_t MOSI = PIN_SPI_MOSI ;
static const uint8_t MISO = PIN_SPI_MISO ;
static const uint8_t SCK  = PIN_SPI_SCK ;





...and I am using my fork of arduino-at86rf233: https://github.com/femtoio/arduino-at86rf233/tree/library


:-/ I'm out of ideas. I'm hoping someone may come across something I've missed.


--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

Alex Albino

unread,
Apr 19, 2016, 8:47:00 PM4/19/16
to devel...@arduino.cc

Hi Daniel,

You were right!

I erroneously presumed variant.cpp definitions would automatically set pinmuxing during pin setup... But it turns out I had to call pinPeripheral() manually to set PMUX stuff before initializing SPI on SERCOM4.

Success, Arduino comparability for the Atmel SAM R21E is working, radio and all.

- Alex

IMG_20160419_174126.jpg
IMG_20160419_174149.jpg
IMG_20160419_131528.jpg

William Westfield

unread,
Apr 19, 2016, 9:17:05 PM4/19/16
to devel...@arduino.cc
> I erroneously presumed variant.cpp definitions would automatically set pinmuxing during pin setup

It’s not too surprising that it doesn’t. After all, the “core” doesn’t directly support SPI…

BillW/WestfW

Reply all
Reply to author
Forward
0 new messages