eeprom应用程序

来源:互联网 发布:php移动端源码 编辑:程序博客网 时间:2024/05/03 21:30

磨蹭了这么久。。写了一个相对看的过去的eeprom的应用程序。。

板子是fl2440。。跑的是Linux3.0。。主机是centos6.5。。

eeprom.h头文件

14 #include<stdio.h>
 15 #include<string.h>
 16 #include<unistd.h>
 17 #include<sys/ioctl.h>
 18 #include<stdlib.h>
 19 #include<fcntl.h>
 20 #include<sys/io.h>
 21 #include <getopt.h>
 22 
 23 #define SN_LEN     12 //定义的sn数据的长度
 24 #define MAC_LEN    17 //定义的mac数据的长度
 25 #define OWN_LEN    20 //定义的own数据的长度
 26  //这里的sn和mac是定长的。。而own是变长的最大长度为20
 27 #define SN_OFFSET  0 //各个数据的偏移量
 28 #define MAC_OFFSET SN_OFFSET + SN_LEN
 29 #define OWN_OFFSET MAC_OFFSET + MAC_LEN


eeprom.c直接操作eeprom的函数文件

 14 #include "eeprom.h"
 15 
 16 int open_eeprom(void) //打开设备节点。。并返回文件描述符
 17 {
 18     int fd;
 19 
 20     fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom", O_RDWR);
 21     if(fd < 0)
 22     {
 23         printf("open 24c02 fail\n");
 24         return -1;
 25     }
 26 
 27     return fd;
 28 }
 29 
 30 int close_eeprom(void) //关闭文件节点的函数
 31 {
 32 
 33     return 0;
 34 }
 35 
 36 int read_eeprom(int offset, char *buf, int len)//读eeprom。。参数是偏移。。buf。。长度
 37 {
 38     int fd;
 39     int ret;
 40 
 41     fd = open_eeprom();
 42     lseek(fd, offset, SEEK_SET); //设置偏移
 43 
 44     ret = read(fd, buf, len); //读指定长度到buf中
 45     if(ret < 0) //出错返回
 46     {
 47         printf("read error!\n");
 48         return -1;
 49     }
 50     else if(ret < len)
 51     {
 52         perror("incomplete read\n");
 53         printf("%d\n", ret);
 54         return -1;

 55     }
 56 
 57     close_eeprom();
 58 
 59     return 0;
 60 }
 61 
 62 int write_eeprom(int offset, char *buf, int len)//写到eeprom数据的函数。。
 63 {
 64     int fd;
 65     int ret;
 66 
 67     fd = open_eeprom();
 68 
 69     if(0 != strlen(buf)) //如果buf长度不是0。。就写到对应的偏移。。如果是0就不写入。。即保持原始数据不变
 70     {
 71         lseek(fd, offset, SEEK_SET);
 72         ret = write(fd, buf, len);
 73         if(ret < 0)
 74         {
 75             printf("Write  error\n");
 76             return -1;
 77         }
 78     }
 79 
 80     close_eeprom();
 81 
 82     return 0;
 83 }


