How Access spi on torizon using python

Hi, I’m the Toradex AI Assistant.

I might make mistakes. Our support team will step in if needed. If I answered your question, please tick the “Solution” checkbox. For follow-up questions to me, please use @ToradexAI in your post.

Did you like my answer? :+1: :-1:


Hello! Thank you for providing all the details and code. Let’s walk through the setup and identify what might be causing your fixed SPI values.


1. Device Tree Overlay Validation

Your device tree overlay looks close to correct. Important things to check:

  • The SPI bus ecspi3 is correctly enabled.
  • The node under ecspi3 (spidev@0) uses compatible = "rohm,dh2228fv" to get spidev devices visible.
  • Ensure there are no other overlays or device tree fragments conflicting with your pinmux or enabling the same SPI bus differently.

Carefully verify that your overlay is properly built and deployed to the board. Here are steps you can follow:

  • Check for the expected SPI device in Linux:
    ls /dev/spidev*
    
    You should see /dev/spidev2.0.
  • Confirm the device tree overlay is applied using:
    cat /proc/device-tree/chosen/overlays/
    
    Or validate it in /sys/firmware/devicetree/base.

(Device Tree Overlay Guide)


2. GPIO and SPI Container Access

Your Docker commands and permissions look mostly correct. A couple of notes:

  • Ensure the container has access to /dev/spidev2.0 and /dev/gpiochip6.

  • When running the container, mount the host’s /dev directory, or explicitly pass both devices:

    docker run --rm -it \
      --device /dev/gpiochip6 \
      --device /dev/spidev2.0 \
      -v /dev:/dev \
      --device-cgroup-rule='c 254:* rmw' \
      <your-image>
    

    (How to Use SPI on Torizon)

  • Ensure your container’s user is a member of the right groups:


3. libgpiod Setup in Container

Make sure your Python environment in the container has the libgpiod library available.

For Debian-based images, the following packages are often required:

{
    "deps": [
       "libgpiod2"
    ],
    "devDeps": [
       "libgpiod-dev"
    ]
}

(libgpiod setup)


4. Python SPI Usage and Manual Chip Select

A few checks for your code:

  • Since you’re controlling CS manually, ensure that you do not let the SPI driver toggle CS itself. This is usually the default on spidev (it expects hardware CS). Because you control CS with GPIO, you must verify that the pinmux is NOT assigning SODIMM 63 to a native SPI CS function, and it is set as a GPIO in the overlay and not driven by the SPI hardware.

(SPI Pin Multiplexing)

Timing

  • The timing between toggling CS and performing the SPI transfer is important. Sleeping 10ms before each transfer is usually more than required, but it’s safe for debugging.

Data Format

  • spi.bits_per_word = 16 means each entry in spiData_Range is a single transfer of 16 bits (2 bytes). Confirm your slave device expects this format.
  • If your device expects 8-bit words, use spi.bits_per_word = 8.

Debug SPI Communication

  • You’re always receiving [69, 0, 0, 0]. This often indicates:
    • The wiring to the slave is not correct.
    • The slave is not selected (CS gating issue).
    • SPI mode or speed is mismatched.
    • The overlay is not properly setting up the pins for SPI and CS GPIO.
    • The device attached to SPI does not respond.

5. General Recommendations

  • Double-check the hardware connection (especially MISO, MOSI, SCK, CS).
  • Use an oscilloscope or logic analyzer to verify SPI lines toggle as expected.
  • Test the SPI bus with a known good slave, or try a loopback test (short MISO and MOSI).
  • If possible, try to use the default hardware-controlled CS first, see if it works, and then switch to manual CS if required.

Summary Checklist

  1. Confirm SPI bus and device tree overlay is loaded (SPI on Torizon).
  2. Validate GPIO pin SODIMM 63 is configured as a GPIO and not a hardware CS in the device tree (Pin Multiplexing).
  3. Ensure proper permissions and device nodes are passed into your container.
  4. Make sure your software setup matches the electrical and logical requirements of the SPI device.
  5. Check physical connections and logic levels for SPI signals.

If after all these steps you still get only constant values, there is likely a hardware wiring or incorrect CS handling issue.

Please follow the above, and let me know where you get stuck or if results change!

Best regards,
Toradex AI Assistant.