Difference between revisions of "BeagleBoard/SPI"

From eLinux.org
Jump to: navigation, search
(Background)
(25 intermediate revisions by 8 users not shown)
Line 73: Line 73:
 
following,
 
following,
 
<pre>
 
<pre>
static struct spi_board_info beagle_mcspi_board_info = {
+
static struct spi_board_info beagle_mcspi_board_info[] __initdata = {
 +
        {
 
.modalias = "spidev",
 
.modalias = "spidev",
 
.max_speed_hz = 48000000, //48 Mbps
 
.max_speed_hz = 48000000, //48 Mbps
Line 79: Line 80:
 
.chip_select = 0,
 
.chip_select = 0,
 
.mode = SPI_MODE_1,
 
.mode = SPI_MODE_1,
 +
        }
 
};
 
};
 
</pre>
 
</pre>
Line 89: Line 91:
  
 
== Patches ==
 
== Patches ==
This [[BeagleBoard/SPIPatch-2.6.32|patch]] was developed against 2.6.32 and exposes through spidev all
+
* [[BeagleBoard/SPI/Patch-2.6.37|2.6.37]] ([[BeagleBoard/SPI/Config-2.6.37|config]]) Patch against kernel 2.6.37 to expose both McSPI 3 and 4 to userspace through spidev.
of the McSPI controllers and chip selects brought out on the BeagleBoard.
+
* [[BeagleBoard/SPI/Patch-rcn-ee-2.6.38.2-d9_mcspi3_gpio144_gpio183|rcn-ee 2.6.38.2-d9]] ([[BeagleBoard/SPI/Config-rcn-ee-2.6.38.2-d9|config]]) Patch against [[https://github.com/RobertCNelson/2.6.38-devel/tree/2.6.38.2-d9 rcn-ee patched kernel 2.6.38.2-d9]] to expose McSPI3 to userspace through spidev. One of the rcn-ee patches enables MMC2 which overrides mux settings for the McSPI3 pins; this patch disables this. It also disables I2C2 and makes GPIO_144 and GPIO_183 available via /sys/class/gpio.
 +
 
 +
== Little Word about recompiling the Kernel ==
 +
Ok, one would have acquired by now all the knowledge necessary to get SPI going on the Beagle Board. However, reconstructing the kernel via OE may be a little tricky, since it is a complex tool and some time is demanded to really understand it. Therefore, it is really useful to know how to compile the kernel by yourself, the instructions on [http://elinux.org/BeagleBoardLinuxKernel] tell you how to do it using "Quilt" (very very handy) kernel 2.6.28 is a good option for the ones using Quilt (take a look at the recipe and you will get why).
 +
 
 +
Debian/Ubuntu: For a more recent kernel, and as an alternative to using Quilt and/or patching the kernel by hand, [[https://github.com/RobertCNelson/2.6.38-devel Robert C Nelson's rcn-ee omap kernel tools]] are recommended. This link is for 2.6.38.2 but other kernel versions are also available. You'll probably need a uInitrd as well; the easiest way is to start at [[BeagleBoardUbuntu#Demo_Image]] using one of the prebuilt kernels and base system images, then build your own up from there.
 +
 
 +
Openembedded: Andruk has been very kind to provide his files mux.c(arch/arm/mach-omap2), mux.h(arch/arm/plat-omap/include/mach) and board-omap3beagle.c(arch/arm/mach-omap2) they are not to be copied but to be consulted and adapted to your needs.  Philip Balister has also made his patch for OE available (useful because it shows which configuration must be active in the kernel .config file).
 +
 
 +
They can be found here: [[FILE:Reference_spi_files.tar.gz|get them here]]
 +
 
 +
TIP: apply the patches, then modify your board-omap3beagle.c. mux.c and mux.h
 +
 
 +
TIP2: some people complained about the MMC card stopping when CONFIG_OMAP_MUX is active. One pointed solution was to comment this line "omap_cfg_reg(AH8_34XX_GPIO29);" in the "board-omap3beagle.c" file (apparently u-boot configures this pin in a
 +
different way). More about this issue can be found in:
 +
[http://groups.google.com/group/beagleboard/browse_thread/thread/4e6b753d2e8a1a68/4d9402047cc32bf1?lnk=raot]
 +
[http://groups.google.com/group/beagleboard/browse_thread/thread/3a7f625695275812/fe01e7f5fbe27f98?lnk=gst&q=config_omap_mux#fe01e7f5fbe27f98]
 +
[http://markmail.org/message/iqqyqmtyskvqq2is]
 +
 
 +
== Chip Select polarity ==
 +
Some devices expect that the CS lines are active-high (instead of the more common active-low). This can be accomplished by modifying spi_board_info in the board file as follows,
 +
 
 +
<pre>
 +
diff --git a/arch/arm/mach-omap2/board-omap3beagle.c b/arch/arm/mach-omap2/board-omap3beagle.c
 +
index 58bacb6..44643b7 100644
 +
--- a/arch/arm/mach-omap2/board-omap3beagle.c
 +
+++ b/arch/arm/mach-omap2/board-omap3beagle.c
 +
@@ -526,7 +526,7 @@ static struct spi_board_info beagle_mcspi_board_info[] = {
 +
                .max_speed_hz  = 48000000, //48 Mbps
 +
                .bus_num        = 3,
 +
                .chip_select    = 0,   
 +
-              .mode = SPI_MODE_1,
 +
+              .mode = SPI_MODE_1 | SPI_CS_HIGH,
 +
        },
 +
</pre>
 +
 
 +
== Hardware ==
 +
 
 +
* [[BeagleBoard_Trainer|Trainer Board]] can be used for access to level shifter SPI and provides prototype area
 +
* [[BeagleBoard_Zippy|Zippy Board]] uses [[Enc28j60|Microchip's ENC28J60]] via the SPI interface, schematics are available
 +
* [[BeagleBoard_Zippy2|Zippy2 Board]] uses Micrel's KSZ8851 via the SPI interface, schematics are available
  
 
== Discussion ==
 
== Discussion ==
 
Here are some mailing list threads of various peoples' experiences getting SPI running on the Beagle:
 
Here are some mailing list threads of various peoples' experiences getting SPI running on the Beagle:
  
* [http://groups.google.com/group/beagleboard/browse_thread/thread/42988f0e14db0f01/816397901ec999c4|MCSPI3 working, both ways]
+
* [http://www.brianhensley.net/2012/02/spi-working-on-beagleboard-xm-rev-c.html MCSPI3 Detailed example (BBXm), Start->Finish]
* [http://groups.google.com/group/beagleboard/browse_thread/thread/c72b69ed45d74403/39094d6bf4f457ad|MCSPI TX works fine, but RX all zeros]
+
* [http://www.brianhensley.net/2012/02/python-controlling-spi-bus-on.html PYTHON SPI how to, BBXM]
* [http://groups.google.com/group/beagleboard/browse_thread/thread/d6a4e0703033cada|SPI testing]
+
* [http://groups.google.com/group/beagleboard/browse_thread/thread/42988f0e14db0f01/816397901ec999c4 MCSPI3 working, both ways]
* [http://groups.google.com/group/beagleboard/browse_thread/thread/320ceba30172fba5/9db5bcf44392d4a0?show_docid=9db5bcf44392d4a0|SPI Troubles]
+
* [http://groups.google.com/group/beagleboard/browse_thread/thread/c72b69ed45d74403/39094d6bf4f457ad MCSPI TX works fine, but RX all zeros]
* [http://markmail.org/message/ktm4e2qytiv4tcm3|SPI with Beagleboard (for a newbie in Linux)]
+
* [http://groups.google.com/group/beagleboard/browse_thread/thread/d6a4e0703033cada SPI testing]
* [http://markmail.org/thread/oyrfigv37df26223|McSPI with REV C]
+
* [http://groups.google.com/group/beagleboard/browse_thread/thread/320ceba30172fba5/9db5bcf44392d4a0?show_docid=9db5bcf44392d4a0 SPI Troubles]
 +
* [http://markmail.org/message/ktm4e2qytiv4tcm3 SPI with Beagleboard (for a newbie in Linux)]
 +
* [http://markmail.org/thread/oyrfigv37df26223 McSPI with REV C]
 +
 
 +
[[Category:BeagleBoard]]

Revision as of 01:12, 27 February 2012

Background

Serial Perhiperal Interface (also known under the names of Microwire or four-wire) is a general-purpose digital I/O interface used by many ICs including sensors, converters, audio codecs, and various types of memory. An SPI bus consists of at least three pins: a clock, a slave-input/master-output (SIMO) pin, a slave-output/master-input (SOMI) pin, and zero or more chip-select(CS) pins.

The OMAP3 has four McSPI controllers, each capable of driving an SPI interface at up to 48 MHz. Each of these controllers has a finite number of chip select lines. Of these four, only SPI3 and SPI4 are brought out on the BeagleBoard.

  • McSPI1: 4 channels
  • McSPI2: 2 channels
  • McSPI3: 3 channels (2 CS brought out)
  • McSPI4: 1 channel (1 CS brought out)

Below is a description of the steps necessary to use SPI on BeagleBoard. If you are particularly impatient, you can start with one of the patches found in the Kernel Patches section.

Configuring Pinmux

These devices are fully supported by the Linux kernel's mcspi driver. That being said, one will need to make some minor modifications to the BeagleBoard's board file (arch/arm/mach-omap2/board-omap3beagle.c) in order to use them.

First, the pin multiplexer needs to be configured to expose the McSPI signals. As of BeagleBoard rev.C4, the following SPI signals are available on the Beagle's 28-pin expansion header; the SPI signals are available in pinmux mode 1, while mode 0 names are also given for reference.

McSPI 3
Header Pin Mode 0 Mode 1 (SPI) Pinmux configuration
21 MMC2_CLKO McSPI3_CLK OMAP_PIN_INPUT (*)
17 MMC2_DAT0 McSPI3_SOMI OMAP_PIN_INPUT
19 MMC2_CMD McSPI3_SIMO OMAP_PIN_OUTPUT
11 MMC2_DAT3 McSPI3_CS0 OMAP_PIN_OUTPUT
13 MMC2_DAT2 McSPI3_CS1 OMAP_PIN_OUTPUT
McSPI 4
Header Pin Mode 0 Mode 1 (SPI) Pinmux configuration
20 BcBSP1_CLKR McSPI4_CLK OMAP_PIN_INPUT (*)
18 McBSP1_DR McSPI4_SOMI OMAP_PIN_INPUT
12 McBSP1_DX McSPI4_SIMO OMAP_PIN_OUTPUT
16 McBSP1_FSX McSPI4_CS0 OMAP_PIN_OUTPUT

(*) Important: The CLK pins must be put in input mode for the chip to correctly register input on SOMI. Failure to set CLK as an input will result in all reads producing zeros.

Configuring spi_board_info

In addition to the pin multiplexer, one must also tell the kernel a bit about the SPI controller itself. This is done in the board file using an spi_board_info struct. For example, to configure McSPI3.0, one might use the following,

static struct spi_board_info beagle_mcspi_board_info[] __initdata = {
        {
	.modalias	= "spidev",
	.max_speed_hz	= 48000000, //48 Mbps
	.bus_num	= 3,
	.chip_select	= 0,	
	.mode = SPI_MODE_1,
        }
};

One would then register this with the SPI subsystem during board initialization (i.e. omap3_beagle_init()), with the following,

spi_register_board_info(beagle_mcspi_board_info,
	ARRAY_SIZE(beagle_mcspi_board_info));

Patches

  • 2.6.37 (config) Patch against kernel 2.6.37 to expose both McSPI 3 and 4 to userspace through spidev.
  • rcn-ee 2.6.38.2-d9 (config) Patch against [rcn-ee patched kernel 2.6.38.2-d9] to expose McSPI3 to userspace through spidev. One of the rcn-ee patches enables MMC2 which overrides mux settings for the McSPI3 pins; this patch disables this. It also disables I2C2 and makes GPIO_144 and GPIO_183 available via /sys/class/gpio.

Little Word about recompiling the Kernel

Ok, one would have acquired by now all the knowledge necessary to get SPI going on the Beagle Board. However, reconstructing the kernel via OE may be a little tricky, since it is a complex tool and some time is demanded to really understand it. Therefore, it is really useful to know how to compile the kernel by yourself, the instructions on [1] tell you how to do it using "Quilt" (very very handy) kernel 2.6.28 is a good option for the ones using Quilt (take a look at the recipe and you will get why).

Debian/Ubuntu: For a more recent kernel, and as an alternative to using Quilt and/or patching the kernel by hand, [Robert C Nelson's rcn-ee omap kernel tools] are recommended. This link is for 2.6.38.2 but other kernel versions are also available. You'll probably need a uInitrd as well; the easiest way is to start at BeagleBoardUbuntu#Demo_Image using one of the prebuilt kernels and base system images, then build your own up from there.

Openembedded: Andruk has been very kind to provide his files mux.c(arch/arm/mach-omap2), mux.h(arch/arm/plat-omap/include/mach) and board-omap3beagle.c(arch/arm/mach-omap2) they are not to be copied but to be consulted and adapted to your needs. Philip Balister has also made his patch for OE available (useful because it shows which configuration must be active in the kernel .config file).

They can be found here: File:Reference spi files.tar.gz

TIP: apply the patches, then modify your board-omap3beagle.c. mux.c and mux.h

TIP2: some people complained about the MMC card stopping when CONFIG_OMAP_MUX is active. One pointed solution was to comment this line "omap_cfg_reg(AH8_34XX_GPIO29);" in the "board-omap3beagle.c" file (apparently u-boot configures this pin in a different way). More about this issue can be found in: [2] [3] [4]

Chip Select polarity

Some devices expect that the CS lines are active-high (instead of the more common active-low). This can be accomplished by modifying spi_board_info in the board file as follows,

diff --git a/arch/arm/mach-omap2/board-omap3beagle.c b/arch/arm/mach-omap2/board-omap3beagle.c
index 58bacb6..44643b7 100644
--- a/arch/arm/mach-omap2/board-omap3beagle.c
+++ b/arch/arm/mach-omap2/board-omap3beagle.c
@@ -526,7 +526,7 @@ static struct spi_board_info beagle_mcspi_board_info[] = {
                .max_speed_hz   = 48000000, //48 Mbps
                .bus_num        = 3,
                .chip_select    = 0,    
-               .mode = SPI_MODE_1,
+               .mode = SPI_MODE_1 | SPI_CS_HIGH,
        },

Hardware

Discussion

Here are some mailing list threads of various peoples' experiences getting SPI running on the Beagle: