GPIO used in the application layer

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/100108086

Interface Description

The GPIO (General Purpose the Input / the Output) GPIOs i.e., hereinafter referred to as I / O ports. I / O ports provide input, output or interrupt the three types of functions embedded field is the most common and basic hardware.

GPIO in the introduction SylixOS in front of the driver how to write, we start to understand how to call the next application layer is the GPIO, the first will be followed by, in order to better drive to develop and test. For the application layer, GPIO is /dev/gpiofda device file directory through standard file functions can operate. In view of the inherent characteristics of GPIO used, and to simplify the interface, SylixOS package further standard functions obtained three simple file operation GPIO function, still use file closing closefunction.

/*********************************************************************************************************
** 函数名称: gpiofd
** 功能描述: 打开 gpiofd 文件
** 输 入  : gpio           gpio 号
**           flags          打开标志 GFD_CLOEXEC / GFD_NONBLOCK
**           gpio_flags     gpio 属性标志
** 输 出  : gpiofd 文件描述符
*********************************************************************************************************/
int gpiofd(unsigned int gpio, int flags, int gpio_flags);
/*********************************************************************************************************
** 函数名称: gpiofd_read
** 功能描述: 读取 gpiofd 文件
** 输 入  : fd        文件描述符
**           value     读取缓冲
** 输 出  : 0 : 成功  -1 : 失败
*********************************************************************************************************/
int gpiofd_read(int fd, uint8_t *value);
/*********************************************************************************************************
** 函数名称: gpiofd_write
** 功能描述: 写 gpiofd 文件
** 输 入  : fd        文件描述符
**           value     写入数据
** 输 出  : 0 : 成功  -1 : 失败
*********************************************************************************************************/
int gpiofd_write(int fd, uint8_t  value);
  • Use these three operations functions you will need to include the header file <sys/gpiofd.h>.
  • Function is gpiofdused to open a GPIO device file, but the argument gpiois not a file name but figures. To improve readability GPIO figures can be represented by the macro definition, another blog " SylixOS GPIO number in the macro definition ," and explained in detail to achieve.
  • Parameter flagsand openthe second parameter function similar meaning, i.e., may be O_RDONLY,O_RDWR,O_RDWRlike, but are in fact in accordance with the internal O_RDWRparameters of the operation;
  • Parameters gpio_flagsassociated with the identification GPIO features, which may be identified by a combination of a plurality of bits. Refer to the following table:
Bit identifier name Explanation
GPIO_FLAG_DIR_OUT To set the output GPIO
GPIO_FLAG_DIR_IN Set GPIO input function
GPIO_FLAG_IN The same GPIO_FLAG_DIR_IN
GPIO_FLAG_OUT_INIT_LOW GPIO function to set the output, while output low initialization
GPIO_FLAG_OUT_INIT_HIGH GPIO output is set, and outputs a high level to initialize
GPIO_FLAG_OPEN_DRAIN Set GPIO output drain output mode
GPIO_FLAG_OPEN_SOURCE GPIO output is provided to a source output mode
GPIO_FLAG_PULL_DEFAULT Using the default pull-up / pull-down mode
GPIO_FLAG_PULL_UP A pullup resistor pattern
GPIO_FLAG_PULL_DOWN Mode using pull-down resistor
GPIO_FLAG_PULL_DISABLE Prohibit pull-up / pull-down mode
GPIO_FLAG_TRIG_FALL Set GPIO interrupt function, and the falling edge-triggered interrupts
GPIO_FLAG_TRIG_RISE Set GPIO interrupt function, and Interrupt on rising edge
GPIO_FLAG_TRIG_LEVEL Set GPIO interrupt function, and level-triggered interrupts

