目录

概述

1 GPIO定义

1.1 GPIO 属性标志

 1.2 驱动模式配置

1.3 高级自定义配置

1.3.1 条件编译配置

1.3.2 GPIO 映射器

1.3.3 中断控制器集成

1.4 代码中使用自定义 GPIO

1.4.1 获取 GPIO 规范

1.4.2 配置和使用 GPIO

1.4.3 自定义 GPIO 控制器驱动

1.5 控制器节点结构

2  完整设备树配置示例

2.1 基础 GPIO 定义

2.2 调试与验证

2.2.1  运行时诊断

2.2.2 设备树可视化

2.3 应用案例


概述

在 nRF Connect SDK 中,自定义 GPIO 定义是设备树配置的核心能力之一。以下是一个完整的自定义 GPIO 设备树配置指南,包含基础定义、高级应用和调试技巧。这些技能对于在 nRF Connect SDK 中开发专业级嵌入式应用至关重要,能够显著提高开发效率和产品质量。

1 GPIO定义

自定义 GPIO 设备树配置技术,其可以实现:

  1. 创建硬件抽象层,简化应用开发

  2. 实现跨硬件平台的代码复用

  3. 优化电源管理和低功耗设计

  4. 构建复杂的硬件配置方案

  5. 提高系统的可靠性和可维护性

1.1 GPIO 属性标志

标志 描述
GPIO_ACTIVE_HIGH 0 高电平有效 (默认)
GPIO_ACTIVE_LOW 1 低电平有效
GPIO_INPUT 1 输入模式
GPIO_OUTPUT 1 输出模式
GPIO_OPEN_DRAIN 2 开漏输出
GPIO_OPEN_SOURCE 4 开集输出
GPIO_PULL_UP 16 上拉电阻
GPIO_PULL_DOWN 32 下拉电阻

 1.2 驱动模式配置

nordic,drive-mode = <GPIO_DRIVE_S0S1>; // 标准驱动 (默认)
nordic,drive-mode = <GPIO_DRIVE_H0S1>; // 高驱动0, 标准驱动1
nordic,drive-mode = <GPIO_DRIVE_S0H1>; // 标准驱动0, 高驱动1
nordic,drive-mode = <GPIO_DRIVE_H0H1>; // 高驱动
nordic,drive-mode = <GPIO_DRIVE_D0S1>; // 断开0驱动, 标准驱动1
nordic,drive-mode = <GPIO_DRIVE_D0H1>; // 断开0驱动, 高驱动1
nordic,drive-mode = <GPIO_DRIVE_S0D1>; // 标准驱动0, 断开1驱动
nordic,drive-mode = <GPIO_DRIVE_H0D1>; // 高驱动0, 断开1驱动

1.3 高级自定义配置

1.3.1 条件编译配置

/ {
    #if DT_NODE_EXISTS(DT_NODELABEL(my_sensor))
        my_sensor_enable: sensor_enable {
            gpios = <&gpio0 15 GPIO_ACTIVE_HIGH>;
        };
    #endif
    
    #if defined(CONFIG_LOW_POWER_MODE)
        my_low_power_config {
            gpios = <&gpio1 0 GPIO_ACTIVE_LOW>;
            low-power-enable;
        };
    #endif
};

1.3.2 GPIO 映射器

/ {
    gpio_mapper {
        compatible = "gpio-mapper";
        #gpio-cells = <2>;
        
        // 创建虚拟GPIO映射
        led1_virtual = <&gpio0 13 GPIO_ACTIVE_LOW>;
        button1_virtual = <&gpio0 11 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
    };
    
    my_app {
        // 使用虚拟GPIO
        led = <&led1_virtual>;
        button = <&button1_virtual>;
    };
};

1.3.3 中断控制器集成

/ {
    my_interrupt_controller: my_irq_ctrl {
        compatible = "my-company,irq-controller";
        interrupt-parent = <&gpio0>;
        interrupts = <11 (GPIO_INT_EDGE | GPIO_ACTIVE_LOW)>,
                     <12 (GPIO_INT_LEVEL | GPIO_ACTIVE_HIGH)>;
        #interrupt-cells = <2>;
    };
    
    my_sensor {
        compatible = "my-company,sensor";
        interrupt-parent = <&my_interrupt_controller>;
        interrupts = <0 IRQ_TYPE_EDGE_RISING>;
    };
};

