SPI
The header SPI bus, the two platform-specific SPI methods, and the configuration required by SD cards.
SPI is the standard Arduino master on the header pins. Chip select is driven by the sketch from any digital pin; D10 is the convention. The peripheral is SPI2. The Thermal module's video link uses SPI1 on the 40-pin board-to-board connector, on pins that are not on the header.
| Signal | Header pin | MCU pin |
|---|---|---|
| MOSI | MOSI | PD7 |
| MISO | MISO | PD11 |
| SCK | SCK | PF2 |
| CS | any, D10 by convention |
Transfer
#include <SPI.h>
const int cs = D10;
void setup() {
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(cs, LOW);
byte reply = SPI.transfer(0x00);
digitalWrite(cs, HIGH);
SPI.endTransaction();
Serial.print("received 0x");
Serial.println(reply, HEX);
delay(500);
}transfer(byte), transfer16(word) and transfer(buffer, length) are implemented. SPISettings takes the clock in hertz, the bit order and one of the four modes.
Clock range
The default kernel clock is the 200 MHz peripheral bus, divided by a prescaler of 2 to 256, giving approximately 780 kHz to 100 MHz. SD cards require initialisation below 400 kHz, which this range cannot reach.
Platform-specific methods
SPI.setKernelSource(source) selects the clock feeding the peripheral. RCC_SPI2CLKSOURCE_HSI selects the 64 MHz internal oscillator, whose largest division reaches 250 kHz.
SPI.forceDataMode(mode) overrides the mode requested by a library's SPISettings, for libraries that hard code a mode the device does not accept.
SD cards
SD cards use the SdFat library, which is not bundled:
arduino-cli lib install SdFatBoth platform-specific methods are required before initialising a card:
#include <SPI.h>
#include <SdFat.h>
SdFat sd;
void setup() {
SPI.begin();
SPI.setKernelSource(RCC_SPI2CLKSOURCE_HSI); // init clock below 400 kHz
SPI.forceDataMode(SPI_MODE3); // SdFat hard codes MODE0
if (!sd.begin(SdSpiConfig(A5, DEDICATED_SPI, SD_SCK_MHZ(20), &SPI))) {
Serial.println("card failed");
}
}The kernel source is required because the default clock's slowest setting, 781 kHz, exceeds the 400 kHz limit at which a card is guaranteed to respond during initialisation. HSI divided by 256 gives 250 kHz. After initialisation SdFat raises the clock to the configured rate.
The data mode is required because SdFat requests SPI_MODE0 and cards on this peripheral respond reliably only in SPI_MODE3.
Wiring for an SD adapter: MOSI, MISO and SCK from the header, CS on A5, 3.3 V and a common ground. A 10 uF and a 100 nF capacitor at the adapter's supply pin are recommended. On flying leads a fast load step can dip the local supply, and a dip can latch a card until power is removed.
Three bench sketches exercise SD cards: SdCardDoctor for wiring faults, SdCardLogStability for a long soak, and SdCardWriteBenchmark for throughput against clock speed.
Other devices
Displays, radios and converters that use SPI work as on any 3.3 V Arduino. The three SPI pins are usable as general purpose digital pins when SPI is not in use; the LTE example uses MISO as the modem power key output.