kabla
Posts: 6
Joined: Fri Feb 14, 2014 3:17 pm

PI 2 spidev SPI buffer size

Wed Oct 28, 2015 2:17 pm

We are trying to change the SPI buffer size in the PI 2 to 32768, with kernel version 4.1.10-v7+.

Here are the steps taken so far:

1) Enable SPI from raspi-config
- Gives the error "modprobe: ERROR: could not insert 'spi_bcm2708': No such device"
- The following works: sudo modprobe spi-bcm2835
2) There's nothing in /etc/modprobe.d/raspi-blacklist.conf
3) "/boot/config.txt" has "dtparam=spi=on" uncommented
4) "/etc/modprobe.d/spidev.conf" has "options spidev bufsiz=32768"
5) Tried manually setting the value by adding write permissions to /sys/module/spidev/parameters/bufsiz
- Previously this worked, we had a crontab script that changed the value
- Now this doesn't work, instead we get an "input/output error"

After all these steps, "cat /sys/module/spidev/parameters/bufsiz" still returns 4096. We're at a loss here. How do you change the buffer size?

User avatar
KevinA
Posts: 64
Joined: Tue Oct 27, 2015 3:50 pm
Location: Park Valley, Utah

Re: PI 2 spidev SPI buffer size

Fri Oct 30, 2015 6:19 pm

My PI 2 with 4.1.7-v7 does not have a /etc/modprobe.d/spidev.conf file. The raspberrypi.org Help on SPI does not have any 'help'.

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

Re: PI 2 spidev SPI buffer size

Sat Oct 31, 2015 8:07 pm

The spidev module is built into the kernel proper (kernel7.img), so you have to set the module parameter on the kernel command line.
Add to /boot/cmdline.txt:

Code: Select all

spidev.bufsiz=32768

kabla
Posts: 6
Joined: Fri Feb 14, 2014 3:17 pm

Re: PI 2 spidev SPI buffer size

Sun Nov 01, 2015 11:40 am

This works! bufsiz finally changed. Thank you so much.

madbutter
Posts: 14
Joined: Mon Sep 07, 2015 4:24 am

Re: PI 2 spidev SPI buffer size

Thu Jun 02, 2016 9:06 am

I have a Pi3 and I am trying change the bufsiz used by spidev, I added
spidev.bufsiz=32768
to /boot/cmdline.txt and rebooted but no change. Still getting error in my python script that 4096 is the maximum transfer size.
I have latest Jessie installed (4.1.19-v7+)
I also tried earlier suggestions to add spidev.conf file to modprobe.d but no luck there either.
Can anyone help?

User avatar
joan
Posts: 13916
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PI 2 spidev SPI buffer size

Thu Jun 02, 2016 9:53 am

madbutter wrote:I have a Pi3 and I am trying change the bufsiz used by spidev, I added
spidev.bufsiz=32768
to /boot/cmdline.txt and rebooted but no change. Still getting error in my python script that 4096 is the maximum transfer size.
I have latest Jessie installed (4.1.19-v7+)
I also tried earlier suggestions to add spidev.conf file to modprobe.d but no luck there either.
Can anyone help?
Perhaps 4096 is the maximum allowed by the Python script.

madbutter
Posts: 14
Joined: Mon Sep 07, 2015 4:24 am

Re: PI 2 spidev SPI buffer size

Fri Jun 03, 2016 3:20 am

Thanks Joan,
Yes that occurred to me. I was hoping that I could snoop around the spidev module and see if it is set in there. I couldn't find the module anywhere though. After a bit of research I typed in Idle:
import spidev
import inspect
inspect.getfile(spidev)
and it reports it as a .so: '/usr/lib/python2.7/dist-packages/spidev.arm-linux-gnueabihf.so'.
I'm a bit out of my depth here. I understand that the module is a wrapper around a C program and that the .so can't be inspected as its compiled.
I guess I have to look for the source and try to see where this 4096 buffer size is set in the wrapper or in the code?
If anyone has any advice on this I would appreciate it - its absolutely crucial I figure out how to change the buffer size!

User avatar
joan
Posts: 13916
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PI 2 spidev SPI buffer size

Fri Jun 03, 2016 7:34 am

madbutter wrote: ...
If anyone has any advice on this I would appreciate it - its absolutely crucial I figure out how to change the buffer size!
I don't think my pigpio Python SPI module has that restriction. Probably just shy of 64k.

