Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A13 => Topic started by: jeroends on February 08, 2013, 10:06:18 AM

Title: SPI in userspace aka spidev
Post by: jeroends on February 08, 2013, 10:06:18 AM
Hello,
I'm trying to enable the spidev driver but I keep getting errors. My spidev is created but nothing is sended through.
This is my setup in the fex file (did also changed the PB02 and PB03 to PE02 and PE03 for the Uext connector):

[spi_devices]
spi_dev_num = 1

[spi1_para]
spi_used = 0
spi_cs0 = port:PG09<2><default><default><default>
spi_cs1 = port:PG13<2><default><default><default>
spi_sclk = port:PG10<2><default><default><default>
spi_mosi = port:PG11<2><default><default><default>
spi_miso = port:PG12<2><default><default><default>

[spi2_para]
spi_used = 1
spi_cs0 = port:PE00<4><default><default><default>
spi_sclk = port:PE01<4><default><default><default>
spi_mosi = port:PE02<4><default><default><default>
spi_miso = port:PE03<4><default><default><default>

[spi_board0]
modalias = "spidev"
max_speed_hz = 20000000
bus_num = 2
chip_select = 0
mode = 3


and the following error keeps bumping in:

[spi]: sw spi init !!
[spi]: sw spi init fetch spi0 uning configuration failed
[spi]: Found 1 spi devices in config files
[spi]: boards num modalias         max_spd_hz       bus_num  cs   mode
[spi]: 0          spidev           20000000         2        0    0x3
[spi]: drivers/spi/spi_sunxi.c(L1872) get spi 2 para failed, err code = -4
[spi]: source = sdram_pll_p, src_clk = 408000000, mclk 102000000
sun5i-spi sun5i-spi.2: master is unqueued, this is deprecated
[spi]: allwinners SoC SPI Driver loaded for Bus SPI-2 with 1 Slaves attached
[spi]: [spi-2]: driver probe succeed, base f1c17000, irq 12, dma_id 2!


I assume I'm looking over something, but I don't know what anymore (same with the imx233, but I edited the dts file and going to try later today, when this fails, the same question will rise over there)
Title: Re: SPI in userspace aka spidev
Post by: jeroends on February 10, 2013, 02:51:05 PM
Hello,

I just had a logic analyser connected to the board hoping to see something happening on the bus when running the standard spi test. But unfortunatly, it stays dead.

Doesn't anybody have any experience with the spidev?

ps: same with the imx233, spidev created (with some strange high number, but it stays dead)
Title: Re: SPI in userspace aka spidev
Post by: jwischka on February 10, 2013, 04:23:19 PM
Quote from: jeroends on February 10, 2013, 02:51:05 PM
Hello,

I just had a logic analyser connected to the board hoping to see something happening on the bus when running the standard spi test. But unfortunatly, it stays dead.

Doesn't anybody have any experience with the spidev?

ps: same with the imx233, spidev created (with some strange high number, but it stays dead)

Are you running a kernel with SPI support built-in?

Title: Re: SPI in userspace aka spidev
Post by: jeroends on February 10, 2013, 07:10:12 PM
idd,
I have them build in (not as module) and made the adjustments in the fex file as listed above (and made it to the script.bin of course).
Spidev2.0 is created, same as the rest:
root@A13Micro:/dev# find / -name "spi*"
/usr/share/vim/vim73/syntax/spice.vim
/usr/share/ca-certificates/spi-inc.org
/usr/share/ca-certificates/spi-inc.org/spi-ca-2003.crt
/usr/share/ca-certificates/spi-inc.org/spi-cacert-2008.crt
/usr/include/linux/spi
/usr/include/linux/spi/spidev.h
/etc/ssl/certs/spi-ca-2003.pem
/etc/ssl/certs/spi-cacert-2008.pem
/sys/bus/spi
/sys/bus/spi/devices/spi2.0
/sys/bus/spi/drivers/spidev
/sys/bus/spi/drivers/spidev/spi2.0
/sys/devices/platform/sun5i-spi.2/spi_master
/sys/devices/platform/sun5i-spi.2/spi_master/spi2
/sys/devices/platform/sun5i-spi.2/spi_master/spi2/spi2.0
/sys/devices/platform/sun5i-spi.2/spi_master/spi2/spi2.0/spidev
/sys/devices/platform/sun5i-spi.2/spi_master/spi2/spi2.0/spidev/spidev2.0
/sys/class/spi_master
/sys/class/spi_master/spi2
/sys/class/spidev
/sys/class/spidev/spidev2.0
/sys/module/spidev
/dev/spidev2.0

Title: Re: SPI in userspace aka spidev
Post by: jeroends on February 11, 2013, 10:13:02 AM
hello,
made a little progress and seen your post about zeroing out the receiver buffer. That did indeed the trick for sending spi data on the spidev. Now let connect some ws2801 led drivers to it and play :-)

