yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
RDK X3 Board📖
1. RDK X3 Board Introduction
  • 1.1 About RDK X3 board
  • 1.2 Write system
  • 1.3 SD expansion
  • 1.4 Hardware interface
  • 1.5 Install case antenna
2. Basic setting
  • 2.1 Network Configuration
  • 2.2 Remote Login
  • 2.3 System Update
  • 2.4 Configure srpi-config
  • 2.5 confit.txt file
  • 2.6 Thermal control-CPU frequency
  • 2.7 Auto-start at boot
  • 2.8 RDK-specific commands usage
  • 2.9 Linux command usage
  • 2.10 GPIO read write
  • 2.11 Image classification algorithm
  • 2.12 USB camera usage
  • 2.13 MIPI camera uasge
3. Basic course
C language
  • 1. Camera image display
  • 2. Save camera image
  • 3. Camera image encoding
  • 4. Video decoding display
  • 5. VPS scaling example
  • 6. Object detection algorithm-fcos
  • 7. Object detection algorithm—YOLOv5
Python
  • 1. Using GPIO
  • 2. Using PWM
  • 3. Using serial port
  • 4. Using I2C
  • 5. Using SPI
Download
  • Image_system
  • Code
  • Software_Tools
Download ZIP

Welcome to RDK X3 Board repository

1. Using GPIO

1. Using GPIO

1. Using GPIO

1. Using GPIO1. Import GPIO libaray2. Set pin encoding mode3. Warning message4. Pin configuration5. Input operation6. Output operation7. Clean up pin occupation8. Check pin status9. Edge detection and interruptwait_for_edge() functionevent_detected() functionRun callback function when edge event is detectedDisable interrupts10. Test routine

1. Import GPIO libaray

The development board comes with a pre-installed GPIO Python library Hobot.GPIO. Users can import the GPIO library using the following command.

xxxxxxxxxx
sunrise@ubuntu:~$ sudo python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
Type "help", "copyright", "credits" or "license" for more information.
>>> import Hobot.GPIO as GPIO
Get board ID: 0x504
>>> GPIO.VERSION
'0.0.2'
>>> GPIO.model
'X3PI'

2. Set pin encoding mode

There are 4 modes for the pin coding of the development board.

  • BOARD: physical pin number, corresponding to the silkscreen number of the development board.
  • BCM: GPIO naming rules formulated according to Broadcom SoC.
  • CVM: Use strings instead of numbers, corresponding to the signal names of the CVM/CVB connector.
  • SOC: The corresponding number is the GPIO pin number of the X3M chip, which corresponds to the chip data sheet.

This article recommends that users use the BOARD coding mode, and the coding method is as follows.

xxxxxxxxxx
GPIO.setmode(GPIO.BOARD)
# or
GPIO.setmode(GPIO.BCM)
# or
GPIO.setmode(GPIO.CVM)
# or 
GPIO.setmode(GPIO.SOC)

Query encoding method.

xxxxxxxxxx
mode = GPIO.getmode()

The program will output one of the results: BOARD, BCM, CVM, SOC or None.

3. Warning message

When running the code in the following cases, there will be warning log output, but it will not affect normal functions.

  • The GPIO that the user is trying to use has been used in other applications;
  • Before setting the mode and channel, try to call GPIO.cleanup to clean up the pin;

If you want to block the warning message, you can use the following command.

xxxxxxxxxx
GPIO.setwarnings(False)

4. Pin configuration

Before using the GPIO pin, you need to configure it accordingly, as follows.

The way to set it as input is as follows.

xxxxxxxxxx
GPIO.setup(channel, GPIO.IN)

The way to set it to output is as follows.

xxxxxxxxxx
GPIO.setup(channel, GPIO.OUT)

You can also specify an initial value for the output channel, for example.

xxxxxxxxxx
GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH)

In addition, the tool supports setting multiple output channels at the same time, for example.

xxxxxxxxxx
# set gpio(18,12,13) to output
channels = [18, 12, 13]
GPIO.setup(channels, GPIO.OUT)

5. Input operation

To read the value of a channel:

xxxxxxxxxx
GPIO.input(channel)

The command return value is 0 or 1.

0 represents GPIO.LOW, 1 represents GPIO.HIGH.

6. Output operation

To set the output value of a channel.

xxxxxxxxxx
GPIO.output(channel, state)

State can be GPIO.LOW or GPIO.HIGH.

7. Clean up pin occupation

Before exiting the program, you can clean up the channel.

xxxxxxxxxx
GPIO.cleanup()

If you want to clean only a specific channel, use the following command.

x
# Clear a single channel
GPIO.cleanup(channel)
# Clear a group of channels
GPIO.cleanup( (channel1, channel2) )
GPIO.cleanup( [channel1, channel2] )

8. Check pin status

