0

Coming here from electronics SE as it was suggested that I post here on this issue. The electronics SE post is here.

I'm setting up a comms link between an STM32F407 MCU and the Pi3B+. The Pi is running on the standard Raspberry Pi OS and with the SPI clock set to 10MHz. 8-bit word lengths on both ends. Simple hookup wires are used (for now) to connect the two. The code shown below works but when the data is printed out, it looks like the RX buffer overwrites the TX one that was sent. How do I stop that? I could clone the TX buffer before sending it but then I'll have to keep switching between buffers for further communications (before spi.close()). The console output is shown below the code.

import spidev
from time import sleep
from tabulate import tabulate  # pip3 install tabulate

ARRAY_SIZE = 56

spi = spidev.SpiDev()
spi.open(0, 0)
spi.mode = 0
spi.max_speed_hz = 10000000
spi.bits_per_word = 8

tx1 = [0xCA, 0xFE, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0xff,
       0xff, 0x00, 0x00, 0xff, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00]

# Do an SPI transaction
# rx1 is the receive buffer
rx1 = spi.xfer2(tx1)

# Print the RX and TX arrays
headers = ['array #', 'TX', 'RX']
idx = range(0, ARRAY_SIZE, 1)
table = zip(idx, tx1, rx1)

print(tabulate(table, headers=headers))

# close the SPI port
spi.close()

The console output looks like this,

  array #    TX    RX
---------  ----  ----
        0     0     0
        1     0     0
        2     0     0
        3     0     0
        4     0     0
        5     0     0
        6     0     0
        7     0     0
        8     0     0
        9     0     0
       10     0     0
       11     0     0
       12     0     0
       13     0     0
       14     0     0
       15     0     0
       16     0     0
       17     0     0
       18     0     0
       19     0     0
       20     0     0
       21     0     0
       22     0     0
       23     0     0
       24     0     0
       25     0     0
       26     0     0
       27     0     0
       28     0     0
       29   254   254
       30   255   255
       31   134   134
       32   254   254
       33   204   204
       34   254   254
       35   253   253
       36     0     0
       37     0     0
       38     0     0
       39     0     0
       40     0     0
       41     0     0
       42     0     0
       43     0     0
       44     0     0
       45     0     0
       46     0     0
       47    54    54
       48    84    84
       49    52    52
       50   108   108
       51     0     0
       52     0     0
       53     0     0
       54     0     0
       55     0     0
0
1

That seems to be odd behaviour.

Can't help with the spidev module.

Perhaps use a different library.

My pigpio, lgpio, and rgpio Python modules will work okay.

pigpio (own SPI driver)

import pigpio

tx1 = [0xCA, 0xFE, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0xff,
       0xff, 0x00, 0x00, 0xff, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00]

pi = pigpio.pi()
if not pi.connected:
   exit()

h = pi.spi_open(0, 10000000, 0)

status, rx1 = pi.spi_xfer(h, tx1)

print(status, rx1, tx1)

pi.spi_close(h)

lgpio (uses Linux SPI driver)

import lgpio as sbc

tx1 = [0xCA, 0xFE, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0xff,
       0xff, 0x00, 0x00, 0xff, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00,
       0x00]

h = sbc.spi_open(0, 0, 10000000)

status, rx1 = sbc.spi_xfer(h, tx1)

print(status, rx1, tx1)

sbc.spi_close(h)

Both produce the following

(56L,
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'),
[202, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0,
 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
10
  • Thanks! I installed pigpiod from (your?) website and followed the instructions. Everything went smoothly till I tried to start the daemon. Then it gave me the error: 2021-03-23 23:09:59 initInitialise: Can't lock /var/run/pigpio.pid Can't initialise pigpio library Mar 23 at 17:41
  • When I tried to check the status, it shows me this: pigpiod.service - Daemon required to control GPIO pins via pigpio Loaded: loaded (/lib/systemd/system/pigpiod.service; disabled; vendor preset: enabled) Active: inactive (dead) Mar 23 at 17:46
  • pigpio is installed by default. I suggest you do sudo killall pigpiod to get a clean state and then run sudo pigpiod to start the daemon.
    – joan
    Mar 23 at 17:55
  • Strange but I still see the same inactive (dead) message. I rebooted the Pi too and it is still the same. Do I need to set any pre-requisites via raspi-config? Mar 24 at 3:02
  • 1
    Not sure what is going on with pigpio. It might be a conflict between the way it is pre-installed and the way it is installed manually. lgpio may be the better way for your needs.
    – joan
    Mar 24 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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