Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A20 => Topic started by: KNK on August 24, 2013, 10:26:02 PM

Title: SPI ports
Post by: KNK on August 24, 2013, 10:26:02 PM
Hello,
i have just started my A20 for a fist time today, so apologies if this a stupid and already answered question, but need to use the SPI ports for my application and can't figure it out or find relevant info while searching the forum or web.
I have looked at the A13 python code, but don't know how/where to find the addresses for the SPI ports of A20 and there is no software section for it at all.
What i need is the values for _SC_PAGESIZE and SW_PORTC_IO_BASE for the code below:

PageSize = sysconf(_SC_PAGESIZE);
PageMask = (~(PageSize-1));

addr_start = SW_PORTC_IO_BASE & PageMask;
addr_offset = SW_PORTC_IO_BASE & ~PageMask;

gpio_map = (void *)mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start);
Title: Re: SPI ports
Post by: KNK on September 02, 2013, 08:51:51 PM
Does anyone know how SPI ports should be defined in the fex file ... is the section below correct for UEXT1 (copied from A13 example with small modification) and what it should be for UEXT2

[spi_devices]
spi_dev_num = 2

[spi_board0]
modalias = "spi-sun7i"
max_speed_hz = 100000000
bus_num = 2
chip_select = 0
mode = 0
spi_irq = port:PB04<6><default><default><default>

the problem is that there is no /dev/spi* interface even the driver is compiled in the kernel i.e. not as module
Title: Re: SPI ports
Post by: Mouchon on September 03, 2013, 09:54:14 AM
may be look at

[spiX_para]
spi_used = 0


in the fex file where X=0,1,2,3

but you should take care that spi 0 is shared with  uart6 and uart7
and other devices

see http://linux-sunxi.org/A20/PIO

regards
Title: Re: SPI ports
Post by: KNK on September 03, 2013, 02:08:40 PM
Yes, i have looked at spiX_para, but there is a different piece of information there. I have also read the board PDF and looked the chip documentation and kernel source code, but ...
This is my first time dealing with embedded Linux systems and it is a bit difficult for me yet to get the whole picture, so maybe i am missing something basic here:

1. I am not sure if the name of the section should be spi_board0 or spi_board1 for spi1, but dmesg shows that it is asking for board0 and as spi0 is set to used = 0 my guess is that spi1 is board0.

2. modalias = "spi-sun7i" ... seems to be OK or i should create different alias for each port?

3. max_speed_hz = 100000000 - i got this from the chip documentation 100MHz max speed, but now i see that in mach/spi.h it is defined as:
/* spi controller just suppport 20Mhz */
#define SPI_MAX_FREQUENCY       80000000
and few lines below "int max_speed_hz;       // 80MHz", so it seems 20MHz is a typo and the max is 80000000 - will change that

4. "bus_num = 2" - don't know where to get the info for it ... should it be the MUX column (i.e. 3 from MUX 3) or bus_num = 1, because it's on PA or something else?

5. chip_select = 0 and mode = 0 are OK, because both UEXT1 and UEXT2 have CS0 exported only and the mode depend on the connected hardware

6. spi_irq - if i understand properly this should be an available/unused EINT pin from PH MUX6 or PI MUX 4

Thanks for your help

  EDIT: (from the kernel module source) looks like bus_num should be 1 for SPI1 (2 for SPI2) and then section name spi_board0 seems logical for the first device
now only IRQ remains unclear - should it be some specific pin or any unused is OK?
Title: Re: SPI ports
Post by: KNK on September 03, 2013, 04:59:58 PM
Added to the fex file

[spi_devices]
spi_dev_num = 2

[spi_board0]
modalias = "spi-sun7i"
max_speed_hz = 80000000
bus_num = 1
chip_select = 0
mode = 0
spi_irq = port:PI14<4><default><default><default>

[spi_board1]
modalias = "spi-sun7i"
max_speed_hz = 80000000
bus_num = 2
chip_select = 0
mode = 0
spi_irq = port:PI15<4><default><default><default>

but still no spi in /dev even it is now present in /sys and no errors reported in dmesg:

