Using SPI on a Raspberry Pi

I've managed to install OpenWrt on a Raspberry Pi, and now would like to know if it is possible to enable an SPI interface such as a Piface Digital,,:-

http://www.piface.org.uk/products/piface_digital_2/

Under Raspbian this is done using the raspi-config utility

  1. Run opkg install kmod-spi-gpio-custom
  2. Run opkg install kmod-spi-gpio
  3. Run opkg install kmod-spi-dev

Usage

You can use the module to configure up to 4 buses with up to 8 devices each, according to the parameters that you use when loading the module.

The command you’ll use is:
#insmod spi-gpio-custom <parameters>

Here is the official doc from the source:

 *  The following parameters are adjustable:
 *
 *    bus0    These four arguments can be arrays of
 *    bus1    unsigned integers as follows:
 *    bus2
 *    bus3    <id>,<sck>,<mosi>,<miso>,<mode1>,<maxfreq1>,<cs1>,...   
 *
 *  where:
 *
 *  <id>       ID to used as device_id for the corresponding bus (required)
 *  <sck>      GPIO pin ID to be used for bus SCK (required)
 *  <mosi>     GPIO pin ID to be used for bus MOSI (required*)
 *  <miso>     GPIO pin ID to be used for bus MISO (required*)
 *  <modeX>    Mode configuration for slave X in the bus (required)
 *             (see /include/linux/spi/spi.h) 
 *  <maxfreqX> Maximum clock frequency in Hz for slave X in the bus (required)
 *  <csX>      GPIO pin ID to be used for slave X CS (required**)
 *
 *    Notes:
 *    *        If a signal is not used (for example there is no MISO) you need
 *             to set the GPIO pin ID for that signal to an invalid value.
 *    **       If you only have 1 slave in the bus with no CS, you can omit the
 *             <cs1> param or set it to an invalid GPIO id to disable it. When
 *             you have 2 or more slaves, they must all have a valid CS.

Your platform will have GPIOs numbered in a certain range, for example 0-50 or 400-900 (see the OpenWrt Wiki for your router). Anything outside that range is an “invalid value” per the notes above.

Examples

We all know, examples make everything so much easier to understand. (examples are for a TP-Link MR3020)

Single bus with id 1, using gpio 7 as CLK, 29 as MOSI, no MISO and single device in spi mode 0, max 1Khz, with no CS:
#insmod spi-gpio-custom bus0=1,7,29,100,0,1000
This will result in:
/dev/spidev1.0

Single bus with id 1, using gpio 7 as CLK, 29 as MOSI, 26 as MISO, first device in spi mode 0, max 1Khz, with gpio 0 as CS, second device in spi mode 2, max 125Khz, with gpio 17 as CS:
#insmod spi-gpio-custom bus0=1,7,29,26,0,1000,0,2,125000,17
This will result in:
/dev/spidev1.0 and /dev/spidev1.1

Bus with id 1, using gpio 7 as CLK, 29 as MOSI, no MISO, with single device in spi mode 0, max 1Khz, with no CS and Bus with id 2 using gpio 26 as CLK, 17 as MOSI, no MISO with single device in spi mode 2, max 125Khz, with no CS:
#insmod spi-gpio-custom bus0=1,7,29,100,0,1000 bus1=2,26,17,100,2,125000
This will result in:
/dev/spidev1.0 and /dev/spidev2.0

Transferring data

Ok, now how do we transfer data?

Simplex communication is done by just writing and reading that /dev file.
For example,
#echo hello > /dev/spidev1.0
will send “hello\n”, where \n (LF) is added by echo .

For full duplex data transfer you need to use ioctl calls. See the spi-dev documentation. You can also see an example here.

Troubleshooting

If something fails, insmod will give you a description of the fault code, which is very generic and will usually tell you nothing about what happened.
To understand what’s wrong, see the kernel log running dmesg .

If you want to change your configuration, you need to unload the module and reload it with different parameters:
#rmmod spi-gpio-custom #insmod spi-gpio-custom <new parameters>

Last, but not least, remember to unload other modules that may keep the gpio busy, for example leds_gpio:
#rmmod leds_gpio

Reference: https://randomcoderdude.wordpress.com/2013/08/15/spi-over-gpio-in-openwrt/