Kioi
Posts: 9
Joined: Mon Nov 27, 2023 5:19 pm

SPI speed locked on rpi5

Tue Nov 28, 2023 2:30 am

Hi,
I've been trying to send some data through spi at some "higher" speed (arround 160-200MHz) on the raspi 5 but after testing for a bit I've noticed that the speed seems to be locked at arround 100MHz (my measuring tools aren't too precise).
Lowering the speed works fine and the data is sent through correctly.
Also some things that I noticed, on the pi 4 this works correctly (on spidev0.0 only) and also according to the datasheed for the rp1 it seems that the clock should go up to 200MHz on the raspi5

My code looks something like this

Code: Select all

#include <cstdint>
#include <linux/spi/spidev.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int spifd;
static uint8_t mode =  SPI_MODE_3;
static uint8_t bits = 8;
static uint32_t speed = 200000000;

// the buffer looks like this because the logic analyzer that i use only goes up to 24MHz
uint8_t buf[] = {0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF};

int main(){

    spifd = open("/dev/spidev0.0",O_RDWR);
    
    ioctl(spifd, SPI_IOC_WR_MODE, &mode);
    
    ioctl(spifd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    
    ioctl(spifd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    
    struct spi_ioc_transfer tr = {
        .tx_buf = (uint64_t)&buf,
        .len = (uint32_t)sizeof(buf),
        .speed_hz = speed,
        .delay_usecs = 0,
        .bits_per_word = bits,
    };

    ioctl(spifd, SPI_IOC_MESSAGE(1), &tr);
}

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 7:03 am

From memory, the SPI upper limit on the clock is set at 125MHz. However, regardless of how fast the SPI clock can be driven, I think you'll struggle to get data through a header pin at more than 50MHz.

Kioi
Posts: 9
Joined: Mon Nov 27, 2023 5:19 pm

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 1:05 pm

is there any way to set the clk_sys higher than 200MHz maybe 250MHz or 300MHz?

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 1:32 pm

Can you get reliable transfers at 100MHz?

Kioi
Posts: 9
Joined: Mon Nov 27, 2023 5:19 pm

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 1:42 pm

the st7789 that i'm trying to drive isn't complaining about it so i guess so

aBUGSworstnightmare
Posts: 9219
Joined: Tue Jun 30, 2015 1:35 pm

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 1:48 pm

Kioi wrote:
Tue Nov 28, 2023 1:42 pm
the st7789 that i'm trying to drive isn't complaining about it so i guess so
Are you doing this baremetal or do you use a kernel driver driver?

I'm not able to run at such high SPI clock with the ST7789xx based display I have on hands.

Kioi
Posts: 9
Joined: Mon Nov 27, 2023 5:19 pm

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 2:01 pm

aBUGSworstnightmare wrote:
Tue Nov 28, 2023 1:48 pm
Kioi wrote:
Tue Nov 28, 2023 1:42 pm
the st7789 that i'm trying to drive isn't complaining about it so i guess so
Are you doing this baremetal or do you use a kernel driver driver?

I'm not able to run at such high SPI clock with the ST7789xx based display I have on hands.
I must say I'm still kind of a novice at this so i dont quite understand what you mean with baremetal or kernel driver.
what i'm using at the moment is the gpiod and spidev libs to send the data through to the st7789.
Also i'm using the raspi 5 now which sends the data though spi a bit differently as it seems that the raspi 4 waits for a clock cycle between each word sent through and the raspi 5 doesent have that little delay.
I'm getting about 28ms per frame on my 240*240 display which would be about 35fps (this would probably be enough for most use cases but for what i'm doing the least latency the better, this is also why i'd like to run the spi at higher speeds)

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 2:10 pm

I think your signal integrity is going to be poor at 100MHz. But to answer your question, to push CLK_SYS to above 200MHz would be to overclock RP1 - we're not going to do that. There is a table of assigned clock rates in the Pi 5 DTB, one of which is CLK_SYS, but when it comes to changing it you are on your own. If it doesn't work, you won't get any assistance.

aBUGSworstnightmare
Posts: 9219
Joined: Tue Jun 30, 2015 1:35 pm

Re: SPI speed locked on rpi5

Tue Nov 28, 2023 7:10 pm

Kioi wrote:
Tue Nov 28, 2023 2:01 pm
aBUGSworstnightmare wrote:
Tue Nov 28, 2023 1:48 pm
Kioi wrote:
Tue Nov 28, 2023 1:42 pm
the st7789 that i'm trying to drive isn't complaining about it so i guess so
Are you doing this baremetal or do you use a kernel driver driver?

I'm not able to run at such high SPI clock with the ST7789xx based display I have on hands.
I must say I'm still kind of a novice at this so i dont quite understand what you mean with baremetal or kernel driver.
what i'm using at the moment is the gpiod and spidev libs to send the data through to the st7789.
Also i'm using the raspi 5 now which sends the data though spi a bit differently as it seems that the raspi 4 waits for a clock cycle between each word sent through and the raspi 5 doesent have that little delay.
I'm getting about 28ms per frame on my 240*240 display which would be about 35fps (this would probably be enough for most use cases but for what i'm doing the least latency the better, this is also why i'd like to run the spi at higher speeds)
viewtopic.php?t=355870&start=25#p2155250

In case you've not seen that. That's 30FPS from the camera, the display 'claims' to run st 60Hz but I assume it's not quite there

Kioi
Posts: 9
Joined: Mon Nov 27, 2023 5:19 pm

Re: SPI speed locked on rpi5

Wed Nov 29, 2023 1:42 am

aBUGSworstnightmare wrote:
Tue Nov 28, 2023 7:10 pm
Kioi wrote:
Tue Nov 28, 2023 2:01 pm
aBUGSworstnightmare wrote:
Tue Nov 28, 2023 1:48 pm


Are you doing this baremetal or do you use a kernel driver driver?

I'm not able to run at such high SPI clock with the ST7789xx based display I have on hands.
I must say I'm still kind of a novice at this so i dont quite understand what you mean with baremetal or kernel driver.
what i'm using at the moment is the gpiod and spidev libs to send the data through to the st7789.
Also i'm using the raspi 5 now which sends the data though spi a bit differently as it seems that the raspi 4 waits for a clock cycle between each word sent through and the raspi 5 doesent have that little delay.
I'm getting about 28ms per frame on my 240*240 display which would be about 35fps (this would probably be enough for most use cases but for what i'm doing the least latency the better, this is also why i'd like to run the spi at higher speeds)
viewtopic.php?t=355870&start=25#p2155250

In case you've not seen that. That's 30FPS from the camera, the display 'claims' to run st 60Hz but I assume it's not quite there
I'm not sure if this is getting kind of out of topic but no i dont have any camera connected to it (yet) but taking as a reference some "slow-mo" recordings from my cellphone at 960fps it took about 16 frames for the screen to update, that would be arround (960/16) 60 fps. Also i got this speed on an esp32 changing between 2 frames updating the whole 240*240 pixels (given the color mode is set to 12bit per pixel or 4 bit per color).
But i haven't yet recorded it while running on the raspi5, i'm basing those 35fps more of the time it takes for the spi to send the data through to update the display.
I could clean up a bit my code and share it if anyone would be interested in looking through it, I took a bit as reference the st77XX lib from adafruit and also read through some of the st7789 datasheet to add some "optimizations" for what i want to use it for

edit*
also after a not so thorough visual inspection of a recording(at 960fps) of the st7789 display running from the raspi 5 it took about 30 frames for the display to update, this would be arround 32 fps which would fall close to the 35fps that i calculated earlier

(also on a side note could someone hint me towards how to read the camera buffer as in the rgb values of each pixel from the camera with libcamera... or any other lib if needed 🙏)
Last edited by Kioi on Wed Nov 29, 2023 2:08 am, edited 1 time in total.

prabus
Posts: 15
Joined: Fri Dec 01, 2023 9:58 am

Re: SPI speed locked on rpi5

Fri Dec 01, 2023 10:08 am

Hi, Related to this post. Are the SPI pins for rpi5 different from rpi4?
I was using spidev0.0 for rpi4 before and it was working for gpio8, gpio9, gpio10,gpio11.
now with new rpi5 I don't get the spi working for the same pins.
Please let me know if there are any changes in the GPIO's between rpi4 and rpi5

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Fri Dec 01, 2023 3:42 pm

Please let me know if there are any changes in the GPIO's between rpi4 and rpi5
SPI0 uses the same GPIOs on Pi 5 - 7-11. Some of the higher numbered interfaces do use different pins compared to the Pi 4. You can use "pinctrl funcs 0-27" to show all functions (including SPI) available on the 40-pin header.
now with new rpi5 I don't get the spi working for the same pins.
What have you done to enable SPI0, and how is it failing for you? What output do you get from "pinctrl get 8-11"?

danjperron
Posts: 4667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SPI speed locked on rpi5

Fri Dec 01, 2023 5:01 pm

SPI0 uses the same GPIOs on Pi 5 - 7-11. Some of the higher numbered interfaces do use different pins compared to the Pi 4. You can use "pinctrl funcs 0-27" to show all functions (including SPI) available on the 40-pin header.
Yeahhh... hard do read

Better reformat the print
pi5.jpg
pi5.jpg (132.07 KiB) Viewed 2680 times
Side note from the pinctrl results, what is SYS ,PROC and DSI

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Fri Dec 01, 2023 5:09 pm

Or you could read it on pages 15 and 16 of the RP1 datasheet: Raspberry Pi RP1 Peripherals https://datasheets.raspberrypi.com/rp1/ ... herals.pdf

DSI is the MIPI display interface (the inverse of CSI for cameras), SYS_RIO is GPIO from the BCM2712 host, and PROC_RIO is the same but accessed from RP1's M3s, i.e. there are two independent GPIO blocks and you switch between them using the alt function selectors.

danjperron
Posts: 4667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: SPI speed locked on rpi5

Fri Dec 01, 2023 5:58 pm

Or you could read it on pages 15 and 16 of the RP1 datasheet: Raspberry Pi RP1 Peripherals https://datasheets.raspberrypi.com/rp1/ ... herals.pdf
Quite a dull black and white table. I prefer mine.

Ok RP1 is for the PI5. I did wonder why I can't find anything about Pi5 peripherals. Now it is solved.
Thanks.

prabus
Posts: 15
Joined: Fri Dec 01, 2023 9:58 am

Re: SPI speed locked on rpi5

Mon Dec 04, 2023 12:52 pm

Thank you for the replies. Raspi config and Enabled SPI in interfaces. We are using Ubuntu 23.10
we see as below when we ask for SPI device.

ubuntu@ubuntu:~$ sudo raspi-config nonint get_spi
0
ubuntu@ubuntu:~$ ls /dev/spi*
/dev/spidev10.0

we see only spidev10. but for RPI4 we see spidev0.0 and spidev1.0 both

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Tue Dec 05, 2023 9:51 am

"sudo raspi-config nonint get_spi" just returns the status - you need "... do_spi 0" to enable it:

Code: Select all

pi@raspberrypi:~$ sudo raspi-config nonint get_spi
1
pi@raspberrypi:~$ grep spi /boot/firmware/config.txt
#dtparam=spi=on
pi@raspberrypi:~$ ls /dev/spi*
/dev/spidev10.0
pi@raspberrypi:~$ sudo raspi-config nonint do_spi 0
pi@raspberrypi:~$ sudo raspi-config nonint get_spi
0
pi@raspberrypi:~$ grep spi /boot/firmware/config.txt
dtparam=spi=on
pi@raspberrypi:~$ ls /dev/spi*
/dev/spidev0.0  /dev/spidev0.1  /dev/spidev10.0
Run this sequence on Ubuntu and show me the results. If the config.txt changes correctly but /dev/spidev0.0 doesn't appear, show the output of "dtoverlay -l".

prabus
Posts: 15
Joined: Fri Dec 01, 2023 9:58 am

Re: SPI speed locked on rpi5

Tue Dec 05, 2023 1:37 pm

below is the log we get. but spidev0.0 and spidev0.1 is not visible.

ubuntu@ubuntu:~$ sudo raspi-config nonint get_spi
1
ubuntu@ubuntu:~$ grep spi /boot/firmware/config.txt
#dtparam=spi=on
ubuntu@ubuntu:~$ ls /dev/spi*
/dev/spidev10.0
ubuntu@ubuntu:~$ sudo raspi-config nonint do_spi 0
DTOVERLAY[warn]: no matching platform found
ubuntu@ubuntu:~$ sudo raspi-config nonint get_spi
0
ubuntu@ubuntu:~$ grep spi /boot/firmware/config.txt
dtparam=spi=on
ubuntu@ubuntu:~$ ls /dev/spi*
/dev/spidev10.0
ubuntu@ubuntu:~$ dtoverlay -l
DTOVERLAY[warn]: no matching platform found
Overlays (in load order):
0: dtparam spi=off
1: dtparam spi=on

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Tue Dec 05, 2023 1:43 pm

Thanks, that's all useful information.

What do these other commands show?

Code: Select all

$ lsmod | grep spi
$ dmesg -l err,warn

prabus
Posts: 15
Joined: Fri Dec 01, 2023 9:58 am

Re: SPI speed locked on rpi5

Tue Dec 05, 2023 4:10 pm

ubuntu@ubuntu:~$ lsmod | grep spi

spidev 24576 0

spi_dw_mmio 12288 0

spi_dw 36864 1 spi_dw_mmio

spi_bcm2835 32768 0


ubuntu@ubuntu:~$ sudo dmesg -l err,warn
[ 2.504805] gpio gpiochip0: Static allocation of GPIO base is deprecated, use dynamic allocation.
[ 2.534267] gpio gpiochip1: Static allocation of GPIO base is deprecated, use dynamic allocation.
[ 2.540427] gpio gpiochip2: Static allocation of GPIO base is deprecated, use dynamic allocation.
[ 2.540587] gpio gpiochip3: Static allocation of GPIO base is deprecated, use dynamic allocation.
[ 2.908978] sdhci-brcmstb 1001100000.mmc: invalid resource (null)
[ 2.915932] sdhci-brcmstb 1001100000.mmc: invalid resource (null)
[ 5.724318] systemd[1]: memfd_create() called without MFD_EXEC or MFD_NOEXEC_SEAL set
[ 6.686594] device-mapper: core: CONFIG_IMA_DISABLE_HTABLE is disabled. Duplicate IMA measurements will not be recorded in the IMA log.
[ 8.266209] rpivid_hevc: module is from the staging directory, the quality is unknown, you have been warned.
[ 8.608239] hci_uart_bcm serial0-0: supply vbat not found, using dummy regulator
[ 8.608352] hci_uart_bcm serial0-0: supply vddio not found, using dummy regulator
[ 471.890716] OF: overlay: WARNING: memory leak will occur if overlay removed, property: /axi/pcie@120000/rp1/spi@50000/status
[ 563.762546] OF: overlay: WARNING: memory leak will occur if overlay removed, property: /axi/pcie@120000/rp1/spi@50000/status
[ 1537.148179] OF: overlay: WARNING: memory leak will occur if overlay removed, property: /axi/pcie@120000/rp1/i2c@74000/status

has context menu

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Tue Dec 05, 2023 4:24 pm

I'm concerned about the fact that you have two overlays/parameters affecting SPI:

Code: Select all

[ 471.890716] OF: overlay: WARNING: memory leak will occur if overlay removed, property: /axi/pcie@120000/rp1/spi@50000/status
[ 563.762546] OF: overlay: WARNING: memory leak will occur if overlay removed, property: /axi/pcie@120000/rp1/spi@50000/status
Please confirm that you have a single reference to spi in /boot/firmware/config.txt, and that it says:

Code: Select all

dtparam=spi=on
Then reboot, login and do nothing else. Does /dev/spidev0.* exist?

prabus
Posts: 15
Joined: Fri Dec 01, 2023 9:58 am

Re: SPI speed locked on rpi5

Tue Dec 05, 2023 5:06 pm

ubuntu@ubuntu:~$ ls -al /dev/spi*
crw------- 1 root root 153, 0 Dec 5 16:50 /dev/spidev10.0
ubuntu@ubuntu:~$ lsmod | grep spi
spidev 24576 0
spi_dw_mmio 12288 0
spi_dw 36864 1 spi_dw_mmio
spi_bcm2835 32768 0
ubuntu@ubuntu:~$ grep spi /boot/firmware/config.txt
dtparam=spi=on

ubuntu@ubuntu:~$ sudo ls /dev/spidev0.0
ls: cannot access '/dev/spidev0.0': No such file or directory

ubuntu@ubuntu:~$ sudo ls /dev/spidev10.0
/dev/spidev10.0

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5791
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: SPI speed locked on rpi5

Tue Dec 05, 2023 5:18 pm

Weird. OK - here's a bucket load of things to try:

Code: Select all

ls /sys/devices/platform/*.spi
ls /sys/bus/spi/devices/
cat /proc/device-tree/axi/pcie@120000/rp1/spi@50000/status
sudo grep spi /sys/kernel/debug/gpio
pinctrl get 7-11
If you don't have pinctrl you can build and install it from: https://github.com/raspberrypi/utils/tr ... er/pinctrl

prabus
Posts: 15
Joined: Fri Dec 01, 2023 9:58 am

Re: SPI speed locked on rpi5

Wed Dec 06, 2023 6:24 am

Hi,

Thanks for reply. please see below the response to commands given

ubuntu@ubuntu:~/utils/pinctrl$ sudo ls /sys/devices/platform/*.spi
ls: cannot access '/sys/devices/platform/*.spi': No such file or directory
ubuntu@ubuntu:~/utils/pinctrl$ sudo ls /sys/bus/spi/devices/
spi10.0
ubuntu@ubuntu:~/utils/pinctrl$ cat /proc/device-tree/axi/pcie@120000/rp1/spi@50000/status
okayubuntu@ubuntu:~/utils/pinctsudo grep spi /sys/kernel/debug/gpiogpio
gpio-1 (2712_BOOT_CS_N |spi10 CS0 ) out hi ACTIVE LOW
ubuntu@ubuntu:~/utils/pinctrl$ pinctrl get 7-11
Must be root
ubuntu@ubuntu:~/utils/pinctrl$ sudo pinctrl get 7-11
7: ip pu | hi // PIN26/GPIO7 = input
8: ip pu | hi // PIN24/GPIO8 = input
9: ip pn | hi // PIN21/GPIO9 = input
10: ip pn | lo // PIN19/GPIO10 = input
11: ip pn | lo // PIN23/GPIO11 = input

prabus
Posts: 15
Joined: Fri Dec 01, 2023 9:58 am

Re: SPI speed locked on rpi5

Wed Dec 06, 2023 6:33 am

below is the config.txt I have

[all]
kernel=vmlinuz
cmdline=cmdline.txt
initramfs initrd.img followkernel

[pi4]
max_framebuffers=2
arm_boost=1

[all]
# Enable the audio output, I2C and SPI interfaces on the GPIO header. As these
# parameters related to the base device-tree they must appear *before* any
# other dtoverlay= specification
dtparam=audio=on
dtparam=i2c_arm=off
dtparam=spi=on

# Comment out the following line if the edges of the desktop appear outside
# the edges of your display
disable_overscan=1

# If you have issues with audio, you may try uncommenting the following line
# which forces the HDMI output into HDMI mode instead of DVI (which doesn't
# support audio output)
#hdmi_drive=2

# Enable the serial pins
enable_uart=1

# Autoload overlays for any recognized cameras or displays that are attached
# to the CSI/DSI ports. Please note this is for libcamera support, *not* for
# the legacy camera stack
camera_auto_detect=1
display_auto_detect=1

# Config settings specific to arm64
arm_64bit=1
dtoverlay=dwc2

[cm4]
# Enable the USB2 outputs on the IO board (assuming your CM4 is plugged into
# such a board)
dtoverlay=dwc2,dr_mode=host

[all]

Return to “Interfacing (DSI, CSI, I2C, etc.)”