GPIO读写

/* 
 * \file        gpio_test.c 
 * LIS3LV02DQ 
 * 
 * \version     1.0.0 
 * \date        2014年03月10日 
 * \author      jiangdou  <jiangdouu88@126.com> 
 * 
 * Copyright (c) 2014 jiangdou. All Rights Reserved. 
 * 
 *
dou@ubuntu:~/a10/linshi_chenxu$ /home/dou/a10/a20/A20-420-V12/lichee/out/linux/common/buildroot/external-toolchain/bin/arm-linux-gnueabi-gcc -o gpio gpio_test.c -static

*/
 

#include <stdio.h>    
#include <linux/types.h>    
#include <fcntl.h>    
#include <unistd.h>    
#include <stdlib.h>    
#include <sys/types.h>    
#include <sys/ioctl.h>    
#include <errno.h>    
#include <assert.h>    
#include <string.h>    



static const char *dev_data = "/sys/class/gpio_sw/PH19/data";		//raed write data
static const char *dev_mul_sel = "/sys/class/gpio_sw/PH19/mul_sel";	//0 is in,and 1 is out
static const char *dev_pull = "/sys/class/gpio_sw/PH19/pull";		//GPIO的内置电阻状态 0代表高阻,1代表上拉,2代表下拉
static const char *dev_drv_level = "/sys/class/gpio_sw/PH19/drv_level";	//*GPIO的驱动能力 有0到3四个等级


int gpio_init(void)
{
	int fd3;
	int ret = 0;
	FILE *fd1 = fopen(dev_mul_sel, "w");
	if (fd1 < 0)
		printf("can't open device\n\r");
	
	fprintf(fd1,"0");					//设置为输入,6为中断模式
	fclose(fd1);
	FILE *fd2 = fopen(dev_pull, "w");
	if (fd2 < 0)
		printf("can't open device\n\r");
	
	fprintf(fd2,"1");			//端口上拉
	fclose(fd2);
	
	
	return 0;
}




int read_gpio(void)
{
#if 0
	int value;
	char *valueStr;
	memset(valueStr, 0,sizeof(valueStr));
	
	read(file, valueStr, 1);
	printf("valueStr 1 is:  %02x \n\r",valueStr);
	value = (int)valueStr;
	value = value & 0x01;
	printf("value is:  %02x \n\r",value);
#endif

	int value;
	
	char valueStr[32];

	int fd3 = open(dev_data, O_RDONLY);
		if (fd3 < 0)
		{
			printf("can't open device33\n\r");
			close(fd3);
			return -1;
		}
		
		
		//char valueStr[32];
		memset(valueStr, 0,sizeof(valueStr));
		read(fd3, (void*)valueStr, sizeof(valueStr)-1);
		char *end;
		printf("valueStr 1 is:  %02x \n\r",valueStr);
		value =strtol(valueStr, &end, 0);
	
		if(end == valueStr){
			printf("Fail inconvert string %s to number.", valueStr);
		close(fd3);
		return -1;
		}
	close(fd3);

	return value;

}


int main(void)
{
	printf("mcp2515 in\n\r");
	
	int fdo;
	int value;
	unsigned char temp = 0;
	int a;
	gpio_init();
	while(1)
	{	
		int a = read_gpio();
		
		//printf("a is %d\n\r",a);
		
		switch (a) 
		{	
		case 0:
			printf("mcp2515 receive data\n\r");
			break;
		default:
			break;
		
		}
			
	usleep(8000);
	
	}


return 0;

}




///

#if 0

static int readGpio_native(JNIEnv *env, jobject clazz, jstring path)
{
	int fd;
	int value;
	if (path == NULL)
	{
		return -1;
	}
	else
	{
		const char *chars =env->GetStringUTFChars(path, NULL);
		fd = open(chars,O_RDWR);
	if(fd < 0){
		LOGE("fail inopen file %s", chars);
			env->ReleaseStringUTFChars(path, chars);
		return -1;
		}
	char valueStr[32];
	memset(valueStr, 0,sizeof(valueStr));
	read(fd, (void*)valueStr, sizeof(valueStr)-1);
		//LOGD("value str= %s", valueStr);
	char *end;
	value =strtol(valueStr, &end, 0);
	
	if(end == valueStr){
		LOGE("Fail inconvert string %s to number.", valueStr);
		env->ReleaseStringUTFChars(path,chars);
		close(fd);
		return -1;
	}
	return value;
	}
}

#endif




a20   gpio_test

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/**
* Here we use the GPIO expansion near the SATA connector, from PD1 ~ PD7
* */
#define MIN_PIN 1
#define MAX_PIN 7

/**
* Binary value that GPIO pins could accept 
* */
typedef enum{
    LOW=0,
    HIGH=1
}value_t;

/**
* GPIO pins get/set direction
* */
typedef enum{
   IN=0,
   OUT=1
}direction_t;

FILE* value_fp[MAX_PIN+1];

/**
* @brief Initialize the GPIO ports' value file pointers
* 
* */
void gpio_init()
{
    int i;
    for(i=MIN_PIN;i<=MAX_PIN;i++)
        value_fp[i]=NULL;
}