On the other hand, playing with the olinuxino maxi spidev didn't gave a good result, data is beeing send, but doesn't compare. I 'll start a new topic on the imx thread.

Btw isn't the bus full duplex that the rx buffer needed to be zero'ed out?
Title: Re: SPI in userspace aka spidev
Post by: vinifr on February 11, 2013, 09:53:58 PM
Hi,

Currently there is no support for full duplex in spi_sunxi.c
Title: Re: SPI in userspace aka spidev
Post by: jeroends on February 12, 2013, 09:09:38 AM
ok, everything works fine (my led's are glowing in the dark) but...
In the debug terminal, the message 'xfer setup" keeps enoying me, is this forgotten to be commented out in the spi_sunxi driver (line 972) or more the way I send my message to the driver?

sorry for the questions, I'm not very formiliar with spi (more like an i2c men  ;) )
Title: Re: SPI in userspace aka spidev
Post by: BJFreeman on May 23, 2013, 12:13:23 AM
Quote from: jeroends on February 10, 2013, 07:10:12 PM
idd,
I have them build in (not as module) and made the adjustments in the fex file as listed above (and made it to the script.bin of course).
Spidev2.0 is created, same as the rest:
root@A13Micro:/dev# find / -name "spi*"
/usr/share/vim/vim73/syntax/spice.vim
/usr/share/ca-certificates/spi-inc.org
/usr/share/ca-certificates/spi-inc.org/spi-ca-2003.crt
/usr/share/ca-certificates/spi-inc.org/spi-cacert-2008.crt
/usr/include/linux/spi
/usr/include/linux/spi/spidev.h
/etc/ssl/certs/spi-ca-2003.pem
/etc/ssl/certs/spi-cacert-2008.pem
/sys/bus/spi
/sys/bus/spi/devices/spi2.0
/sys/bus/spi/drivers/spidev
/sys/bus/spi/drivers/spidev/spi2.0
/sys/devices/platform/sun5i-spi.2/spi_master
/sys/devices/platform/sun5i-spi.2/spi_master/spi2
/sys/devices/platform/sun5i-spi.2/spi_master/spi2/spi2.0
/sys/devices/platform/sun5i-spi.2/spi_master/spi2/spi2.0/spidev
/sys/devices/platform/sun5i-spi.2/spi_master/spi2/spi2.0/spidev/spidev2.0
/sys/class/spi_master
/sys/class/spi_master/spi2
/sys/class/spidev
/sys/class/spidev/spidev2.0
/sys/module/spidev
/dev/spidev2.0


I don't have these in my Debian distribution, where did you get the files?
Title: Re: SPI in userspace aka spidev
Post by: jeroends on May 23, 2013, 07:32:42 AM
do you have a kernel with spi support compiled and made the adjustments in the fex file?
Title: Re: SPI in userspace aka spidev
Post by: BJFreeman on May 23, 2013, 07:20:09 PM
I have the debian distribution updated to ver 7.
that kernel does not have the SPI support as far as I can tell.
I am looking for the file you list, are they in a repository I can access.
Title: Re: SPI in userspace aka spidev
Post by: jeroends on May 23, 2013, 07:51:39 PM
what files do you mean? The spi's in the "/sys"? Those are created by the system itself.
For the debian version, I'm on debian (wheezy) to. I used the debian version available for the micro and compiled then a kernel (3.4.24 at that time) for myself using the guide user "iso9660" wrote (very usefull!).
Title: Re: SPI in userspace aka spidev
Post by: BJFreeman on May 24, 2013, 05:06:20 AM
Ok did a find

found /data/a13/linux-sunxi/Documentation/spi/spidev_fdx.c
/data/a13/linux-sunxi/drivers/spi/spi_sunxi.c

got a link ?
Title: Re: SPI in userspace aka spidev
Post by: jeroends on May 24, 2013, 09:55:38 AM
for the kernel compilation? -> https://www.olimex.com/forum/index.php?topic=790 (https://www.olimex.com/forum/index.php?topic=790)
keep in mind, this is for the MICRO (I think you should use https://www.olimex.com/wiki/Build_Bootable_SD_Card_with_Debian for the MAXI (https://www.olimex.com/wiki/Build_Bootable_SD_Card_with_Debian%20for%20the%20MAXI)).
Enable the spi driver (incl. spidev), if you are intent to use the spi always, you can build it in the kernel instead of building it as module.
Make also the modifications (as above) to your fex file (and convert the fex). After that you should have your spidev in place.
Title: Re: SPI in userspace aka spidev
Post by: vinifr on May 25, 2013, 04:35:06 PM
hi,

What code you are using to test spi? Can you post here, please. ;)
Title: Re: SPI in userspace aka spidev
Post by: jeroends on May 25, 2013, 04:52:54 PM
to test spi output: http://leachy.homeip.net/olinuxino/spi/tst_spi.c (http://leachy.homeip.net/olinuxino/spi/tst_spi.c)
my fex file for the A13 micro: http://leachy.homeip.net/olinuxino/spi/a13.fex (http://leachy.homeip.net/olinuxino/spi/a13.fex)

Notice that the spi test program is just the standard test program, the only difference is that the spi receive buffer is zero'd out. The driver doesn't support full duplex communication (sadly).
Title: Re: SPI in userspace aka spidev
Post by: vinifr on May 25, 2013, 05:04:56 PM
QuoteNotice that the spi test program is just the standard test program, the only difference is that the spi receive buffer is zero'd out. The driver doesn't support full duplex communication (sadly).

Exactly.

You need also change csi_used = 1 to csi_used = 0 because there is a conflict
csi_pck = port:PE00<3> and spi_cs0 = port:PE00<4>
Title: Re: SPI in userspace aka spidev
Post by: jeroends on May 25, 2013, 05:37:37 PM
the spi cs's are a problem I'm dealing with right now (or going to deal with, cfr my other post). Since I'm not going to use the csi interface (for the moment it is still enabled in my fex file) I'm not seeing a big problem with conflicts.
Title: Re: SPI in userspace aka spidev
Post by: vinifr on May 25, 2013, 05:46:52 PM
If you do not resolve this conflict, the kernel does not know what you want:
csi_pck or spi_cs0

And others conflicts:

spi_sclk = port:PE01<4><default><default><default>
spi_mosi = port:PE02<4><default><default><default>
spi_miso = port:PE03<4><default><default><default>



csi_ck = port:PE01<3><default><default><default>
csi_hsync = port:PE02<3><default><default><default>
csi_vsync = port:PE03<3><default><default><default>


Please, just test it! ;)
Title: Re: SPI in userspace aka spidev
Post by: jeroends on May 25, 2013, 06:31:45 PM
For know I didn't noticed problems with it, but I only tried the spi out to a slave (in my case the leds).
Maybe to be sure I'd better put the csi used to zero. But first I'm going to dig deeper into the chip selects because I need more than 2 of them. I know by now the A13 supports 4 of them but they aren't implemented into the driver (only the first 2).
Title: Re: SPI in userspace aka spidev
Post by: BJFreeman on May 25, 2013, 06:35:07 PM
the conflict I see is you can only have in or out, you can switch directions.
both those use the same pins but different directions.
Title: Re: SPI in userspace aka spidev
Post by: Tele on May 26, 2013, 09:56:34 AM
Quote from: jeroends on May 25, 2013, 05:37:37 PM
the spi cs's are a problem I'm dealing with right now (or going to deal with, cfr my other post). Since I'm not going to use the csi interface (for the moment it is still enabled in my fex file) I'm not seeing a big problem with conflicts.

I could be wrong, but I think you forgot an important line:


[spi2_para]
spi_used = 1
spi_cs0 = port:PE00<4><default><default><default>
spi_sclk = port:PE01<4><default><default><default>
spi_mosi = port:PE02<4><default><default><default>
spi_miso = port:PE03<4><default><default><default>


add a line:

[spi2_para]
spi_used = 1
spi_cs_bitmap = 1
spi_cs0 = port:PE00<4><default><default><default>
spi_sclk = port:PE01<4><default><default><default>
spi_mosi = port:PE02<4><default><default><default>
spi_miso = port:PE03<4><default><default><default>


Title: Re: SPI in userspace aka spidev
Post by: jeroends on May 26, 2013, 10:31:07 AM
correct, if you need the chip selects. The example fex file I gave is no more no less to get some spi output.
Title: Re: SPI in userspace aka spidev
Post by: oldpenguin on November 07, 2013, 08:12:51 PM
Hi
I was following this thread and others to be able to use spidev, but with no success.

I found that script.bin is missing the following entry
[spi_devices]
spi_dev_num = 1

This is needed by driver spi_sunxi.c when parsing the script.bin file, otherwise the spi_board_info is empty by default.

Title: Re: SPI in userspace aka spidev
Post by: oldpenguin on November 13, 2013, 08:17:39 PM
Hello all

I had a hard time putting spidev to work.
I have followed many threads and most of them contain useful info, although some info seems a bit outdated, because of recent kernel updates.
Anyway, the following procedure should be enough:

Make sure script.bin contains
[spi2_para]
spi_used = 1
spi_cs_bitmap = 1
spi_cs0 = port:PE00<4><default><default><default>
spi_sclk = port:PE01<4><default><default><default>
spi_mosi = port:PE02<4><default><default><default>
spi_miso = port:PE03<4><default><default><default>

[spi_devices]
spi_dev_num = 1

