当前位置:网站首页>Implementation of sylixos -- gpiofd interface

Implementation of sylixos -- gpiofd interface

2020-12-08 08:19:45 osc_ 5zaxkz1e

SylixOS I will put GPIO Abstract as device file , The application layer can operate by accessing the device file GPIO. In addition to accessing as files GPIO Outside , The system also encapsulates GpioFd Interface , Simplify the application layer to GPIO The operation of . This article explains GpioFd How the interface is implemented , The implementation code is as follows :

/*********************************************************************************************************
**  The name of the function : gpiofd
**  Function description :  open  gpiofd  file 
**  transport   Enter into   : gpio           gpio  Number 
**           flags           Open the sign  GFD_CLOEXEC / GFD_NONBLOCK
**           gpio_flags     gpio  Attribute flags 
**  transport   Out   : gpiofd  File descriptor 
*********************************************************************************************************/
LW_API  
int  gpiofd (unsigned int gpio, int flags, int gpio_flags)
{
   
   
    INT  iFd;
    INT  iError;
    CHAR cGpioName[MAX_FILENAME_LENGTH];

    flags &= (GFD_CLOEXEC | GFD_NONBLOCK);
    
    snprintf(cGpioName, MAX_FILENAME_LENGTH, "%s/%d", LW_GPIOFD_DEV_PATH, gpio);
    
    iFd = open(cGpioName, O_RDWR | flags);
    if (iFd >= 0) {
   
   
        iError = ioctl(iFd, GPIO_CMD_SET_FLAGS, gpio_flags);
        if (iError < 0) {
   
   
            close(iFd);
            return  (iError);
        }
    }
    
    return  (iFd);
}
/*********************************************************************************************************
**  The name of the function : gpiofd_read
**  Function description :  Read  gpiofd  file 
**  transport   Enter into   : fd         File descriptor 
**           value      Read buffer 
**  transport   Out   : 0 :  success   -1 :  Failure 
*********************************************************************************************************/
LW_API  
int  gpiofd_read (int fd, uint8_t *value)
{
   
   
    return  (read(fd, value, sizeof(uint8_t)) != sizeof(uint8_t) ? PX_ERROR : ERROR_NONE);
}
/*********************************************************************************************************
**  The name of the function : gpiofd_write
**  Function description :  Write  gpiofd  file 
**  transport   Enter into   : fd         File descriptor 
**           value      Write data 
**  transport   Out   : 0 :  success   -1 :  Failure 
*********************************************************************************************************/
LW_API  
int  gpiofd_write (int fd, uint8_t  value)
{
   
   
    return  (write(fd, &value, sizeof(uint8_t)) != sizeof(uint8_t) ? PX_ERROR : ERROR_NONE);
}

  • gpiofd Interface is based on the device file access standard interface (open,close,read,write,ioctl) Realized .
  • The input file opening mode has been adjusted , Retain the GFD_CLOEXEC and GFD_NONBLOCK, Added O_RDWR.

GFD_CLOEXEC            close_on_exec
GFD_NONBLOCK          Non blocking access , There is no blocking when reading and writing data
O_RDWR                        Read and write access

  • take GPIO The number is changed to the corresponding GPIO Device file name , Include file path .
  • If you open it successfully, you can go through ioctl Function settings gpio_flags, standard open Function is not gpio_flags Parametric , So we have to go through ioctl Function settings .
  • Read and write are called directly read,write function , At the same time, set the read-write cache to be fixed to 1 byte .
  • Refer to the implementation code , The application layer can also directly use the device file access standard interface to operate GPIO.

版权声明
本文为[osc_ 5zaxkz1e]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208081900376q.html