/**
* @brief Uninitialize the GPIO ports, unexport those exported and close files.
* 
* */
int gpio_uninit()
{
    int i;
    FILE *unexport_fp=fopen("/sys/class/gpio/unexport","w");
    if(!unexport_fp) return -1;

    for(i=MIN_PIN;i<=MAX_PIN;i++){
        if(value_fp[i]){
            fclose(value_fp[i]);
            fprintf(unexport_fp,"%d",i);
            fflush(unexport_fp);
        }    
    }

    fclose(unexport_fp);

    return 0;
}

/**
* @brief Set the GPIO pins to specified direction
* 
* @param pin: GPIO pin to set direction
* @param dir: IN or OUT
* 
* */
int pinMode(int pin, direction_t dir)
{
    char direction_file[64];
    char value_file[64];

    FILE *export_fp=fopen("/sys/class/gpio/export","w");
    if(!export_fp) return -1;
    
    fprintf(export_fp,"%d",pin);
    sleep(1);
    fclose(export_fp);

    sprintf(direction_file,"/sys/class/gpio/gpio%d_pd%d/direction", pin, pin);
    FILE *direction_fp=fopen(direction_file,"w");
    if(!direction_fp) return -2;

    if(dir==OUT){
        fprintf(direction_fp,"out");
    }else fprintf(direction_fp,"in");

    fclose(direction_fp);

    sprintf(value_file,"/sys/class/gpio/gpio%d_pd%d/value", pin, pin);
    value_fp[pin]=fopen(value_file,"w");

    if(!value_fp[pin]) return -3;


    return 0;

}

/**
* @brief Set the GPIO pins to specified binary value
* 
* @param pin: GPIO pin to set value
* @param value: LOW or HIGH
* 
* */
int digitalWrite(int pin, value_t value)
{

    if(!value_fp[pin])return -1;

    if(pin<MIN_PIN) pin=MIN_PIN;
    if(pin>MAX_PIN) pin=MAX_PIN;

    fprintf(value_fp[pin],"%d", value);
    fflush(value_fp[pin]);

    return 0;
}

/**
* @brief A demo to alternately set two GPIO pin's power level (LOW or HIGH), 
*       to flash a LED connected between the two pins. 
* */
int main()
{
    gpio_init();

    pinMode(1,OUT);
    pinMode(2,OUT);

    int i;
    for(i=0;i<10;i++){
        digitalWrite(1,HIGH);
        digitalWrite(2,LOW);
        sleep(1);

        digitalWrite(1,LOW);
        digitalWrite(2,HIGH);
        sleep(1);
    }

    gpio_uninit();
}


修改script.bin

1,挂载nanda 分区
  # mount  /dev/nanda  /mnt/nanda

2  # cd /mnt/nanda/   

3  转换  # bin2fex  script.bin > script.fex

4, 修改  # vi   script.fex

5,转换 # fex2bin   script.fex  > script.bin

6 同步一下  #sync

7 卸载  # umount  /mnt/nanda 




vvv



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 AXI GPIO 读写例子: ```c #include <stdio.h> #include <stdlib.h> #include "xparameters.h" #include "xgpio.h" #include "xstatus.h" #define GPIO_DEVICE_ID XPAR_AXI_GPIO_0_DEVICE_ID XGpio Gpio; /* The Instance of the GPIO Driver */ int main(void) { int Status; u32 DataRead; u32 DataWrite = 0x55; /* Initialize the GPIO driver */ Status = XGpio_Initialize(&Gpio, GPIO_DEVICE_ID); if (Status != XST_SUCCESS) { printf("GPIO initialization failed\n"); exit(EXIT_FAILURE); } /* Set the direction of the GPIO pins */ XGpio_SetDataDirection(&Gpio, 1, 0x00); /* Set as input */ XGpio_SetDataDirection(&Gpio, 2, 0xFF); /* Set as output */ /* Write to the GPIO pins */ XGpio_DiscreteWrite(&Gpio, 2, DataWrite); /* Read from the GPIO pins */ DataRead = XGpio_DiscreteRead(&Gpio, 1); printf("GPIO data read: 0x%x\n", DataRead); return 0; } ``` 在此例子中,我们使用 Xilinx 提供的 AXI GPIO 驱动程序来进行读写操作。首先,我们需要定义一个 `XGpio` 结构体以及一个设备 ID 变量 `GPIO_DEVICE_ID`,它们分别表示 GPIO 的实例和设备 ID。在 `main` 函数中,我们使用 `XGpio_Initialize` 函数来初始化 GPIO 实例。然后,使用 `XGpio_SetDataDirection` 函数来设置 GPIO 管脚的方向,这里我们将管脚 1 设置为输入,管脚 2 设置为输出。接着,我们使用 `XGpio_DiscreteWrite` 函数将数据写入 GPIO 管脚 2 中,使用 `XGpio_DiscreteRead` 函数从 GPIO 管脚 1 中读取数据。最后,我们将读取到的数据打印到控制台上。 需要注意的是,这个例子仅仅是一个简单的演示,实际应用中需要根据具体的需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值