1.4 代码中使用自定义 GPIO

1.4.1 获取 GPIO 规范

#include <zephyr/drivers/gpio.h>

/* 通过节点标识符获取 */
#define MY_DEVICE_NODE DT_NODELABEL(my_custom_device)

/* 获取GPIO规范 */
static const struct gpio_dt_spec enable_gpio = 
    GPIO_DT_SPEC_GET_BY_IDX(MY_DEVICE_NODE, enable_gpio, 0);

static const struct gpio_dt_spec data_gpios[] = {
    GPIO_DT_SPEC_GET_BY_IDX(MY_DEVICE_NODE, data_gpios, 0),
    GPIO_DT_SPEC_GET_BY_IDX(MY_DEVICE_NODE, data_gpios, 1)
};

1.4.2 配置和使用 GPIO

int init_custom_gpio(void) {
    int ret;
    
    /* 配置使能GPIO */
    if (!device_is_ready(enable_gpio.port)) {
        return -ENODEV;
    }
    
    ret = gpio_pin_configure_dt(&enable_gpio, GPIO_OUTPUT_INACTIVE);
    if (ret < 0) {
        return ret;
    }
    
    /* 配置数据GPIO */
    for (int i = 0; i < ARRAY_SIZE(data_gpios); i++) {
        if (!device_is_ready(data_gpios[i].port)) {
            return -ENODEV;
        }
        
        ret = gpio_pin_configure_dt(&data_gpios[i], GPIO_INPUT);
        if (ret < 0) {
            return ret;
        }
    }
    
    /* 激活设备 */
    gpio_pin_set_dt(&enable_gpio, 1);
    
    return 0;
}

1.4.3 自定义 GPIO 控制器驱动

#include <zephyr/drivers/gpio.h>

#define DT_DRV_COMPAT my_company_gpio_controller

static int my_gpio_configure(const struct device *dev,
                             gpio_pin_t pin,
                             gpio_flags_t flags) {
    // 实现GPIO配置逻辑
    return 0;
}

static int my_gpio_port_get_raw(const struct device *dev,
                               gpio_port_value_t *value) {
    // 实现读取GPIO状态
    return 0;
}

static const struct gpio_driver_api my_gpio_api = {
    .pin_configure = my_gpio_configure,
    .port_get_raw = my_gpio_port_get_raw,
    // 实现其他必要API
};

static int my_gpio_init(const struct device *dev) {
    // 初始化代码
    return 0;
}

#define MY_GPIO_INIT(n) \
    DEVICE_DT_INST_DEFINE(n, \
                          my_gpio_init, \
                          NULL, \
                          NULL, \
                          NULL, \
                          POST_KERNEL, \
                          CONFIG_GPIO_INIT_PRIORITY, \
                          &my_gpio_api);

DT_INST_FOREACH_STATUS_OKAY(MY_GPIO_INIT)

1.5 控制器节点结构

my_gpio_controller: my_gpio_controller {
    compatible = "my-company,gpio-controller"; // 必须匹配驱动
    status = "okay";                          // 启用节点
    #gpio-cells = <2>;                        // 每个GPIO说明符的cell数量
    
    // GPIO组定义
    gpio_groups: gpio_groups {
        group1: group1 {
            gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>, 
                    <&gpio0 11 GPIO_ACTIVE_HIGH>;
            label = "custom_group1";
        };
    };
};

2  完整设备树配置示例

2.1 基础 GPIO 定义

/ {
    // 自定义GPIO控制器节点
    my_gpio_controller: my_gpio_controller {
        compatible = "my-company,gpio-controller";
        status = "okay";
        #gpio-cells = <2>; // 每个GPIO说明符有2个cell: pin number 和 flags
        
        // GPIO组定义
        gpio_groups: gpio_groups {
            group1: group1 {
                gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>,  // P0.10
                        <&gpio0 11 GPIO_ACTIVE_HIGH>;  // P0.11
                label = "custom_group1";
            };
            
            group2: group2 {
                gpios = <&gpio1 2 GPIO_ACTIVE_LOW>,    // P1.02
                        <&gpio1 3 GPIO_ACTIVE_LOW>;    // P1.03
                label = "custom_group2";
            };
        };
    };

    // 自定义外设节点
    my_custom_device {
        compatible = "my-company,custom-device";
        status = "okay";
        
        // 直接引用GPIO
        enable-gpio = <&gpio0 12 GPIO_ACTIVE_HIGH>;      // P0.12
        
        // 引用自定义控制器
        data-gpios = <&my_gpio_controller 0 GPIO_ACTIVE_HIGH>,  // group1[0]
                     <&my_gpio_controller 1 GPIO_ACTIVE_HIGH>;  // group1[1]
        
        // 引脚控制状态
        pinctrl-0 = <&my_custom_default>;
        pinctrl-1 = <&my_custom_sleep>;
        pinctrl-names = "default", "sleep";
    };
};

