Difference between revisions of "Category:SPI"

From Gumstix User Wiki
Jump to: navigation, search
m (Switched order of sections)
(Clean up SPI description and add link for writing a driver)
Line 1: Line 1:
 
== Overo SPI ==
 
== Overo SPI ==
  
Kernel documentation for the SPI framework is excellent and should be your starting point. You can find it under the Documentation directory of your linux source tree or here [http://www.kernel.org/doc/Documentation/spi/spi-summary Documentation/spi/spi-summary].
+
Kernel documentation for the SPI framework can be found in the Documentation directory of your linux source tree or here [http://www.kernel.org/doc/Documentation/spi/spi-summary Documentation/spi/spi-summary].
  
 
Linux SPI drivers are broken into two parts. A controller driver that handles direct communication with the hardware and a protocol driver to handle the details of the data for a particular device. The interface defined in spi.c/spi.h is the bridge between the two.
 
Linux SPI drivers are broken into two parts. A controller driver that handles direct communication with the hardware and a protocol driver to handle the details of the data for a particular device. The interface defined in spi.c/spi.h is the bridge between the two.
  
For the OMAP3's, the controller driver is omap2_mcspi.c. The OMAP3's have 4 SPI busses which the omap2_mcspi driver handles. The kernel config option to include the omap2_mcspi driver is CONFIG_SPI_OMAP24XX.
+
For the OMAP3's, the controller driver is omap2_mcspi.c. The OMAP3's have 4 SPI busses which the omap2_mcspi driver handles. The kernel config option to include the omap2_mcspi driver is CONFIG_SPI_OMAP24XX and is already built into standard Gumstix Overo kernels.
  
The OMAP3 SPI controller has the ability to act as either a SPI master or SPI slave, but the current kernel code only supports the SPI master configuration. There are some new checkins to the kernel code indicating that SPI slave functionality might be coming.
+
The OMAP3 SPI controller has the ability to act as either a SPI master or SPI slave, but the current kernel code only supports the SPI master configuration.
 +
 
 +
Gumstix brings out SPI bus 1 chip select pins 0 and 1 on most of the Overo expansion boards.
  
 
=== Protocol Drivers ===
 
=== Protocol Drivers ===
A protocol driver is probably what you want to write.
 
  
There are a couple of steps you need to follow in writing a SPI protocol driver.
+
A protocol driver is what you need if you want to use the SPI system.
  
# You need to tell the SPI subsystem about the slave device(s) on each SPI bus that you want to use. This gets passed along to the controller driver when it's needed. You can do this during kernel init in a board file like board-overo.c or it can be done at run time in module code.  A device name (modalias) is specified as a key part of this process.
+
There are a couple of steps every SPI protocol driver needs to follow.
# You need to register a SPI protocol driver. This driver should have the same name (modalias) as was specified above. You register a spi_device using the spi_register_driver() call. When this internal mapping connection is made by the SPI subsystem, the result is a probe() call to your protocol driver.
+
  
 +
# Slave devices must be registered for each SPI bus and chip select position used.
 +
# The SPI protocol driver needs to be registered with the system using the same name as the devices it will be handling so the kernel can link the two.
  
It doesn't matter which order you perform these steps. But after both are completed and your probe() function returns success, your driver will be ready for SPI communication. You are given a spi_device in the probe() function that you need to hang onto.
+
It doesn't matter which order these steps are performed. Most existing SPI drivers do the device registration completely outside the driver code in a board file. If you are writing your own driver, it is probably more convenient to do it in the driver code itself. When both device and driver registration are completed, the probe() function for the driver will be called and SPI communications can start.
  
 
SPI data communication happens using I/O queues. One or more SPI transfers are bundled up into SPI messages in the protocol driver. The protocol driver then submits the SPI messages to the controller driver's I/O queue with calls to spi_async(). These messages are processed in FIFO order asynchronously by the controller driver. As each message finishes, the controller notifies the protocol driver via 'completion' callbacks. The protocol driver can then process the raw data as necessary.
 
SPI data communication happens using I/O queues. One or more SPI transfers are bundled up into SPI messages in the protocol driver. The protocol driver then submits the SPI messages to the controller driver's I/O queue with calls to spi_async(). These messages are processed in FIFO order asynchronously by the controller driver. As each message finishes, the controller notifies the protocol driver via 'completion' callbacks. The protocol driver can then process the raw data as necessary.
  
There is also a simple synchronous interface exposed by spi.h/spi.c. Here the SPI framework puts the caller, a protocol driver, to sleep while the same asynchronous interface to the controller driver is used behind the scenes. When the controller is done, the protocol driver is woken up and can then proceed to process the data.  
+
There are some simple synchronous functions exposed by the spi.h/spi.c interface to simplify the asynchronous behaviour of the Linux SPI system for protocol drivers.
  
 
=== Spidev ===
 
=== Spidev ===
  
Spidev is a stock linux module that implements a generic SPI protocol driver while exposing a char device interface to userland via sysfs.
+
Spidev is a standard Linux module that implements a generic SPI protocol driver while exposing a char device interface to userland via sysfs.
  
The documentation can be found in the linux source tree. Here is a link [http://www.mjmwired.net/kernel/Documentation/spi/spidev Documentation/spi/spidev].  
+
The documentation for spidev can be found here - [http://www.mjmwired.net/kernel/Documentation/spi/spidev Documentation/spi/spidev].  
  
Spidev is not designed to dynamically register itself with the SPI subsystem. You will need to add code to do this in either a kernel module or more likely in the board file, board-overo.c, so registration takes place during boot.  
+
The spidev driver does not register devices so a board file patch like this one [https://github.com/scottellis/spike/blob/0d633a22da47022073abd9224ec8709c5b9db932/patches/spidev-enable.patch spidev-enable.patch] is required to use spidev with the Overos. You should customize that patch for the particular device you are using. Things you might want to change are the bus speed and the mode.
  
Spidev implements its own synchronous interface to the SPI system.  
+
The spidev driver is built in to the standard Overo kernels. If you are using OE, apply the board patch by copying it to the org.openembedded.dev/recipes/linux/linux-omap3-2.6.xx/ directory and then adding a line to include the patch in your kernel recipe org.openembedded.dev/recipes/linux/linux-omap3_2.6.xx.bb
 +
 
 +
As you can see from the patch, the standard Overo kernels already occupy the SPI bus 1 chip select lines 0 and 1 if either the ADS7846 touchscreen controller (CS0) or the LGPHILIPS LB035Q02 display driver (CS1) are enabled. You will have to disable these drivers in your kernel config if you want to connect your device to either of these lines. Only one protocol driver at a time can manage a particular CS line on a given SPI bus.
  
 
Here's one Gumstix [http://www.nabble.com/SPI%2C-spidev%2C-et.-al-to24588398.html#a24594755 mailing list thread] posted on 7/21/09, where some users have successfully used spidev with the Overos.
 
Here's one Gumstix [http://www.nabble.com/SPI%2C-spidev%2C-et.-al-to24588398.html#a24594755 mailing list thread] posted on 7/21/09, where some users have successfully used spidev with the Overos.
Line 38: Line 42:
 
You can also search the [http://www.nabble.com/forum/Search.jtp?query=spidev&sort=date&local=y&forum=22543 Gumstix mailing list archives for spidev]
 
You can also search the [http://www.nabble.com/forum/Search.jtp?query=spidev&sort=date&local=y&forum=22543 Gumstix mailing list archives for spidev]
  
=== Another Example Driver ===
+
=== Custom Driver ===
 
+
A simpler OMAP3 linux SPI driver can be found [http://www.github.com/scottellis/overo-adc-mcp3002 here].
+
 
+
Two inexpensive ADC's are used as the slave devices. The protocol for these devices is sufficiently simple that other devices could be easily swapped in place of them and the code modified appropriately. 
+
 
+
This driver demonstrates how to add SPI devices to a SPI bus at runtime instead of at boot time, which can simplify development.
+
 
+
The driver also demonstrates how to handle asynchronous I/O with the SPI controller.
+
 
+
There is no attempt to do anything useful with the ADC data. The driver is just intended to be used to explore the linux SPI framework.
+
  
The driver also exposes a simple character device interface to interact with userland.
+
Here is an article that can get you started writing your own custom SPI driver for Linux using Gumstix Overos as the example board - [http://www.jumpnowtek.com/index.php?option=com_content&view=article&id=57:gumstix-spi-driver-part1&catid=35:gumstix&Itemid=62 Writing a Linux SPI driver]

Revision as of 05:11, 20 January 2011

Overo SPI

Kernel documentation for the SPI framework can be found in the Documentation directory of your linux source tree or here Documentation/spi/spi-summary.

Linux SPI drivers are broken into two parts. A controller driver that handles direct communication with the hardware and a protocol driver to handle the details of the data for a particular device. The interface defined in spi.c/spi.h is the bridge between the two.

For the OMAP3's, the controller driver is omap2_mcspi.c. The OMAP3's have 4 SPI busses which the omap2_mcspi driver handles. The kernel config option to include the omap2_mcspi driver is CONFIG_SPI_OMAP24XX and is already built into standard Gumstix Overo kernels.

The OMAP3 SPI controller has the ability to act as either a SPI master or SPI slave, but the current kernel code only supports the SPI master configuration.

Gumstix brings out SPI bus 1 chip select pins 0 and 1 on most of the Overo expansion boards.

Protocol Drivers

A protocol driver is what you need if you want to use the SPI system.

There are a couple of steps every SPI protocol driver needs to follow.

  1. Slave devices must be registered for each SPI bus and chip select position used.
  2. The SPI protocol driver needs to be registered with the system using the same name as the devices it will be handling so the kernel can link the two.

It doesn't matter which order these steps are performed. Most existing SPI drivers do the device registration completely outside the driver code in a board file. If you are writing your own driver, it is probably more convenient to do it in the driver code itself. When both device and driver registration are completed, the probe() function for the driver will be called and SPI communications can start.

SPI data communication happens using I/O queues. One or more SPI transfers are bundled up into SPI messages in the protocol driver. The protocol driver then submits the SPI messages to the controller driver's I/O queue with calls to spi_async(). These messages are processed in FIFO order asynchronously by the controller driver. As each message finishes, the controller notifies the protocol driver via 'completion' callbacks. The protocol driver can then process the raw data as necessary.

There are some simple synchronous functions exposed by the spi.h/spi.c interface to simplify the asynchronous behaviour of the Linux SPI system for protocol drivers.

Spidev

Spidev is a standard Linux module that implements a generic SPI protocol driver while exposing a char device interface to userland via sysfs.

The documentation for spidev can be found here - Documentation/spi/spidev.

The spidev driver does not register devices so a board file patch like this one spidev-enable.patch is required to use spidev with the Overos. You should customize that patch for the particular device you are using. Things you might want to change are the bus speed and the mode.

The spidev driver is built in to the standard Overo kernels. If you are using OE, apply the board patch by copying it to the org.openembedded.dev/recipes/linux/linux-omap3-2.6.xx/ directory and then adding a line to include the patch in your kernel recipe org.openembedded.dev/recipes/linux/linux-omap3_2.6.xx.bb

As you can see from the patch, the standard Overo kernels already occupy the SPI bus 1 chip select lines 0 and 1 if either the ADS7846 touchscreen controller (CS0) or the LGPHILIPS LB035Q02 display driver (CS1) are enabled. You will have to disable these drivers in your kernel config if you want to connect your device to either of these lines. Only one protocol driver at a time can manage a particular CS line on a given SPI bus.

Here's one Gumstix mailing list thread posted on 7/21/09, where some users have successfully used spidev with the Overos.

You can also search the Gumstix mailing list archives for spidev

Custom Driver

Here is an article that can get you started writing your own custom SPI driver for Linux using Gumstix Overos as the example board - Writing a Linux SPI driver

This category currently contains no pages or media.