1

I wrote a script to display data on a led matrix, controlled by a few GPIO for line selection and SPI to feed data for this line. This was working perfectly 2 days ago, I let the script display random data for a few hours then stopped the rpi via ssh.

Today it is not working anymore and I'm surprised by the ouput of gpioinfo:

gpiochip0 - 54 lines:

...
    line   7:   "SPI_CE1_N"         output active-low consumer="spi0 CS1"
    line   8:   "SPI_CE0_N"         output active-low consumer="spi0 CS0"
    line   9:   "SPI_MISO"          input
    line  10:   "SPI_MOSI"          input
    line  11:   "SPI_SCLK"          input
...

Shouldn't MOSI and SCLK be output, consumed by the same than CE1/CE0 ports ?

SPI modules are loaded (with spidev Used by column switching to 2 when I start my script)

# lsmod | grep spi
spidev                 20480  0
spi_bcm2835            20480  0

The SPI devices are present and udev correctly set up the groups

# ls -l /dev/spidev0.*
crw-rw---- 1 root spi 153, 0 Apr 17 08:43 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 Apr 17 08:43 /dev/spidev0.1

# cat /etc/udev/rules.d/localr.rules 
SUBSYSTEM=="gpio", KERNEL=="gpiochip*", GROUP="gpio", MODE="0660"
KERNEL=="spidev*", GROUP="spi", MODE="0660"

# grep spi /boot/config.txt 
dtparam=spi=on

Display part of my code, where img is a basic 16x16 bit array. GPIO code is for line selection and is working well

spi_bus=0
spi_device=0

spi = spidev.SpiDev()
spi.open(spi_bus, spi_device)

...

    with libgpiod.Chip(gpio_chip_path) as chip:
        lines_settings = {line: libgpiod.LineSettings(direction=Direction.OUTPUT, output_value=Value(0))
                          for line in gpio_lines}
        linebulk = chip.request_lines(lines_settings, consumer="ledmatrix")
        while True:
            for c in range(0, 16):
                values = {
                        21: Value(((c<<0)&0b1000) != 0),
                        20: Value(((c<<1)&0b1000) != 0),
                        16: Value(((c<<2)&0b1000) != 0),
                        12: Value(((c<<3)&0b1000) != 0),
                        5: Value(1)
                        }
                linebulk.set_values(values)
                spi.xfer(img[c][:])
                #img[c][randint(0, 15)] = randint(0, 1)
                values = { 5: Value(0) }
                linebulk.set_values(values)
                sleep(0.0001)

How could I debug this?

2
  • What does gpioinfo report after boot (before you run your script).
    – joan
    Commented Apr 17, 2023 at 9:54
  • Same thing. I tried to unplug all (pins, usb …), same result …
    – low
    Commented Apr 17, 2023 at 10:55

1 Answer 1

1

This may be a change to the way the driver works.

The chip select GPIO are driven active low by default. The other SPI GPIO are left as inputs.

During a SPI transaction MOSI and SCLK are driven as outputs.

I checked by looking at the line levels when writing something to /dev/spidev0.0.

echo abcdefghijklmnopqrstuv >/dev/spidev0.0

During the transaction CE0 was driven low and MOSI and SCLK were seen to toggle.

3
  • thank for testing it ! Did gpioinfo reported something ? i don't have test hardware except a basic multimeter, but gpioinfo doesnt give me result neither with echo, neither with my code running
    – low
    Commented Apr 17, 2023 at 14:45
  • You won't see it on gpioinfo unless you are very lucky. I used piscope. abyz.me.uk/rpi/pigpio/piscope.html You can run it in a window as the same time as your script. If you want to see individual bits change the SPI speed to 50k bits per second or so.
    – joan
    Commented Apr 17, 2023 at 14:59
  • I still did had time to test with piscope but I will. Thank for the hint !
    – low
    Commented Apr 21, 2023 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.