OpenHarmony 驱动子系统开发—GPIO
作者:鸿蒙之旅
- 2023-04-28 广东
本文字数:3706 字
阅读完需:约 12 分钟
OpenHarmony 驱动子系统开发—GPIO
概述
GPIO(General-purpose input/output)即通用型输入输出。通常,GPIO 控制器通过分组的方式管理所有 GPIO 管脚,每组 GPIO 有一个或多个寄存器与之关联,通过读写寄存器完成对 GPIO 管脚的操作。
GPIO 接口定义了操作 GPIO 管脚的标准方法集合,包括:
设置管脚方向: 方向可以是输入或者输出(暂不支持高阻态)
读写管脚电平值: 电平值可以是低电平或高电平
设置管脚中断服务函数:设置一个管脚的中断响应函数,以及中断触发方式
使能和禁止管脚中断:禁止或使能管脚中断
GPIO 接口定义在
base/iot_hardware/peripheral/interfaces/kits/iot_gpio.h
文件中
/**
* @addtogroup IotHardware
* @{
*
* @brief Provides APIs for operating devices,
* including flash, GPIO, I2C, PWM, UART, and watchdog APIs.
*
*
*
* @since 2.2
* @version 2.2
*/
/**
* @file iot_gpio.h
*
* @brief Declares functions for operating GPIO devices.
*
* These functions are used for GPIO initialization, input/output settings, and level settings. \n
*
* @since 2.2
* @version 2.2
*/
#ifndef IOT_GPIO_H
#define IOT_GPIO_H
/**
* @brief Enumerates GPIO level values.
*/
typedef enum {
/** Low GPIO level */
IOT_GPIO_VALUE0 = 0,
/** High GPIO level */
IOT_GPIO_VALUE1
} IotGpioValue;
/**
* @brief Enumerates GPIO directions.
*/
typedef enum {
/** Input */
IOT_GPIO_DIR_IN = 0,
/** Output */
IOT_GPIO_DIR_OUT
} IotGpioDir;
/**
* @brief Enumerates GPIO interrupt trigger modes.
*/
typedef enum {
/** Level-sensitive interrupt */
IOT_INT_TYPE_LEVEL = 0,
/** Edge-sensitive interrupt */
IOT_INT_TYPE_EDGE
} IotGpioIntType;
/**
* @brief Enumerates I/O interrupt polarities.
*/
typedef enum {
/** Interrupt at a low level or falling edge */
IOT_GPIO_EDGE_FALL_LEVEL_LOW = 0,
/** Interrupt at a high level or rising edge */
IOT_GPIO_EDGE_RISE_LEVEL_HIGH
} IotGpioIntPolarity;
/**
* @brief Indicates the GPIO interrupt callback.
*
*/
typedef void (*GpioIsrCallbackFunc) (char *arg);
/**
* @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);
/**
* @brief Deinitializes a GPIO device.
*
* @param id Indicates the GPIO pin number.
* @return Returns {@link IOT_SUCCESS} if the GPIO device is deinitialized;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTGpioDeinit(unsigned int id);
/**
* @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);
/**
* @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);
/**
* @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);
/**
* @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);
/**
* @brief Obtains the input level value of a GPIO pin.
*
* @param id Indicates the GPIO pin number.
* @param val Indicates the pointer to the input level value.
* @return Returns {@link IOT_SUCCESS} if the input 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 IoTGpioGetInputVal(unsigned int id, IotGpioValue *val);
/**
* @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);
/**
* @brief Disables the interrupt feature for a GPIO pin.
*
* @param id Indicates the GPIO pin number.
* @return Returns {@link IOT_SUCCESS} if the interrupt feature is disabled;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTGpioUnregisterIsrFunc(unsigned int id);
/**
* @brief Masks the interrupt feature for a GPIO pin.
*
* @param id Indicates the GPIO pin number.
* @param mask Indicates whether the interrupt function is masked.
* The value <b>1</b> means to mask the interrupt function, and <b>0</b> means not to mask the interrupt function.
* @return Returns {@link IOT_SUCCESS} if the interrupt feature is masked;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTGpioSetIsrMask(unsigned int id, unsigned char mask);
/**
* @brief Sets the interrupt trigger mode of a GPIO pin.
*
* This function configures a GPIO pin based on the interrupt type and interrupt polarity.
*
* @param id Indicates the GPIO pin number.
* @param intType Indicates the interrupt type.
* @param intPolarity Indicates the interrupt polarity.
* @return Returns {@link IOT_SUCCESS} if the interrupt trigger mode 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 IoTGpioSetIsrMode(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity);
#endif
/** @} */
复制代码
接口说明
使用指导
使用流程
GPIO 标准 API 通过 GPIO 管脚号来操作指定管脚,使用 GPIO 的一般流程如下所示。
划线
评论
复制
发布于: 2023-04-28阅读数: 84
版权声明: 本文为 InfoQ 作者【鸿蒙之旅】的原创文章。
原文链接:【http://xie.infoq.cn/article/d202388bdf6162f84a319cfd9】。文章转载请联系作者。
鸿蒙之旅
关注
还未添加个人签名 2022-10-26 加入
还未添加个人简介
评论