I/O方式操作GPIO

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34770510/article/details/78657731
 / ********************************************************
@function:  对GPIO操作,依次是GPIO_EXPORT 、GPIO_DIRECTION
          GPIO_VALUE  、  GPIO_UNEXPORT
@author:    caoyang
@date:       2017.11.28
@blog:       https://cy-hello.github.io/
*********************************************************/

#include <pthread.h>
#include <unistd.h>
#include "dev_so.h"


/ ********用来定义led对应的GPIO的编号和状态 ************/
#define GPIO_39    39
#define GPIO_40    40
#define GPIO_41    41
#define GPIO_42    42

#define GPIO_VALUE_ON      1   
#define GPIO_VALUE_OFF     0  
#define GPIO_STATUS_IN   "in"
#define GPIO_STATUS_OUT  "out"


extern DEV_TEST_GLOBAL_DATA_T stGlobalData;


/ ********对/sys/class/gpio/export 操作 *********************/
COMMON_ERROR_ENUM GPIO_EXPORT(int CPIO_N)
{
    /*system("echo "GPIO_N" > /sys/class/gpio/export");*/

    int iLengthOfSprintf;
    int iFileDescriptor;
    char acBuffer_Of_GPIO[64];

    debug_msg("[%s][%d][Show Info] 0000", __func__, __LINE__);
    iFileDescriptor = open("/sys/class/gpio/export",O_WRONLY);
    if(iFileDescriptor < 0)
    {    
        debug_msg("[%s][%d][Show Info] open /sys/class/gpio/export failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }

    debug_msg("[%s][%d][Show Info] 0001", __func__, __LINE__);
    
    
    iLengthOfSprintf = sprintf(acBuffer_Of_GPIO,"%d", CPIO_N);
    debug_msg("[%s][%d][Show Info] 0001:%d,%s", __func__, __LINE__,iLengthOfSprintf,acBuffer_Of_GPIO);
    if(write(iFileDescriptor,acBuffer_Of_GPIO,iLengthOfSprintf) == -1)
    {
        debug_msg("[%s][%d][Show Info] write /sys/class/gpio/export failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }
    
    debug_msg("[%s][%d][Show Info] 0002", __func__, __LINE__);
    
    close(iFileDescriptor);
    return CommonError_OK;
}

COMMON_ERROR_ENUM GPIO_DIRECTION(int CPIO_N,char *GPIO_status)
{
    //"echo out > /sys/class/gpio/gpio"#GPIO_N"/direction";
    int iFileDescriptor;
    int iLengthOfSprintf;
    char acEchoStatus[64];
    char acPathOfGPIO[64];
    

    sprintf(acPathOfGPIO,  "/sys/class/gpio/gpio%d/direction", CPIO_N);
    iFileDescriptor = open(acPathOfGPIO,O_WRONLY);
    if(iFileDescriptor < 0)
    {
        debug_msg("[%s][%d][Show Info] open /sys/class/gpio/gpioX/direction failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }

    debug_msg("[%s][%d][Show Info] 1000", __func__, __LINE__);

    
    iLengthOfSprintf = sprintf(acEchoStatus,"%s",GPIO_status);
    debug_msg("[%s][%d][Show Info] 1500:%d,%s", __func__, __LINE__,iLengthOfSprintf,acEchoStatus);
    
    if(write(iFileDescriptor,acEchoStatus,iLengthOfSprintf) == -1)
    {
        debug_msg("[%s][%d][Show Info] write /sys/class/gpio/gpioX/direction failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }

    debug_msg("[%s][%d][Show Info] 2000", __func__, __LINE__);
    
    return CommonError_OK;
}

COMMON_ERROR_ENUM GPIO_VALUE(int CPIO_N,int GPIO_VALUE)
{
    
    int iFileDescriptor;
    int iLengthOfSprintf;
    char acEchoValue[64];
    char acPathOfGPIO[64];
    

    sprintf(acPathOfGPIO,  "/sys/class/gpio/gpio%d/value", CPIO_N);
    iFileDescriptor = open(acPathOfGPIO,O_WRONLY);
    if(iFileDescriptor < 0)
    {
        debug_msg("[%s][%d][Show Info] open /sys/class/gpio/gpioX/value failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }

    debug_msg("[%s][%d][Show Info] 4000", __func__, __LINE__);


    iLengthOfSprintf = sprintf(acEchoValue,"%d",GPIO_VALUE);
    debug_msg("[%s][%d][Show Info] 1500:%d,%s", __func__, __LINE__,iLengthOfSprintf,acEchoValue);
    if(write(iFileDescriptor,acEchoValue,iLengthOfSprintf) < 0)
    {
        debug_msg("[%s][%d][Show Info] write /sys/class/gpio/gpioX/value failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }

    debug_msg("[%s][%d][Show Info] 5000", __func__, __LINE__);
    
    return CommonError_OK;
}

COMMON_ERROR_ENUM GPIO_UNEXPORT(int CPIO_N)
{
    /*system("echo "GPIO_N" > /sys/class/gpio/export");*/

    int iLengthOfSprintf;
    int iFileDescriptor;
    char acBuffer_Of_GPIO_Number[64];
    
    iFileDescriptor = open("/sys/class/gpio/unexport",O_WRONLY);
    if(iFileDescriptor < 0)
    {    
        debug_msg("[%s][%d][Show Info] open /sys/class/gpio/unexport failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }

    iLengthOfSprintf = sprintf(acBuffer_Of_GPIO_Number, "%d", CPIO_N);
    if(write(iFileDescriptor,acBuffer_Of_GPIO_Number,iLengthOfSprintf) < 0)
    {
        debug_msg("[%s][%d][Show Info] write /sys/class/gpio/unexport failed", __func__, __LINE__);
        return CommonError_SysCallError;
    }

    close(iFileDescriptor);
    return CommonError_OK;
}


COMMON_ERROR_ENUM led_test(DEV_TEST_INFO_T *pstDevTestInfo)
{    
    /*控制gpio命令
    "echo "#N" > /sys/class/gpio/export";
    "echo out > /sys/class/gpio/gpio"#GPIO_N"/direction";
    "echo "#0_OR_1"/sys/class/gpio/"#GPIO_N"/value";
    "echo "#GPIO_N"/sys/class/gpio/unexport"
    */
    if(NULL == pstDevTestInfo)
    {
        debug_msg("[%s][%d][Show Info] led_test is error", __func__, __LINE__);
        return CommonError_NullPointer;
    }
    
    
    GPIO_EXPORT(GPIO_42);
    GPIO_DIRECTION(GPIO_42,GPIO_STATUS_OUT);
    GPIO_VALUE(GPIO_42,GPIO_VALUE_ON);
    GPIO_UNEXPORT(GPIO_42);

    return CommonError_OK;
}

猜你喜欢

转载自blog.csdn.net/qq_34770510/article/details/78657731