SAMD21G18 MOSI-/SCK-pin definition

Hello,
I have a SAMD21G18 on an individually designed PCB, so there is no board definition for it. I am using the SparkFun SAMD21 Dev Breakout board definition instead as this board uses the same µC. According to the Dev Breakout board’s schematic (see attached), Pin 19 (PB10) on the µC is called SPI_MOSI and Pin 20 (PB11) is called SPI_SCK for this board.

On my board, I have set up an SPI to communicate with a DAC using the following pins:

Pin 19 (PB10) for MOSI
Pin 20 (PB11) for SCK
PA13 for slave select

I then I assigned the pins like this:

latchPin      	= PIN_PA13;		// Slave Select.
clockPin      	= PIN_SPI_SCK;		// SCK.
dataPin       	= PIN_SPI_MOSI;		// MOSI.

However, this doesn’t seem to work and when I try to transfer data, there is no signal coming out of either of these pins, which I checked with an oscilloscope.

I have tried verifying the pin assignment by using the simple program attached to this post for each of the pins and checking with an oscilloscope, but there is nothing coming out of these pins on the µC. I am suspecting that there is something wrong with the pin assignment and checked the SAMD21 datasheet, but couldn’t find anything that helped solve my problem.

Does anyone know if there is a way to tell the compiler that I want dataPin to be Pin 19 on the µC? What would the syntax be for that?

Thank you!

sparkfun-samd21-pro-breakout-v10.pdf (188 KB)

Testprogram.ino (303 Bytes)

Hi samdstudent,

It's not necessary to redefine pin names: SCK is SCK and MOSI is MOSI.

Your test program doesn't test the SPI port. Have you tried testing the SPI with the Arduino SPI library?

Most of the earlier SAMD21 boards, like the Sparkfun SAMD21 Pro Breakout, follow the same pin pattern as the Arduino Zero, therefore by default SCK should be on PB11 and MOSI on PB10.

Hi MartinL,
thank you for your answer! :slight_smile:
So I could assign the pins like this?

latchPin = PIN_PA13;
clockPin = SCK;
dataPin = MOSI;

It still doesn't work with this code. I get no error message though, which means the compiler recognizes these definitions, they just don't seem to match the pins on the µC I have actually connected.
I did not have a chance to test the pins with an oscilloscope with this code yet, so I am wondering if clockPin and dataPin work now and PIN_PA13 could be the problem? I will test this as soon as possible.

My code should be working even without using the Arduino SPI library, as it did before and the only thing that changed is the pin numbers for MOSI, SCK and slave select.

In searching for a solution to my problem, I had a look at the variant.h file for the Dev Breakout board.
The pins in question are defined there as follows:

#define PIN_SPI_MISO         (22u)
#define PIN_SPI_MOSI         (23u)
#define PIN_SPI_SCK          (24u)

I am not sure if I understand this right, but does this mean that PIN_SPI_MOSI is the name assigned to Pin 23 on the µC? This would explain why I don't get a signal at Pin 19 by using PIN_SPI_MOSI to address it... Could it help to change these entries to the following?

#define PIN_PA13            (22u)
#define PIN_SPI_MOSI        (19u)
#define PIN_SPI_SCK         (20u)

Hi samdstudent,

So I could assign the pins like this?

Yes, that's possible.

but does this mean that PIN_SPI_MOSI is the name assigned to Pin 23 on the µC? This would explain why I don't get a signal at Pin 19 by using PIN_SPI_MOSI to address it... Could it help to change these entries to the following?

Yes, it does, but rather than change the entries, I'd check that you're using the correct "variant.h" file for your board. It looks wrong to me.

Here's the schematic for the Sparkfun SAMD21 Pro Breakout: https://cdn.sparkfun.com/datasheets/Dev/Arduino/Boards/sparkfun-samd21-pro-breakout-v10.pdf.

The schematic show that the default SPI is on port pins: PB11 (SCK), PB10 (MOSI) and PA12 (MISO). It uses the same pins as the Arduino Zero.

Hi MartinL,
I am using the board definitions for the SparkFun SAMD21 Dev Breakout board, which I downloaded using the Arduino board manager. When I look up this board's variant.h file under
C:\Users\myuser\AppData\Local\Arduino15\packages\SparkFun\hardware\samd\1.7.5\variants\SparkFun_SAMD21_Dev,
I find the defintions I mentioned earlier. In fact, when I look at the variant.h file for the Arduino zero under
C:\Users\myuser\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.8\variants\arduino_zero,
I find the same (wrong?) definitions:

#define PIN_SPI_MISO         (22u)
#define PIN_SPI_MOSI         (23u)
#define PIN_SPI_SCK          (24u)

I am really not sure where this is coming from or what to do about it.

Hi samdstudent,

My apologies, these numbers: 22, 23 and 24 are actually the digital pin numbers in the "variant.cpp" file and not the IC pin numbers, as I stated in the previous post. Sorry for causing confusion.

This means that your variant files are correct.

If you call on the default SPI library your SCK will be on port pins: PB11 (SCK), PB10 (MOSI) and PA12 (MISO).