本实验将演示如何在小凌派-RK2206开发板上使用IOT库的GPIO中断模式,进行GPIO编程开发。
例程将创建一个任务,通过配置GPIO引脚为中断模式,实现GPIO中断操作。
例程源代码:https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts/tree/master/vendor/lockzhiner/rk2206/samples/b19_iot_gpio_int
GPIO在日常设备中非常常见,以下我们将演示IOT库的GPIO接口如何进行GPIO中断。
base/iot_hardware/peripheral/interfaces/kits/iot_gpio.h
unsignedintIoTGpioRegisterIsrFunc(unsignedintid, IotGpioIntTypeintType, IotGpioIntPolarityintPolarity, GpioIsrCallbackFuncfunc, 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目录下。
unsignedintIoTGpioRegisterIsrFunc(unsignedintid, IotGpioIntTypeintType, IotGpioIntPolarityintPolarity, GpioIsrCallbackFuncfunc, char*arg)
{
unsignedintret=0;
LzGpioIntTypetype;
if (id>GPIO0_PC7&&id!=GPIO1_PD0) {
PRINT_ERR("id(%d) > %d || id(%d) != %d\n", id, GPIO0_PC7, GPIO1_PD0);
returnIOT_FAILURE;
}
if (intType==IOT_INT_TYPE_LEVEL&&intPolarity==IOT_GPIO_EDGE_FALL_LEVEL_LOW)
type=LZGPIO_INT_LEVEL_LOW;
elseif (intType==IOT_INT_TYPE_LEVEL&&intPolarity==IOT_GPIO_EDGE_RISE_LEVEL_HIGH)
type=LZGPIO_INT_LEVEL_HIGH;
elseif (intType==IOT_INT_TYPE_EDGE&&intPolarity==IOT_GPIO_EDGE_FALL_LEVEL_LOW)
type=LZGPIO_INT_EDGE_FALLING;
elseif (intType==IOT_INT_TYPE_EDGE&&intPolarity==IOT_GPIO_EDGE_RISE_LEVEL_HIGH)
type=LZGPIO_INT_EDGE_RISING;
elseif (intType==IOT_INT_TYPE_EDGE&&intPolarity==IOT_GPIO_EDGE_BOTH_TYPE)
type=LZGPIO_INT_EDGE_BOTH;
else
returnIOT_FAILURE;
ret=LzGpioRegisterIsrFunc(id, type, (GpioIsrFunc)func, arg);
if (ret!=LZ_HARDWARE_SUCCESS) {
returnIOT_FAILURE;
}
returnIOT_SUCCESS;
}
unsignedintIoTGpioUnregisterIsrFunc(unsignedintid);
描述:
禁用GPIO引脚的中断功能。
参数:
参数 | 类型 | 描述 |
---|---|---|
id | unsigned int | GPIOID编号 |
返回值:
返回值 | 描述 |
---|---|
IOT_SUCCESS | 成功 |
IOT_FAILURE | 失败 |
实现:
hal_iot_gpio.c文件在device/rockchip/rk2206/adapter/hals/iot_hardware/wifiiot_lite目录下。
unsignedintIoTGpioUnregisterIsrFunc(unsignedintid)
{
unsignedintret=0;
if (id>GPIO0_PC7&&id!=GPIO1_PD0) {
PRINT_ERR("id(%d) > %d || id(%d) != %d\n", id, GPIO0_PC7, GPIO1_PD0);
returnIOT_FAILURE;
}
ret=LzGpioUnregisterIsrFunc(id);
if (ret!=LZ_HARDWARE_SUCCESS) {
returnIOT_FAILURE;
}
returnIOT_SUCCESS;
}
unsignedintIoTGpioSetIsrMask(unsignedintid, unsignedcharmask);
描述:
屏蔽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目录下。
unsignedintIoTGpioSetIsrMask(unsignedintid, unsignedcharmask)
{
unsignedintret=0;
if (id>GPIO0_PC7&&id!=GPIO1_PD0) {
PRINT_ERR("id(%d) > %d || id(%d) != %d\n", id, GPIO0_PC7, GPIO1_PD0);
returnIOT_FAILURE;
}
if (!mask)
{
ret=LzGpioEnableIsr(id);
if (ret!=LZ_HARDWARE_SUCCESS) {
returnIOT_FAILURE;
}
}
else
{
ret=LzGpioDisableIsr(id);
if (ret!=LZ_HARDWARE_SUCCESS) {
returnIOT_FAILURE;
}
}
returnIOT_SUCCESS;
}
主要代码分析
gpio_int_example函数中,创建一个任务。
voidgpio_int_example()
{
unsignedintthread_id;
TSK_INIT_PARAM_Stask= {0};
unsignedintret =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的值。
voidgpio_int_thread()
{
unsignedintret;
/* 初始化引脚为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自增。
voidgpio_int_func()
{
m_gpio_interrupt_count++;
}
修改 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
示例代码编译烧录代码后,按下开发板的RESET按键,按下按键,通过串口助手查看日志,显示如下:
***************GPIOInterruptExample*************
gpiointerruptcount=0
***************GPIOInterruptExample*************
gpiointerruptcount=1
***************GPIOInterruptExample*************
gpiointerruptcount=2
***************GPIOInterruptExample*************
gpiointerruptcount=3
全部0条评论
快来发表一下你的评论吧 !