To use the GPIO interrupt function, first set it when you open the device gpio_flagsparameters in response to the interrupt mode. Secondly because GPIO devices are non-blocking file read and write, but also with selectfunction blocking and timeout function. When a GPIO with interrupt function generates an interrupt, the kernel wakes up all waiting threads by calling the select state of the GPIO readable file descriptor, notification thread interrupt is generated. When the select returns properly, threading corresponding transaction, which is similar to the completion of a service interruption.
Note that when using the GPIO_FLAG_TRIG_LEVEtime identified, we use only GPIO_FLAG_TRIG_FALLthe GPIO_FLAG_TRIG_RISEuse of a combination with, respectively, low and high level triggering. When not used GPIO_FLAG_TRIG_LEVE, we can GPIO_FLAG_TRIG_FALLand GPIO_FLAG_TRIG_RISEin combination represent a double-edge triggered.

Complete routine

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

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

    keyfd = gpiofd(KEY08, O_RDWR, GPIO_FLAG_IN);                        /*  打开 KEY 的 GPIO 文件       */
    if (keyfd < 0) {
        printf("open gpio %d failed!\n", KEY08);
        return  (PX_ERROR);
    }

    ledfd = gpiofd(LED02, O_RDWR, GPIO_FLAG_OUT_INIT_LOW);              /*  打开 LED 的 GPIO 文件       */
    if (ledfd < 0) {
        printf("open gpio %d failed!\n", LED02);
        close(keyfd);
        return  (PX_ERROR);
    }

    while (1) {
        gpiofd_read(keyfd, &value);                                     /*  读取当前按键值              */
        printf("The key value = %d\n", value);
        gpiofd_write(ledfd, value & 0x01);                              /*  设置LED引脚输出             */
        sleep(1);
    }
    close(keyfd);                                                       /*  关闭 KEY 的 GPIO 文件       */
    close(ledfd);                                                       /*  关闭 LED 的 GPIO 文件       */

    return  (ERROR_NONE);
}
SHELL_CMD_REG("gpioExample1", gpioExample1);
/*********************************************************************************************************
** 函数名称: gpioExample2
** 功能描述: gpio 中断例程
** 输    入: NONE
** 输    出: ERROR_CODE
*********************************************************************************************************/
static  int  gpioExample2 (void)
{
    int      n = 0;
    fd_set   fdset;
    int      ret;
    int      keyfd;
    int      ledfd;

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

    keyfd = gpiofd(KEY08, O_RDWR, GPIO_FLAG_TRIG_FALL);                 /*  打开 KEY 的 GPIO 文件       */
    if (keyfd < 0) {
        printf("open gpio %d failed!\n", KEY08);
        return  (PX_ERROR);
    }

    ledfd = gpiofd(LED02, O_RDWR, GPIO_FLAG_OUT_INIT_LOW);              /*  打开 LED 的 GPIO 文件       */
    if (ledfd < 0) {
        printf("open gpio %d failed!\n", LED02);
        close(keyfd);
        return  (PX_ERROR);
    }

    FD_ZERO(&fdset);

    while (1) {
        FD_SET(keyfd, &fdset);
        ret = select(keyfd + 1, &fdset, NULL, NULL, NULL);              /*  调用 select 等待按键被按下  */
        if (ret == 1) {
            printf("The key effective, n = %d\n", n++);
            gpiofd_write(ledfd, n & 0x01);                              /*  设置LED引脚输出             */
        } else if (ret < 0) {
            printf("select error!\n");
            break;
        }
    }

    close(keyfd);                                                       /*  关闭 KEY 的 GPIO 文件       */
    close(ledfd);                                                       /*  关闭 LED 的 GPIO 文件       */

    return  (ERROR_NONE);
}
SHELL_CMD_REG("gpioExample2", gpioExample2);
/*********************************************************************************************************
  END
*********************************************************************************************************/

Experimental Procedure

1. The input command gpioExample1, to start a query routine gpio;
2. The console output once per second key value, press the button to see the key value becomes zero. LED pin also changes the key state is changed in accordance with.
Here Insert Picture Description
3. The system reset input command gpioExample2to start the interrupt routine gpio;
4. Press the button triggers the falling edge of the interrupt, so that a wake-select function. Each press of the push button will print out the number, and the LED changes again.
Here Insert Picture Description

Guess you like

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