GPIO#
Overview#
The K230 platform provides two GPIO controllers:
GPIO0: Controller base address
0x9140b000, provides 32 GPIO pins (0-31)GPIO1: Controller base address
0x9140c000, provides 32 GPIO pins (0-31)
Device Tree Configuration#
IO pins have multiple functions. Refer to the following to configure a certain io (via IOMUX) as GPIO function:
&iomux {
gpio21_pins: gpio21_pins {
pins = K230_IO21;
function = "alt0";
};
gpio52_pins: gpio52_pins {
pins = K230_IO52;
function = "alt0";
};
};
If already configured as gpio function under uboot, it is not necessary to configure under linux The k230_iomux.py tool can also configure the related io as gpio function
Driver usage gpio reference
// Button configuration (GPIO21)
btn {
compatible = "gpio-keys";
pinctrl-names = "default";
pinctrl-0 = <&gpio21_pins>;
status = "okay";
btn0: btn0 {
label = "btn";
linux,code = <KEY_VOLUMEUP>;
gpios = <&gpio0_ports 21 GPIO_ACTIVE_LOW>;
};
};
// LED configuration (GPIO52)
led {
compatible = "gpio-leds";
pinctrl-names = "default";
pinctrl-0 = <&gpio52_pins>;
status = "okay";
led0:led0 {
label = "led";
gpios = <&gpio1_ports 20 GPIO_ACTIVE_HIGH>;
};
};
k230 does not enable key and led framework by default (after enabling, libgpiod cannot be used to operate the corresponding pins). The above configuration is for reference only
libgpiod2 Tool Usage#
K230 Buildroot has integrated the libgpiod2 tool by default. Usage reference is as follows:
gpioget Reading Value#
# Read a single GPIO value gpio0 (GPIO0_0) - must use -c to specify chip
gpioget -c 0 0
# Read a single GPIO value GPIO52(GPIO1_20) - must use -c to specify chip
gpioget -c 1 20
gpioset Setting Value#
## Set GPIO52(GPIO1_20) to high level
gpioset -c 1 20=1
## Set GPIO52(GPIO1_20) to low level
gpioset -c 1 20=0
# Use -t to achieve automatic toggle (LED blinking, no need to write a loop)
# Toggle every 500ms, exit after 5 toggles
gpioset -c 1 -t 500ms,500ms 20=1
gpiomon Monitoring#
gpiomon - Monitor GPIO interrupt events
# Monitor both rising and falling edges of GPIO0_10
gpiomon -e both -c 0 21
# Monitor only falling edge (button press)
gpiomon -e falling -c 0 21
# Monitor only rising edge
gpiomon -e rising -c 0 21
# Exit after monitoring 5 events
gpiomon -e falling -n 5 -c 0 21
gpiodetect#
# List all GPIO controllers in the system
gpiodetect
# List all GPIO controllers and line information (recommended)
gpioinfo
C Language Programming Examples#
LED Blinking (C Language)#
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gpiod.h>
#define LED_GPIO_CHIP_PATH "/dev/gpiochip1"
#define LED_LINE_NUM 20
// 闪烁间隔(毫秒),可通过命令行参数指定
#define DEFAULT_INTERVAL_MS 500
int main(int argc, char *argv[]) {
struct gpiod_chip *led_chip = NULL;
struct gpiod_line_settings *settings = NULL;
struct gpiod_line_config *line_cfg = NULL;
struct gpiod_request_config *req_cfg = NULL;
struct gpiod_line_request *led_request = NULL;
unsigned int offsets[1];
int interval_ms = DEFAULT_INTERVAL_MS;
int ret = EXIT_FAILURE;
// 解析命令行参数:led_blink [间隔毫秒]
if (argc > 1) {
interval_ms = atoi(argv[1]);
if (interval_ms <= 0) {
fprintf(stderr, "无效的间隔时间: %s\n", argv[1]);
fprintf(stderr, "用法: %s [间隔毫秒]\n", argv[0]);
return EXIT_FAILURE;
}
}
printf("LED 闪烁测试 - 间隔: %d ms\n", interval_ms);
// 1. 打开 LED GPIO 芯片控制器
led_chip = gpiod_chip_open(LED_GPIO_CHIP_PATH);
if (!led_chip) {
perror("打开 LED gpiochip 失败");
goto cleanup;
}
// 2. 创建并配置引脚设置
settings = gpiod_line_settings_new();
if (!settings) {
perror("创建 line settings 失败");
goto cleanup;
}
// 3. 创建引脚配置
line_cfg = gpiod_line_config_new();
if (!line_cfg) {
perror("创建 line config 失败");
goto cleanup;
}
// 配置 LED 引脚:设置为输出,初始值为 0(熄灭)
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_OUTPUT);
gpiod_line_settings_set_output_value(settings, 0);
offsets[0] = LED_LINE_NUM;
if (gpiod_line_config_add_line_settings(line_cfg, &offsets[0], 1, settings) < 0) {
perror("配置 LED 引脚失败");
goto cleanup;
}
// 4. 创建请求配置
req_cfg = gpiod_request_config_new();
if (!req_cfg) {
perror("创建 request config 失败");
goto cleanup;
}
gpiod_request_config_set_consumer(req_cfg, "k230_led");
// 5. 请求 LED GPIO 引脚
led_request = gpiod_chip_request_lines(led_chip, req_cfg, line_cfg);
if (!led_request) {
perror("请求 LED GPIO 引脚失败");
goto cleanup;
}
printf("LED 初始化成功!开始闪烁...\n");
printf("LED: %s:%u\n", LED_GPIO_CHIP_PATH, LED_LINE_NUM);
// 6. 主循环:LED 闪烁
while (1) {
// LED 点亮
ret = gpiod_line_request_set_value(led_request, LED_LINE_NUM, 1);
if (ret < 0) {
perror("写入 LED 失败");
break;
}
printf("LED ON\n");
usleep(interval_ms * 1000);
// LED 熄灭
ret = gpiod_line_request_set_value(led_request, LED_LINE_NUM, 0);
if (ret < 0) {
perror("写入 LED 失败");
break;
}
printf("LED OFF\n");
usleep(interval_ms * 1000);
}
ret = EXIT_SUCCESS;
cleanup:
// 释放资源
if (led_request) gpiod_line_request_release(led_request);
if (req_cfg) gpiod_request_config_free(req_cfg);
if (line_cfg) gpiod_line_config_free(line_cfg);
if (settings) gpiod_line_settings_free(settings);
if (led_chip) gpiod_chip_close(led_chip);
return ret;
}
Python Examples#
LED Example (adafruit)#
import board
from digitalio import DigitalInOut, Direction
from time import sleep
# Initialize LED
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
# Blink LED in loop
while True:
led.value = 1 # Output high level, turn on built-in blue LED
sleep(1)
led.value = 0 # Output low level, turn off built-in blue LED
sleep(1)
LED Example (periphery)#
from periphery import GPIO
import time
# Modify the chip path and pin number according to your K230 or other board
# Assuming LED is connected to line 20 of /dev/gpiochip1
LED_CHIP_PATH = "/dev/gpiochip1"
LED_LINE_NUM = 20
try:
# Open GPIO and configure as output mode ("out")
# New version of python-periphery no longer supports initial_value parameter
led = GPIO(LED_CHIP_PATH, LED_LINE_NUM, "out")
# Manually set initial value to low (LED off)
led.write(False)
print(f"Successfully initialized LED pin: {LED_CHIP_PATH} [Line {LED_LINE_NUM}]")
print("Starting LED blink, press Ctrl+C to exit...")
while True:
# Output high level (True), turn on LED
# Note: If your hardware is active-low, write(True) will turn it off,
# and write(False) will turn it on
print("LED ON")
led.write(True)
time.sleep(0.9) # Keep on for 0.5 seconds
# Output low level (False), turn off LED
print("LED OFF")
led.write(False)
time.sleep(0.9) # Keep off for 0.5 seconds
except KeyboardInterrupt:
print("\nProgram terminated.")
except Exception as e:
print(f"Error occurred: {e}")
finally:
# Release GPIO resource properly and turn off LED before exiting
if 'led' in locals():
led.write(False)
led.close()
print("LED resource released.")
