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

[BUG] Cannot Open SPI Interface #18

Closed
TheCedarPrince opened this issue Dec 19, 2024 · 3 comments
Closed

[BUG] Cannot Open SPI Interface #18

TheCedarPrince opened this issue Dec 19, 2024 · 3 comments

Comments

@TheCedarPrince
Copy link

Hi @Alexander-Barth and co,

Carrying on from a discussion from Discourse, the merge b48be39 did help me get a bit further with opening an SPI interface, but I am still receiving an error:

julia> using PiGPIO

julia> p = Pi()
[ Info: Successfully connected!
Pi("localhost", 8888, true, PiGPIO.SockLock(Sockets.TCPSocket(RawFD(17) open, 0 bytes waiting), ReentrantLock(nothing, 0x00000000, 0x00, Base.GenericCondition{Base.Threads.SpinLock}(Base.IntrusiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), (2, 547614619512, 547614189232))), PiGPIO.CallbackThread(PiGPIO.SockLock(Sockets.TCPSocket(RawFD(17) open, 0 bytes waiting), ReentrantLock(nothing, 0x00000000, 0x00, Base.GenericCondition{Base.Threads.SpinLock}(Base.IntrusiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), (2, 547614619512, 547614189232))), PiGPIO.SockLock(Sockets.TCPSocket(RawFD(18) paused, 0 bytes waiting), ReentrantLock(nothing, 0x00000000, 0x00, Base.GenericCondition{Base.Threads.SpinLock}(Base.IntrusiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), (0, 547589698944, 0))), true, true, 0, 0x00000000, Any[]))

julia> PiGPIO.spi_^C

julia> h = PiGPIO.spi_open(p, 1, 100000, Int32(0))
ERROR: MethodError: no method matching iterate(::IOBuffer)

Closest candidates are:
  iterate(::Pipe, ::Int64)
   @ Base stream.jl:1263
  iterate(::Pipe)
   @ Base stream.jl:1262
  iterate(::Base.MethodSpecializations)
   @ Base reflection.jl:1148
  ...

Stacktrace:
 [1] _pigpio_command_ext(sl::PiGPIO.SockLock, cmd::Int64, p1::Int64, p2::Int64, p3::Int64, extents::IOBuffer, rl::Bool)
   @ PiGPIO ~/.julia/packages/PiGPIO/1FebP/src/pi.jl:128
 [2] _pigpio_command_ext(sl::PiGPIO.SockLock, cmd::Int64, p1::Int64, p2::Int64, p3::Int64, extents::IOBuffer)
   @ PiGPIO ~/.julia/packages/PiGPIO/1FebP/src/pi.jl:126
 [3] spi_open(self::Pi, spi_channel::Int64, baud::Int64, spi_flags::Int32)
   @ PiGPIO ~/.julia/packages/PiGPIO/1FebP/src/spiSerial.jl:93
 [4] top-level scope
   @ REPL[5]:1

After debugging, I can observe that the error occurs within this PiGPIO.jl function:

PiGPIO.jl/src/pi.jl

Lines 115 to 138 in e34746d

"""
Runs an extended pigpio socket command.
* `sl`: command socket and lock.
* `cmd`: the command to be executed.
* `p1`: command parameter 1 (if applicable).
* `p2`: command parameter 2 (if applicable).
* `p3`: total size in bytes of following extents
* `extents`: additional data blocks
"""
function _pigpio_command_ext(sl, cmd, p1, p2, p3, extents, rl=true)
ext = IOBuffer()
Base.write(ext, Array(reinterpret(UInt8, [cmd, p1, p2, p3])))
for x in extents
write(ext, string(x))
end
lock(sl.l)
write(sl.s, ext)
msg = reinterpret(Cuint, sl.s)[4]
if rl
unlock(sl.l)
end
return res
end

In particular, I know it is this loop:

for x in extents 
    write(ext, string(x)) 
end 

As it is trying to iterate over a IOBuffer -- but I am unsure what it is trying to actually do or access. I tested old Julia versions starting from 1.0 and I have concluded this is just buggy code. There was not a point in time post-1.0 Julia that I could find which allows iteration over an IOBuffer.

Any thoughts here? Thanks!

~ tcp 🌳

@Alexander-Barth
Copy link
Contributor

I have also seen this problem while looking at the issue:
#15

There are some changes in the i2c branch, in particular:
c80dd92

I just merged the i2c branch into the master, since the problem also affects spi as your example shows,.

Can you re-try with the master branch?

I don't know the details of the history of this code, but it was converted from the python code. Probably only a subset of the pigpio.py functionality was ever tested. Rather than looking at old julia version, it might be easier to find the issue by looking at the python code.

@aviks
Copy link
Member

aviks commented Dec 20, 2024

I tested old Julia versions starting from 1.0

Not to say that the code wasn't always buggy, but this code is prehistoric. From the first commit, I see a comment saying Julia Version 0.5.0-dev+3050 [c96f322] 😃 😱

@TheCedarPrince
Copy link
Author

I have also seen this problem while looking at the issue: #15

There are some changes in the i2c branch, in particular: c80dd92

I just merged the i2c branch into the master, since the problem also affects spi as your example shows,.

Can you re-try with the master branch?

I have good news! This now appears to work!

julia> using PiGPIO

julia> p = Pi()
[ Info: Successfully connected!
Pi("localhost", 8888, true, PiGPIO.SockLock(Sockets.TCPSocket(RawFD(17) open, 0 bytes waiting), ReentrantLock(nothing, 0x00000000, 0x00, Base.GenericCondition{Base.Threads.SpinLock}(Base.IntrusiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), (0, 0, 0))), PiGPIO.CallbackThread(PiGPIO.SockLock(Sockets.TCPSocket(RawFD(17) open, 0 bytes waiting), ReentrantLock(nothing, 0x00000000, 0x00, Base.GenericCondition{Base.Threads.SpinLock}(Base.IntrusiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), (0, 0, 0))), PiGPIO.SockLock(Sockets.TCPSocket(RawFD(18) paused, 0 bytes waiting), ReentrantLock(nothing, 0x00000000, 0x00, Base.GenericCondition{Base.Threads.SpinLock}(Base.IntrusiveLinkedList{Task}(nothing, nothing), Base.Threads.SpinLock(0)), (0, 0, 0))), true, true, 0, 0x00000000, Any[]))

julia> h = PiGPIO.spi_open(p, 1, 100000, 0)
1

And oh my gosh @aviks -- I had no idea it was that old 😱 I'll take your advice as well regarding the original Python code @Alexander-Barth!

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

3 participants