rafal-raf
Posts: 6
Joined: Fri Apr 04, 2014 12:21 pm

Re: SPI driver latency and a possible solution

Tue Apr 08, 2014 6:34 am

My kernel is 3.10.16 TOSSUG Baby Fish from yours master branch git repository https://github.com/msperl/linux

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Tue Apr 08, 2014 10:25 am

OK - 3.10 is not supported directly

Unless you remove those offending lines 502 to 506, which is handling fields introduced with linux 3.12

rafal-raf
Posts: 6
Joined: Fri Apr 04, 2014 12:21 pm

Re: SPI driver latency and a possible solution

Wed Apr 09, 2014 1:51 pm

Hi
What kernel do you use ?
I try kernels form yours git hub and from oficial raspberry git hub master branch.
Still with this result :

/root/ras-new/spi-bcm2835/drivers/spi/spi-bcm2835dma_drv.c: In function 'bcm2835dma_schedule_dma_fragment':
/root/ras-new/spi-bcm2835/drivers/spi/spi-bcm2835dma_drv.c:239:3: error: implicit declaration of function 'list_last_entry' [-Werror=implicit-function-declaration]
/root/ras-new/spi-bcm2835/drivers/spi/spi-bcm2835dma_drv.c:240:20: error: expected expression before 'struct'
Best Regards

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Wed Apr 09, 2014 2:03 pm

I am using a clone of the official 3.13 kernel from the foundation... (exact Version is 3.13.4) but any 3.12 or later should do without any changes...

Martin

rafal-raf
Posts: 6
Joined: Fri Apr 04, 2014 12:21 pm

Re: SPI driver latency and a possible solution

Wed Apr 09, 2014 2:51 pm

Maybe I wrong prepares kernel, can you share yours kernel confige.

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Wed Apr 09, 2014 2:58 pm

no - that is another piece of code that is related to 3.13 or newer.

please try the 3.13 kernel and the standard configs from /proc/config.gz (adding CAN naturally)

martin

Ansible
Posts: 10
Joined: Fri Mar 28, 2014 7:09 pm

Re: SPI driver latency and a possible solution

Wed Apr 16, 2014 3:38 pm

Ok I'm a little lost trying to apply this patch.

I cloned the kernel source and checked it out as of 3.11.4 (I think), and then did a cd into the kernel source directory and issued:

Code: Select all

patch -p1 < ../spi-bcm2835/spi-optimize.patch
That's fixed up spi.c and spi.h.

What do I do with the rest of the spi-bcm2835 folder? I tried doing this:

Code: Select all