[    1.800345] [spi-inf] sun7i spi init
[    1.803939] [spi-inf] spi_sun7i_init: spi_used: 0x6
[    1.808857] [spi-inf] sun7i_spi_register_spidev: found 2 spi devices in sys_config
[    1.816446] [spi-inf] boards_num       modalias         max_spd_hz       bus_num  cs   mode
[    1.824813] [spi-inf] 0                spi-sun7i        80000000         1        0    0   
[    1.833166] [spi-inf] 1                spi-sun7i        80000000         2        0    0   
[    1.841526] [spi-inf] sun7i_spi_norflash: Register norflash:at25df641 information OK
[    1.849520] [spi-inf] sun7i_spi_probe: spi1 dma type: dedicate
[    1.855377] [spi-inf] sun7i_spi_probe: spi1 cs bitmap: 0x1
[    1.861120] [spi-inf] sun7i_spi_set_mclk: spi1 source = sata_pll, src_clk = 600000000, mclk 100000000
[    1.870579] [spi-inf] sun7i_spi_probe: reuuimlla's SoC SPI Driver loaded for Bus SPI1 with 2 Slaves at most
[    1.880339] [spi-inf] sun7i_spi_probe: spi1 driver probe succeed, base f0036000, irq 43, dma_id_rx 9, dma_id_tx 25
[    1.890726] [spi-inf] sun7i_spi_probe: spi2 dma type: dedicate
[    1.896573] [spi-inf] sun7i_spi_probe: spi2 cs bitmap: 0x1
[    1.902314] [spi-inf] sun7i_spi_set_mclk: spi2 source = sata_pll, src_clk = 600000000, mclk 100000000
[    1.911731] [spi-inf] sun7i_spi_probe: reuuimlla's SoC SPI Driver loaded for Bus SPI2 with 2 Slaves at most
[    1.921489] [spi-inf] sun7i_spi_probe: spi2 driver probe succeed, base f0038000, irq 44, dma_id_rx 29, dma_id_tx 26

Title: Re: SPI ports
Post by: HalfHour on October 07, 2013, 10:09:09 PM
Hi All,
Also using an A20 board, and need to access the SPI1 interface. Having trouble identifying what I need to do to get a user-mode driver working.
Title: Re: SPI ports
Post by: KNK on October 07, 2013, 10:55:02 PM
There is no driver for the SPI in the latest kernel (images 3 and 4). The logs above are from the 3.3cb kernel, but still couldn't get it to register in /dev and gave up
Title: Re: SPI ports
Post by: KNK on November 06, 2013, 09:26:33 AM
QuoteThe Ultimate A20-OLinuXino #Debian #Linux image done!
And still no SPI support :(
Title: Re: SPI ports
Post by: Mouchon on November 06, 2013, 11:53:21 AM
i have a partial (half duplex) spi support kernel working but still need more investigation to enable full duplex support. If needed i can make an image with my wip.
Title: Re: SPI ports
Post by: Mouchon on November 06, 2013, 11:59:12 AM
forget to say to have /dev/spidevX.Y

you need to change modalias in your fex file with modalias="spidev"


Title: Re: SPI ports
Post by: KNK on November 06, 2013, 12:57:16 PM
Quote from: Mouchon on November 06, 2013, 11:59:12 AM
forget to say to have /dev/spidevX.Y

you need to change modalias in your fex file with modalias="spidev"
I did for the previous image (v3 and v4), but there is just no driver compiled in the kernel.
I have tried to compile the kernel driver from https://github.com/linux-sunxi/linux-sunxi/drivers/spi/spi_sunxi.c but it only supports sun4i and sun5i platforms - no sun7i and there are no changes since when I've tried

Thank you for your suggestion. No need of a full image - the compiled module, git url or just a patch would suffice. I will probably need a full duplex (not sure yet), but may try to extend the driver myself.
Title: Re: SPI ports
Post by: WIll-tm on March 06, 2014, 01:29:14 AM
Hi everyone,

I came across this topic several times, looking for a solution to make SPI work on my Cubieboard2 (A20 based).
I finally got it working and made a blog post about it : http://will-tm.com/spi-on-the-cubieboard2/ (http://will-tm.com/spi-on-the-cubieboard2/), I think it might be useful if some of you are still looking for way to enable an SPI master on A20 devices.
I also share a precompiled kernel (with modules), script.fex, script.fex, kernel source code and defconfig.

Will.
Title: Re: SPI ports
Post by: dave-at-axon on March 06, 2014, 04:36:29 AM
Hi Will,

Thanks for the effort.

I followed your blog and I now have SPI for UEXT1 and 2 on my Android system. Well, it's compiled and they appear as devices as I have not written any test for it yet :)

Title: Re: SPI ports
Post by: Mouchon on March 06, 2014, 10:01:29 AM
does it support full duplex ?
Title: Re: SPI ports
Post by: WIll-tm on March 06, 2014, 10:24:03 AM
I didn't get a chance to test it, but looking at the source code of the driver, it looks like it does.
I just realized that "full_duplex" and "manual_cs" flags are not parsed from the script.fex but full duplex mode seams to activate itself as long as a write buffer AND a read buffer are given to the spi_ioc_transfer.
Title: Re: SPI ports
Post by: HenningA on March 15, 2014, 03:20:02 PM
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?

Title: Re: SPI ports
Post by: sdrollinger on April 10, 2017, 12:37:40 AM
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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* 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");
        exit(1);
    }

    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);
    sleep(1);

    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);



    close(fd);
    close(fd_cs);

        return 0;
}
Title: Re: SPI ports
Post by: pratike14 on April 10, 2017, 01:27:18 PM
A20-Lime2

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,
Pratik
Title: Re: SPI ports
Post by: LubOlimex on April 20, 2017, 01:31:30 PM
Hello,

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,
Lub/OLIMEX
Title: Re: SPI ports
Post by: bubi on December 13, 2017, 05:01:52 PM
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?

Thanks