I/O方式操作GPIO
来源:互联网 发布:阅读刷量软件 编辑:程序博客网 时间:2024/05/21 21:46
/********************************************************
@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;
}
@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;
}
阅读全文
0 0
- I/O方式操作GPIO
- 訪問Super I/O W83627EHF的GPIO
- Android Things:外设I/O接口-GPIO
- Android Things:外设I/O接口-GPIO
- STM32 GPIO I/O Compensation Cell
- I/O控制方式
- I/O 控制方式
- 程序I/O方式
- I/O控制方式
- I/O控制方式
- I/O控制方式
- I/O控制方式
- C++I/O操作
- I/O操作
- I/O操作
- 文件I/O操作
- 文件I/O操作
- 文件I/O操作
- postgresql 数据库的备份与还原
- 微信红包实现原理
- 熊掌号落地页体验规范
- BZOJ3437 小P的牧场 【斜率优化dp】
- 隐藏resent记录,隐藏菜单栏记录
- I/O方式操作GPIO
- 写给未来的我
- 12_Java面向对象_第4天(构造方法、this、super)
- Android 之路9---Java基础5
- Luogu p1605 迷宫
- CGI/FastCgi/PHP-FPM
- macOS10.13.1 上编译wxWidgets
- php函数系列 array函数相关一
- 部署和应用程序没有匹配的安全区域错误