Sub topic 9
Transactions: Keeping SPI Settings Correct on a Shared Bus
Assume you know basic Arduino sketches, digital I/O, hexadecimal numbers, and the SPI signals. This chapter focuses on SPI transactions, which keep one peripheral’s settings from affecting another device on the same bus.
Why transactions matter
Several SPI peripherals can share the SCLK, MOSI, and MISO wires. Each peripheral normally has its own chip-select pin. However, devices may require different:
- Clock speeds
- SPI modes
- Bit orders
The Arduino SPI library stores these settings in an SPISettings object. A transaction applies the correct settings for one uninterrupted exchange.
| Call | Purpose |
|---|---|
| `SPI.beginTransaction(settings)` | Apply speed, bit order, and mode |
| `digitalWrite(CS, LOW)` | Select one peripheral |
| `SPI.transfer(...)` | Exchange bytes |
| `digitalWrite(CS, HIGH)` | Stop selecting the peripheral |
| `SPI.endTransaction()` | Finish using the bus |
The chip-select pin should go low only after beginTransaction(). Raise it before endTransaction().
A shared-bus example
This sketch communicates with a sensor and a flash memory device. The command values are placeholders. Replace them with commands from each device's datasheet.
#include <SPI.h>
const uint8_t CS_SENSOR = 9;
const uint8_t CS_FLASH = 10;
SPISettings sensorSettings(2000000, MSBFIRST, SPI_MODE3);
SPISettings flashSettings(8000000, MSBFIRST, SPI_MODE0);
void sendCommand(uint8_t csPin, const SPISettings& settings,
uint8_t command) {
SPI.beginTransaction(settings);
digitalWrite(csPin, LOW);
SPI.transfer(command);
digitalWrite(csPin, HIGH);
SPI.endTransaction();
}
void setup() {
pinMode(CS_SENSOR, OUTPUT);
pinMode(CS_FLASH, OUTPUT);
// Both devices start deselected.
digitalWrite(CS_SENSOR, HIGH);
digitalWrite(CS_FLASH, HIGH);
SPI.begin();
}
void loop() {
sendCommand(CS_SENSOR, sensorSettings, 0x01);
sendCommand(CS_FLASH, flashSettings, 0x02);
delay(100);
}The first transaction changes the SPI peripheral to 2 MHz, most-significant bit first, mode 3. The second changes it to 8 MHz, most-significant bit first, mode 0. The settings do not permanently belong to a chip-select pin. They must be applied again whenever that device is used.
Interrupts and bus ownership
An interrupt is a signal that temporarily runs another piece of code. If an interrupt handler also uses SPI, it can interrupt a transaction and change the hardware settings while the main code is communicating.
On Arduino platforms that support SPI transactions, beginTransaction() helps coordinate SPI use with interrupt code that uses the library's transaction functions. The interrupt handler must still use its own complete transaction and chip-select pin. Do not manually change SPI registers in one part of the program while another part uses SPISettings.
Transaction sequence
Exercise
Why must a program call beginTransaction before selecting a peripheral?
Show answer
It applies that peripheral's speed, bit order, and clock mode before any clock pulses occur.