HarmonyOS小熊派 | HarmonyOS基础外设开发--GPIO输入

举报
Yuchuan 发表于 2021/07/04 19:07:22 2021/07/04
【摘要】 开发板基础外设开发——GPIO输入

BearPi-HM_Nano开发板基础外设开发——GPIO输入

本示例将演示如何在BearPi-HM_Nano开发板上使用GPIO输入功能去读取按键状态

BearPi-HM_Nano

GPIO API分析

本案例主要使用了以下几个API完成GPIO输出功能

1、IoTGpioInit()

/**
 * @brief Initializes a GPIO device.
 *
 * @param id Indicates the GPIO pin number.
 * @return Returns {@link IOT_SUCCESS} if the GPIO device is initialized;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTGpioInit(unsigned int id);

描述:

初始化GPIO外设

2、IoTGpioSetFunc()

unsigned int IoTGpioSetFunc(unsigned int id, unsigned char val);

描述:

设置GPIO引脚复用功能

参数:

名字 描述
id 表示GPIO引脚号.
val 表示GPIO复用功能


3、IoTGpioSetDir()

/**
 * @brief Sets the direction for a GPIO pin.
 *
 * @param id Indicates the GPIO pin number.
 * @param dir Indicates the GPIO input/output direction.
 * @return Returns {@link IOT_SUCCESS} if the direction is set;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTGpioSetDir(unsigned int id, IotGpioDir dir);

描述:

设置GPIO输出方向

参数:

名字 描述
id 表示GPIO引脚号.
dir 表示GPIO输出方向.


4、IoTGpioSetPull()

unsigned int IoTGpioSetPull(unsigned int id, IotGpioPull val);

描述:

设备GPIO的上下拉方式

参数:

名字 描述
id 表示GPIO引脚号.
val 表示要设置的上拉或下拉.


5、IoTGpioRegisterIsrFunc()

/**
 * @brief Enables the interrupt feature for a GPIO pin.
 *
 * This function can be used to set the interrupt type, interrupt polarity, and interrupt callback for a GPIO pin.
 *
 * @param id Indicates the GPIO pin number.
 * @param intType Indicates the interrupt type.
 * @param intPolarity Indicates the interrupt polarity.
 * @param func Indicates the interrupt callback function.
 * @param arg Indicates the pointer to the argument used in the interrupt callback function.
 * @return Returns {@link IOT_SUCCESS} if the interrupt feature is enabled;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity,
                                    GpioIsrCallbackFunc func, char *arg);

描述:

启用GPIO引脚的中断功能。这个函数可以用来为GPIO pin设置中断类型、中断极性和中断回调。

参数:

名字 描述
id 表示GPIO引脚号.
intType 表示中断类型.
intPolarity 表示中断极性.
func 表示中断回调函数.
arg 表示中断回调函数中使用的参数的指针.


6、IoTGpioGetDir()

/**
 * @brief Obtains the direction for a GPIO pin.
 *
 * @param id Indicates the GPIO pin number.
 * @param dir Indicates the pointer to the GPIO input/output direction.
 * @return Returns {@link IOT_SUCCESS} if the direction is obtained;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTGpioGetDir(unsigned int id, IotGpioDir *dir);

描述:

获取GPIO输出方向

参数:

名字 描述
id 表示GPIO引脚号.
*dir 表示GPIO输出方向.


7、IoTGpioSetOutputVal()

/**
 * @brief Sets the output level value for a GPIO pin.
 *
 * @param id Indicates the GPIO pin number.
 * @param val Indicates the output level value.
 * @return Returns {@link IOT_SUCCESS} if the output level value is set;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTGpioSetOutputVal(unsigned int id, IotGpioValue val);

描述:

设置GPIO输出的值

参数:

名字 描述
id 表示GPIO引脚号.
val 表示GPIO输出的值


8、IoTGpioGetOutputVal()

/**
 * @brief Obtains the output level value of a GPIO pin.
 *
 * @param id Indicates the GPIO pin number.
 * @param val Indicates the pointer to the output level value.
 * @return Returns {@link IOT_SUCCESS} if the output level value is obtained;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 2.2
 * @version 2.2
 */
unsigned int IoTGpioGetOutputVal(unsigned int id, IotGpioValue *val);

描述:

获取GPIO输出的值

参数:

名字 描述
id 表示GPIO引脚号.
*val 表示GPIO输出的值


硬件设计

本案例将使用板载的两个用户按键来验证GPIO的输入功能,在BearPi-HM_Nano开发板上用户按键的连接电路图如下图所示,按键F1的检测引脚与主控芯片的GPIO_11连接,按键F2的检测引脚与主控芯片的GPIO_12连接,所以需要编写软件去读取GPIO_11和GPIO_12的电平值,判断按键是否被按下。

软件设计

主要代码分析

这部分代码主要分析按键触发中断的功能代码,这里以按键F1为例,按键F1的检测引脚与主控芯片的GPIO_11连接,首先通过调用IoTGpioSetFunc()和IoTGpioSetDir()将GPIO_11设置为普通GPIO的输出模式。从前面原理图可知,当按键按下时,GPIO_11会被下拉到地,所以这里要使用IoTGpioSetPull()将GPIO_11引脚设置为上拉,这样才能产生电平的跳变。最后通过IoTGpioRegisterIsrFunc()将中断类型设置为边沿触发,且为下降沿触发,当按键被按下时,GPIO_11会从高电平转为低电平,产生一个下降,这个时候就会触发中断并回调F1_Pressed函数。在F1_Pressed函数中实现点亮LED灯操作。

