The driver layer using the GPIO

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ScilogyHunter/article/details/100164562

Usage scenarios

In addition to using GPIO in the application layer, the fact is to call in other more complex driving. Such as SD card driver to be used to monitor the GPIO insert a card and the extractor, the card requires the use of a GPIO driver to control the phy chip hard reset, some cases need to simulate GPIO like the I2C bus. When the drive is used GPIO, in order to improve efficiency is not calling GPIO device file, but the use of GPIO call interface SylixOS provided.

GPIO API

/*********************************************************************************************************
  GPIO API (以下 API 仅供驱动程序内部使用, 应用程序不可使用)
*********************************************************************************************************/
LW_API VOID             API_GpioInit(VOID);
LW_API INT              API_GpioChipAdd(PLW_GPIO_CHIP pgchip);
LW_API INT              API_GpioChipDelete(PLW_GPIO_CHIP pgchip);
LW_API PLW_GPIO_CHIP    API_GpioChipFind(PVOID pvData, BOOL (*pfuncMatch)(PLW_GPIO_CHIP pgchip, PVOID  pvData));                                
LW_API INT              API_GpioIsValid(UINT uiGpio);
LW_API INT              API_GpioHasDrv(UINT uiGpio);
LW_API INT              API_GpioRequest(UINT uiGpio, CPCHAR pcLabel);
LW_API INT              API_GpioRequestOne(UINT uiGpio, ULONG ulFlags, CPCHAR pcLabel);
LW_API VOID             API_GpioFree(UINT uiGpio);
LW_API INT              API_GpioRequestArray(PLW_GPIO pgArray, size_t stNum);
LW_API VOID             API_GpioFreeArray(PLW_GPIO pgArray, size_t stNum);
LW_API INT              API_GpioGetFlags(UINT uiGpio, ULONG *pulFlags);
LW_API VOID             API_GpioOpenDrain(UINT uiGpio, BOOL bOpenDrain);
LW_API VOID             API_GpioOpenSource(UINT uiGpio, BOOL bOpenSource);
LW_API INT              API_GpioSetDebounce(UINT uiGpio, UINT uiDebounce);
LW_API INT              API_GpioSetPull(UINT uiGpio, UINT uiType);
LW_API INT              API_GpioDirectionInput(UINT uiGpio);
LW_API INT              API_GpioDirectionOutput(UINT uiGpio, INT iValue);
LW_API INT              API_GpioGetValue(UINT uiGpio);
LW_API VOID             API_GpioSetValue(UINT uiGpio, INT iValue);
LW_API ULONG            API_GpioGetIrq(UINT uiGpio, BOOL bIsLevel, UINT uiType);
LW_API ULONG            API_GpioSetupIrq(UINT uiGpio, BOOL bIsLevel, UINT uiType);
LW_API VOID             API_GpioClearIrq(UINT uiGpio);
LW_API irqreturn_t      API_GpioSvrIrq(UINT uiGpio);

While the system also for these API functions have been defined macros

#define gpioInit                API_GpioInit
#define gpioChipAdd             API_GpioChipAdd
#define gpioChipDelete          API_GpioChipDelete
#define gpioChipFind            API_GpioChipFind
#define gpioIsValid             API_GpioIsValid
#define gpioHasDrv              API_GpioHasDrv
#define gpioRequest             API_GpioRequest
#define gpioRequestOne          API_GpioRequestOne
#define gpioFree                API_GpioFree
#define gpioRequestArray        API_GpioRequestArray
#define gpioFreeArray           API_GpioFreeArray
#define gpioGetFlags            API_GpioGetFlags
#define gpioOpenDrain           API_GpioOpenDrain
#define gpioOpenSource          API_GpioOpenSource
#define gpioSetDebounce         API_GpioSetDebounce
#define gpioSetPull             API_GpioSetPull
#define gpioDirectionInput      API_GpioDirectionInput
#define gpioDirectionOutput     API_GpioDirectionOutput
#define gpioGetValue            API_GpioGetValue
#define gpioSetValue            API_GpioSetValue
#define gpioGetIrq              API_GpioGetIrq
#define gpioSetupIrq            API_GpioSetupIrq
#define gpioClearIrq            API_GpioClearIrq
#define gpioSvrIrq              API_GpioSvrIrq

Common Interface

GPIO is generally used to read or control the input level at the output level, not too complicated operation, only the following four functions can be invoked.