This function allows you to check the functionality of the corresponding GPIO channel.

xxxxxxxxxx
GPIO.gpio_function(channel)

该函数返回 IN 或 OUT。

9. Edge detection and interrupt

An edge is a change in an electrical signal from low to high (rising edge) or from high to low (falling edge). This change can be regarded as an event that can be used to trigger a CPU interrupt signal.

The GPIO library provides three methods to detect input events.

wait_for_edge() function

This function blocks the calling thread until the corresponding edge change is detected.

The function call is as follows.

xxxxxxxxxx
GPIO.wait_for_edge(channel, GPIO.RISING)

The second parameter specifies the edge to detect, and the value range is GPIO.RISING, GPIO.FALLING, or GPIO.BOTH.

If you want to specify a waiting time, you can choose to set a timeout.

x
# Timeout in milliseconds
GPIO.wait_for_edge(channel, GPIO.RISING, timeout=500)

If the external signal changes within the timeout period, the function returns the detected channel number.

If a timeout occurs, the function returns None.

event_detected() function

This function can be used to periodically check if any events have occurred since the last call.

This function can be set and called as follows.

x
# Set up rising edge detection on channel GPIO
GPIO.add_event_detect(channel, GPIO.RISING)
if GPIO.event_detected(channel):
    print("Rising edge event detected")

You can detect events of GPIO.RISING, GPIO.FALLING, or GPIO.BOTH.

Run callback function when edge event is detected

This function can be used to register callback function, which runs in an independent processing thread.

The instructions are as follows.

xxxxxxxxxx
# define callback function
def callback_fn(channel):
    print("Callback called from channel %s" % channel)

# enable rising detection
GPIO.add_event_detect(channel, GPIO.RISING, callback=callback_fn)

If necessary, you can also add multiple callbacks, as follows.

xxxxxxxxxx
def callback_one(channel):
    print("First Callback")

def callback_two(channel):
    print("Second Callback")

GPIO.add_event_detect(channel, GPIO.RISING)
GPIO.add_event_callback(channel, callback_one)
GPIO.add_event_callback(channel, callback_two)

Since all callbacks run on the same thread, different callbacks are run sequentially, not simultaneously.

To prevent callbacks from being called multiple times by combining multiple events into one, you can optionally set a debounce time.

xxxxxxxxxx
# bouncetime unit is ms
GPIO.add_event_detect(channel, GPIO.RISING, callback=callback_fn, bouncetime=200)

Disable interrupts

If edge detection is no longer needed, it can be removed, as follows.

xxxxxxxxxx
GPIO.remove_event_detect(channel)

10. Test routine

The main test routines are provided in the /app/40pin_samples/ directory.

Test routine nameExplanation
simple_out.pySingle pin output test
simple_input.pySingle pin input test
button_led.pyOne pin is used as a key input, and one pin is used as an output to control the LED
test_all_pins_input.pyInput test code for all pins
test_all_pins.pyOutput test code for all pins
button_event.pyCapture the rising and falling edge events of the pin
button_interrupt.pyInterrupt mode to handle rising and falling edge events of the pin

As shown in the figure, take running simple_input.py and button_event.py as an example.

例程

  • GPIO is set to output mode, and the output level is switched every 1 second. It can be used to control the LED light to turn on and off in a cycle.

    Test code simple_out.py

xxxxxxxxxx
#!/usr/bin/env python3

import Hobot.GPIO as GPIO
import time

# Define the GPIO channel used as 38
output_pin = 38 # BOARD Coding 38

def main():
    # Set the pin encoding mode to hardware encoding BOARD
    GPIO.setmode(GPIO.BOARD)
    # Set to output mode and initialize to high level
    GPIO.setup(output_pin, GPIO.OUT, initial=GPIO.HIGH)
    # Record the current pin status
    curr_value = GPIO.HIGH
    print("Starting demo now! Press CTRL+C to exit")
    try:
        # 1 second interval, cycle control LED light on and off
        while True:
            time.sleep(1)
            GPIO.output(output_pin, curr_value)
            curr_value ^= GPIO.HIGH
    finally:
        GPIO.cleanup()

if __name__=='__main__':
    main()
  • Set GPIO to input mode and read the pin level value through busy polling. Test code simple_input.py.
x
#!/usr/bin/env python3

import Hobot.GPIO as GPIO
import time

# Define the GPIO channel used as 38
input_pin = 38 # BOARD coding 38 

def main():
    prev_value = None

    # Set the pin encoding mode to hardware encoding BOARD
    GPIO.setmode(GPIO.BOARD)
    # Set as input mode
    GPIO.setup(input_pin, GPIO.IN)

    print("Starting demo now! Press CTRL+C to exit")
    try:
        while True:
         &nb
Object detection algorithm—YOLOv5
Using PWM

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文