Innovation... driven by intelligence and logic

Project.016: SPI Device Driver Development


The "Serial Peripheral Interface" (SPI) is a synchronous four wire serial link used to connect microcontrollers to sensors, memory, and peripherals. It's a simple "de facto" standard, not complicated enough to acquire a standardization body. It is a hardware and firmware communications protocol developed by Motorola and later adopted by everybody. The SPI Bus is used only on the PCB. SPI interfaces are available on popular communication processors such as the MPC8260 and microcontrollers such as the M68HC11. It is a synchronous serial data link that operates in full duplex (signals carrying data go in both directions simultaneously). Devices communicate using a master/slave relationship, in which the master initiates the data frame. When the master generates a clock and selects a slave device, data may be transferred in either or both directions simultaneously.


SPI (synchronous Peripheral Interface ) is bi-directional full duplex data transfer with separate bit and frame synchronization signals. Data is transferred via two unidirectional data lines called MISO (Master In Slave Out) and MOSI (Master Out Slave In). MOSI is sometimes called in master SDO(Serial Data Out) and MISO called as SDI(Serial Data In). MOSI is always driven by master and is always input in slaves. MISO is always input in master and it is three state signal in slaves. Only slave addressed with its representative CS signal asserted will drive MISO . SPI transfer has always read (MISO) and write (MOSI) signals an always operate as simultaneous full duplex data transfer. In case we have simple Write only devices like simple digital/analog converters (DAC) that have only MOSI signal, the master still samples MISO signal and reads meaningless data that should be discarded. Similarly with input only devices, master still transfers data out from MOSI signal but it is ignored or not connected to slave device.

Almost any type of device can be connected to the SPI bus, from simple analog-digital converters to ethernet adapters. Even SD cards have an “SPI mode,” which allows a developer to connect them to the SPI bus.

Linux SPI subsystem driver is divided into three types:
  • SPI bus driver: The main purpose was to provide an API to the underlying master driver and the upper protocol driver to register and send the packet to the underlying driver.
  • SPI master driver: The main work is to initialize the controller, and is responsible for the register write and interrupt processing.
  • SPI protocol driver: Mainly to send SPI message packet to the selected device.

There are two steps you need to get your driver recognized by the Linux SPI system.

  • You need to register a SPI slave device on a particular SPI bus and cable-select position. You can do this during kernel init or you can do it dynamically in your driver code. The device name is specified as a part of this process.
  • You need to register a SPI protocol driver. This driver should use the same name as the devices it will be handling so the kernel can link the two.

From Architecture point of view, bus driver plays the role of an abstraction layer to hide the details of the hardware hidden under a protocol on a specific device.
Therefore master driver does not need to know what peripheral controller connected, the only focus is on how to operate the controller to the sent the message packet. Protocol driver only needs to concentrate on communication and specific peripherals, without the need to understand how to send a message packet.

The format for message packets is defined in spi.h, under spi_message structure. To send data the protocol driver, simply insert spi_message structure's corresponding field and call spi_sync to initiate sending.

In the kernel header include/linux/spi/spi.h is a struct spi_board_info structure that should be populated for any statically registered spi device. You then add the device to the system using the spi_register_board_info() function.


Training Objective:



  • The Duration of Training is:


  • The course is split into Seven modules:


Training Topics in Brief:
  • Creation of spi_board_info structure.
  • spi_register_board_info : Board-specific early init code for registration.
  • spi_alloc_master : This call is used only by SPI master controller drivers, which are the only ones directly touching chip registers. It's how they allocate an spi_master structure, prior to calling spi_register_master.
  • Get a handle to the spi_master controller driver managing the bus .
  • Allocate a spi_device structure for that bus .
  • Verify that no other device has been registered for that particular SPI bus.cs position.
  • Populate the spi_device fields with device specific values (speed, data size, etc...)
  • Adding the new spi_device to the bus
Go to Top ^