/*********************************************************************************************************
** 函数名称: API_GpioRequestOne
** 功能描述: 请求使用一个 GPIO
** 输 入  : uiGpio        请求 GPIO 号
**           ulFlags       需要设置的属性
**           pcLabel       标签
** 输 出  : ERROR_NONE or PX_ERROR
*********************************************************************************************************/
LW_API INT  API_GpioRequestOne(UINT uiGpio, ULONG ulFlags, CPCHAR pcLabel);
/*********************************************************************************************************
** 函数名称: API_GpioGetValue
** 功能描述: 获取指定 GPIO 的值
** 输 入  : uiGpio        GPIO 号
** 输 出  : GPIO 当前电平状态 1: 高电平 0: 低电平
** 注  意  : 此函数不做任何参数有效性判断, 所以用户必须保证 uiGpio 已经请求成功, 方向与其他参数设置正确.
*********************************************************************************************************/
LW_API INT  API_GpioGetValue(UINT uiGpio);
/*********************************************************************************************************
** 函数名称: API_GpioSetValue
** 功能描述: 设置指定 GPIO 的值
** 输 入  : uiGpio        GPIO 号
**           iValue        1: 高电平 0: 低电平
** 输 出  : NONE
** 注  意  : 此函数不做任何参数有效性判断, 所以用户必须保证 uiGpio 已经请求成功, 方向与其他参数设置正确.
*********************************************************************************************************/
LW_API VOID  API_GpioSetValue(UINT uiGpio, INT iValue);
/*********************************************************************************************************
** 函数名称: API_GpioFree
** 功能描述: 释放使用一个 GPIO
** 输 入  : uiGpio        GPIO 号
** 输 出  : NONE
*********************************************************************************************************/
LW_API VOID   API_GpioFree(UINT uiGpio);

These interfaces are API can only use the internal driver in the kernel mode, user mode application are not available. Before use need to include "SylixOS.h" header file.
First call API_GpioRequestOnefunction, turn on and follow the ulFlagsGPIO initialization parameters. Other functions can be called only after the interface opens GPIO. The GPIO can later be input or output, respectively, to call mode API_GpioGetValueor API_GpioSetValuefunction is operated. If the GPIO no longer in use, need to call API_GpioFreethe function to be released.
ulFlagsParameters and gpiofdfunctions flagssimilar to the parameters used to configure some GPIO related properties. Specific options are as follows:

#define LW_GPIOF_DIR_OUT                (0 << 0)
#define LW_GPIOF_DIR_IN                 (1 << 0)
#define LW_GPIOF_INIT_LOW               (0 << 1)
#define LW_GPIOF_INIT_HIGH              (1 << 1)
#define LW_GPIOF_IN                     (LW_GPIOF_DIR_IN)
#define LW_GPIOF_OUT_INIT_LOW           (LW_GPIOF_DIR_OUT | LW_GPIOF_INIT_LOW)
#define LW_GPIOF_OUT_INIT_HIGH          (LW_GPIOF_DIR_OUT | LW_GPIOF_INIT_HIGH)
#define LW_GPIOF_OPEN_DRAIN             (1 << 2)
#define LW_GPIOF_OPEN_SOURCE            (1 << 3)

Demonstration routine

/*********************************************************************************************************
**
**                                    中国软件开源组织
**
**                                   嵌入式实时操作系统
**
**                                SylixOS(TM)  LW : long wing
**
**                               Copyright All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: gpioDrvExample1.c
**
** 创   建   人: Hou.JinYu (侯进宇)
**
** 文件创建日期: 2017 年 12 月 13 日
**
** 描        述: gpio 在驱动中的调用例程,针对NXP i.MX-RT1050 EVK开发版。
**               因为连接板载 LED01 的引脚同时也连接了 enet 的复位脚,硬件冲突故不能使用。
**               LED02 只是一个空闲的gpio(为J22.7),需要用户连接LED或万用表来检测电平变化。
*********************************************************************************************************/
#define __SYLIXOS_KERNEL
#include "stdio.h"
#include "SylixOS.h"
#include "gpio/gpio.h"
/*********************************************************************************************************
   引脚宏定义
 ********************************************************************************************************/
#define KEY08        GPIO_E_00                                          /*  GPIO5--00                   */
#define LED01        GPIO_A_09                                          /*  GPIO1--09                   */
#define LED02        GPIO_A_18                                          /*  GPIO1--18                   */
/*********************************************************************************************************
** 函数名称: gpioDrvExample1
** 功能描述: gpio 轮询读取例程
** 输    入: NONE
** 输    出: ERROR_CODE
*********************************************************************************************************/
INT  gpioDrvExample1 (VOID)
{
    UINT8  ucValue;

    printf("Gpio poll example. Waiting press the key SW8.\n");

    API_GpioRequestOne(KEY08, LW_GPIOF_IN, "KEY08");
    API_GpioRequestOne(LED02, LW_GPIOF_OUT_INIT_LOW, "LED02");

    while (1) {
        ucValue = API_GpioGetValue(KEY08);                              /*  读取当前按键值              */
        printf("The key value = %d\n", ucValue);
        API_GpioSetValue(LED02, ucValue);                               /*  设置LED引脚输出             */
        sleep(1);
    }

    API_GpioFree(KEY08);
    API_GpioFree(LED02);

    return  (ERROR_NONE);
}
/*********************************************************************************************************
  END
*********************************************************************************************************/

And achieve the effect of " used in the application layer GPIO as" the gpioExample1 routine.

Guess you like

Origin blog.csdn.net/ScilogyHunter/article/details/100164562