/*
* \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
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