SPI ports

Started by KNK, August 24, 2013, 10:26:02 PM

Previous topic - Next topic


Hi Wlll-tm,

I'm trying to use the SPI on the first UEXT-Port at the moment, but I have a bad start so far because I don't know how spi is handled with linux at all. Is there somewhere a good introduction to linux and spi? I found out that there wa change to devicetree at some point and many tutorials cover the procedures before device tree.

Do you know why the spidev module doesn't work that comes with the olimex debian image?

will your kernel work with the olimex-board when i use the script.bin for the olimex board?


I know this is an old thread but I'm trying to get spi working.  I found some code on my A20 board under /opt/spidev/max6675.  Maybe this will help someone else.  There is also a compiled version in the same directory that runs. 

Best Regards,

Steve Drollinger

* untitled.c
* Copyright 2014 Stefan Mavrodiev <support@olimex.com>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>

static const char *device = "/dev/spidev2.0";
static uint8_t mode = 0;
static uint8_t bits = 8;
static uint32_t speed = 50000;

int write_on = 6;
int write_off = 5;

int main(int argc, char **argv)
    int fd;
    int fd_cs;

    fd = open(device, O_RDWR);
    if(fd < 0){
        perror("Failed to open device");

    ioctl(fd, SPI_IOC_WR_MODE, &mode);
    ioctl(fd, SPI_IOC_RD_MODE, &mode);
    ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);

    write(fd_cs, &write_on, 4);

    write(fd_cs, &write_off, 4);
    uint8_t buff[2];

    read(fd, buff, 2);
    write(fd_cs, &write_on, 4);

    uint16_t temp = buff[0] << 8 | buff[1];
    printf("TEMP: %d.%d\n", temp >> 5, ((temp >> 3) & 0x03) * 25);


        return 0;



I too am looking for a way to get SPI working and also I2C port which is shared with PS2 pins. I2C 0 to 4 work, but all the I2C shared with PS2 ports don't seem to have any support. Any direction for this issue would be really helpful.

Thanks in advance,



As you noticed we forgot to enable SPI in the Jessie images for all our A20 boards.

The latest releases for the official Debian images, released yesterday (19.04.2017), should have proper SPI support enabled. Please head to the wiki article of your board and download the latest stable Jessie image.

Image description and typical interfacing: https://github.com/OLIMEX/OLINUXINO/raw/master/SOFTWARE/A20/A20-build-3.4.103-release-5/USAGE-AND-COMMON-PRACTICEs-A20-Olimex_kernel_3.4.103%2B_Jessie_rel_3.txt

Image build instructions and required files for latest Debian Jessie release might be found here: https://github.com/OLIMEX/OLINUXINO/blob/master/SOFTWARE/A20/A20-build-3.4.103-release-5/BUILD_DESCRIPTION_A20_Olimex_kernel_3.4.103%2B_Jessie_rel_5.txt

Best regards,
Technical support and documentation manager at Olimex


Hello, sorry for resume this old topic but I encounter problems using SPI.

I have an A20 micro running the latest linux distribution (A20 OLinuxino Micro debian Jessie 34 103 2G eMMC release 17).

I'm able to see both /dev/spidev1.0 and /dev/spidev2.0 and I'm able to open and configure them (with superuser permissions)

I have connected the LCCD3310 to UEXT1 and executed the test founded in https://github.com/OLIMEX/OLINUXINO/tree/master/SOFTWARE/A20/A20-OLinuXino-Micro%20with%20MOD-LCD3310
What I got is an error message
Can't send spi message!: Invalid argument

I also try the example in the linux-sunxi page http://linux-sunxi.org/SPIdev but I obtain a similar error
SPI_IOC_MESSAGE: Invalid argument

Any suggestion on how I can made SPI work properly?