Serial Peripheral Interface (SPI, "spy") is a serial, unicast communication protocol based on the master/slave model. It is primarily used for short-range intraboard communication. On EVT, we most often use SPI buses for communication between our microcontrollers (uCs) and onboard peripherals, like temperature sensors or storage devices. One important thing to note is that SPI is not a well-standardized protocol. This means each manufacturer may handle the implementation slightly differently and may refer to the same concepts with different terms. More information about SPI can be found on Wikipedia.
Each SPI bus can have one master and many slave devices. In most cases, the master will be an STM32 uC, and the slaves will be peripherals. There are four wires that connect each slave device to the master, Serial Clock (SCK), Master-In Slave-Out (MISO), Master-Out Slave-In (MOSI), and Chip Select (CS). For SPI to work properly, the master and all slaves will also need to be connected to the same Ground (GND). SCK is used to help devices properly interpret data sent on the bus. MISO is used to send data from the slave to the master, and MOSI is used to send data from the master to the slave. CS is used to identify which slave the master is communicating with. While all the other connections are shared with the other slaves, each slave gets its own CS connection directly to the master. None of these wires should need pull-up or -down resistors for simple debugging setups.
All SPI communication is is handled with transactions. All transactions are initiated by the master device, which starts each one by toggling the CS wire of the slave device that it wants to communicate with. This causes that slave to become active and begin reading SCK. When the master begins sending a clock signal on SCK, the slave simultaneously reads data off MOSI and sends data on MISO. Not all devices take advantage of this simultaneous send and receive functionality, but some do.
The image below is a screenshot from Logic, the Saleae logic analyzer software. See Saleae Logic Analyzer for more information. The screenshot shows a transaction, where the master toggles the CS (Enable) signal from high to low and exchanges 3 bytes with the slave device. Whenever SCK has a rising edge, going from low to high, the data on MOSI is read by the slave and the data on MISO is read by the master. When CS returns to the idle state of high, it appears the master and slave both continue sending data, but this is not valid data under the SPI specifications.
If CS doesn't toggle properly, the slave won't respond to the master appropriately. If the clock signal isn't well-formed, each device won't be able to interpret the data sent by the other. If Logic isn't able to decode the data on the bus, but it looks good to you, play around with your settings. SPI has a lot of settings, so it's easy to mess one up and fail to read the data.
When reading a datasheet for a SPI device, there are a number of important things you need to find. First, you'll need to find all the configuration parameters to set up the SPI bus. Start by finding the max transmission speed that the device is capable of. Then, you should identify the SPI mode that the device operates in, which is made up of Clock Phase (CPHA) and Clock Polarity (CPOL). This is usually the most difficult thing to identify in the datasheet, but if you're unsure, you can always just guess and check. The last configuration parameter to identify is the bit order, either most significant or least significant first. You can try looking for these parameters stated somewhere explicitly, but if that doesn't work, most datasheets have timing diagrams that implicitly provide this information.
Once you have the SPI bus configured, you'll also need to find register addresses for the data you're interested in. Most of the time, the devices will have more data than we need, so you won't need all of the registers. Make sure you understand the end goal of the driver you're working on, so you get the data you need without pulling everything.