Coder Social home page Coder Social logo

increased spi buffer sizer about py-spidev HOT 6 OPEN

CrabbyPete avatar CrabbyPete commented on August 23, 2024
increased spi buffer sizer

from py-spidev.

Comments (6)

gnbl avatar gnbl commented on August 23, 2024 1

According to https://www.raspberrypi.org/forums/viewtopic.php?t=124472&p=837140#p1138833, there are several occurances of the magic number 4096 (hardcoded).

Searching myself, it just seems to be defined here:

#define SPIDEV_MAXPATH 4096
and used only once here https://github.com/doceme/py-spidev/search?utf8=%E2%9C%93&q=SPIDEV_MAXPATH+&type=

So your error is likely unrelated. You could try to check the arguments' type or ensure they are int.


sudo python3 setup.py install

However, changing the buffer size works surprisingly easy (Raspbian Stretch lite with some prior build setups I don't remember, so you may have to figure out some dependencies):

  • add spidev.bufsiz=32768 to the line in /boot/cmdline.txt and reboot sometime
    • test with cat /sys/module/spidev/parameters/bufsiz
  • git clone https://github.com/doceme/py-spidev.git
  • cd py-spidev/
  • nano spidev_module.c
    • change #define SPIDEV_MAXPATH 4096
  • make PYTHON=python3
  • sudo python3 setup.py install
  • rm -rf py-spidev/

from py-spidev.

mweber-ovt avatar mweber-ovt commented on August 23, 2024 1

@gnbl, your solution works without problems. Thanks!

from py-spidev.

dimas avatar dimas commented on August 23, 2024 1

Okay, @doceme , here comes the patch :) #75

I decided not to touch any of the existing methods and added xfer3. It accepts lists of arbitrary size and splits large lists into smaller chunks and transfers them.
It also gets maximum SPI buffer size from /sys/module/spidev/parameters/bufsiz but caps it to 65535 because I believe DMA is not possible with larger buffers. If it fails to read /sys/module/spidev/parameters/bufsiz, it defaults to the standard 4K.

I have very little knowledge on Python C APIs so I do not really understand tricks in the original code like this one https://github.com/doceme/py-spidev/blob/master/spidev_module.c#L450-L453 . However from what I read, it looks like PySequence_Fast increments the counter even if it returns you the same object so you always have to do Py_DECREF(seq) which original code does not do in many cases (like error handling or just the normal return). Again, not sure about all of this.

from py-spidev.

dimas avatar dimas commented on August 23, 2024

Would be much better if spidev either

  • allowed changing the buffer size with a method call. The buffers for xfer are allocated in the xfer itself so just in theory the size can be changed between calls to xfer no problem
  • or just hid the fact that buffer of limited size is used - it can collect a chunk of data from the list passed to fill the buffer, flush it, collect the next chunk etc. Because this is what people end up doing anyway
        for i in range(0, len(data), chunk_size):
            chunk = data[i:i + chunk_size]
            self.spi.xfer(chunk)

it works, just not as efficient as it would be if done by xfer itself.

Still, allowing setting buffer size may not be the worst idea too because by changing /sys/module/spidev/parameters/bufsiz (on Raspberry at least) I can increase that buffer but there is no way to communicate this optimisation to spidev. (Well, if spidev hid the fact it is using limited buffer AND also got default size from this file that would be just brilliant, right? :) )

from py-spidev.

doceme avatar doceme commented on August 23, 2024

Pull request are welcome. 😉

from py-spidev.

vanquoc12b4 avatar vanquoc12b4 commented on August 23, 2024

I had follow this tutorial and the problem had fixed: https://youtu.be/1VJDhAClKMc.
Thanks.

from py-spidev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.