【NCS随笔】nRF54L15初始化GPIOTE

一、ZEPHYR API方式

1.1 prj.conf配置

 CONFIG_GPIO=y

1.2 overlay设备树配置

/ {
    buttons {
        compatible = "gpio-keys";
        my_button0: button_0 {
            gpios = <&gpio1 5 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
            label = "My Button 0";
            zephyr,code = <INPUT_KEY_0>;
        };
        my_button1: button_1 {
            gpios = <&gpio1 7 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
            label = "My Button 1";
            zephyr,code = <INPUT_KEY_1>;
        };
    };
};

1.3 代码示例

#include <zephyr/drivers/gpio.h>

static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(DT_ALIAS(my_button1), gpios);
static struct gpio_callback button_cb_data;

void button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
    // 这里可以放置你的中断处理逻辑
    printk("Button interrupt triggered!\n");
}

void main(void)
{
    int ret;

    if (!device_is_ready(button.port)) {
        printk("Error: button device not ready\n");
        return;
    }

    ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
    if (ret != 0) {
        printk("Error %d: failed to configure button pin\n", ret);
        return;
    }

    ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);
    if (ret != 0) {
        printk("Error %d: failed to configure interrupt\n", ret);
        return;
    }

    gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
    gpio_add_callback(button.port, &button_cb_data);

    printk("GPIO interrupt initialized.\n");
}

二、NRFX 库方式

2.1 prj.conf

CONFIG_NRFX_GPIO=y

2.2 代码示例

#include <nrfx_gpiote.h>
#include <nrf_gpio.h>

#define GPIO_INPUT_PIN   13  // 例如P1.13,请根据实际硬件修改

void gpio_interrupt_handler(nrfx_gpiote_pin_t pin,
                           nrfx_gpiote_trigger_t trigger,
                           void *p_context)
{
    // 这里放置你的中断处理逻辑
    // 例如:切换LED、设置标志位等
}

void gpio_interrupt_init(void)
{
    nrfx_err_t err;

    // 初始化 GPIOTE 实例(nRF54L15 推荐使用实例方式)
    static nrfx_gpiote_t const pio_instance = NRFX_GPIOTE_INSTANCE(0);
    err = nrfx_gpiote_init(&pio_instance, NRFX_GPIOTE_DEFAULT_CONFIG_IRQ_PRIORITY);
    if (err != NRFX_SUCCESS) {
        // 错误处理
        return;
    }

    // 配置输入引脚
    static const nrf_gpio_pin_pull_t pull_cfg = NRF_GPIO_PIN_PULLUP;
    nrfx_gpiote_trigger_config_t trigger_cfg = {
        .trigger = NRFX_GPIOTE_TRIGGER_HITOLO, // 高到低触发
        .p_in_channel = NULL,                  // 让驱动自动分配通道
    };
    static const nrfx_gpiote_handler_config_t handler_cfg = {
        .handler = gpio_interrupt_handler,
    };
    nrfx_gpiote_input_pin_config_t input_cfg = {
        .p_pull_config = &pull_cfg,
        .p_trigger_config = &trigger_cfg,
        .p_handler_config = &handler_cfg,
    };

    err = nrfx_gpiote_input_configure(&pio_instance, GPIO_INPUT_PIN, &input_cfg);
    if (err != NRFX_SUCCESS) {
        // 错误处理
        return;
    }

    nrfx_gpiote_trigger_enable(&pio_instance, GPIO_INPUT_PIN, true);
}
posted @ 2025-08-29 13:54  TedLee  阅读(54)  评论(0)    收藏  举报