&pinctrl {
    // 自定义引脚控制状态
    my_custom_default: my_custom_default {
        group1 {
            // 使用标准引脚选择宏
            psels = <NRF_PSEL(GPIO, 0, 13)>,  // P0.13
                    <NRF_PSEL(GPIO, 0, 14)>;  // P0.14
            
            // 高级配置
            nordic,drive-mode = <GPIO_DRIVE_H0H1>;  // 高驱动能力
            bias-pull-up;  // 上拉电阻
        };
    };
    
    my_custom_sleep: my_custom_sleep {
        group1 {
            psels = <NRF_PSEL(GPIO, 0, 13)>,
                    <NRF_PSEL(GPIO, 0, 14)>;
                    
            // 低功耗配置
            low-power-enable;
            bias-pull-down;  // 睡眠时下拉
        };
    };
};

2.2 调试与验证

2.2.1  运行时诊断

void print_gpio_status(const struct gpio_dt_spec *spec) {
    printk("GPIO %s:%d: %s\n", 
           spec->port->name, 
           spec->pin, 
           gpio_pin_get_dt(spec) ? "HIGH" : "LOW");
}

void check_all_gpios(void) {
    print_gpio_status(&enable_gpio);
    for (int i = 0; i < ARRAY_SIZE(data_gpios); i++) {
        print_gpio_status(&data_gpios[i]);
    }
}

2.2.2 设备树可视化

# 安装dt-viewer
pip3 install dt-viewer

# 生成可视化设备树
west build -t dt_view

2.3 应用案例

1) 工业控制面板

/ {
    control_panel {
        compatible = "industrial-control-panel";
        
        // 电机控制
        motor_enable = <&gpio0 22 GPIO_ACTIVE_HIGH>;
        motor_direction = <&gpio0 23 GPIO_ACTIVE_HIGH>;
        
        // 传感器接口
        sensors {
            compatible = "sensor-interface";
            #address-cells = <1>;
            #size-cells = <0>;
            
            sensor@0 {
                reg = <0>;
                int-gpio = <&gpio1 0 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
            };
            
            sensor@1 {
                reg = <1>;
                int-gpio = <&gpio1 1 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
            };
        };
        
        // 安全互锁
        safety_interlock = <&gpio0 31 GPIO_ACTIVE_LOW>;
    };
    
    // 引脚控制状态
    pinctrl_industrial: pinctrl_industrial {
        group1 {
            psels = <NRF_PSEL(GPIO, 0, 22)>,
                    <NRF_PSEL(GPIO, 0, 23)>;
            nordic,drive-mode = <GPIO_DRIVE_H0H1>;
        };
    };
};

2) 智能家居网关

/ {
    smart_hub {
        compatible = "smart-home-hub";
        
        // 无线模块控制
        wifi_enable = <&gpio0 15 GPIO_ACTIVE_HIGH>;
        ble_enable = <&gpio0 16 GPIO_ACTIVE_HIGH>;
        zigbee_reset = <&gpio0 17 GPIO_ACTIVE_LOW>;
        
        // 状态指示
        status_leds {
            wifi_led = <&gpio1 0 GPIO_ACTIVE_LOW>;
            ble_led = <&gpio1 1 GPIO_ACTIVE_LOW>;
            zigbee_led = <&gpio1 2 GPIO_ACTIVE_LOW>;
        };
        
        // 用户接口
        button_factory_reset = <&gpio0 18 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
        touch_button = <&gpio0 19 GPIO_ACTIVE_HIGH>;
    };
    
    // 低功耗配置
    pinctrl_sleep: pinctrl_sleep {
        group1 {
            psels = <NRF_PSEL(GPIO, 0, 15)>,
                    <NRF_PSEL(GPIO, 0, 16)>,
                    <NRF_PSEL(GPIO, 0, 17)>;
            low-power-enable;
            bias-disable;
        };
    };
};

Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