yuchuan_basic_button.c

#include <stdio.h>
#include "ohos_init.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"

#define GPIO_LED 2
#define BUTTON_F1_GPIO 11
#define BUTTON_F2_GPIO 12

void f1PressFunc(void *argument)
{
    (void)argument;
    IotGpioValue buttonF1Value, ledValue;

    //当按下F1按钮时获取LED GPIO的值
    IoTGpioGetInputVal(GPIO_LED,&ledValue);

    if (ledValue == IOT_GPIO_VALUE0)
    {
        /* code */
        printf("F1 LED Status is Value : %d\n",ledValue);
        //当按下F1按钮时获取Button F1 GPIO的值并打印出来
        IoTGpioGetInputVal(BUTTON_F1_GPIO,&buttonF1Value);
        printf("ButtonF1 is Value : %d\n",buttonF1Value);
        osDelay(10);
        //当按下F1按钮是点亮LED灯
        IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE1);
    }
    
}

void f2PressFunc(void *argument)
{
    (void)argument;
    IotGpioValue buttonF2Value, ledValue;

    //当按下按钮F2时获取LED GPIO的值
    IoTGpioGetInputVal(GPIO_LED,&ledValue);

    if (ledValue == IOT_GPIO_VALUE1)
    {
        /* code */
        printf("F2 LED Status is Value : %d\n",ledValue);

        //当按下F2按钮时获取Button F2 GPIO的值并打印出来
        IoTGpioGetInputVal(BUTTON_F2_GPIO,&buttonF2Value);
        printf("ButtonF2 is Value : %d\n",buttonF2Value);
        osDelay(10);
        //当按下F2按钮是设置LED GPIO输出低电平熄灭LED灯
        IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE0);
    }    
    
}

static void yuchuanBasicButtonExample(void)
{
    /*LED灯*/
    //初始化GPIO
    IoTGpioInit(GPIO_LED);
    //设置GPIO的复用功能为普通GPIO
    IoTGpioSetFunc(GPIO_LED,IOT_GPIO_FUNC_GPIO_2_GPIO);
    //设置GPIO为输出方向
    IoTGpioSetDir(GPIO_LED,IOT_GPIO_DIR_OUT);

    /*F1按钮*/
    //初始化F1按钮
    IoTGpioInit(BUTTON_F1_GPIO);
    //设置F1按钮GPIO的复用功能为普通GPIO
    IoTGpioSetFunc(BUTTON_F1_GPIO,IOT_GPIO_FUNC_GPIO_11_GPIO);
    //设置F1按钮GPIO为输入方向
    IoTGpioSetDir(BUTTON_F1_GPIO,IOT_GPIO_DIR_IN);
    //设置F1按钮GPIO为上升沿触发
    IoTGpioSetPull(BUTTON_F1_GPIO,IOT_GPIO_PULL_UP);
    //注册一个F1按钮的方法
    IoTGpioRegisterIsrFunc(BUTTON_F1_GPIO,IOT_INT_TYPE_EDGE,IOT_GPIO_EDGE_FALL_LEVEL_LOW,f1PressFunc,NULL);

    /*F2按钮*/
    //初始化F2按钮GPIO
    IoTGpioInit(BUTTON_F2_GPIO);
    //设置F2按钮复用功能为普通GPIO
    IoTGpioSetFunc(BUTTON_F2_GPIO,IOT_GPIO_FUNC_GPIO_12_GPIO);
    //设置F2按钮GPIO为输入方向
    IoTGpioSetDir(BUTTON_F2_GPIO,IOT_GPIO_DIR_IN);
    //设置F2按钮GPIO为上升沿触发
    IoTGpioSetPull(BUTTON_F2_GPIO,IOT_GPIO_PULL_UP);
    //注册F2按钮的方法
    IoTGpioRegisterIsrFunc(BUTTON_F2_GPIO,IOT_INT_TYPE_EDGE,IOT_GPIO_EDGE_FALL_LEVEL_LOW,f2PressFunc,NULL);
}

APP_FEATURE_INIT(yuchuanBasicButtonExample);

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/B2_YuchuanBasicButton

static_library("yuchuanButton"){
    sources = [
        "yuchuan_basic_button.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//base/iot_hardware/peripheral/interfaces/kits",
        
    ]
}

修改 BUILD.gn 文件

修改applications\BearPi\BearPi-HM_Nano\sample 路径下 BUILD.gn 文件,指定 yuchuanButton 参与编译。

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/lite/config/component/lite_component.gni")

lite_component("sample") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example", 
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        #"B1_YuchuanBasicLEDBlink:yuchuanLED",
        "B2_YuchuanBasicButton:yuchuanButton"
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,开发板开始正常工作,按下F1按键LED会点亮,按下F2按键LED会熄灭。

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!

hiview init success.00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b02c8 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b02d4 TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b07a0 TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b02d4 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b02c8 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b07a0 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
F1 LED Status is Value : 0
ButtonF1 is Value : 0
F2 LED Status is Value : 1
ButtonF2 is Value : 0
F1 LED Status is Value : 0
ButtonF1 is Value : 0
F2 LED Status is Value : 1
ButtonF2 is Value : 0
F1 LED Status is Value : 0
ButtonF1 is Value : 0
F2 LED Status is Value : 1
ButtonF2 is Value : 0
F1 LED Status is Value : 0
ButtonF1 is Value : 0
F2 LED Status is Value : 1
ButtonF2 is Value : 0
F1 LED Status is Value : 0
ButtonF1 is Value : 0
F2 LED Status is Value : 1
ButtonF2 is Value : 0


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200