HarmonyOS驱动子系统开发(GPIO、GPIO中断、PWM)
HarmonyOS驱动子程序开发GPIO接口简介wifiiot_gpio.h接口简介这个.h中包含声明GPIO接口函数,这些功能用于初始化GPIO接口名功能描述unsigned int GpioInit(void)初始化GPIOunsigned int GpioDeinit(void)取消初始化GPIOunsigned int GpioSetDir(WifiIotGpioIdx id, WifiI
HarmonyOS驱动子程序开发
GPIO
接口简介
wifiiot_gpio.h接口简介
这个.h中包含声明GPIO接口函数,这些功能用于初始化GPIO
接口名 | 功能描述 |
---|---|
unsigned int GpioInit(void) | 初始化GPIO |
unsigned int GpioDeinit(void) | 取消初始化GPIO |
unsigned int GpioSetDir(WifiIotGpioIdx id, WifiIotGpioDir dir) | 设置GPIO引脚方向 |
unsigned int GpioGetDir(WifiIotGpioIdx id, WifiIotGpioDir *dir) | 获取GPIO引脚方向 |
unsigned int GpioSetOutputVal(WifiIotGpioIdx id, WifiIotGpioValue val) | 设置GPIO引脚输出电平值 |
unsigned int GpioGetOutputVal(WifiIotGpioIdx id, WifiIotGpioValue *val) | 获取GPIO引脚输出电平值 |
wifiiot_gpio_ex.h接口简介
这个.h中包含声明拓展的GPIO接口函数,这些功能用于设置GPIO拉力和驱动器强度
接口名 | 功能描述 |
---|---|
unsigned int IoSetPull(WifiIotIoName id, WifiIotIoPull val) | 设置GPIO引脚上拉 |
unsigned int IoGetPull(WifiIotIoName id, WifiIotIoPull *val) | 获取GPIO引脚上拉 |
unsigned int IoSetFunc(WifiIotIoName id, unsigned char val) | 设置GPIO引脚功能 |
unsigned int IoGetFunc(WifiIotIoName id, unsigned char *val) | 获取GPIO引脚功能 |
unsigned int IOSetDriverStrength(WifiIotIoName id, WifiIotIoDriverStrength val) | 设置GPIO驱动能力 |
unsigned int IOGetDriverStrength(WifiIotIoName id, WifiIotIoDriverStrength *val) | 获取GPIO驱动能力 |
查看LED对应的GPIO引脚
LED对应的GPIO引脚是GPIO2通过控制GPIO2输出的电平信号来实现LED灯闪烁。
高电平时点亮LED灯;低电平熄灭LED灯
操作GPIO点亮LED
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void LedTask(void)
{
//初始化GPIO
GpioInit();
//设置GPIO_2的复用功能为普通GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
//设置GPIO_2为输出模式
GpioSetDir(WIFI_IOT_GPIO_IDX_2, WIFI_IOT_GPIO_DIR_OUT);
while (1)
{
//设置GPIO_2输出高电平点亮LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 1);
//延时1s
usleep(1000000);
//设置GPIO_2输出低电平熄灭LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 0);
//延时1s
usleep(1000000);
}
}
static void LedExampleEntry(void)
{
osThreadAttr_t attr;
attr.name = "LedTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 512;
attr.priority = 25;
if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL)
{
printf("Falied to create LedTask!\n");
}
}
APP_FEATURE_INIT(LedExampleEntry);
烧录并查看
重启可以看到LED灯开始闪烁
GPIO扩展实验
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void LedTask(void)
{
osDelay(100);
//初始化GPIO
GpioInit();
//设置GPIO_2的复用功能为普通GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
//设置GPIO_2为输出模式
GpioSetDir(WIFI_IOT_GPIO_IDX_2, WIFI_IOT_GPIO_DIR_OUT);
//定义变量存储方向
WifiIotGpioDir val={0};
//获取方向
GpioGetDir(WIFI_IOT_GPIO_IDX_2,&val);
printf("GPIO_2 Dir is %d\r\n",val);
//定义变量存储电平值
WifiIotGpioValue OutputVal={0};
while (1)
{
//设置GPIO_2输出高电平点亮LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 1);
//获取电平值
GpioGetOutputVal(WIFI_IOT_GPIO_IDX_2,&OutputVal);
printf("GPIO_2 OutputVal is %d\r\n",OutputVal);
//延时1s
usleep(1000000);
//设置GPIO_2输出低电平熄灭LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 0);
GpioGetOutputVal(WIFI_IOT_GPIO_IDX_2,&OutputVal);
printf("GPIO_2 OutputVal is %d\r\n",OutputVal);
//延时1s
usleep(1000000);
}
}
static void LedExampleEntry(void)
{
osThreadAttr_t attr;
attr.name = "LedTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
//进程需要打印内容,我们将内存大小增加一些
attr.stack_size = 1024*2;
attr.priority = 25;
if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL)
{
printf("Falied to create LedTask!\n");
}
}
APP_FEATURE_INIT(LedExampleEntry);
GPIO中断
接口简介
wifiiot_gpio.h接口简介
wifiiot_gpio.h中包含声明GPIO中断相关函数。
接口名 | 功能 |
---|---|
unsigned int GpioRegisterIsrFunc(WifiIotGpioIdx id, WifiIotGpioIntType intType, WifiIotGpioIntPolarity intPolarity,GpioIsrCallbackFunc func, char *arg) | 设置GPIO引脚中断功能 |
unsigned int GpioUnregisterIsrFunc(WifiIotGpioIdx id) | 取消GPIO引脚中断 |
unsigned int GpioSetIsrMask(WifiIotGpioIdx id, unsigned char mask) | 屏蔽GPIO引脚中断 |
unsigned int GpioSetIsrMode(WifiIotGpioIdx id, WifiIotGpioIntType intType, WifiIotGpioIntPolarity intPolarity) | 设置GPIO引脚中断触发模式 |
查看按键对应的GPIO引脚
F1和F2按键对应的GPIO引脚是分别是GPIO11和GPIO12,通过检查GPIO的电平信号来判断按键的状态。
通过按键控制LED
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void F1_Pressed(char *arg)
{
(void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
}
static void F2_Pressed(char *arg)
{
(void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);
}
static void ButtonExampleEntry(void)
{
GpioInit();
//初始化LED灯
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);
//初始化F1按键,设置为下降沿触发中断
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1_Pressed, NULL);
//初始化F2按键,设置为下降沿触发中断
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2_Pressed, NULL);
}
APP_FEATURE_INIT(ButtonExampleEntry);
按下F1LED灯会打开,按下F2LED灯会熄灭。
GPIO中断扩展实验
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void F1_Pressed(char *arg)
{
(void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
printf("This is F1_Pressed\r\n");
//将GPIO11设置为上升沿
GpioSetIsrMode(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_INT_TYPE_EDGE,WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH);
}
static void F2_Pressed(char *arg)
{
(void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);
printf("This is F2_Pressed\r\n");
//屏蔽GPIO12的中断
GpioSetIsrMask(WIFI_IOT_IO_NAME_GPIO_12,1);
}
static void ButtonExampleEntry(void)
{
GpioInit();
//初始化LED灯
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);
//初始化F1按键,设置为下降沿触发中断
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1_Pressed, NULL);
//初始化F2按键,设置为下降沿触发中断
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2_Pressed, NULL);
}
APP_FEATURE_INIT(ButtonExampleEntry);
按下F1后GPIO11被设置为上升沿,此时按下F1没有输出,抬起F1后输出。(抬起时会出现低电平向高电平跳变,但是这种跳变不稳定,可能会波动多次最终稳定高电平,所以一次抬起会触发多次上升沿,不建议之后做抬起时的上升沿中断。)
按下F2后GPIO12中断被屏蔽,此时再次按下F2无输出。
扩展2
使用电平模式中断时,因为只有引脚处于高/低电平,就会不停引发中断导致系统错误,所以使用时,需要在调用函数中及时屏蔽监听或更换监听,使其不至于一直触发。
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void F1_Pressed_up(char *arg);
static void F1_Pressed_down(char *arg)
{
(void)arg;
GpioUnregisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11);
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
printf("This is F1_Pressed_down\r\n");
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_INT_TYPE_LEVEL,WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH,F1_Pressed_up,NULL);
}
static void F1_Pressed_up(char *arg)
{
(void)arg;
GpioUnregisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11);
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);
printf("This is F1_Pressed\r\n");
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_INT_TYPE_LEVEL,WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,F1_Pressed_down,NULL);
}
static void ButtonExampleEntry(void)
{
//初始化GPIO
GpioInit();
//初始化LED
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_GPIO_DIR_OUT);
//初始化F1按键
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_INT_TYPE_LEVEL,WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,F1_Pressed_down,NULL);
}
APP_FEATURE_INIT(ButtonExampleEntry);
按下后LED亮起,抬起后LED灯熄灭。
PWM输出
wifiiot_pwm.h接口简介
接口名 | 功能 |
---|---|
unsigned int PwmInit (WifiIotPwmPort port) | 初始化PWM |
unsigned int PwmDeinit (WifiIotPwmPort port) | 取消初始化PWM |
unsigned int PwmStart (WifiIotPwmPort port, unsigned short duty, unsigned short freq ) | 根据输入参数输出PWM |
unsigned int PwmStop(WifiIotPwmPort port); | 停止PWM输出 |
PwmInit()
unsigned int PwmInit (WifiIotPwmPort port)
描述:
初始化PWM功能
参数:
名字 | 描述 |
---|---|
id | 表示GPIO引脚号. |
val | 表示要设置的上拉或下拉. |
PwmStart()
unsigned int PwmStart (WifiIotPwmPort port, unsigned short duty, unsigned short freq )
描述:
根据输入参数输出PWM信号。
参数:
名字 | 描述 |
---|---|
port | PWM端口号. |
duty | 占空比. |
freq | 分频倍数. |
输出PWM波来改变LED亮度
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void F1_Pressed_up(char *arg);
static void F1_Pressed_down(char *arg)
{
(void)arg;
GpioUnregisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11);
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
printf("This is F1_Pressed_down\r\n");
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_INT_TYPE_LEVEL,WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH,F1_Pressed_up,NULL);
}
static void F1_Pressed_up(char *arg)
{
(void)arg;
GpioUnregisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11);
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);
printf("This is F1_Pressed\r\n");
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_INT_TYPE_LEVEL,WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,F1_Pressed_down,NULL);
}
static void ButtonExampleEntry(void)
{
//初始化GPIO
GpioInit();
//初始化LED
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_GPIO_DIR_OUT);
//初始化F1按键
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11,WIFI_IOT_INT_TYPE_LEVEL,WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,F1_Pressed_down,NULL);
}
APP_FEATURE_INIT(ButtonExampleEntry);
看到4秒内从暗到明,然后顺间熄灭,重新开始。
更多推荐
所有评论(0)