fivdi
Posts: 208
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: PI 2 spidev SPI buffer size

Fri Jun 03, 2016 7:44 pm

If I'm not mistaken, the limit of 4096 is defined as bufsiz in the user mode SPI device interface spidev.c here https://github.com/raspberrypi/linux/bl ... idev.c#L92

bufsiz is used at various locations in spidev.c to to check the buffer size and return an EMSGSIZE error if the limit is exceeded, for example, here https://github.com/raspberrypi/linux/bl ... #L191-L192

madbutter
Posts: 14
Joined: Mon Sep 07, 2015 4:24 am

Re: PI 2 spidev SPI buffer size

Tue Jun 07, 2016 4:09 am

Thanks for the reply fivdi,
I have been using a python module that wraps some C code. The file I tired to edit to make a bigger buffer is here:
https://github.com/doceme/py-spidev/blo ... dule.c#L30
I changed the hilighted line to 32768 and the error changed from an Overflow Error (like the one at line 418) to an IOError 90 or sometimes a "Segmentation Fault". So in this case there is something else in the file other than changing that SPIDV_MAXPATH value. I'm not up to understanding what that might be or, alternately, how to wrap the c code you point to make it accessible by python!
For now I have solved the problem using the pigpio module, but this spidev module is simpler to use, doesn't require that I run a daemon, so its frustrating that I can't change the buffer size.

madbutter
Posts: 14
Joined: Mon Sep 07, 2015 4:24 am

Re: PI 2 spidev SPI buffer size

Tue Jun 07, 2016 4:31 am

@Joan, Thanks so much for pointing me to your module, it is pretty sophisticated, maybe a bit dauntingly so from my perspective.;) It works with my expanded buffer size I am happy to report. Interestingly enough, I managed to get the py-spidev module working for me by simply calling its writebytes method 6 times (duh!) with 4096 bytes each time through my loop to transfer the 24567 bytes I need to pass every 100ms or so and there is plenty of time when we check it on the scope (it takes ~43ms to transfer those bytes at 8Mhz).
Your module more efficiently transfers the same number of bytes and my partner who is programming the firmware on the Pic32 receiving these bytes may want them in one chunk rather than 6 bursts so we continue to experiment. Running my pigpio-based program continuously did lock out my SSH and VNC connections to the Pi consistently after a bit (but kept running the transfer fine) but still investigating that. Maybe I am maxing out the CPU? I am using a time.sleep(.05) after each 24567 byte send (in both versions) to give the processor some breathing room.
The only other thing we couldn't figure out was that on the scope using the py-spidev module we can see the Slave Select go low when sending data and with the pigpio version we are seeing nothing on that pin. Have been looking through the spi_flags settings but they do not seem related to this particular setting. My partner thinks he can figure it out by looking more closely at the SPI spec, but if you can shortcut this for me I would appreciate it. Thanks again for your help.

User avatar
joan
Posts: 13916
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: PI 2 spidev SPI buffer size

Tue Jun 07, 2016 7:43 am

