openSUSE:SPI

Jump to: navigation, search

Configuration

Load spidev kernel module

To be able to access SPI from userspace, you need to load spidev kernel module, at each boot.

 modprobe  spidev

To load it automatically at each boot, create /etc/modules-load.d/10-spi.conf file with the following content:

 # Load spidev.ko
 spidev

For Raspberry Pi (all versions), you need to enable I2C-1 access by adding the following lines to /boot/efi/extraconfig.txt:

 # Enable SPI access from Linux
 dtparam=spi=on

spi-tools

spi-tools contains a set of tools to access SPI from command line.

Installation

To install spi-tools, you need to add hardware repo for your openSUSE flavor. For example, for Factory ARM: https://download.opensuse.org/repositories/hardware/openSUSE_Factory_ARM/hardware.repo

One click installation is also available : https://software.opensuse.org/package/spi-tools

List registered SPI devices

ls /dev/spidev*.* will list SPI devices registered on SPI buses. For example, /dev/spidev0.1 is device 1 on SPI bus 0.


Check SPI config

spi-config -d /dev/spidev0.1 -q will return the SPI config for device 1 on SPI bus 0. For RPi3, it will return:

 /dev/spidev0.1: mode=0, lsb=0, bits=8, speed=125000000

Read/Write simultaneously

Sending data from command_1 to SPI link and receiving data from SPI link to command_2.

 command_1 | spi-pipe -d /dev/spidev0.1 | command_2

Example to send 0x010F value, to device 1 on SPI bus 0, at 10 MHz and display read value as raw hexadecimal value:

 echo -n -e "\x01\x0F" | spi-pipe -d /dev/spidev0.1 -s 10000000 | hexdump

Read only

Sending data 0 to SPI link and receiving data from SPI link to command-2.

 spi-pipe -d /dev/spidev0.1 < /dev/zero | command_2

Example to read value as raw hexadecimal value from device 1 on SPI bus 0, at 10 MHz:

 spi-pipe -d /dev/spidev0.1 -s 10000000 < /dev/zero | hexdump


Write only

Sending data from command-1 to SPI link:

 command_1 | spi-pipe -d /dev/spidev0.1

Example to send 0x010F value, to device 1 on SPI bus 0, at 10 MHz:

 echo -n -e "\x01\x0F" | spi-pipe -d /dev/spidev0.1 -s 10000000


Python spidev

python3-spidev contains a set of tools to access SPI from python3.

Installation

To install python3-spidev, just install it from main OSS repo. For Leap 15.1, you need to install it from devel:languages:python repo. One click installation is also available : https://software.opensuse.org/package/python3-spidev

Example

Example of a script which configure spidev0.1 at 10 MHz and write 0x0F01 value on SPI bus:

#!/usr/bin/python3

import spidev

# Define which bus/device to access
spi = spidev.SpiDev()
bus = 0
device = 1
spi.open(bus, device)

# Settings
spi.max_speed_hz = 10000000 # 10 MHz
#spi.mode = 0b01

to_send = [0x0F, 0x01]
spi.xfer(to_send)

# Release SPI
spi.close

See also