I2C子系统之at24c02读写测试
I2C子系统之at24c02读写测试
结合ioctl和at24c02的介绍,写了个at24c02的测试程序
测试硬件平台:TQ2440、at24c02
内核版本:linux-2.6.37.1
读写单独分开成两个小程序。
相关阅读:
I2C子系统之at24c02读写测试
I2C子系统之ioctl()
I2C子系统之at24c02简介
I2C子系统之总结
I2C子系统之内核中I2C子系统的结构
I2C子系统之I2C bus初始化——I2C_init()
I2C子系统之platfor_device初始化——smdk2440_machine_init()
I2C子系统之platform_driver初始化——I2C_adap_s3c_init()
I2C子系统之I2C总线时钟频率设置
I2C子系统之adapter device和client device注册——I2C_add_number_adapter()
I2C子系统之__I2C_first_dynamic_bus_num变量的相关分析
I2C子系统之 adapter driver注册——I2C_dev_init()
I2C子系统之write()
源码如下:
写测试程序:
- #include <stdio.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <linux/i2c-dev.h>
- #include <errno.h>
- int main(int argc, char *argv[])
- {
- int num, err, i, j;
- int fd, addr;
- char *buff;
- printf("please input as:");
- printf("./wat24 [data]\n");
- fflush(stdout);
- if(argc < 3){
- printf("arg error\n");
- return -1;
- }
- num = argc - 1;
- buff = malloc(num*sizeof(char));
- if(buff < 0){
- printf("alloc failed\n");
- return -1;
- }
- buff[0] = atoi(argv[1]);
- printf("write data:\n");
- for(i = 1; i < num; i++){
- buff[i] = atoi(argv[i + 1]);
- printf("%d\n",buff[i]);
- }
- printf("from word addr:%d\n",buff[0]);
- fd = open("/dev/i2c-0",O_RDWR);
- if(fd < 0){
- printf("device open failed\n");
- return -1;
- }
- err = ioctl(fd, I2C_SLAVE_FORCE, 0x50);
- if(err < 0){
- printf("ioctl failed:%d\n",err);
- return -1;
- }
- write(fd, buff, num);
- close(fd);
- return 0;
- }
|
评论暂时关闭