×
单片机 > 单片机程序设计 > 详情

S3C2440 Linux驱动移植——AT24C02(EEPROM)驱动

发布时间:2020-08-31 发布时间:
|

开发板:TQ2440

内核:Linux 2.6.32

PC OS:Ubuntu 11.04


1.配置内核

打开I2C功能:

  • #include   

  • static struct at24_platform_data at24c02 = {  

  •     .byte_len   = SZ_2K / 8,  

  •     .page_size  = 8,  

  •     .flags      = 0,  

  • };  

  •   

  • static struct i2c_board_info __initdata smdk_i2c_devices[] = {  

  •     /* more devices can be added using expansion connectors */  

  •     {  

  •         I2C_BOARD_INFO("24c02", 0x50),  

  •         .platform_data = &at24c02,  

  •     },  

  • };  



  • 在smdk2440_machine_init函数中增加如下:



    1. i2c_register_board_info(0, smdk_i2c_devices, ARRAY_SIZE(smdk_i2c_devices));  


    注意:上面许多参数是根据at24c02的参数来设置的,at24c02使用8位地址,内存大小2K比特位,也就是256K字节,页大小为8字节。

    最后,需要注意,手册中at24c02的设备地址是0b 1 0 1 0 0 0 0 R/W, 其最低位是读写标志位,

    但是在Linux中,I2C设备地址的最高位为0,而低七位地址就是手册中去掉R/W的剩余7位。因此,地址为0b 01010000(0x50)

    3. 测试代码

           系统启动后,如果一切正常。会在/sys文件系统下展示出该设备,如下:

     

    [root@yj4230-0050]#pwd

    /sys/devices/platform/s3c2440-i2c/i2c-0/0-0050

    [root@yj4230-0050]#ls

    bus        eeprom     name      subsystem

    driver     modalias  power      uevent

    [root@yj4230-0050]#cat name

    24c02

     

    其中eeprom即为驱动导出的bin属性,通过读写eeprom即可访问设备,如下:

     

    [root@yj4230-0050]#cat eeprom

    i2ci2c-0: master_xfer[0] W, addr=0x50, len=1

    i2ci2c-0: master_xfer[1] R, addr=0x50, len=128

    i2ci2c-0: master_xfer[0] W, addr=0x50, len=1

    i2ci2c-0: master_xfer[1] R, addr=0x50, len=128

     !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€亗儎厗噲墛媽崕彁憭摂晼棙櫄洔潪煚、¥ウЖ┆辈炒刀犯购患骄坷谅媚牌侨墒颂臀闲岩釉罩棕仝圮蒉哙徕沅彐玷殛腱眍镳耱篝貊鼬

     

    接着,编写代码进行测试,如下:


    1. #include   

    2. #include   

    3. #include   

    4. #include   

    5. #include   

    6. #include   

    7. #include   

    8.   

    9. int main(int argc, char **argv)  

    10. {  

    11.     int ret, fd, i, j;  

    12.     char read_data[256];  

    13.     char write_data[256];  

    14.     char offset;  

    15.       

    16.     fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom", O_RDWR);  

    17.     if(fd 

    18.         printf("Open at24c02 fail\n");  

    19.         return -1;  

    20.     }  

    21.       

    22.     ret = read(fd, &offset, 1);  

    23.     if(ret 

    24.         printf("Read error\n");  

    25.         return -1;  

    26.     }else if(ret 

    27.         perror("Incomplete read\n");  

    28.         printf("%d\n", ret);  

    29.         return -1;  

    30.     }  

    31.       

    32.     for(i = 0; i 

    33.         write_data[i] = offset+ 1 + i;  

    34.       

    35.     lseek(fd, 0 , SEEK_SET);        //It's a must, or something wierd will happen  

    36.       

    37.     ret = write(fd, write_data, 256);  

    38.     if(ret 

    39.         printf("Write error\n");  

    40.         return -1;  

    41.     }  

    42.       

    43.     lseek(fd, 0 , SEEK_SET);     //It's a must, or something wierd will happen  

    44.       

    45.     ret = read(fd, read_data, 256);  

    46.     if(ret 

    47.         printf("Read error\n");  

    48.         return -1;  

    49.     }else if(ret 

    50.         perror("Incomplete read\n");  

    51.         printf("%d\n", ret);  

    52.         return -1;  

    53.     }  

    54.       

    55.     for(i = 0; i 

    56.         if(i %16 == 0)  

    57.             printf("\n");  

    58.         printf(" %03d ", read_data[i]);  

    59.   

    60.     }     

    61.     printf("\n");                     

    62. }  


    代码结果如下:

     


    关键字:S3C2440  Linux驱动  移植  AT24C02  EEPROM

    『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

    热门文章 更多
    51单片机CO2检测显示程序解析