[spi_board0]
modalias = "spidev"
max_speed_hz = 1000000
bus_num = 2
chip_select = 0
mode = 3

Make sure there are no conflicts on PE00..PE03 pins, namely by deactivating csi (camera) interface:
[csi0_para]
csi_used = 0
...

linux-sunxi/drivers/spi/spi_sunxi.c code will read script.bin and fill the spi_board_info structure, that would otherwise be empty.

Be sure to enable spidev in kernel by setting
Device drivers → SPI support → User mode SPI device driver support as module or compiled inside the kernel.

When the driver is loaded you should see:
root@debian:~# dmesg | grep spi
[    1.187576] [spi]: sw spi init !!
[    1.195933] [spi]: sw spi init fetch spi0 uning configuration failed
[    1.206125] [spi]: Found 1 spi devices in config files
[    1.217520] [spi]: boards num modalias         max_spd_hz       bus_num  cs   mode
[    1.228313] [spi]: spi_board0 irq gpio not used
[    1.239261] [spi]: 0          spidev           1000000          2        0    0x3   
[    1.250157] [spi]: bus num = 2, spi used = 1
[    1.260474] [spi]: source = sdram_pll_p, src_clk = 408000000, mclk 102000000
[    1.273374] sun5i-spi sun5i-spi.2: master is unqueued, this is deprecated
[    1.287005] [spi]: allwinners SoC SPI Driver loaded for Bus SPI-2 with 2 Slaves attached
[    1.301349] [spi]: [spi-2]: driver probe succeed, base f1c17000, irq 12, dma_id 2!

And obviously a device should have been created:
root@debian:~# ls -l /dev/spidev2.0
crw------- 1 root root 153, 0 Nov 13 17:11 /dev/spidev2.0

To test, use the standard spidev_test.c (The version from https://www.kernel.org/doc/Documentation/spi/spidev_test.c)

I had some trouble with this app because the cross-compiler toolchain I am using is missing an entry in spidev.h. The app failed with the following error:
./spidev_test -D/dev/spidev2.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)
can't send spi message: Invalid argument
Aborted
Can you believe it ? It took many ours to find the solution. I finally found it here https://groups.google.com/forum/#!msg/cubieboard/Mjm8c5umEfU/4msuJgZYQJcJ
Make sure you add the interbyte_usecs definition if missing.
__u16 delay_usecs;
__u16 interbyte_usecs;
__u8 bits_per_word;

root@debian:~# ./spidev_test -D/dev/spidev2.0 -s1000000
spi mode: 0
bits per word: 8
max speed: 1000000 Hz (1000 KHz)
[spi]: /home/asc/projects/olinuxino/a13/sunxi-bsp-20131018/linux-sunxi/drivers/spi/spi_sunxi.c(L1025) [ 3189.553812] [spi]: /home/asc/projects/olinuxino/a13/sunxi-bsp-20131018/linux-sunxi/drivers/spi/spi_sunxi.c(L1025) cpu tx data time out!
cpu tx data time out!

FF FF FF FF FF FF
40 00 00 00 00 95
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
DE AD BE EF BA AD
F0 0D

Data is correctly output at the MOSI pin (UEXT pin 8 )
The kernel I am using (3.4.61) already has the DMA patch.
With .rx_buf = 0,//(unsigned long)rx, in the source code, the timeout message disappears.

Intend to test with an MCP23S17. Will post when I have something useful.

Hope this info is useful to someone.
Title: A13 olinuxino spidev mcp23s17 test
Post by: oldpenguin on November 22, 2013, 08:49:58 PM
Hello again

I did manage to make MCP23S17 work with olinuxino-a13.
As promised, the source code is here: https://drive.google.com/file/d/0B_6t4W1iA8KqRi1YMXVaQUNjVGc/edit?usp=sharing

The code is based on several sources: spidev.c, spidev_fdx.c and some test code for MCP23S17.
The most important function is spiSendReceive that performs a write or write-and-then-a-read operation. In the former case, the write operation is performed with spi.rx_buf = 0; to avoid the dreadful timeout. The latter is done with a full-duplex structure.

Enjoy
Title: Re: A13 olinuxino spidev mcp23s17 test
Post by: vinifr on July 12, 2014, 03:08:44 PM
Quote from: oldpenguin on November 22, 2013, 08:49:58 PM
Hello again

I did manage to make MCP23S17 work with olinuxino-a13.
As promised, the source code is here: https://drive.google.com/file/d/0B_6t4W1iA8KqRi1YMXVaQUNjVGc/edit?usp=sharing

The code is based on several sources: spidev.c, spidev_fdx.c and some test code for MCP23S17.
The most important function is spiSendReceive that performs a write or write-and-then-a-read operation. In the former case, the write operation is performed with spi.rx_buf = 0; to avoid the dreadful timeout. The latter is done with a full-duplex structure.

Enjoy

Great job!  ;D