User Tools

Site Tools


Sidebar

Launch of Linino.org

wiki:spidriverstream

The SPI TTY Driver and the Stream SPI

The communication between the main processor and the MCU can be done through the serial bus or the SPI bus. In order to leave available the serial interface active as console, you have to use the SPI bus.

Almost all of the application out of there use the serial interface to communicate with an MCU; moreover, there are tons of libraries that use the serial interface but few libraries use SPI as communication bus. For this reason, we developed a Linux SPI driver which export a TTY interface to the user space, but it implements the communication on the SPI bus. In this way you can use already existent code without any change.

The SPI TTY Driver

To compile the SPI TTY driver you have to add it to your configuration file. You can directly add the following line to your .config file:

CONFIG_PACKAGE_kmod-spi-tty-ds=y

Or, you can use the pseudo grafical interface

make menuconfig
    Kernel Modules -> SPI Support
       enable kmod-spi-tty-ds module

Once you driver is compiled, you can load it on your device and it will appear the ttySPI0 interface.

ls /dev/tty*
/dev/tty      /dev/ttyS1    /dev/ttyS13   /dev/ttyS3    /dev/ttyS7
/dev/ttyACM0  /dev/ttyS10   /dev/ttyS14   /dev/ttyS4    /dev/ttyS8
/dev/ttyATH0  /dev/ttyS11   /dev/ttyS15   /dev/ttyS5    /dev/ttyS9
/dev/ttyS0    /dev/ttyS12   /dev/ttyS2    /dev/ttyS6    /dev/ttySPI0

Now you are able to use the ttySPI0 interface as an usual tty/serial interface.

This Linux module has three parameters that you can use to configure the module at load time.

  • transfer_delay: it accepts an integer value. It configures the numer of micro-seconds to wait between two consecutive SPI transfers within the same SPI message. The default value is 25us.
  • transfer_len: it accepts an integer value. It configures the maximum SPI transfer lenght. This configuration depends on the MCU capabilities. The default value is 1.
  • max_pending_byte: it accepts an integer value. It configures the maximum number of pending bytes in the SPI controller queue. The default value is 1024.

You can also configure the module at run time thanks to sysfs attributes. You can find the following configuration attributes within the tty device in the sysfs directory /sys/class/tty/ttySPI0.

  • transfer_delay: it accepts an integer value. It configures the numer of micro-seconds to waits between two consecutive SPI transfer within the same SPI message. The default value is 25us. This attribute is initialized by the omonimous module parameter.

The SPI Stream (AVR - Arduino)

On the other side of the bus, on the MCU, there a library object that does the same job. The source library is available here. It communicate over the SPI bus, but it appears to the system like other serial interfaces. The AVR processor with the Arduino framework this is implemented within the class StreamSPI.

To use the StreamSPI you have to include the SPI library:

#include <StreamSPI.h>

Now you can use the pre-instanciated object StreamSPI0 as you can use any other stream object (like Serial).

Use StreamSPI with Firmata

Use the StreamSPI with the Firmata library is very easy. The Firmata library provide an initialization function that accepts a Stream parameter. So in your code you can just write the following line and use Firmata over the SPI bus:

StreamSPI0.begin();
Firmata.begin(StreamSPI0);

Use StreamSPI with Bridge

The StreamSPI can be used also with the Bridge library, but you have to create a new instance of the BridgeClass. Then, you must use the new instance in order to communicate on the SPI bus.

BridgeClass BridgeSPI(StreamSPI0);
StreamSPI0.begin();
BridgeSPI.begin();
/var/www/wiki.linino.org/data/pages/wiki/spidriverstream.txt · Last modified: 2014/03/29 03:04 by administrator

2014 © dog hunter llc and the Linino Community. Linino.org is a dog hunter sponsored community project. Credits