BeagleY-AI: spi

Hi

I have just started to work with beagleY-AI. Using spi tools I cannot not change speed and it is always about 1.25 MHz. Trying to directly change registers I found that there is no detailed description of spi and other peripherals registers on technical reference of AM67!
Anyway I tried to read and change a register at spi base address 0x04b00000.
My python code read register but do not change it.
Any Idea?

The out of box spi port, is actually a gpio emulated port to match the RPI4’s pin location. There are two real hardware spi ports on other pins…

Regards,

Thank you for your help. Does it mean that I have to remove spi overlay to take control of spi port.
According to the board schematic the pins (19,21,23) are connected directely to MCU_SPI0.
What do you think is wrong with my code which do not change register value?
Here is the code:
import mmap
import os
import struct

Define the base address and register offset

BASE_ADDRESS = 0x4b00000 # Example base address, replace with your specific address
REGISTER_OFFSET = 0x0 # Example offset for the register
REGISTER_ADDRESS = BASE_ADDRESS + REGISTER_OFFSET

Open /dev/mem with read and write permissions

with open(“/dev/mem”, “r+b”) as f:
# Memory-map the file, size = size of the register (usually 4 bytes for 32-bit register)
mmapped_region = mmap.mmap(f.fileno(), mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE, offset=REGISTER_ADDRESS)

# Read the current value of the register
current_value = struct.unpack('I', mmapped_region[0:4])[0]  # Read 4 bytes
print(f"Current Value: {current_value:#x}")

# Change the value (for example, let's increment it by 1)
new_value = current_value + 1
print(f"New Value: {new_value:#x}")

# Write the new value back to the register
mmapped_region[0:4] = struct.pack('I', new_value)

#mmapped_region.flush()
# Close the mmap object
mmapped_region.close()

Best Regards

What about an overlay that is specifically for RPI boards and another overlay that is clearly bby-ai with the actual pins going direct to the hardware solution. I got tired of trying to get the spi and uart 4 up and dropped it. Did get the rpi 4 & 8 channel relay board overlays going however, the other stuff …

I am having problem with SPI0. I have

Import spidev
spi=SpiDev()
spi.open(0,0)

Gives error message on open. Any ihelp?

Sorry I just viewed your post! if you still have problem I may be able to help you.

Yes please

what type is your board? you have to check if spi0 exist by following command:

ls /dev/spi*
you should see spidev0.0 as result.

ls /dev/spi gives:
No such file or directory

The board is BeagleY-AI

Just tried this out…

  1. enable overlay
  2. install python3-spidev
  3. fix permissions
  4. test

Enable overlay

Edited /boot/firmware/extlinux/extlinux.conf to add overlay “k3-am67a-beagley-ai-spidev0.dtbo”:

menu title BeagleY-AI microSD (extlinux.conf) (swap enabled)

timeout 50

default microSD (default)

label microSD (production test)
    kernel /Image
    append console=ttyS2,115200n8 root=/dev/mmcblk1p3 ro rootfstype=ext4 rootwait net.ifnames=0 quiet
    fdtdir /
    fdt /ti/k3-am67a-beagley-ai.dtb
    fdtoverlays /overlays/k3-am67a-beagley-ai-hdmi-dss0-dpi1.dtbo /overlays/k3-am67a-beagley-ai-lincolntech-185lcd-panel.dtbo /overlays/k3-am67a-beagley-ai-csi0-imx219.dtbo /overlays/k3-am67a-beagley-ai-csi1-imx219.dtbo
    #initrd /initrd.img

label transfer microSD rootfs to NVMe (advanced)
    kernel /Image
    append console=ttyS2,115200n8 root=/dev/mmcblk1p3 ro rootfstype=ext4 rootwait net.ifnames=0 init=/usr/sbin/init-beagle-flasher-mv-rootfs-to-nvme
    fdtdir /
    fdt /ti/k3-am67a-beagley-ai.dtb
    initrd /initrd.img

label microSD (debug)
    kernel /Image
    append console=ttyS2,115200n8 earlycon=ns16550a,mmio32,0x02800000 root=/dev/mmcblk1p3 ro rootfstype=ext4 rootwait net.ifnames=0
    fdtdir /
    fdt /ti/k3-am67a-beagley-ai.dtb
    #initrd /initrd.img

label microSD (default)
    kernel /Image
    append console=ttyS2,115200n8 root=/dev/mmcblk1p3 ro rootfstype=ext4 resume=/dev/mmcblk1p2 rootwait net.ifnames=0 quiet
    fdtdir /
    fdt /ti/k3-am67a-beagley-ai.dtb
    fdtoverlays /overlays/k3-am67a-beagley-ai-spidev0.dtbo
    #initrd /initrd.img

Then I rebooted:

sudo shutdown -r now

Install python3-spidev

By default, I didn’t see this python library installed, so my import was failing.

sudo apt-get update
sudo apt-get install -y python3-spidev

Fix permissions

By default, the /dev/spidev* files were not accessible by the default user.

sudo chmod ugo+rwx /dev/spidev*

Test

beagle@beagle:~$ python3
Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import spidev
>>> x = spidev.SpiDev()
>>> x.open(0,0)
>>> x
<SpiDev object at 0xffff8a88ad90>
>>> dir(x)
['__class__', '__delattr__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'bits_per_word', 'close', 'cshigh', 'fileno', 'loop', 'lsbfirst', 'max_speed_hz', 'mode', 'no_cs', 'open', 'read0', 'readbytes', 'threewire', 'writebytes', 'writebytes2', 'xfer', 'xfer2', 'xfer3']

I don’t have any SPI devices connected right now, so I don’t know if I can generate any traffic.

Let me know how it goes!

2 Likes

Wonderful, it works, thanks a lot Jason.

1 Like