|
| 1 | +# SPI |
| 2 | + |
| 3 | +## Page Contents |
| 4 | + |
| 5 | +* [Overview](#overview) |
| 6 | +* [Software](#software) |
| 7 | +* [Hardware](#hardware) |
| 8 | +* [Linux driver](#driver) |
| 9 | +* [Troubleshooting](#troubleshooting) |
| 10 | + |
| 11 | + |
| 12 | +## <a name="overview"></a>Overview |
| 13 | + |
| 14 | +The Raspberry Pi is equipped with one [SPI](http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus) bus that has 2 chip selects. |
| 15 | + |
| 16 | +The SPI master driver is disabled by default on Raspian. To enable it, remove the blacklisting for `spi-bcm2708` in `/etc/modprobe.d/raspi-blacklist.conf`, or use [raspi-config](../../configuration/raspi-config.md). |
| 17 | +Reboot or load the driver manually with: |
| 18 | +``` |
| 19 | +$ sudo modprobe spi-bcm2708 |
| 20 | +``` |
| 21 | + |
| 22 | +The SPI bus is available on the P1 Header: |
| 23 | +``` |
| 24 | +MOSI P1-19 |
| 25 | +MISO P1-21 |
| 26 | +SCLK P1-23 P1-24 CE0 |
| 27 | +GND P1-25 P1-26 CE1 |
| 28 | +``` |
| 29 | + |
| 30 | + |
| 31 | +## <a name="software"></a>Software |
| 32 | + |
| 33 | +### WiringPi |
| 34 | +WiringPi includes a library which can make it easier to use the Raspberry Pi’s on-board SPI interface. Accesses the hardware registers directly. |
| 35 | + |
| 36 | +http://wiringpi.com/ |
| 37 | + |
| 38 | +### bcm2835 library |
| 39 | +This is a C library for Raspberry Pi (RPi). It provides access to GPIO and other IO functions on the Broadcom BCM 2835 chip. Accesses the hardware registers directly. |
| 40 | + |
| 41 | +http://www.airspayce.com/mikem/bcm2835/ |
| 42 | + |
| 43 | +### Use spidev from C |
| 44 | +There's a loopback test program in the Linux documentation that can be used as a starting point. See the [Troubleshooting](#troubleshooting) section. Uses the Linux `spidev` driver to access the bus. |
| 45 | + |
| 46 | +### Shell |
| 47 | +``` |
| 48 | +# Write binary 1, 2 and 3 |
| 49 | +echo -ne "\x01\x02\x03" > /dev/spidev0.0 |
| 50 | +``` |
| 51 | + |
| 52 | +## <a name="hardware"></a>Hardware |
| 53 | +The BCM2835 on the Raspberry Pi has 3 SPI Controllers. Only the SPI0 controller is available on the header. |
| 54 | +Chapter 10 in the [BCM2835 ARM Peripherals](http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf) datasheet describes this controller. |
| 55 | + |
| 56 | +### Master modes |
| 57 | + |
| 58 | +Signal name abbreviations |
| 59 | +``` |
| 60 | +SCLK - Serial CLocK |
| 61 | +CE - Chip Enable (often called Chip Select) |
| 62 | +MOSI - Master Out Slave In |
| 63 | +MISO - Master In Slave Out |
| 64 | +SDA - Serial DAta |
| 65 | +MOMI - Master Out Master In |
| 66 | +MIMO - Master In Master Out |
| 67 | +``` |
| 68 | + |
| 69 | +#### Standard mode |
| 70 | +In Standard SPI master mode the peripheral implements the standard 3 wire serial protocol (SCLK, MOSI and MISO). |
| 71 | + |
| 72 | +#### Bidirectional mode |
| 73 | +In bidirectional SPI master mode the same SPI standard is implemented except that a single wire is used for data (MIMO) instead of two as in standard mode (MISO and MOSI). |
| 74 | + |
| 75 | +#### LoSSI mode (Low Speed Serial Interface) |
| 76 | +The LoSSI standard allows issuing of commands to peripherals (LCD) and to transfer data to and from them. LoSSI commands and parameters are 8 bits long, but an extra bit is used to indicate whether the byte is a command or parameter/data. This extra bit is set high for a data and low for a command. The resulting 9-bit value is serialized to the output. LoSSI is commonly used with [MIPI DBI](http://mipi.org/specifications/display-interface) type C compatible LCD controllers. |
| 77 | + |
| 78 | +**Note:** |
| 79 | +Some commands trigger an automatic read by the SPI controller, so this mode can't be used as a multipurpose 9-bit SPI. |
| 80 | + |
| 81 | +### Transfer modes |
| 82 | +* Polled |
| 83 | +* Interrupt |
| 84 | +* DMA |
| 85 | + |
| 86 | +### Speed |
| 87 | +The CDIV (Clock Divider) field of the CLK register sets the SPI clock speed: |
| 88 | +``` |
| 89 | +SCLK = Core Clock / CDIV |
| 90 | +If CDIV is set to 0, the divisor is 65536. The divisor must be a power of 2. Odd numbers rounded down. The maximum SPI clock rate is of the APB clock. |
| 91 | +``` |
| 92 | +[Errata](http://elinux.org/BCM2835_datasheet_errata): "must be a power of 2" probably should be "must be a multiple of 2" |
| 93 | + |
| 94 | +See the [Linux driver](#driver) section for more info. |
| 95 | + |
| 96 | +### Chip Select |
| 97 | +The controller supports 3 Chip selects, but only 2 is available on the header. |
| 98 | + |
| 99 | +Setup and Hold times related to the automatic assertion and de-assertion of the CS lines when operating in **DMA** mode are as follows: |
| 100 | +* The CS line will be asserted at least 3 core clock cycles before the msb of the first byte of the transfer. |
| 101 | +* The CS line will be de-asserted no earlier than 1 core clock cycle after the trailing edge of the final clock pulse. |
| 102 | + |
| 103 | + |
| 104 | +## <a name="driver"></a>Linux driver |
| 105 | +The default Linux driver is [spi-bcm2708](https://github.com/raspberrypi/linux/blob/rpi-3.12.y/drivers/spi/spi-bcm2708.c). |
| 106 | +The following information was valid 2014-07-05. |
| 107 | + |
| 108 | +### Speed |
| 109 | +The driver supports the following speeds |
| 110 | +``` |
| 111 | + cdiv speed |
| 112 | + 2 125.0 MHz |
| 113 | + 4 62.5 MHz |
| 114 | + 8 31.2 MHz |
| 115 | + 16 15.6 MHz |
| 116 | + 32 7.8 MHz |
| 117 | + 64 3.9 MHz |
| 118 | + 128 1953 kHz |
| 119 | + 256 976 kHz |
| 120 | + 512 488 kHz |
| 121 | + 1024 244 kHz |
| 122 | + 2048 122 kHz |
| 123 | + 4096 61 kHz |
| 124 | + 8192 30.5 kHz |
| 125 | + 16384 15.2 kHz |
| 126 | + 32768 7629 Hz |
| 127 | +``` |
| 128 | +When asking for say 24 MHz, the actual speed will be 15.6 MHz. |
| 129 | + |
| 130 | +Forum post: [SPI has more speeds](http://www.raspberrypi.org/phpBB3/viewtopic.php?f=44&t=43442&p=347073) |
| 131 | + |
| 132 | +### Supported Mode bits |
| 133 | +``` |
| 134 | +SPI_CPOL - Clock polarity |
| 135 | +SPI_CPHA - Clock phase |
| 136 | +SPI_CS_HIGH - Chip Select active high |
| 137 | +SPI_NO_CS - 1 device per bus, no Chip Select |
| 138 | +``` |
| 139 | +Bidirectional mode is currently not supported. |
| 140 | + |
| 141 | +### Supported bits per word |
| 142 | +* 8 - Normal |
| 143 | +* 9 - This is supported using LoSSI mode. |
| 144 | + |
| 145 | +### Transfer modes |
| 146 | +Only interrupt mode is supported. |
| 147 | + |
| 148 | +### Deprecated warning |
| 149 | +The following appears in the kernel log: |
| 150 | +``` |
| 151 | +bcm2708_spi bcm2708_spi.0: master is unqueued, this is deprecated |
| 152 | +``` |
| 153 | + |
| 154 | +### SPI driver latency |
| 155 | +This [thread](http://www.raspberrypi.org/phpBB3/viewtopic.php?f=44&t=19489) discusses latency problems. |
| 156 | + |
| 157 | +### DMA capable driver |
| 158 | +This is a fork of spi-bcm2708 which enables DMA support for SPI client drivers that support DMA. |
| 159 | + |
| 160 | +https://github.com/notro/spi-bcm2708 ([wiki](https://github.com/notro/spi-bcm2708/wiki)) |
| 161 | + |
| 162 | + |
| 163 | +## <a name="troubleshooting"></a>Troubleshooting |
| 164 | + |
| 165 | +### Loopback test |
| 166 | +This can be used to test SPI send and receive. Put a wire between MOSI and MISO. It does not test CE0 and CE1. |
| 167 | +``` |
| 168 | +$ wget https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c |
| 169 | +$ gcc -o spidev_test spidev_test.c |
| 170 | +$ ./spidev_test -D /dev/spidev0.0 |
| 171 | +spi mode: 0 |
| 172 | +bits per word: 8 |
| 173 | +max speed: 500000 Hz (500 KHz) |
| 174 | +
|
| 175 | +FF FF FF FF FF FF |
| 176 | +40 00 00 00 00 95 |
| 177 | +FF FF FF FF FF FF |
| 178 | +FF FF FF FF FF FF |
| 179 | +FF FF FF FF FF FF |
| 180 | +DE AD BE EF BA AD |
| 181 | +F0 0D |
| 182 | +``` |
| 183 | + |
| 184 | +If you get compilation errors, try the latest version instead |
| 185 | +``` |
| 186 | +$ wget https://raw.github.com/torvalds/linux/master/Documentation/spi/spidev_test.c |
| 187 | +``` |
0 commit comments