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

举报
Yuchuan 发表于 2021/07/04 17:20:12 2021/07/04
【摘要】 基础外设开发——GPIO输出

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

本示例将演示如何在BearPi-HM_Nano开发板上使用GPIO输出功能去点亮LED灯。

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、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输出方向.

5、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输出的值

6、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输出的值

硬件设计

本案例将使用板载的LED来验证GPIO的输出功能,在BearPi-HM_Nano开发板上LED的连接电路图如下图所示,LED的控制引脚与主控芯片的GPIO_2连接,所以需要编写软件去控制GPIO_2输出高低电平实现LED灯的亮灭。

LED灯电路

软件设计

主要代码分析

yuchuanThreadBasicLED()为LED灯测试主任务,该任务先调用 IoTGpioInit()初始化GPIO,因为LED灯的控制引脚接在GPIO_2上,所以通过调用IoTGpioSetFunc()和IoTGpioSetDir()将GPIO_2设置为普通GPIO的输出模式。最后在死循环里面间隔 1s 输出GPIO_2的高低电平,实现LED灯闪烁的现象

yuchuan_basic_led.c

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

#define GPIO_LED 2

void yuchuanThreadBasicLED(void *argumemt)
{
    (void)argumemt;
    IotGpioDir gpioDir;
    IotGpioValue gpiovalue;
    //初始化GPIO
    IoTGpioInit(GPIO_LED);
    //设置GPIO为普通复用功能
    IoTGpioSetFunc(GPIO_LED,IOT_GPIO_FUNC_GPIO_2_GPIO);
    //设置GPIO为输出方向
    IoTGpioSetDir(GPIO_LED,IOT_GPIO_DIR_OUT);
    //获取GPIO的输出方向
    IoTGpioGetDir(GPIO_LED,&gpioDir);
    printf("IotGpioDir is %d\n",gpioDir);
    while (1)
    {
        //设置输出高电平点亮LED等
        IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE1);
        //延迟1秒后再设置GPIO输出低电平维平稳
        IoTGpioGetOutputVal(GPIO_LED,&gpiovalue);
        printf("GpioGetOutputValue is %d\n",gpiovalue);
        osDelay(100);

        IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE0);
        IoTGpioGetOutputVal(GPIO_LED,&gpiovalue);
        printf("GpioGetOutputValue is %d\n",gpiovalue);
        osDelay(100);
    }
    
}

static void yuchuanBasicLED(void)
{
    osThreadAttr_t threadAttr;
    threadAttr.attr_bits = 0U;
    threadAttr.cb_mem = NULL;
    threadAttr.cb_size = 0U;
    threadAttr.stack_mem = NULL;
    threadAttr.stack_size = 1024 * 4;
    threadAttr.priority = 25;

    threadAttr.name = "YuchuanThreadBasicLED";
    if (osThreadNew((osThreadFunc_t)yuchuanThreadBasicLED,NULL,&threadAttr) == NULL)
    {
        /* code */
        printf("Falied to Create YuchuanThreadBasicLED !!!\n");
    }
    
}

APP_FEATURE_INIT(yuchuanBasicLED);

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/B1_YuchuanBasicLEDBlink

static_library("yuchuanLED"){
    sources = [
        "yuchuan_basic_led.c",
    ]

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

修改 BUILD.gn 文件

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

# 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",
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,开发板的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.IotGpioDir is 1
GpioGetOutputValue is 1
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:0x4b0268 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0274 TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0740 TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0274 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0268 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b0740 <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).
GpioGetOutputValue is 0
GpioGetOutputValue is 1
GpioGetOutputValue is 0
GpioGetOutputValue is 1
GpioGetOutputValue is 0
GpioGetOutputValue is 1
GpioGetOutputValue is 0
GpioGetOutputValue is 1
GpioGetOutputValue is 0
GpioGetOutputValue is 1
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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