cp -r spi-bcm2835/* linux/
That copies in the subdirectories with some new C source files, ok. I assume those are needed with the new patch of spi.c. But it also writes over the Makefile, so that's not going to work. Am I doing it wrong?

Should I try to copy parts of the Makefile from spi-bcm2835 into the Makefile that comes with the linux source? If so, where should those lines go? Or, maybe I should do a make with the spi-bcm2835 Makefile first, and then swap it for the regular kernel makefile? Or maybe I need to do a make in the spi-bcm2835 directory, and then copy some stuff over? Confused!

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Wed Apr 16, 2014 3:43 pm

it is an out-of-tree kernel module.

So just run

Code: Select all

make install
inside this directory
(assuming you run the "kernel" that you have compiled already)

Note that the pathc is not 100% necessary - it is an extension to allow SPI drivers to be more efficient...

Martin

Ansible
Posts: 10
Joined: Fri Mar 28, 2014 7:09 pm

Re: SPI driver latency and a possible solution

Wed Apr 16, 2014 3:48 pm

Ok I'm attempting to cross compile the kernel, so I'm running an amd64 kernel on my machine. So I should:

1) cross compile the kernel, with the spi-bcm2835 patch to spi.c and spi.h, but no other changes from the spi-bch2835 directory
2) transfer the kernel to the PI and boot from that kernel
3) check out spi-bcm2835 onto the PI
4) cd spi-bcm2835
5) make
6) make install

Is that correct? thx!

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Wed Apr 16, 2014 3:55 pm

Unfortunately no: as you cross-compile the kernel you will also have to cross-compile the external module with the same parameters on the x86_64 box.

So follow the analog steps there in the spi-bcm2835 directory you would use in the linux directory...

The only thing you might need to define is KDIR=<path of your kernel source> as a MAKE parameter as well...

Ansible
Posts: 10
Joined: Fri Mar 28, 2014 7:09 pm

Re: SPI driver latency and a possible solution

Thu Apr 17, 2014 3:44 pm

Thanks for all the help. I'm almost there, but not quite 100%.

From the spi-bcm2835 folder I'm issuing this:

make ARCH=arm CROSS_COMPILE=${CCPREFIX} KDIR=../linux

the result is a missing mach/dma.h file. Any ideas on what might be the cause?

Code: Select all

bburdette@Pirx:~/code/pikernel/spi-bcm2835$ make ARCH=arm CROSS_COMPILE=${CCPREFIX} KDIR=../linux
make -C ../linux M=/home/bburdette/code/pikernel/spi-bcm2835 modules
make[1]: Entering directory `/home/bburdette/code/pikernel/linux'
  CC [M]  /home/bburdette/code/pikernel/spi-bcm2835/drivers/spi/spi-bcm2835dma_drv.o
In file included from /home/bburdette/code/pikernel/spi-bcm2835/drivers/spi/spi-bcm2835dma_drv.c:39:0:
/home/bburdette/code/pikernel/spi-bcm2835/drivers/spi/spi-bcm2835dma.h:34:22: fatal error: mach/dma.h: No such file or directory
 #include <mach/dma.h>
                      ^
compilation terminated.
make[2]: *** [/home/bburdette/code/pikernel/spi-bcm2835/drivers/spi/spi-bcm2835dma_drv.o] Error 1
make[1]: *** [_module_/home/bburdette/code/pikernel/spi-bcm2835] Error 2
make[1]: Leaving directory `/home/bburdette/code/pikernel/linux'
make: *** [all] Error 2

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Thu Apr 17, 2014 3:49 pm

OK - then I have to investigate if it really works against linux 3.10.

But it may also be a config option of the kernel that might be different which is why it does not find mach/dma.h.
(i remember reading something about mach/... and a config option, so it might relate)

Which kernel version are you using exactly?

Martin

Ansible
Posts: 10
Joined: Fri Mar 28, 2014 7:09 pm

Re: SPI driver latency and a possible solution

Thu Apr 17, 2014 5:09 pm

I think I'm using 3.13.4. That's what the version is in the makefile.

I initially tried the newest 3.13, but spi.c has changed and so the patch didn't work. according to git history spi.c was changed about 2 months ago. I looked at the makefile git history (I guess the makefile is changed each time the version changes, hopefully after all other changes for that version), and 3.13.4 was the last version before the spi change, so I went with that:

get checkout 93ee5dcd2126535286cf7fe4de7977d51f959bcf

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Fri Apr 18, 2014 8:23 am

I will check myself with the latest updated version...

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Fri Apr 18, 2014 10:37 am

OK - did check against 3.13.y with checksum "37e55d7".
For me I do not see any issues compiling spi-bcm2835dma against this kernel.

So it might be related to .config settings, that may influence this...

Martin

Ansible
Posts: 10
Joined: Fri Mar 28, 2014 7:09 pm

Re: SPI driver latency and a possible solution

Fri Apr 18, 2014 5:37 pm

Thanks for checking. it turns out the .config I started with is from kernel 3.6.11, so its pretty old. I probably answered a question or two wrong from the myriad of questions that 'make oldconfig' asks. I'll see if I can turn up a more current .config to use.

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Fri Apr 18, 2014 6:06 pm

To start: take a current foundation kernel, boot it and then take /proc/config.gz
It contains the settings used by the foundation kernel.

copy it to your directory as .config and tune it to your preferences prior to compiling

Martin

Ansible
Posts: 10
Joined: Fri Mar 28, 2014 7:09 pm

Re: SPI driver latency and a possible solution

Fri Apr 18, 2014 9:16 pm

Ok, I assume by foundation you mean the raspberry pi foundation and not the linux foundation! so I downloaded their arch linux and booted that, which has 3.10.25. Got the /proc/config.gz copied over and used that as .config, answered a number of questions from 'oldconfig' (but not as many as before), and compiled the kernel ok. With the spi make I still have the same result of mach/dma.h not found, unfortunately.

I tried changing mach/dma.h to asm/mach/dma.h; then the error is bcm_dma_chan_free is not found - I can't find that in any of the kernel source, with grep.

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Sat Apr 19, 2014 5:10 am

Seems as if you are not compiling from this git repository:
https://github.com/raspberrypi/linux (which is from the RPI foundation)
the branch: rpi-3.13.y
(right now this maps to 3.13.10)

This one is working for me - I still need to move to the official kernel to get it correctly upstream...

But before I do that I have to find and fix the (hopefully) last race condition in the DMA scheduling, which stalls SPI...
(and then I will probably also have to rewrite the generation code to be more generic)

Martin

Ansible
Posts: 10
Joined: Fri Mar 28, 2014 7:09 pm

Re: SPI driver latency and a possible solution

Sat Apr 19, 2014 2:40 pm

TLDR: got it to compile!

Well I'm inching closer to the goal... it turns out that issuing this:

git checkout 93ee5dcd2126535286cf7fe4de7977d51f959bcf

causes the bcm2708 folder to disappear, and that folder contains include/mach/dma.h, which has bcm_dma_chan_free in it. You have to use:

git checkout 93ee5dcd2126535286cf7fe4de7977d51f959bcf .

with the dot at the end, when you are in the root dir of the working copy. otherwise not everything gets updated by git apparently. Also, I probably wasn't checking out the 3.13.y branch correctly to begin with. Sucks being a noob sometimes! Now I've switched to the 3.13.y branch and not that specific commit, and the patch works. Ok.

Current issue is that the config from the arch kernel specifies bcm2708 (CONFIG_ARCH_BCM2708=y), I was thinking I needed to pick bcm2835. Since arch/arm/mach-bcm2708 is the folder which contains the correct dma.h, maybe I need to pick that. However, when I do 'make oldconfig', 2708 is no longer an option to choose from, I have to pick armv6 and then bcm2835 family.

Nevermind, with the 3.13.y branch, when I do regular make (after oldconfig and setting y for 2835), it brings up the config again, but this time 2708 is an option.

Ok the spi driver compiles! : ) Next up installing and testing!

msperl
Posts: 344
Joined: Thu Sep 20, 2012 3:40 pm

Re: SPI driver latency and a possible solution

Sat Apr 19, 2014 3:24 pm

Something seems to be happening with the foundation kernels - see the 3.13.y-next branch.

Also beware that there are yet some issues with anything not tested yet...
It still eats lots of memory unnecessarily for any driver that does not use the SPI_OPTIMIZE patch.
(at least my dev versions do - I found out...)

Anyway: there is still a race-condition that needs to get addressed (SPI stalls after some time)

As said: I hope I find some time this weekend to do the next rewrite to a simpler interface that will solve all those issues found...

Martin

gregd72002_
Posts: 49
Joined: Mon Apr 14, 2014 8:50 am

Re: SPI driver latency and a possible solution

Tue Jul 22, 2014 12:08 pm

Hi msperl,

Is there any update on the patch, did you manage to close the last bug?

I'm about to give it a try in my project as I experience these strange stalls... It is on 3.10 kernel.

Thanks,
Gregory

manuele.f
Posts: 2
Joined: Sat Sep 13, 2014 10:06 am

Re: SPI driver latency and a possible solution

Sat Sep 13, 2014 10:23 am

Hi guys,

I've been reading your thread and I have a few questions. First of all, sorry if I am totally new to this environment but I still have to read a lot and I'm gonna do this.

I just want to understand what's the approach here. I see that you take advantage of the customized driver for MCP2515 which, I guess has some kind of interaction with the embedded SPI driver for linux. I guess you send and read CAN messages through linux command line, correct me if I am wrong.

I am going to approach to the same hw (RPI + MCP2515) by python (this is a pre-requirement that I cannot discuss :mrgreen: ) and I was wondering if in this scenario implementing a full SPI commands set for the CAN controller is a good idea?!

In this landscape, SPI latency you have been discussing here, can affect in the same way a different implementation like this?

Please feel free to raise any question or doubt or put me on the right path :shock:

alucardku
Posts: 1
Joined: Thu Dec 04, 2014 5:34 pm

Re: SPI driver latency and a possible solution

Thu Dec 04, 2014 10:47 pm

msperl wrote:I am using a clone of the official 3.13 kernel from the foundation... (exact Version is 3.13.4) but any 3.12 or later should do without any changes...

Martin
Thanks Martin and everyone else for your tremendous effort.
I'm currently working under the released raspbian image, that has linux 3.12.22+, I saw way back then that the initial effort started on 3.2, and there was some talk to include it in the linux tree, but never really understood where all this went. The question is, did all the latency improvement got added on 3.10 or is there still some room to improve?

I'm very interested on optimizing the Interrupt version (not DMA) of spi_bcm2708, I understand the limitation on the kernel that context switch latency is inevitable, but using bare-bone drivers I get ridiculously bad results:
first there is a ~13 us jitter between cs active low and clock, which not a whole lot. but then there is a ~500 us delay between transfers (all my test program is doing is blasting serial outputs on a loop)
interrupt_spidev.PNG
interrupt_spidev.PNG (10.11 KiB) Viewed 3426 times
Can anyone recommend a way to speed this up? and what kernel version to use if need to change?
Current trunk https://github.com/msperl/spi-bcm2835 compiles on raspbian but drivers don't seem to work :(
Thanks for the help!!

Cheers
Omar

notro
Posts: 695
Joined: Tue Oct 16, 2012 6:21 pm
Location: Drammen, Norway

Re: SPI driver latency and a possible solution

Fri Dec 05, 2014 5:01 pm

I have Martins DMA driver here: https://github.com/notro/spi-bcm2708/wiki
I use it with SPI connected displays and have been able to do >4MB/s: https://github.com/notro/fbtft/wiki/Performance
There is one issue though: SPI message with multiple transfers fails
I tried to get it included in the kernel before I knew about that issue: https://github.com/raspberrypi/linux/issues/567

Return to “Interfacing (DSI, CSI, I2C, etc.)”