Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI 32 bits per word errors #1

Closed
sjkelly opened this issue Aug 3, 2020 · 5 comments
Closed

SPI 32 bits per word errors #1

sjkelly opened this issue Aug 3, 2020 · 5 comments

Comments

@sjkelly
Copy link

sjkelly commented Aug 3, 2020

Thank you for this package, it has been very helpful for interactive debugging in the REPL!

I am trying to set the bits_per_word parameter and receiving a few errors in the IOCtl.

julia> using BaremetalPi

julia> init_spi("/dev/spidev0.0", max_speed_hz = 100_000, mode=0x00, bits_per_word=32)
ERROR: Could not open `/dev/spidev0.0`. Make sure you have the required permissions.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] init_spi(::Array{String,1}; mode::UInt8, max_speed_hz::Int64, bits_per_word::Int64) at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/spi.jl:93
 [3] #init_spi#1 at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/spi.jl:35 [inlined]
 [4] top-level scope at REPL[2]:1
caused by [exception 1]
SystemError: Error in IOCTL call: Invalid argument
Stacktrace:
 [1] _ioctl(::Int64, ::UInt64, ::Base.RefValue{Int64}) at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/ioctl.jl:20
 [2] _ioctl at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/ioctl.jl:10 [inlined]
 [3] init_spi(::Array{String,1}; mode::UInt8, max_speed_hz::Int64, bits_per_word::Int64) at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/spi.jl:82
 [4] #init_spi#1 at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/spi.jl:35 [inlined]
 [5] top-level scope at REPL[2]:1

julia> init_spi("/dev/spidev0.0", max_speed_hz = 100_000, mode=0x00)

julia> a = [zero(UInt32)]
1-element Array{UInt32,1}:
 0x00000000

julia> spi_transfer!(1, [0x00000001], a, bits_per_word=32)
ERROR: SystemError: Error in IOCTL call: Invalid argument
Stacktrace:
 [1] _ioctl at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/ioctl.jl:20 [inlined]
 [2] spi_transfer!(::Int64, ::Array{UInt32,1}, ::Array{UInt32,1}; max_speed_hz::Int64, delay_usecs::Int64, bits_per_word::Int64, cs_change::Bool) at /home/ubuntu/.julia/packages/BaremetalPi/vlIDG/src/spi.jl:298
 [3] top-level scope at REPL[11]:1

It is possible I have something wrong or a missing driver. This is on the Ras pi 4 with 64 bit ubuntu 20.04. It works fine with 8 bit words.

@ronisbr
Copy link
Owner

ronisbr commented Aug 3, 2020

Hi @sjkelly

Thank you for this package, it has been very helpful for interactive debugging in the REPL!

Nice! I am glad it is being useful.

I am trying to set the bits_per_word parameter and receiving a few errors in the IOCtl.

I think you found a bug. Can you please test if this work passing a Int8 or UInt8 to the keyword bits_per_word:

julia> init_spi("/dev/spidev0.0", max_speed_hz = 100_000, mode=0x00, bits_per_word=Int8(32))

julia> init_spi("/dev/spidev0.0", max_speed_hz = 100_000, mode=0x00, bits_per_word=UInt8(32))

@sjkelly
Copy link
Author

sjkelly commented Aug 3, 2020

I tried both, and still get the same error. FWIW the permission error is just default. I tried changing this line to UInt8 with no avail either:

bits_per_word = [bits_per_word for _ = 1:num_init_devices]

It is possible the Ras Pi does not support 32 bit per word send, as the error occurs setting the SPI_IOC_WR_BITS_PER_WORD. If not, the error should probably be at the ioctl call like here:

https://github.com/cpb-/spi-tools/blob/4a36a84f7df291ddaebd397aecf0c8515256a8e0/src/spi-config.c#L231-L236

I am setting up a python test to verify real quick if it is hardware or otherwise.

@ronisbr
Copy link
Owner

ronisbr commented Aug 3, 2020

It is possible the Ras Pi does not support 32 bit per word send, as the error occurs setting the SPI_IOC_WR_BITS_PER_WORD. If not, the error should probably be at the ioctl call like here:

https://github.com/cpb-/spi-tools/blob/4a36a84f7df291ddaebd397aecf0c8515256a8e0/src/spi-config.c#L231-L236

Yes, the IOCTL is failing due to an invalid argument. Initially, I thought it could be the format of the variable bits_per_word, which would be solved by my proposal. Now, it can be that the hardware does not support 32 bits per word. If you can check with python and tell me, I will appreciate!

@sjkelly
Copy link
Author

sjkelly commented Aug 3, 2020

Yes, It seems to be the hardware:

>>> import spidev
>>> spi = spidev.SpiDev()
>>> spi.open(0,0)
>>> spi.max_speed_hz
125000000
>>> spi.max_speed_hz = 100000
>>> spi.mode
0
>>> spi.bits_per_word
8
>>> spi.bits_per_word = 0x20
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> spi.bits_per_word
8

I also validated with spi-tools and it does not support 32 bit words.

This is also confirmed in the docs: https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md

Thanks for the help!

@ronisbr
Copy link
Owner

ronisbr commented Aug 3, 2020

Nice! Thanks for the info :) This is important to me because I only have the Pi Zero to test. Knowing that it is working in another device is really nice!

@ronisbr ronisbr closed this as completed Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants