I want to know the configuration of Nordic's GPIO pins. I only want to use one callback function, but due to the many buttons on my mouse device, it is not possible to configure an interrupt callback function for all of them. It is too troublesome. How can I identify different pins, such as p1.1 and p2.1? Although I have seen that callback functions can be passed back to ports, I have to use a for loop to traverse the entire number of ports and pin_mask devices every time. I feel it is too troublesome. Is there a better way
// 禁止修改
#define BSP_GPIO_NODE DT_PATH(zephyr_user)
// 按键触发宏定义
#define BSP_KEY_LOW_LEVEL GPIO_INT_LOW_0
#define BSP_KEY_HIGH_LEVEL GPIO_INT_HIGH_1
#define BSP_KEY_EDGE_RISING GPIO_INT_EDGE_RISING
#define BSP_KEY_EDGE_FALLING GPIO_INT_EDGE_FALLING
#define BSP_KEY_EDGE_BOTH GPIO_INT_EDGE_BOTH
// 鼠标案件数量
#define BSP_MOUSE_NUM_MAX 4
// 鼠标案件 L 左 R 右 C 中 FB 前进 BB 后退
static const struct gpio_dt_spec mouse_key_gpios[BSP_MOUSE_NUM_MAX] = {
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, mouse_key_left_gpios),
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, mouse_key_right_gpios),
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, mouse_key_center_gpios),
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, mouse_key_fb_gpios),
// GPIO_DT_SPEC_GET(BSP_GPIO_NODE, mouse_key_bb_gpios),
};
// 定义回调函数结构体
static struct gpio_callback mouse_key_callback[BSP_MOUSE_NUM_MAX];
/* LED 测试 */
const struct gpio_dt_spec mouse_test_led[4] = {
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, board_led0_gpios),
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, board_led1_gpios),
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, board_led2_gpios),
GPIO_DT_SPEC_GET(BSP_GPIO_NODE, board_led3_gpios),
};
#include "bsp_ble.h"
extern struct k_sem ble_sem;
// 创建 中断回调函数
static void mouse_key_callback_funct( const struct device *port,
struct gpio_callback *cb,
gpio_port_pins_t pins )
{
BSP_DEBUG_LOG("intterupt\n");
// gpio_pin_toggle_dt(&mouse_test_led[0]);
}
char bsp_gpio_init()
{
int err;
unsigned char i;
// 测试 才使用
for (i=0; i<4; i++)
{
if ( !( gpio_is_ready_dt(&mouse_test_led[i]) ) )
{
BSP_DEBUG_LOG("mouse_led: error\r\n");
return BSP_ERROR_CODE;
}
err = gpio_pin_configure_dt(&mouse_test_led[i], GPIO_OUTPUT);
if ( err < 0)
{
BSP_DEBUG_LOG("mouse gpio pin config error\r\n");
return BSP_ERROR_CODE;
}
}
// key code
for ( i=0; i<BSP_MOUSE_NUM_MAX; i++)
{
if ( !( gpio_is_ready_dt(&mouse_key_gpios[i]) ) )
{
BSP_DEBUG_LOG("mouse_key_id: %d error\r\n", i);
return BSP_ERROR_CODE;
}
err = gpio_pin_configure_dt(&mouse_key_gpios[i], GPIO_INPUT);
if ( err < 0 )
{
BSP_DEBUG_LOG("mouse_key_id: %d error\r\n", i);
return BSP_ERROR_CODE;
}
err = gpio_pin_interrupt_configure_dt(&mouse_key_gpios[i], BSP_KEY_EDGE_FALLING );
if ( err < 0)
{
BSP_DEBUG_LOG("mouse gpio pin%d config error\r\n", i);
return BSP_ERROR_CODE;
}
// 初始化回调函数
gpio_init_callback(&mouse_key_callback[i], mouse_key_callback_funct, BSP_BIT(mouse_key_gpios[i].pin) );
// 添加回调函数
gpio_add_callback(mouse_key_gpios[i].port, &mouse_key_callback[i]);
}
return BSP_OK_CODE;
}