-1

I'm developing a system based on Raspberry pi 4B, with a 32-bit Bullseye OS. I'm using pigpio for SPI and GPIO, and I have observed that the delay between command executions is huge. I have run a simple blink test without any delay between 'on' and 'off' commands, the delay is 80 us. I took a second Raspberry, burnt a clean OS and tested the same code, same result. Then I tested the blink code but this time using RPi.GPIO - 0.6 us delay, much faster. Do you know what may be the reason? I expected pigpio to be much faster.

pigpio code:

import pigpio as pi

pi1 = pi.pi('')

pin = 23

pi1.write(pin, 1)
pi1.write(pin, 0)
pi1.write(pin, 1)
pi1.write(pin, 0)
pi1.write(pin, 1)
pi1.write(pin, 0)

Result: pigpio

RPi.GPIOcode:

import RPi.GPIO as GPIO

pin = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)

GPIO.output(pin, 1)
GPIO.output(pin, 0)
GPIO.output(pin, 1)
GPIO.output(pin, 0)
GPIO.output(pin, 1)
GPIO.output(pin, 0)

Result: RPi.GPIO

2 Answers 2

1

You are reinventing the wheel.

If you did any research you would realise pigpio uses a socket interface which inevitably has a significant overhead BUT if you use it appropriately it is the most capable and functional library available because the daemon includes code to perform complex tasks.

What your code does is probably the most inefficient part of pigpio but if you actually had a real task pigpio could do it much faster.

RPi.GPIO is OK for these basic tasks but far from the fastest.

Of course in any real life application basic read/write speed is almost invariably irrelevant. My pi_gpio c library is faster than both (at least an order of magnitude faster for your example - toggling a pin in 200nSec) BUT I use all for different tasks, selecting the most appropriate for the application.

Your question is also misleading RPi.GPIO has no support for SPI.

Mind you all of these are probably irrelevant for Pi5.

0

Thanks or your answer. So I decided to write a shared library in C with functions from pigpiod (in C), so that I can call them from Python. Overall the performance is much better now, but every second, the program freezes for about 50 ms, no matter whether I'm running SPI communication, or just toggling a pin fast.

part of my C shared library:

#include <pigpio.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>


void initialise()
{
    gpioCfgClock(1, 1, 1);
    int cfg = gpioCfgGetInternals();
    cfg |= PI_CFG_STATS;
    cfg |= PI_CFG_NOSIGHANDLER;
    cfg |= PI_CFG_RT_PRIORITY;
    gpioCfgDMAchannels(0, 1);
    gpioCfgSetInternals(cfg);
    gpioInitialise();
}

void terminate()
{
    gpioTerminate();
}

void pinMode(uint8_t pin, char *mode)
{
    if (!strcmp(mode, "OUT"))
    {
        gpioSetMode(pin, PI_OUTPUT);
    }

    else if (!strcmp(mode, "IN"))
    {
        gpioSetMode(pin, PI_INPUT);
    }

    else 
    {
        printf("%s", "Wrong pin mode");
    }
}

int pinRead(uint8_t pin)
{
    return gpioRead(pin);
}

void pinWrite(uint8_t pin, bool value)
{
    gpioWrite(pin, value);
}

blink code:

from ctypes import *
import signal
import sys

so_file = "/home/m0063108/Desktop/LTE_tensometry/C_pigpio/libprog.so"
pi = CDLL(so_file)

def signal_handler(signal, frame):
    pi.terminate()
    print('Closing the program')
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)


pi.terminate()
pi.initialise()

pi.pinMode(2, b'OUT')

while 1:
    pi.pinWrite(2, 1)
    pi.pinWrite(2, 0)

Result (achieved frequency is 500 kHz, but the code freezes every second. : enter image description here

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.