madbutter wrote:@Joan, Thanks so much for pointing me to your module, it is pretty sophisticated, maybe a bit dauntingly so from my perspective.;) It works with my expanded buffer size I am happy to report. Interestingly enough, I managed to get the py-spidev module working for me by simply calling its writebytes method 6 times (duh!) with 4096 bytes each time through my loop to transfer the 24567 bytes I need to pass every 100ms or so and there is plenty of time when we check it on the scope (it takes ~43ms to transfer those bytes at 8Mhz).
Your module more efficiently transfers the same number of bytes and my partner who is programming the firmware on the Pic32 receiving these bytes may want them in one chunk rather than 6 bursts so we continue to experiment. Running my pigpio-based program continuously did lock out my SSH and VNC connections to the Pi consistently after a bit (but kept running the transfer fine) but still investigating that. Maybe I am maxing out the CPU? I am using a time.sleep(.05) after each 24567 byte send (in both versions) to give the processor some breathing room.
The only other thing we couldn't figure out was that on the scope using the py-spidev module we can see the Slave Select go low when sending data and with the pigpio version we are seeing nothing on that pin. Have been looking through the spi_flags settings but they do not seem related to this particular setting. My partner thinks he can figure it out by looking more closely at the SPI spec, but if you can shortcut this for me I would appreciate it. Thanks again for your help.
pigpio uses busy waits to keep the SPI buffer filled/emptied. It will use 100% of a core while it is in the SPI transfer. This would not be noticeable for short transactions (the majority of SPI transfers would be several bytes I'd guess).

All you can do is use the maximum SPI bit rate supported by your hardware.

madsci1016
Posts: 4
Joined: Mon Jul 04, 2016 4:00 pm

Re: PI 2 spidev SPI buffer size

Sun Mar 12, 2017 9:03 pm

Has this changed again? I've made the addition to /boot/cmdline.txt (Now starts with spidev.bufsize=32768 ) and I still get "Message too long" when I try to pass a buffer to the SPI larger than 4096. Running 4.4.34 on a model B.

thomen
Posts: 31
Joined: Mon Sep 01, 2014 3:46 am

Re: PI 2 spidev SPI buffer size

Fri Mar 24, 2017 7:42 am

I have the same problem.. Can anyone shed any light on this? I'm trying to do some work with screens and seeing that error

KnarfB
Posts: 198
Joined: Wed Dec 14, 2016 10:47 am
Location: Germany

Re: PI 2 spidev SPI buffer size

Fri Mar 24, 2017 10:13 am

Hi, for me it works
on a RasPi 3B
running Raspbian Jessie,
Linux raspberrypi 4.9.13-v7+ #974 SMP Wed Mar 1 20:09:48 GMT 2017 armv7l GNU/Linux:

add a new parameter to cmdline.txt: spidev.bufsiz=65536
save and reboot
check that parameter is okay:

Code: Select all

cat /sys/module/spidev/parameters/bufsiz
65536
write a test prog:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#define SIZE 65536

int main()
{
  int rc;
  int fd = open("/dev/spidev0.0", O_RDWR);
  if(fd < 0 ) {
    perror("open spi device");
    return -1;
  }

  static struct spi_ioc_transfer tr;
  static uint8_t rx[SIZE];
  static uint8_t tx[SIZE];
  memset( &tr, 0, sizeof(tr) );
  tr.tx_buf = (unsigned long)tx;
  tr.rx_buf = (unsigned long)rx;
  tr.len = SIZE;
  tr.speed_hz = 10000000;
  memset( tx, 0x55, SIZE );
  rc = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  if(rc < 0 ) {
    perror("send message");
    return -1;
  }
  close(fd);
  return 0;
}
compile and run:

Code: Select all

gcc -o test_spi test_spi.c
 ./test_spi
I see the transfer on a scope, it takes 61.42 ms which is quite reasonable.
hth
Frank

WorkIsForChumps
Posts: 1
Joined: Mon Mar 27, 2017 4:16 am

Re: PI 2 spidev SPI buffer size

Mon Mar 27, 2017 5:21 pm

I also need a larger SPI buffer than what is currently available with spidev (4096 bytes). The limitation is that 4096 byte buffer size is hard coded into the spidev module -- adding/changing the parameter in cmdline.txt will not change this. The solution is to modify the spidev module code and reinstall. Here are the steps I followed:

1. download the py-spidev module as a zip file and extract the contents;
2. edit the 'spidev_module.c' file. There are four instances of '4096' in the file. Use search and replace to change all four of these to the desired value. Save the file.
3. remove the existing spidev module:

Code: Select all

sudo pip uninstall spidev
4. reinstall spidev using the modified source file:

Code: Select all

sudo python setup.py install
spidev should now work fine with the new buffer size. It's probably fairly straightforward to update the code to allow the SPI buffer size to be set when initialized, but I will have to leave that to more proficient users than me.

Hope this helps.

georgetony
Posts: 1
Joined: Sun Jul 09, 2017 4:03 pm

Re: PI 2 spidev SPI buffer size

Sun Jul 09, 2017 4:15 pm

Thank you for the steps. I managed to increase my buffer size in both the spidev module and the cmdline.txt.

Currently:

Code: Select all

cat /sys/module/spidev/parameters/bufsiz shows:
2628288
That is the required buffer size for my application.
In Python, I can do this:

Code: Select all

data = [0xff] * 2628288
spi.writebytes(data)
There is no error and it returns very quickly.
But when I capture the output using logic analyzer it only outputs ~60k of the 0xff.
Could it be because of the 150ms timeout? If yes, any suggestions how I can increase it?
Any other reasons the data is cut off at ~60k bytes without error messages?

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