fl2440_too.c函数

 13 #include "eeprom.h"
 14 
 15 int help_tool(void) //打印帮助信息
 16 {
 17     printf("Usage:./fl2440_too\n");
 18     printf("[-S SN]\n");
 19     printf("[-M MAC]\n");
 20     printf("[-O OWNER]\n");
 21     printf("[-A ALL INFORMATIONS]\n");
 22     printf("[-W WRITE INFORMATIONS LIKE SN-MAC-OWNER]\n");
 23 
 24     return 0;
 25 }
 26 
 27 int printf_data(int opt) //打印信息到屏幕
 28 {
 29     int ret;
 30 
 31     char sn[SN_LEN+1] ;
 32     char mac[MAC_LEN+1] ;
 33     char own[OWN_LEN+1] ;
 34 
 35     memset(sn, 0, strlen(sn)); //初始化对应结构体
 36     memset(mac, 0, strlen(mac));
 37     memset(own, 0, strlen(own));
 38 
 39     ret = read_eeprom(SN_OFFSET, sn, SN_LEN); //判断是否对应信息读出正确
 40     if(ret)
 41         return -1;
 42     sn[SN_LEN]='\0'; //打印字符串要比要打印的字符长度加一。。保存\0。。否则会出错
 43 
 44     ret = read_eeprom(MAC_OFFSET, mac, MAC_LEN);
 45     if(ret)
 46         return -1;
 47     mac[MAC_LEN]='\0';
 48 
 49     ret = read_eeprom(OWN_OFFSET, own, OWN_LEN);
 50     if(ret)
 51         return -1;

 52     own[OWN_LEN]='\0';
 53 
 54     switch (opt) //根据传参。。判断要以何种格式是打印那条数据
 55     {
 56         case 'S':
 57             printf("The SN is: %s\n", sn);
 58             break;
 59 
 60         case 'M':
 61             printf("The MAC is: %s\n", mac);
 62             break;
 63 
 64         case 'O':
 65             printf("The OWNER is: %s\n", own);
 66             break;
 67 
 68         case 'A':
 69             printf("The ALL is: %s-%s-%s\n",sn, mac, own);
 70             break;
 71 
 72         default:
 73             break;
 74     }
 75 
 76     return 0;
 77 }
 78 
 79 int write_data(char **argv) //输入函数
 80 {
 81     int ret;
 82 
 83     char *opt = argv[2];
 84     char *ptr;
 85     char sn[SN_LEN] ;
 86     char mac[MAC_LEN] ;
 87     char own[OWN_LEN] ;
 88 
 89     memset(sn, 0, strlen(sn));
 90     memset(mac, 0, strlen(mac));

 91     memset(own, 0, strlen(own));
 92 
 93     ptr =strchr(opt, '-'); //根据输入格式判断是否有对应数据输入。。并判断输入格式对不对。。并保存到相应的字符串中
 94     if(((ptr-opt) == SN_LEN)||(ptr-opt) == 0)
 95         strncpy(sn, opt, ptr-opt);
 96     else
 97         printf("the sn is error\n");
 98 
 99     opt = ptr+1;
100     ptr = strchr(opt, '-');
101     if(((ptr-opt) == MAC_LEN)||(ptr-opt) == 0)
102         strncpy(mac, opt, ptr-opt);
103     else
104         printf("the mac is error\n");
105 
106     opt = ptr+1;
107     ptr = strchr(opt, '\0');
108     strncpy(own, opt, ptr-opt);
109 
110     write_eeprom(SN_OFFSET, sn, SN_LEN); //写到对应eeprom位置
111     write_eeprom(MAC_OFFSET, mac, MAC_LEN);
112     write_eeprom(OWN_OFFSET, own, OWN_LEN);
113 
114     return 0;
115 }

main函数文件tools.c

14 #include "eeprom.h"
 15 
 16 char* const short_options = "ASMOW:"; //getopt_long配置
 17 struct option long_options[] = {
 18     {"ALL info", 0, NULL, 'A'},
 19     {"SN info", 0, NULL, 'S'},
 20     {"MAC info", 0, NULL, 'M'},
 21     {"OWN info", 0, NULL, 'O'},
 22     {"WR info", 1, NULL, 'W'},
 23     {0, 0, 0, 0},
 24 };
 25 
 26 int main (int argc, char **argv)
 27 {
 28     int opt;
 29 
 30     while((opt = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)                   //getopt_long判断argv[1]跟的参数
 31     {
 32         switch (opt)
 33         {
 34             case 'S': //打印sn
 35                 printf_data(opt);
 36                 break;
 37             case 'M':
 38                 printf_data(opt); //打印mac
 39                 break;
 40             case 'O':
 41                 printf_data(opt); //打印own
 42                 break;
 43             case 'A':
 44                 printf_data(opt); //打印全部信息
 45                 break;
 46             case 'W':

47                 write_data(argv); //写入信息到eeprom
 48                 break;
 49             default:
 50                 help_tool(); //提示信息
 51                 break;
 52         }

 53     }
 54 
 55     return 0;
 56 }


makefile文件

 1 CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc
  2 
  3 objs =tools.o fl2440_too.o eeprom.o
  4 srcs =tools.c fl2440_too.c eeprom.c
  5 
  6 eep.bin: $(objs)
  7             $(CC) -o eep.bin $(objs)
  8                     @make clean
  9 
 10 tools.o: $(srcs) eeprom.h
 11             $(CC) -c  $(srcs)
 12 
 13 fl2440_too.o: fl2440_too.c eeprom.h
 14              $(CC) -c  fl2440_too.c
 15 
 16 eeprom.o: eeprom.c eeprom.h
 17             $(CC) -c    eeprom.c
 18 
 19 clear:
 20              @rm  eep.bin
 21 
 22 .PHONY: clean
 23 clean:  
 24             @rm *.o 


这样就会make出一个eep.bin的文件。。tftp到开发板上就可以直接./eep.bin -    接参数操作我们的eeprom了。。

关于在open函数中打开的文件节点。。可以看一下之前的添加eeprom驱动里面的方法

0 0