Sub topic 4
Chip select: starting and ending a transfer
This chapter assumes you know the four SPI signals (SCLK, MOSI, MISO, chip select), can write a basic Arduino sketch, and are comfortable with pinMode and digitalWrite. Here you will learn what chip select does, how to drive it correctly, and which mistakes cause the most failures.
What chip select does
Chip select (CS, also called SS for slave select, or CSn when it is active low) is a signal the controller drives to one peripheral. It has two jobs:
- Selection. A peripheral ignores the clock and the data lines unless its CS line is asserted. This lets several peripherals share SCLK, MOSI and MISO.
- Framing. The moment CS is asserted marks the start of a transaction. The moment it is released marks the end. Most peripherals use these edges to reset their internal state machines.
Almost all peripherals use an active-low CS. Asserting means driving the pin LOW. Releasing means driving it HIGH. The datasheet may call the pin CS, /CS, CSn, SS or NSS. A bar over the name or an n suffix means active low.
The Arduino SPI library does not control CS for you when the board is the controller. You choose any digital pin, set it as an output, and toggle it yourself.
The life of one transfer
A single transaction has a fixed order of events.
The steps are:
- Idle: CS is HIGH and the peripheral ignores the bus.
- The controller pulls CS LOW.
- The controller clocks one or more bytes. Each byte is exchanged in both directions at once.
- The controller pulls CS HIGH.
The peripheral may do more than listen during step 2. On many chips, the falling edge of CS makes the peripheral drive MISO. When CS is HIGH, MISO is usually tri-stated (high impedance), so it does not interfere with other peripherals that share the line.
Why the framing edges matter
Many chips treat the whole period of CS low as one command. Consider a typical register read:
- Byte 1 is the command: the register address plus a read flag.
- Byte 2 is a dummy byte whose only purpose is to produce clock pulses so the peripheral can shift its reply out.
If you release CS between those two bytes, the peripheral sees two separate, incomplete commands. It may discard the first one, and the reply is wrong or missing. The same applies to multi-byte writes, burst reads and flash memory page programs. A rule of thumb: keep CS low for the entire command, from first byte to last.
Some chips also latch an action on the rising edge of CS. A flash chip, for example, starts a write cycle only when CS goes HIGH after the write command. If you never release CS, the data is clocked in but the write never starts.
Timing around the CS edges
The datasheet lists timing parameters for CS. Names vary, but these are common:
| Parameter (typical name) | Meaning | Practical consequence |
|---|---|---|
| CS setup time | Minimum time from CS falling to the first SCLK edge | Do not clock immediately after pulling CS low on a fast bus |
| CS hold time | Minimum time from the last SCLK edge to CS rising | Do not release CS right at the final clock edge |
| CS disable time (deselect time) | Minimum time CS must stay HIGH between transactions | Back-to-back transactions need a pause |
At the default AVR speeds, digitalWrite is slow enough (a few microseconds on classic AVR boards) that setup and hold times are usually met without effort. On a fast 32-bit board with fast direct port writes and an SPI clock of several MHz, you may need to add delayMicroseconds(1) or a few NOP instructions. Always compare your clock rate with the datasheet before assuming timing is satisfied.
Driving CS correctly in an Arduino sketch
This sketch reads a register. It shows the standard pattern: configure CS as an output, set it HIGH before anything else, and bracket each transaction with CS low and CS high.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t READ_FLAG = 0x80; // device-specific: many chips use the top address bit to mean read
uint8_t readRegister(uint8_t reg) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | READ_FLAG); // command byte: register address plus read flag
uint8_t value = SPI.transfer(0x00); // send a dummy byte to clock the reply out
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH); // keep the peripheral deselected
SPI.begin();
}
void loop() {
Serial.println(readRegister(0x0F), HEX); // register address is an example; take it from your datasheet
delay(1000);
}Note the order inside readRegister. beginTransaction comes first, then CS goes LOW. At the end, CS goes HIGH, then endTransaction. The CS pulse sits inside the transaction, so no other code can change the SPI settings while the peripheral is selected.
In setup, digitalWrite(CS_PIN, HIGH) is called right after pinMode. On AVR boards, the order of these two calls matters less, but writing HIGH first ensures the pin never glitches LOW when it becomes an output. Some cores handle this better when you write the level before switching the mode. Either way, make sure CS is HIGH before SPI.begin() runs.
The hardware SS pin on AVR boards
On boards such as the Uno, pin 10 is the hardware SS pin of the ATmega328P. The hardware has a quirk. If the board is configured as a controller but pin 10 is an input and a LOW level appears on it, the SPI hardware switches itself to peripheral mode. Your transfers then stop working.
The simple rule: if you use an AVR board as a controller, keep pin 10 as an output, even if the peripheral you use is wired to a different CS pin. The SPI library's SPI.begin() sets SS as an output for you on AVR, but it is still worth remembering when you change pin modes by hand.
Floating CS and pull-up resistors
A CS line that is not driven is floating. It can drift to any level and pick up noise, so the peripheral may see random selection edges. This happens most often:
- during reset or boot, before
setupruns and sets the pin as an output - while the controller is being programmed
- when a wire is disconnected
Add a pull-up resistor (typically 10 kΩ) from each CS line to the peripheral's supply voltage. This holds CS HIGH whenever the controller does not drive it, so the peripheral stays deselected. Many breakout boards already include this pull-up. Check the schematic before adding another one.
A related fault affects shared buses. If you have two peripherals and you forget to set one CS pin HIGH in setup, both can be active at once. Both then try to drive MISO, and you read garbage. Initialise every CS pin to HIGH before the first call to SPI.begin() or any transfer.
Several chip-select lines
Each peripheral on the bus gets its own CS pin from the controller. Only one is LOW at a time. The next chapters cover sharing in detail, but one rule belongs here: never assert two CS lines at once unless the design intends it, because multiple peripherals could drive MISO simultaneously.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Always reads `0xFF` | CS never asserted, or peripheral not powered | Check that CS goes LOW during the transfer |
| Always reads `0x00` | MISO shorted low or peripheral not responding | Check wiring and power |
| First read correct, later reads wrong | CS released mid-command | Keep CS low for the whole command |
| Writes seem to be accepted but have no effect | CS never returns HIGH when the chip needs the rising edge | Release CS after the command |
| Works on a bench, fails at boot | CS floating | Add a pull-up |
| Garbage when two devices are attached | Both CS lines active | Set all CS pins HIGH in `setup` |
Summary
Chip select selects one peripheral and defines where each command begins and ends. Keep it HIGH when idle, pull it LOW for the whole command, release it afterwards, and respect the datasheet's setup, hold and deselect times. Pull-ups protect against a floating line, and every CS pin should be initialised before the bus is used.
Exercise
A flash chip needs this sequence to save data: send a write-enable command, then send a page-program command with data. The code below sets CS LOW, sends the write-enable byte, sends the page-program bytes, and then sets CS HIGH. The chip never saves the data. What is wrong, and how do you fix it?
Show answer
The write-enable command needs its own transaction. The chip latches write-enable on the rising edge of CS after that command, so CS must go HIGH after the write-enable byte and go LOW again before the page-program command. Fix: assert CS, send the write-enable byte, release CS, then assert CS again, send the page-program bytes, and release CS. The second rising edge also starts the actual write cycle.
Exercise
Your sketch reads the same register twice in a row, with CS going HIGH and LOW again immediately between reads. The second read sometimes returns garbage on a fast bus. Which datasheet parameter should you check, and what can you do?
Show answer
Check the CS disable (deselect) time, which is the minimum time CS must stay HIGH between transactions. If the gap is shorter, add a short delay such as delayMicroseconds(1) between the two reads, or lower the clock rate if the chip also needs more time to prepare the next reply.