s3c2440开发板Linux系统下eeprom驱动制作以及读写程序

来源:互联网 发布:测评软件 编辑:程序博客网 时间:2024/06/06 04:36

最近一段时间主要将s3c2440开发板下eeprom的驱动做好了,并编写程序进行了eeprom的读写操作的测试,这篇文章主要是总结一下eeprom的驱动制作以及测试程序的编写。


=============================================================================

开发环境:Centos6.5

内核版本:Linux3.0

交叉编译器版本:  buildroot-2012.08

=============================================================================


一、原理图部分

     

上面的图中,左图是eeprom的底板原理图,从图中我们得知,此开发板的eeprom使用的是AT2402的芯片,结合右图可以看出eeprom的时钟和数据信号与开发板的i2c总线的scl和sda接口连接在一起,说明此eeprom是挂载在i2c总线下。


二、开发板内核配置以及内核源程序中i2c总线eeprom驱动程序添加

1.开发板内核配置

首先进入Generic Driver Options  --->  

               <*> I2C support  --->   

                

 [*] Misc devices  --->              

  EEPROM support  ---> 

2.内核源程序中i2c总线eeprom驱动程序添加

修改linux/arch/arm/mach-s3c2440/mach-smdk2440.c文件

在其中添加

#include <linux/i2c/at24.h>static struct at24_platform_data at24c02 = {    .byte_len   = SZ_2K / 8,    .page_size  = 8,    .flags      = 0,};static struct i2c_board_info __initdata smdk2440_i2c_devs[] = {    {          I2C_BOARD_INFO("24c02", 0x50),         .platform_data = &at24c02,    },    /*   more devices can be added using expansion connectors  */};

另外在smdk2440_machine_init函数中添加

i2c_register_board_info(0, smdk2440_i2c_devs, ARRAY_SIZE(smdk2440_i2c_devs));


完后上面的步骤之后,我们重新编译内核,烧录到开发板上,我们进入开发板,进入目录/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/下,我们可以看到name和eeprom显示如下


到这里我们就完成了Linux内核中i2c总线下eeprom的驱动制作。


三、eeprom测试程序

这个测试程序的主要功能是能够在eeprom中读出和写入开发板的sn 、mac以及owner信息,程序具体显示要求如下:

程序编译后的名字叫eeprom, 其使用方法为:

向eeprom中写入信息: eeprom  -s  FL2440-abc

     eeprom  -m  00-11-22-33-44-55

                             eeprom  -o   Michael

从eeprom中读出信息: eeprom -r 

                                             sn= FL2440-abc

                                             mac= 00-11-22-33-44-55

                                             owner= Michael

测试程序如下:

