Skip to content

[BUG] spi_xfer Currently Fails #19

@TheCedarPrince

Description

@TheCedarPrince

Hi @Alexander-Barth and co,

Just wanted to report that while #18 did work to open a SPI interface, it now appears I am unable to write with SPI. Here's code in Python (using your tip about checking out the Python library):

import pigpio

pi = pigpio.pi()

SPI_CHANNEL = 0  # SPI channel (0 or 1)
SPI_SPEED = 1000000  # SPI speed in Hz (1 MHz)
SPI_FLAGS = 0  # SPI flags (0 for default)

handle = pi.spi_open(SPI_CHANNEL, SPI_SPEED, SPI_FLAGS)
data_to_send = [0x01, 0x02, 0x03, 0x04]  # Example data
count, data_received = pi.spi_xfer(handle, data_to_send)

And the output

>>> handle
0 # Or 1 -- this is correct to the Julia version

>>> count
4

>>> data_received
bytearray(b'\x00\x00\x00\x00')

Here's the same version in Julia:

julia> using PiGPIO

julia> p = Pi();
[ Info: Successfully connected!

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

julia> data_to_send = [0x01, 0x02, 0x03, 0x04]
4-element Vector{UInt8}:
 0x01
 0x02
 0x03
 0x04


julia> count, data = PiGPIO.spi_xfer(p, h, data_to_send)
ERROR: UndefVarError: `u2i` not defined
Stacktrace:
 [1] spi_xfer(self::Pi, handle::Int32, data::Vector{UInt8})
   @ PiGPIO ~/.julia/packages/PiGPIO/UOeJH/src/spiSerial.jl:199
 [2] top-level scope
   @ REPL[5]:1

I then tried to patch it with the following fix:

julia> function spi_xfer(self::Pi, handle, data)
           # I p1 handle
           # I p2 0
           # I p3 len
           ## extension ##
           # s len data bytes

           # Don't raise exception.  Must release lock.
           bytes = signed(PiGPIO._pigpio_command_ext(
           self.sl, PiGPIO._PI_CMD_SPIX, handle, 0, length(data), data, false))
           if bytes > 0
               data = PiGPIO.rxbuf(bytes)
           else
               data = ""
           end
           unlock(self.sl.l)
           return bytes, data
       end
spi_xfer (generic function with 1 method)

julia> (count, data) = spi_xfer(p, h, data_to_send)
(0, "")

But that doesn't return what the Python version did. Any thoughts about how to fix this @Alexander-Barth or @aviks? I can examine the Python version a bit more, but was curious what your thought is here first.

Cheers!

~ tcp 🌳

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions