基于小凌派RK2206开发板:OpenHarmony如何使用IoT接口控制GPIO中断 原创
1、实验简介
本实验将演示如何在小凌派-RK2206开发板上使用IOT库的GPIO中断模式,进行GPIO编程开发。
例程将创建一个任务,通过配置GPIO引脚为中断模式,实现GPIO中断操作。
2、基础知识
GPIO在日常设备中非常常见,以下我们将演示IOT库的GPIO接口如何进行GPIO中断。
2.1、头文件
base/iot_hardware/peripheral/interfaces/kits/iot_gpio.h
2.2、启用GPIO引脚的中断功能:IoTGpioRegisterIsrFunc
unsigned int IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity, GpioIsrCallbackFunc func, char *arg);
描述:
启用GPIO引脚的中断功能。
参数:
| 参数 | 类型 | 描述 | 
|---|---|---|
| id | unsigned int | GPIOID编号 | 
| intType | IotGpioIntType | 中断模式 | 
| intPolarity | IotGpioIntPolarity | 中断极性 | 
| func | GpioIsrCallbackFunc | 中断回调函数 | 
| arg | char * | 中断回调函数参数 | 
返回值:
| 返回值 | 描述 | 
|---|---|
| IOT_SUCCESS | 成功 | 
| IOT_FAILURE | 失败 | 
实现:
hal_iot_gpio.c文件在device/rockchip/rk2206/adapter/hals/iot_hardware/wifiiot_lite目录下。
unsigned int IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity, GpioIsrCallbackFunc func, char *arg)
{
    unsigned int ret = 0;
    LzGpioIntType type;
    
    if (id > GPIO0_PC7 && id != GPIO1_PD0) {
        PRINT_ERR("id(%d) > %d || id(%d) != %d\n", id, GPIO0_PC7, GPIO1_PD0);
        return IOT_FAILURE;
    }
    if (intType == IOT_INT_TYPE_LEVEL && intPolarity == IOT_GPIO_EDGE_FALL_LEVEL_LOW)
        type = LZGPIO_INT_LEVEL_LOW;
    else if (intType == IOT_INT_TYPE_LEVEL && intPolarity == IOT_GPIO_EDGE_RISE_LEVEL_HIGH)
        type = LZGPIO_INT_LEVEL_HIGH;
    else if (intType == IOT_INT_TYPE_EDGE && intPolarity == IOT_GPIO_EDGE_FALL_LEVEL_LOW)
        type = LZGPIO_INT_EDGE_FALLING;
    else if (intType == IOT_INT_TYPE_EDGE && intPolarity == IOT_GPIO_EDGE_RISE_LEVEL_HIGH)
        type = LZGPIO_INT_EDGE_RISING;
    else if (intType == IOT_INT_TYPE_EDGE && intPolarity == IOT_GPIO_EDGE_BOTH_TYPE)
        type = LZGPIO_INT_EDGE_BOTH;
    else
        return IOT_FAILURE;
    ret = LzGpioRegisterIsrFunc(id, type, (GpioIsrFunc)func, arg);
    if (ret != LZ_HARDWARE_SUCCESS) {
        return IOT_FAILURE;
    }  
    return IOT_SUCCESS;
}
2.3、禁用GPIO引脚的中断功能:IoTGpioUnregisterIsrFunc
unsigned int IoTGpioUnregisterIsrFunc(unsigned int id);
描述:
禁用GPIO引脚的中断功能。
参数:
| 参数 | 类型 | 描述 | 
|---|---|---|
| id | unsigned int | GPIOID编号 | 
返回值:
| 返回值 | 描述 | 
|---|---|
| IOT_SUCCESS | 成功 | 
| IOT_FAILURE | 失败 | 
实现:
hal_iot_gpio.c文件在device/rockchip/rk2206/adapter/hals/iot_hardware/wifiiot_lite目录下。
unsigned int IoTGpioUnregisterIsrFunc(unsigned int id)
{
    unsigned int ret = 0;
    if (id > GPIO0_PC7 && id != GPIO1_PD0) {
        PRINT_ERR("id(%d) > %d || id(%d) != %d\n", id, GPIO0_PC7, GPIO1_PD0);
        return IOT_FAILURE;
    }
    ret = LzGpioUnregisterIsrFunc(id);
    if (ret != LZ_HARDWARE_SUCCESS) {
        return IOT_FAILURE;
    }   
    return IOT_SUCCESS;
}
2.4、屏蔽GPIO引脚的中断功能:IoTGpioSetIsrMask
unsigned int IoTGpioSetIsrMask(unsigned int id, unsigned char mask);
描述:
屏蔽GPIO引脚的中断功能。
参数:
| 参数 | 类型 | 描述 | 
|---|---|---|
| id | unsigned int | GPIOID编号 | 
| mask | unsigned char | 中断掩码 1:屏蔽开启 0:屏蔽关闭 | 
返回值:
| 返回值 | 描述 | 
|---|---|
| IOT_SUCCESS | 成功 | 
| IOT_FAILURE | 失败 | 
实现:
hal_iot_gpio.c文件在device/rockchip/rk2206/adapter/hals/iot_hardware/wifiiot_lite目录下。
unsigned int IoTGpioSetIsrMask(unsigned int id, unsigned char mask)
{
    unsigned int ret = 0;
    if (id > GPIO0_PC7 && id != GPIO1_PD0) {
        PRINT_ERR("id(%d) > %d || id(%d) != %d\n", id, GPIO0_PC7, GPIO1_PD0);
        return IOT_FAILURE;
    }
    if (!mask)
    {
        ret = LzGpioEnableIsr(id);
        if (ret != LZ_HARDWARE_SUCCESS) {
            return IOT_FAILURE;
        } 
    }
    else
    {
        ret = LzGpioDisableIsr(id);
        if (ret != LZ_HARDWARE_SUCCESS) {
            return IOT_FAILURE;
        } 
    }
    
    return IOT_SUCCESS;
}
3、程序设计
主要代码分析
gpio_int_example函数中,创建一个任务。
void gpio_int_example()
{
    unsigned int thread_id;
    TSK_INIT_PARAM_S task = {0};
    unsigned int ret      = LOS_OK;
    task.pfnTaskEntry = (TSK_ENTRY_FUNC)gpio_int_thread;
    task.uwStackSize  = 2048;
    task.pcName       = "gpio_int_thread";
    task.usTaskPrio   = 20;
    ret               = LOS_TaskCreate(&thread_id, &task);
    if (ret != LOS_OK) {
        printf("Falied to create gpio_int_thread ret:0x%x\n", ret);
        return;
    }
}
gpio_int_thread任务中调用IOT库的GPIO接口初始化、配置,并打印m_gpio_interrupt_count的值。
void gpio_int_thread()
{
    unsigned int ret;
    /* 初始化引脚为GPIO */
    IoTGpioInit(GPIO_TEST);
    /* 引脚配置为输入 */
    IoTGpioSetDir(GPIO_TEST, IOT_GPIO_DIR_IN);
    /* 设置中断触发方式为下降沿和中断处理函数 */
    ret = IoTGpioRegisterIsrFunc(GPIO_TEST, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, gpio_int_func, NULL);
    if (ret != IOT_SUCCESS) {
        printf("IoTGpioRegisterIsrFunc failed(%d)\n", ret);
        return;
    }
    /* 关闭中断屏蔽 */
    IoTGpioSetIsrMask(GPIO_TEST, FALSE);
    while (1) {
        printf("***************GPIO Interrupt Example*************\n");
        printf("gpio interrupt count = %d\n", m_gpio_interrupt_count);
        printf("\n");
        /* 睡眠1秒 */
        LOS_Msleep(1000);
    }
}
gpio_int_func回调函数中m_gpio_interrupt_count自增。
void gpio_int_func()
{
    m_gpio_interrupt_count++;
}
4、编译调试
修改 vendor/lockzhiner/rk2206/sample 路径下 BUILD.gn 文件,指定 iot_gpio_int_example 参与编译。
"./b19_iot_gpio_int:iot_gpio_int_example",
修改 device/lockzhiner/rk2206/sdk_liteos 路径下 Makefile 文件,添加 -liot_gpio_int_example 参与编译。
hardware_LIBS = -lhal_iothardware -lhardware -lshellcmd -liot_gpio_int_example
5、运行结果
示例代码编译烧录代码后,按下开发板的RESET按键,按下按键,通过串口助手查看日志,显示如下:
***************GPIO Interrupt Example*************
gpio interrupt count = 0
***************GPIO Interrupt Example*************
gpio interrupt count = 1
***************GPIO Interrupt Example*************
gpio interrupt count = 2
***************GPIO Interrupt Example*************
gpio interrupt count = 3




