#include <stdio.h>#include <unistd.h>#include <getopt.h>#include <string.h>#include <sys/ioctl.h>#include <stdlib.h>#include <fcntl.h>#include <sys/io.h>#include <errno.h> #define LEN 50#define SN_OFS 0#define SN_LEN 50#define MAC_OFS 50#define MAC_LEN 50#define OWN_OFS 100#define OWN_LEN 50/******  help information *********/void help(){    printf("This program is used to read or write MAC information to eeprom.\n");    printf("Mandatory arguments to long options are mandatory for short options too:\n");    printf(" -r [read] read the information of board in eeprom;\n");printf(" -s [serial] write the serial of board to eeprom ;\n");    printf(" -m [mac] write the mac adress of board to eeprom ;\n");printf(" -o [owner] write the owner of board to eeprom ;\n");    printf(" -h [help] printf the help information.\n");}/**** the struct of board_information ******/struct board_info_s{    char sn[SN_LEN];    char mac[MAC_LEN];    char owner[OWN_LEN];}board_info_t;/**** write the board information into eeprom ******/void write_eeprom_sn(char *a){    int fd = -1;int ret = -1;    fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom",O_WRONLY);if(fd < 0){printf("open file eeprom failed;%s\n",strerror(errno));}    strncpy(board_info_t.sn, a, sizeof(board_info_t.sn));    lseek(fd, SN_OFS, SEEK_SET);    ret = write(fd, board_info_t.sn, sizeof(board_info_t.sn));    if(ret < 0)    {        printf("error: %d:%s\n", errno, strerror(errno));    }    printf("the sn information of board %s is writen into eeprom!\n", board_info_t.sn);}void write_eeprom_mac(char *b){    int fd = -1;int ret = -1;    fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom", O_WRONLY);    if(fd < 0){printf("open file eeprom failed;%s\n", strerror(errno));}strncpy(board_info_t.mac, b, sizeof(board_info_t.mac));    lseek(fd, MAC_OFS, SEEK_SET);    ret = write(fd, board_info_t.mac, sizeof(board_info_t.mac));    if(ret < 0){        printf("error: %d:%s\n", errno, strerror(errno));    }printf("the sn information of board %s is writen into eeprom!\n", board_info_t.mac);}void write_eeprom_owner(char *c){    int fd = -1;int ret = -1;    fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom",O_WRONLY);if(fd < 0){printf("open file eeprom failed;%s\n", strerror(errno));}strncpy(board_info_t.owner, c, sizeof(board_info_t.owner));    lseek(fd, OWN_OFS, SEEK_SET);    ret = write(fd, board_info_t.owner, sizeof(board_info_t.owner));if(ret < 0)    {        printf("error: %d:%s\n", errno, strerror(errno));    }    printf("the sn information of board %s is writen into eeprom!\n", board_info_t.owner);}struct board_write{    void (*write_sn)(char *);    void (*write_mac)(char *);    void (*write_owner)(char *);}board_write_cb;/**** read the board_information from eeprom ****/void board_read(){    int fd = -1;int ret = -1;    fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom", O_RDONLY);if(fd<0){printf("open file eeprom failed;%s\n", strerror(errno));}lseek(fd, SN_OFS, SEEK_SET);ret = read(fd, board_info_t.sn, SN_LEN);if(ret < 0){        printf("read from eeprom eerror:%s !\n", strerror(errno));    }    printf("the sn of board_information in eeprom is SN=%s !\n", board_info_t.sn);     lseek(fd, MAC_OFS, SEEK_SET);    ret=read(fd, board_info_t.mac, MAC_LEN);if(ret < 0){         printf("read from eeprom eerror:%s !\n", strerror(errno));    }    printf("the sn of board_information in eeprom is MAC=%s !\n", board_info_t.mac);    lseek(fd, MAC_OFS, SEEK_SET);    ret = read(fd, board_info_t.owner, MAC_LEN);    if(ret < 0)    {         printf("read from eeprom eerror:%s !\n", strerror(errno));    }    printf("the sn of board_information in eeprom is OWNER=%s !\n", board_info_t.owner);}/******************************************************************************** *  Description: *   Input Args: *  Output Args: * Return Value: ********************************************************************************/int main (int argc, char **argv){    int opt=-1;int ret=-1;        if(argc < 2){        help();    }if(sizeof(argv[2]) > LEN){printf("you putin too much characters!\n");}    board_write_cb.write_sn=write_eeprom_sn;    board_write_cb.write_mac=write_eeprom_mac;    board_write_cb.write_owner=write_eeprom_owner;    struct option long_options[] ={        {"read", no_argument, NULL, 'r'},        {"sn", required_argument, NULL, 's'},        {"mac", required_argument, NULL, 'm'},        {"owner", required_argument, NULL, 'o'},        {"help", no_argument, NULL, 'h'},        {NULL, 0, NULL, 0}    };    while((opt=getopt_long(argc,argv, ":rs:m:o:h", long_options, NULL)) != -1)    {        switch(opt)        {            case 'r':                board_read();                break;            case 's':                board_write_cb.write_sn(optarg);                break;            case 'm':                board_write_cb.write_mac(optarg);                break;            case 'o':                board_write_cb.write_owner(optarg);                break;            case 'h':                help();                break;case '?':printf("Invalid character constant !\n");                break;        }    }  return 0;} /*  ----- End of main() ----- */

测试结果:



0 0
原创粉丝点击