How to synchronize different GPIO pins?

Hello,

I am using 24pin and 26pin. The 24pin trigger 3 times with 26pin. The code is bollow.

But I find when both of they triggered , start time not equal. About 150us diffent.

Is there any way to fix that ?

Thank you!

import RPi.GPIO as GPIO
import time

Pin Definitions

fast_pin = 24
slow_pin = 26

def main():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fast_pin, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(slow_pin, GPIO.OUT, initial=GPIO.HIGH)
curr_value = GPIO.HIGH

print("Starting demo now! Press CTRL+C to exit")
count_dn = 0
count_up = 0
try:
    while True:
        time.sleep(0.015)
        if(count_up % 3 == 0 and curr_value == GPIO.HIGH):
            GPIO.output(slow_pin, curr_value)
        elif(count_dn % 3 == 0 and curr_value != GPIO.HIGH):
            GPIO.output(slow_pin, curr_value)
        GPIO.output(fast_pin, curr_value)

        if curr_value == GPIO.HIGH:
            count_up += 1
        else:
            count_dn += 1

        curr_value ^= GPIO.HIGH

finally:
    GPIO.cleanup()

if name == ‘main’:
main()

hello afhel,

how about using kernel APIs to controls gpio state directly.
for example,

<i>$l4t-r32.2/public_sources/kernel_src/kernel/kernel-4.9/include/linux/gpio.h</i>

static inline void gpio_set_value(unsigned int gpio, int value)
{
  	__gpio_set_value(gpio, value);
}

Hi, JerryChang.

Thank you for replay.

I changed my code like you sayed. It looks like very good.

But I think in the function “gpio_set_value”, the process is openfile-writefile-closefile. So if I call gpio_set_value many times. For example:

init()
{

gpioExport(gpio424);
gpioSetDirection(gpio424, 1);
gpioExport(gpio393);
gpioSetDirection(gpio393, 1);
gpioExport(gpio344);
gpioSetDirection(gpio344, 1);
gpioExport(gpio417);
gpioSetDirection(gpio417, 1);

}

proc()
{

gpio_set_value(gpio424, 1);
gpio_set_value(gpio393, 1);
gpio_set_value(gpio344, 1);
gpio_set_value(gpio417, 1);

}

relase()
{

gpioUnexport(gpio424);
gpioUnexport(gpio393);
gpioUnexport(gpio344);
gpioUnexport(gpio417);

}
How can I ensure gpio424 and gpio417 worked at the same time?

hello afhel,

AFAIK, there’s no APIs to control multiple GPIOs at once.
may I know what’s your purpose or use-case to ensure gpio424 and gpio417 worked at the same time.
thanks

Hi

We have two cameras, one is 30fps,another is 60fps, a gps connect with 60fps_camera. And we want they capture at the same time, so we will know capture time of the 30fps-camera.

hello afhel,

please gather the timestamp of capture frames, you may have comparison of the capture timestamps between these two sensors, and please have synchronize from the user-space application side.
suggest you should also refer to similar discussion thread, such as Topic 1007933.
thanks