一、I2C总线背景知识
SOC芯片平台的外设分为:
-
一级外设:外设控制器集成在SOC芯片内部
-
二级外设:外设控制器由另一块芯片负责,通过一些通讯总线与SOC芯片相连
Inter-Integrated Circuit: 字面意思是用于“集成电路之间”的通信总线,简写:IIC(或者I2C)
一般空闲态都会让SCL与SDA处于低电平。
i2c传输的要点就是: 传输一个字节 后面必然紧跟一个"响应"信号----应答信号.这个响应信号可能来自主机,或者是从机,具体是谁,就要看传输方向。 传输方向分两种情况(每种情况又有两种可能: A无应答和 B有应答):
1.主机->从机,主机对从机发一个字节之后,主机要读取从机的响应信号(主机读SDA线)
A) 主机读SDA为高电平,说明从机无应答(意味着从机接收完毕,主机发送停止信号)
B) 主机读SDA为低电平,说明从机有应答。(可继续发送下一个字节)
2.从机->主机, 主机读取从机一个字节之后,主机要向从机发送一个响应信号(主机写SDA线)
A) 主机写SDA为高电平,从机收到主机的无应答信号之后,从机停止传输,等待主机的停止信号。
B) 主机写SDA为低电平,从机收到主机的应答信号之后,从机继续输出下一字节
是否应答看具体业务需求
二、Exynos4412 I2C收发实现之裸机版
I2CCON寄存器:控制寄存器
第7位:决定是否允许产生应答信号,无论发送还是接收前,需置1
第6位:传输时时钟线分频,一般选置1
第5位:决定是否开启发送或接收结束时发通知,无论发送还是接收前,需置1
第4位:接收或发送是否完毕可以通过检查此位是否为1,接收或发送完毕后需置0
I2CSTAT寄存器:状态寄存器
第6、7位:每次传输前需选择传输模式
第5位:置0产生将产生终止信号,传输前置1产生起始信号
第4位:使能数据输出,传输前需置1
I2CDS寄存器:数据寄存器,发送前被发送的数据存放处,接收后结果也从此处读取
2.1 发送
void iic_write (unsigned char slave_addr, unsigned char addr, unsigned char data)
{//从设备寻址I2C5.I2CDS = slave_addr;I2C5.I2CCON = 1<<7 | 1<<6 | 1<<5;/*ENABLE ACK BIT, PRESCALER:512, ,ENABLE RX/TX */I2C5.I2CSTAT = 0x3 << 6 | 1<<5 | 1<<4;/*Master Trans mode ,START ,ENABLE RX/TX ,*/while(!(I2C5.I2CCON & (1<<4))); //发送完成
//寄存器编号I2C5.I2CDS = addr;I2C5.I2CCON &= ~(1<<4); //Clear pending bit to resume.while(!(I2C5.I2CCON & (1<<4))); //发送完成
//发送数据I2C5.I2CDS = data; // DataI2C5.I2CCON &= ~(1<<4); //Clear pending bit to resume.while(!(I2C5.I2CCON & (1<<4)));
I2C5.I2CSTAT = 0xD0; //stop
I2C5.I2CCON &= ~(1<<4);//Clear pending bit to resume.
mydelay_ms(10);
}
2.2 接收
void iic_read(unsigned char slave_addr, unsigned char addr, unsigned char *data)
{//从设备寻址I2C5.I2CDS = slave_addr;
I2C5.I2CCON = 1<<7 | 1<<6 | 1<<5;/*ENABLE ACK BIT, PRESCALER:512, ENABLE RX/TX Interrupt-enable */I2C5.I2CSTAT = 0x3 << 6 | 1<<5 | 1<<4;/*Master Trans mode ,START ,ENABLE RX/TX ,*/while(!(I2C5.I2CCON & (1<<4))); /*对应位为1表示slave_addr传输完成,线路处于挂起状态*/
I2C5.I2CDS = addr;I2C5.I2CCON &= ~(1<<4); //Clear pending bit to resume. 继续传输while(!(I2C5.I2CCON & (1<<4)));I2C5.I2CSTAT = 0xD0; //stop 第5位写0,表示要求产生stop信号
//发送读I2C5.I2CDS = slave_addr | 0x01; // ReadI2C5.I2CCON = 1<<7 | 1<<6 | 1<<5;/*ENABLE ACK BIT, PRESCALER:512, ENABLE RX/TX Interrupt-enable */
I2C5.I2CSTAT = 2<<6 | 1<<5 | 1<<4;/*Master receive mode ,START ,ENABLE RX/TX , 0xB0*/while(!(I2C5.I2CCON & (1<<4)));
//接收数据I2C5.I2CCON &= ~((1<<7) | (1<<4));/* Resume the operation & no ack*/while(!(I2C5.I2CCON & (1<<4)));
I2C5.I2CSTAT = 0x90; //stop 第5位写0,表示要求产生stop信号I2C5.I2CCON &= ~(1<<4); /*clean interrupt pending bit */
*data = I2C5.I2CDS;mydelay_ms(10);
}
三、Linux内核对I2C总线的支持
通常分为3层
I2C设备驱动:即挂接在I2C总线上的二级外设的驱动,也称客户(client)驱动,实现对二级外设的各种操作,二级外设的几乎所有操作全部依赖于对其自身内部寄存器的读写,对这些二级外设寄存器的读写又依赖于I2C总线的发送和接收。一般对应driver、和device。
I2C总线驱动:即对I2C总线自身控制器的驱动,一般SOC芯片都会提供多个I2C总线控制器,每个I2C总线控制器提供一组I2C总线(SDA一根+SCL一根),每一组被称为一个I2C通道,Linux内核里将I2C总线控制器叫做适配器(adapter),适配器驱动主要工作就是提供通过本组I2C总线与二级外设进行数据传输的接口,每个二级外设驱动里必须能够获得其对应的adapter对象才能实现数据传输(一般开发商做1次移植,大部分不需要我们自己编写,algorithm相当于我们的driver,adapter相当于device)
I2C核心:中间层承上启下,为I2C设备驱动和I2C总线驱动开发提供接口,为I2C设备驱动层提供管理多个i2c_driver、i2c_client对象的数据结构,为I2C总线驱动层提供多个i2c_algorithm、i2c_adapter对象的数据结构
四大核心对象之间的关系图
i2c二级外设驱动开发涉及到核心结构体及其相关接口函数:
struct i2c_board_info {char type[I2C_NAME_SIZE];unsigned short flags;unsigned short addr;void *platform_data;struct dev_archdata *archdata;struct device_node *of_node;int irq;
};
/*用来协助创建i2c_client对象
重要成员
type:用来初始化i2c_client结构中的name成员
flags:用来初始化i2c_client结构中的flags成员,0代表7位地址,1代表10位地址
addr:用来初始化i2c_client结构中的addr成员
platform_data:用来初始化i2c_client结构中的.dev.platform_data成员
archdata:用来初始化i2c_client结构中的.dev.archdata成员
irq:用来初始化i2c_client结构中的irq成员
关键就是记住该结构和i2c_client结构成员的对应关系。在i2c子系统不直接创建i2c_client结构,只是提供struct i2c_board_info结构信息,让子系统动态创建,并且注册。
*/
struct i2c_client {unsigned short flags;unsigned short addr;char name[I2C_NAME_SIZE];struct i2c_adapter *adapter; //重要,找到对应的adapterstruct i2c_driver *driver;struct device dev; //父类int irq;struct list_head detected; //链式管理
};
/*重要成员:
flags:地址长度,如是10位还是7位地址,默认是7位地址。如果是10位地址器件,则设置为I2C_CLIENT_TEN
addr:具体I2C器件如(at24c02),设备地址,低7位
name:设备名,用于和i2c_driver层匹配使用的,可以和平台模型中的平台设备层platform_driver中的name作用是一样的。
adapter:本设备所绑定的适配器结构(CPU有很多I2C适配器,类似单片机有串口1、串口2等等,在linux中每个适配器都用一个结构描述)
driver:指向匹配的i2c_driver结构,不需要自己填充,匹配上后内核会完成这个赋值操作
dev:内嵌的设备模型,可以使用其中的platform_data成员传递给任何数据给i2c_driver使用。
irq:设备需要使用到中断时,把中断编号传递给i2c_driver进行注册中断,如果没有就不需要填充。(有的I2C器件有中断引脚编号,与CPU相连)
*/
/* 获得/释放 i2c_adapter 路径:i2c-core.c linux-3.5\drivers\i2c */
/*功能:通过i2c总线编号(原理图获取)获得内核中的i2c_adapter结构地址,然后用户可以使用这个结构地址就可以给i2c_client结构使用,从而实现i2c_client进行总线绑定,从而增加适配器引用计数。
返回值:
NULL:没有找到指定总线编号适配器结构
非NULL:指定nr的适配器结构内存地址*/
struct i2c_adapter *i2c_get_adapter(int nr);
/*减少引用计数:当使用·i2c_get_adapter·后,需要使用该函数减少引用计数。(如果你的适配器驱动不需要卸载,可以不使用)*/
void i2c_put_adapter(struct i2c_adapter *adap);
/*
功能:根据参数adap,info,addr,addr_list动态创建i2c_client并且进行注册
参数:
adap:i2c_client所依附的适配器结构地址
info:i2c_client基本信息
addt_list: i2c_client的地址(地址定义形式是固定的,一般是定义一个数组,数组必须以I2C_CLIENT_END结束,示例:unsigned short ft5x0x_i2c[]={0x38,I2C_CLIENT_END};
probe:回调函数指针,当创建好i2c_client后,会调用该函数,一般没有什么特殊需求传递NULL。
返回值:
非NULL:创建成功,返回创建好的i2c_client结构地址
NULL:创建失败
*/
struct i2c_client * i2c_new_probed_device
(struct i2c_adapter *adap,struct i2c_board_info *info,unsigned short const *addr_list,int (*probe)(struct i2c_adapter *, unsigned short addr)
);
/*示例:
struct i2c_adapter *ad;
struct i2c_board_info info={""};
unsigned short addr_list[]={0x38,0x39,I2C_CLIENT_END}; //可能是38或者39,需要探测
//假设设备挂在i2c-2总线上
ad=i2c_get_adapter(2);
//自己填充board_info
strcpy(inf.type,"xxxxx");
info.flags=0;
//动态创建i2c_client并且注册
i2c_new_probed_device(ad,&info,addr_list,NULL);
i2c_put_adapter(ad);
*/
/*注销*/
void i2c_unregister_device(struct i2c_client *pclt)
//功能区别:i2c_new_device 知道从设备地址,用这个
// i2c_new_probed_device 不知道从设备地址,但是知道有从设备几个值里的一个。struct i2c_client * i2c_new_device(struct i2c_adapter *padap,struct i2c_board_info const *pinfo);/*示例:
struct i2c_adapter *ad;
struct i2c_board_info info={I2C_BOARD_INFO(name,二级外设地址)
};
//假设设备挂在i2c-2总线上
ad=i2c_get_adapter(2);
//动态创建i2c_client并且注册
i2c_new_device(ad,&info);
i2c_put_adapter(ad);
*/
驱动类似platform
struct i2c_driver {unsigned int class;
/* Standard driver model interfaces */int (*probe)(struct i2c_client *, const struct i2c_device_id *);int (*remove)(struct i2c_client *);
/* driver model interfaces that don't relate to enumeration */void (*shutdown)(struct i2c_client *);int (*suspend)(struct i2c_client *, pm_message_t mesg);int (*resume)(struct i2c_client *);void (*alert)(struct i2c_client *, unsigned int data);
/* a ioctl like command that can be used to perform specific functions* with the device.*/int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
struct device_driver driver; //父类const struct i2c_device_id *id_table; //driver匹配client的的成员/* Device detection callback for automatic device creation */int (*detect)(struct i2c_client *, struct i2c_board_info *);const unsigned short *address_list;struct list_head clients;
};
/*重要成员:
probe:在i2c_client与i2c_driver匹配后执行该函数
remove:在取消i2c_client与i2c_driver匹配绑定后后执行该函数
driver:这个成员类型在平台设备驱动层中也有,而且使用其中的name成员来实现平台设备匹配,但是i2c子系统中不使用其中的name进行匹配,这也是i2c设备驱动模型和平台设备模型匹配方法的一点区别
id_table:用来实现i2c_client与i2c_driver匹配绑定,当i2c_client中的name成员和i2c_driver中id_table中name成员相同的时候,就匹配上了。
补充:i2c_client与i2c_driver匹配问题
- i2c_client中的name成员和i2c_driver中id_table中name成员相同的时候
- i2c_client指定的信息在物理上真实存放对应的硬件,并且工作是正常的才会绑定上,并执行其中的probe接口函数这第二点要求和平台模型匹配有区别,平台模型不要求设备层指定信息在物理上真实存在就能匹配
*/
/*功能:向内核注册一个i2c_driver对象
返回值:0成功,负数 失败*/
#define i2c_add_driver(driver) i2c_register_driver(THIS_MODULE, driver) //宏定义更方便
int i2c_register_driver(struct module *owner, struct i2c_driver *driver);
/*功能:从内核注销一个i2c_driver对象
返回值:无 */
void i2c_del_driver(struct i2c_driver *driver);
struct i2c_msg {__u16 addr; /* slave address */__u16 flags; //发送还是接收都有flag决定
#define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
#define I2C_M_RD 0x0001 /* read data, from slave to master */__u16 len; /* msg length */__u8 *buf; /* pointer to msg data */
};
/* 重要成员:
addr:要读写的二级外设地址
flags:表示地址的长度,读写功能。如果是10位地址必须设置I2C_M_TEN,如果是读操作必须设置有I2C_M_RD······,可以使用或运算合成。
buf:要读写的数据指针。写操作:数据源 读操作:指定存放数据的缓存区
len:读写数据的数据长度
*/
/*i2c收发一体化函数,收还是发由参数msgs的成员flags决定*/
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
/*
功能:根据msgs进行手法控制
参数:
adap:使用哪一个适配器发送信息,一般是取i2c_client结构中的adapter指针作为参数
msgs:具体发送消息指针,一般情况下是一个数组
num:表示前一个参数msgs数组有多少个消息要发送的
返回值:
负数:失败
> 0 表示成功发送i2c_msg数量
*/
/*I2C读取数据函数*/
int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
/*功能:实现标准的I2C读时序,数据可以是N个数据,这个函数调用时候默认已经包含发送从机地址+读方向这一环节了
参数:
client:设备结构
buf:读取数据存放缓冲区
count:读取数据大小 不大于64k
返回值:
失败:负数
成功:成功读取的字节数
*//*I2C发送数据函数*/
int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
/*功能:实现标准的I2C写时序,数据可以是N个数据,这个函数调用时候默认已经包含发送从机地址+写方向这一环节了
参数:
client:设备结构地址
buf:发送数据存放缓冲区
count:发送数据大小 不大于64k
返回值:
失败:负数
成功:成功发送的字节数
*/
四、MPU6050
三轴角速度+三轴加速度+温度传感器
#define SMPLRT_DIV 0x19 //陀螺仪采样率,典型值:0x07(125Hz)
#define CONFIG 0x1A //低通滤波频率,典型值:0x06(5Hz)
#define GYRO_CONFIG 0x1B //陀螺仪自检及测量范围,典型值:0xF8(不自检,+/-2000deg/s)
#define ACCEL_CONFIG 0x1C //加速计自检、测量范围,典型值:0x19(不自检,+/-G)
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B //电源管理,典型值:0x00(正常启用)
五、应用层直接使用I2C通道
5.1 预备工作:
5.1.1 exynos4412平台每个i2c通道的信息是通过设备树提供的,因此需要首先在exynos4412-fs4412.dts中增加5通道的节点:
不要忘记:
-
回内核源码顶层目录执行:make dtbs
-
将新生成的dtb拷贝到/tftpboot
5.1.2 i2c总线驱动层提供了一个字符设备驱动,以便于应用层可以直接通过它去使用i2c总线通讯去操作二级外设,但需要
内核编译时添加此字符设备驱动代码(i2c-dev.c),因此需要修改make menuconfig的配置:
不要忘记:
-
回内核源码顶层目录执行:make uImage
-
将新生成的uImage拷贝到/tftpboot
5.2 应用层直接使用i2c总线的代码实现
5.2.1 调用read、write实现接收、发送
见实例代码
main.c
#include "mpu6050.h"int main(int argc,char *argv[])
{int fd = -1;if(argc < 2){printf("Argument is too few\n");return 1;}/*open*/fd = open(argv[1],O_RDWR);if(fd < 0){printf("open %s failed\n",argv[1]);return 2;}/*init mpu6050*/init_mpu6050(fd);while(1){sleep(2);/*read and print data from 6050*/printf("Accel-X:0x%x\n",read_accelx(fd));printf("Accel-Y:0x%x\n",read_accely(fd));printf("Accel-Z:0x%x\n",read_accelz(fd));printf("Temp:0x%x\n",read_temp(fd));printf("GYRO-X:0x%x\n",read_gyrox(fd));printf("GYRO-Y:0x%x\n",read_gyroy(fd));printf("GYRO-z:0x%x\n",read_gyroz(fd));printf("\n");}/*close*/close(fd);fd = -1;return 0;
}
mpu6060_op_rw.c
#include "mpu6050.h"static int read_data_from_mpu6050(int fd,unsigned char reg,unsigned char *pdata)
{int ret = 0;unsigned char buf[1] = {reg};ret = write(fd,buf,1);if(ret != 1){printf("write reg failed,in read_data_from_mpu6050\n");return -1;}buf[0] = 0;ret = read(fd,buf,1);if(ret != 1){printf("read data failed,in read_data_from_mpu6050\n");return -1;}*pdata = buf[0];return 0;
}static int write_data_to_mpu6050(int fd,unsigned char reg,unsigned char data)
{unsigned char buf[2] = {reg,data};int ret = 0;ret = write(fd,buf,2);if(ret != 2){printf("write data failed,in write_data_to_mpu6050\n");return -1;}return 0;
}int init_mpu6050(int fd)
{int ret = 0;ret = ioctl(fd,I2C_TENBIT,0); //0代表7位从机地址if(ret < 0){printf("ioctl I2C_TENBIT failed,in init_mpu6050\n");return -1;}ret = ioctl(fd,I2C_SLAVE,0x68); //68表示mpu6050地址if(ret < 0){printf("ioctl I2C_TENBIT failed,in init_mpu6050\n");return -1;}ret = write_data_to_mpu6050(fd,PWR_MGMT_1,0x00);ret += write_data_to_mpu6050(fd,SMPLRT_DIV,0x07);ret += write_data_to_mpu6050(fd,ACCEL_CONFIG,0x19);ret += write_data_to_mpu6050(fd,GYRO_CONFIG,0xF8);if(ret < 0){printf("write init data to mpu6050 failed,in init_mpu6050\n");return -1;}return 0;
}int read_accelx(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,ACCEL_XOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,ACCEL_XOUT_H,&d);val |= d << 8;if(ret < 0){printf("read accel x value failed,in read_accelx\n");return -1;}else{return val;}
}int read_accely(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,ACCEL_YOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,ACCEL_YOUT_H,&d);val |= d << 8;if(ret < 0){printf("read accel y value failed,in read_accely\n");return -1;}else{return val;}
}int read_accelz(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,ACCEL_ZOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,ACCEL_ZOUT_H,&d);val |= d << 8;if(ret < 0){printf("read accel z value failed,in read_accelz\n");return -1;}else{return val;}
}int read_temp(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,TEMP_OUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,TEMP_OUT_H,&d);val |= d << 8;if(ret < 0){printf("read temp value failed,in read_temp\n");return -1;}else{return val;}
}int read_gyrox(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,GYRO_XOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,GYRO_XOUT_H,&d);val |= d << 8;if(ret < 0){printf("read gyro x value failed,in read_gyrox\n");return -1;}else{return val;}
}int read_gyroy(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,GYRO_YOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,GYRO_YOUT_H,&d);val |= d << 8;if(ret < 0){printf("read gyro y value failed,in read_gyroy\n");return -1;}else{return val;}
}int read_gyroz(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,GYRO_ZOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,GYRO_ZOUT_H,&d);val |= d << 8;if(ret < 0){printf("read gyro z value failed,in read_gyroz\n");return -1;}else{return val;}
}
mpu6050.h
#ifndef MPU_6050_H
#define MPU_6050_H#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>#include <stdio.h>
#include <stdlib.h>
#include <string.h>int init_mpu6050(int fd);
int read_accelx(int fd);
int read_accely(int fd);
int read_accelz(int fd);
int read_temp(int fd);
int read_gyrox(int fd);
int read_gyroy(int fd);
int read_gyroz(int fd);#define SMPLRT_DIV 0x19
#define CONFIG 0x1A
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48#define PWR_MGMT_1 0x6B//linux-3.14/include/uapi/linux/i2c-dev.h
#define I2C_SLAVE 0x0703 /* Use this slave address */
#define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */#endif
其中在这两个宏需要引入头文件,或者直接在.h中添加宏定义
需要到linux内核源码中找
下面使用使用ioctl,需要引入这样的结构体,不需要的头文件可以去除
实验效果
5.2.2 调用ioctl实现接收、发送
见实例代码
main.c
#include "mpu6050.h"int main(int argc,char *argv[])
{int fd = -1;if(argc < 2){printf("Argument is too few\n");return 1;}/*open*/fd = open(argv[1],O_RDWR);if(fd < 0){printf("open %s failed\n",argv[1]);return 2;}/*init mpu6050*/init_mpu6050(fd);while(1){sleep(2);/*read and print data from 6050*/printf("Accel-X:0x%x\n",read_accelx(fd));printf("Accel-Y:0x%x\n",read_accely(fd));printf("Accel-Z:0x%x\n",read_accelz(fd));printf("Temp:0x%x\n",read_temp(fd));printf("GYRO-X:0x%x\n",read_gyrox(fd));printf("GYRO-Y:0x%x\n",read_gyroy(fd));printf("GYRO-z:0x%x\n",read_gyroz(fd));printf("\n");}/*close*/close(fd);fd = -1;return 0;
}
mpu6050_op_ioctl.c
#include "mpu6050.h"static int read_data_from_mpu6050(int fd,unsigned char slave,unsigned char reg,unsigned char *pdata)
{struct i2c_rdwr_ioctl_data work = {NULL};struct i2c_msg msgs[2] = {{0}};unsigned char buf1[1] = {reg};unsigned char buf2[1] = {0};int ret = 0;work.msgs = msgs;work.nmsgs = 2;msgs[0].addr = slave;msgs[0].flags = 0;msgs[0].buf = buf1;msgs[0].len = 1;msgs[1].addr = slave;msgs[1].flags = I2C_M_RD;msgs[1].buf = buf2;msgs[1].len = 1;ret = ioctl(fd,I2C_RDWR,&work);if(ret < 0){printf("ioctl I2C_RDWR failed,in read_data_from_mpu6050\n");return -1;}else{*pdata = buf2[0];return 0;}
}static int write_data_to_mpu6050(int fd,unsigned char slave,unsigned char reg,unsigned char data)
{struct i2c_rdwr_ioctl_data work = {NULL};struct i2c_msg msg = {0};unsigned char buf[2] = {reg,data};int ret = 0;work.msgs = &msg;work.nmsgs = 1;msg.addr = slave;msg.flags = 0;msg.buf = buf;msg.len = 2;ret = ioctl(fd,I2C_RDWR,&work);if(ret < 0){printf("ioctl I2C_RDWR failed,in write_data_to_mpu6050\n");return -1;}else{return 0;}
}int init_mpu6050(int fd)
{int ret = 0;ret = ioctl(fd,I2C_TENBIT,0);if(ret < 0){printf("ioctl I2C_TENBIT failed,in init_mpu6050\n");return -1;}ret = ioctl(fd,I2C_SLAVE,0x68);if(ret < 0){printf("ioctl I2C_TENBIT failed,in init_mpu6050\n");return -1;}ret = write_data_to_mpu6050(fd,0x68,PWR_MGMT_1,0x00);ret += write_data_to_mpu6050(fd,0x68,SMPLRT_DIV,0x07);ret += write_data_to_mpu6050(fd,0x68,ACCEL_CONFIG,0x19);ret += write_data_to_mpu6050(fd,0x68,GYRO_CONFIG,0xF8);if(ret < 0){printf("write init data to mpu6050 failed,in init_mpu6050\n");return -1;}return 0;
}int read_accelx(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,0x68,ACCEL_XOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,0x68,ACCEL_XOUT_H,&d);val |= d << 8;if(ret < 0){printf("read accel x value failed,in read_accelx\n");return -1;}else{return val;}
}int read_accely(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,0x68,ACCEL_YOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,0x68,ACCEL_YOUT_H,&d);val |= d << 8;if(ret < 0){printf("read accel y value failed,in read_accely\n");return -1;}else{return val;}
}int read_accelz(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,0x68,ACCEL_ZOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,0x68,ACCEL_ZOUT_H,&d);val |= d << 8;if(ret < 0){printf("read accel z value failed,in read_accelz\n");return -1;}else{return val;}
}int read_temp(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,0x68,TEMP_OUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,0x68,TEMP_OUT_H,&d);val |= d << 8;if(ret < 0){printf("read temp value failed,in read_temp\n");return -1;}else{return val;}
}int read_gyrox(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,0x68,GYRO_XOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,0x68,GYRO_XOUT_H,&d);val |= d << 8;if(ret < 0){printf("read gyro x value failed,in read_gyrox\n");return -1;}else{return val;}
}int read_gyroy(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,0x68,GYRO_YOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,0x68,GYRO_YOUT_H,&d);val |= d << 8;if(ret < 0){printf("read gyro y value failed,in read_gyroy\n");return -1;}else{return val;}
}int read_gyroz(int fd)
{unsigned short val = 0;unsigned char d = 0;int ret = 0;ret = read_data_from_mpu6050(fd,0x68,GYRO_ZOUT_L,&d);val = d;ret = read_data_from_mpu6050(fd,0x68,GYRO_ZOUT_H,&d);val |= d << 8;if(ret < 0){printf("read gyro z value failed,in read_gyroz\n");return -1;}else{return val;}
}
mpu6050.h
i2c.h内
#ifndef MPU_6050_H
#define MPU_6050_H#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>#include <stdio.h>
#include <stdlib.h>
#include <string.h>int init_mpu6050(int fd);
int read_accelx(int fd);
int read_accely(int fd);
int read_accelz(int fd);
int read_temp(int fd);
int read_gyrox(int fd);
int read_gyroy(int fd);
int read_gyroz(int fd);#define SMPLRT_DIV 0x19
#define CONFIG 0x1A
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48#define PWR_MGMT_1 0x6B//从内核源码中拷贝过来的3个宏
#define I2C_SLAVE 0x0703 /* Use this slave address */
#define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
#define I2C_RDWR 0x0707 /* Combined R/W transfer (one STOP only) */struct i2c_msg {unsigned short addr; /* slave address */unsigned short flags;
#define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
#define I2C_M_RD 0x0001 /* read data, from slave to master */unsigned short len; /* msg length */unsigned char *buf; /* pointer to msg data */
};/* This is the structure as used in the I2C_RDWR ioctl call */
struct i2c_rdwr_ioctl_data {struct i2c_msg *msgs; /* pointers to i2c_msgs */unsigned int nmsgs; /* number of i2c_msgs */
};
#endif
缺点:
-
需要应用程序开发人员查阅原理图和芯片手册,增加了他们的开发负担
-
开发出的应用程序缺乏可移植性
六、I2C总线二级外设驱动开发方法
-
查阅原理图以便得知二级外设挂在哪条I2C总线上、二级外设的身份标识(二级外设自身的地址)
-
参照platform样式搭建二级外设驱动框架
-
查询二级外设芯片手册以便得知驱动需要用到的寄存器地址
注意:
-
此处寄存器是指二级外设内部的寄存器,每个寄存器在芯片手册里有个对应编号(也被称为地址),但不是内存地址,特别提醒此寄存器不是SOC芯片内部参与内存统一编址的寄存器,更不是ARM核-CPU的寄存器
-
通过调用i2c_tranfer函数完成与相应寄存器的数据交互
-
-
参照字符驱动完成其余代码编写
-
创建对应的i2c_client对象
linux-3.14\Documentation\i2c\instantiating-devices
匹配方式:
-
名称匹配(id与名称匹配一样)
-
设备树匹配
-
ACPI匹配
Advanced Configuration and Power Management Interface 高级配置和电源管理接口
PC机平台采用的一种硬件配置接口(PC机平台的一般windows)
-
i2c二级外设驱动框架:(整体与之前的plathformdriver模块编写非常相似)
//其它struct file_operations函数实现原理同硬编驱动
static int mpu6050_probe(struct i2c_client *pclt,const struct i2c_device_id *pid)
{//做硬编驱动模块入口函数的活
}
static int mpu6050_remove(struct i2c_client *pclt)
{//做硬编驱动模块出口函数的活
}
/*名称匹配时定义struct i2c_device_id数组*/
static struct i2c_device_id mpu6050_ids =
{{"mpu6050",0},//.....{}
};
/*设备树匹配时定义struct of_device_id数组*/
static struct of_device_id mpu6050_dts =
{{.compatible = "invensense,mpu6050"},//....{}
};
/*通过定义struct i2c_driver类型的全局变量来创建i2c_driver对象,同时对其主要成员进行初始化*/
struct i2c_driver mpu6050_driver =
{.driver = {.name = "mpu6050",.owner = THIS_MODULE,.of_match_table = mpu6050_dts,},.probe = mpu6050_probe,.remove = mpu6050_remove,.id_table = mpu6050_ids,
};
/*以下其实是个宏,展开后相当于实现了模块入口函数和模块出口函数*/
module_i2c_driver(mpu6050_driver);
MODULE_LICENSE("GPL");
当应用程序发送一个IO控制命令时,驱动程序会检查命令中的魔数是否匹配,从而确定执行何种操作。
七、I2C总线二级外设驱动开发之名称匹配
7.1 示例:第一步完成模板
拷贝模板进行修改
mpu6050_drv.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/io.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>#include "mpu6050.h"#define SMPLRT_DIV 0x19
#define CONFIG 0x1A
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48#define PWR_MGMT_1 0x6Bint major = 11;
int minor = 0;
int mpu6050_num = 1;struct mpu6050_dev
{struct cdev mydev;struct i2c_client *pclt;
};struct mpu6050_dev *pgmydev = NULL;int mpu6050_open(struct inode *pnode,struct file *pfile)
{pfile->private_data =(void *) (container_of(pnode->i_cdev,struct mpu6050_dev,mydev));return 0;
}int mpu6050_close(struct inode *pnode,struct file *pfile)
{return 0;
}int mpu6050_read_byte(struct i2c_client *pclt,unsigned char reg) //<----------------------
{int ret = 0;char txbuf[1] = {reg};char rxbuf[1] = {0};struct i2c_msg msg[2] = {{pclt->addr,0,1,txbuf},{pclt->addr,I2C_M_RD,1,rxbuf}};ret = i2c_transfer(pclt->adapter,msg,ARRAY_SIZE(msg)); //<----------------------匹配adapterif(ret < 0){printk("ret = %d,in mpu6050_read_byte\n",ret);return ret;}return rxbuf[0];
}int mpu6050_write_byte(struct i2c_client *pclt,unsigned char reg,unsigned char val) //<----------------------
{int ret = 0;char txbuf[2] = {reg,val};struct i2c_msg msg[1] = {{pclt->addr,0,2,txbuf},};ret = i2c_transfer(pclt->adapter,msg,ARRAY_SIZE(msg)); //<----------------------匹配adapterif(ret < 0){printk("ret = %d,in mpu6050_write_byte\n",ret);return ret;}return 0;
}long mpu6050_ioctl(struct file *pfile,unsigned int cmd,unsigned long arg) //<----------------------
{struct mpu6050_dev *pmydev = (struct mpu6050_dev *)pfile->private_data;union mpu6050_data data;switch(cmd){case GET_ACCEL:data.accel.x = mpu6050_read_byte(pmydev->pclt,ACCEL_XOUT_L);data.accel.x = mpu6050_read_byte(pmydev->pclt,ACCEL_XOUT_H) << 8;data.accel.y = mpu6050_read_byte(pmydev->pclt,ACCEL_YOUT_L);data.accel.y = mpu6050_read_byte(pmydev->pclt,ACCEL_YOUT_H) << 8;data.accel.z = mpu6050_read_byte(pmydev->pclt,ACCEL_ZOUT_L);data.accel.z = mpu6050_read_byte(pmydev->pclt,ACCEL_ZOUT_H) << 8;break;case GET_GYRO:data.gyro.x = mpu6050_read_byte(pmydev->pclt,GYRO_XOUT_L);data.gyro.x = mpu6050_read_byte(pmydev->pclt,GYRO_XOUT_H) << 8;data.gyro.y = mpu6050_read_byte(pmydev->pclt,GYRO_YOUT_L);data.gyro.y = mpu6050_read_byte(pmydev->pclt,GYRO_YOUT_H) << 8;data.gyro.z = mpu6050_read_byte(pmydev->pclt,GYRO_ZOUT_L);data.gyro.z = mpu6050_read_byte(pmydev->pclt,GYRO_ZOUT_H) << 8;break;case GET_TEMP:data.temp = mpu6050_read_byte(pmydev->pclt,TEMP_OUT_L);data.temp = mpu6050_read_byte(pmydev->pclt,TEMP_OUT_H) << 8;break;default:return -EINVAL; //内核中无效命令}if(copy_to_user((void *)arg,&data,sizeof(data))) //读到用户空间中{return -EFAULT; //内核中 错误}return sizeof(data);
}void init_mpu6050(struct i2c_client *pclt)
{mpu6050_write_byte(pclt,PWR_MGMT_1,0x00);mpu6050_write_byte(pclt,SMPLRT_DIV,0x07);mpu6050_write_byte(pclt,CONFIG,0x06);mpu6050_write_byte(pclt,GYRO_CONFIG,0xF8);mpu6050_write_byte(pclt,ACCEL_CONFIG,0x19);
}struct file_operations myops = {.owner = THIS_MODULE,.open = mpu6050_open,.release = mpu6050_close,.unlocked_ioctl = mpu6050_ioctl,
};static int mpu6050_probe(struct i2c_client *pclt,const struct i2c_device_id *pid)
{int ret = 0;dev_t devno = MKDEV(major,minor);/*申请设备号*/ret = register_chrdev_region(devno,mpu6050_num,"mpu6050");if(ret){ret = alloc_chrdev_region(&devno,minor,mpu6050_num,"mpu6050");if(ret){printk("get devno failed\n");return -1;}major = MAJOR(devno);//容易遗漏,注意}pgmydev = (struct mpu6050_dev *)kmalloc(sizeof(struct mpu6050_dev),GFP_KERNEL);if(NULL == pgmydev){unregister_chrdev_region(devno,mpu6050_num);printk("kmalloc failed\n");return -1;}memset(pgmydev,0,sizeof(struct mpu6050_dev)); //这里的memset并非c库的函数,而是内核自己实现的memset函数pgmydev->pclt = pclt; //<-------------------------------获得client对象/*给struct cdev对象指定操作函数集*/ cdev_init(&pgmydev->mydev,&myops);/*将struct cdev对象添加到内核对应的数据结构里*/pgmydev->mydev.owner = THIS_MODULE;cdev_add(&pgmydev->mydev,devno,mpu6050_num);init_mpu6050(pgmydev->pclt); //<------------------------初始化设备return 0;
}static int mpu6050_remove(struct i2c_client *pclt)
{dev_t devno = MKDEV(major,minor);cdev_del(&pgmydev->mydev);unregister_chrdev_region(devno,mpu6050_num);kfree(pgmydev);pgmydev = NULL;return 0;
}struct i2c_device_id mpu6050_ids[] =
{{"mpu6050",0}, //0代表驱动程序使用的ID{}
};struct i2c_driver mpu6050_driver =
{.driver = { .name = "mpu6050", //次优先找.owner = THIS_MODULE,},.probe = mpu6050_probe,.remove = mpu6050_remove,.id_table = mpu6050_ids, //设备树最优先,其次ids优先找
};#if 0 //<----------------------两种方式都可以
static int __init mpu6050_driver_init(void)
{ return i2c_add_driver(&mpu6050_driver);
}
static void __exit mpu6050_driver_exit(void)
{i2c_del_driver(&mpu6050_driver);return;
}module_init(mpu6050_init);
module_exit(mpu6050_exit);#else
module_i2c_driver(mpu6050_driver);
#endifMODULE_LICENSE("GPL");
mpu6050.h
#ifndef MPU_6050_H
#define MPU_6050_Hstruct accel_data
{unsigned short x;unsigned short y;unsigned short z;
};
struct gyro_data{unsigned short x;unsigned short y;unsigned short z;};union mpu6050_data
{struct accel_data accel;struct gyro_data gyro;unsigned short temp;
};#define MPU6050_MAGIC 'K'#define GET_ACCEL _IOR(MPU6050_MAGIC,0,union mpu6050_data)
#define GET_GYRO _IOR(MPU6050_MAGIC,1,union mpu6050_data)
#define GET_TEMP _IOR(MPU6050_MAGIC,2,union mpu6050_data)#endif
7.2 创建i2c_client对象
名称匹配这种匹配方式需要自己创建i2c_client对象
创建i2c_client对象有三种方式:
-
i2c_register_board_info(现在比较少用了)
1.当开发板上电内核跑起来的时候,肯定是架构相关的程序首先运行,也就是mach-xxx.c 2. mach-xxx.c文件里首先会定义i2c_board_info的结构体数组,在mach-xxx.c的初始化函数里调用 i2c_register_board_info函数把i2c_board_inifo链接进内核的i2c_board_list链表当中去 3.在驱动i2c目录下和开发板板对应的驱动文件i2c-xxx.c里,创建i2c_adapter对象 4.这种方式严重依赖平台,缺乏灵活性,基本会被遗弃
-
i2c_new_device:明确二级外设地址的情况下可用
i2c二级外设client框架:
#include <linux/kernel.h> #include <linux/module.h> #include <linux/i2c.h>static struct i2c_board_info mpu6050_info = {I2C_BOARD_INFO("mpu6050",二级外设地址) };static struct i2c_client *mpu6050_client; static int __init mpu6050_dev_init(void) {struct i2c_adapter *padp = NULL;padp = i2c_get_adapter(i2c通道编号); //获取client对应的adapter对象mpu6050_client = i2c_new_device(padp,&mpu6050_info); //通过i2c_new_device创建对象i2c_put_adapter(padp);return 0; } module_init(mpu6050_dev_init);static void __exit mpu6050_dev_exit(void) {i2c_unregister_device(mpu6050_client); } module_exit(mpu6050_dev_exit); MODULE_LICENSE("GPL");
-
i2c_new_probed_device
i2c二级外设client框架:不明确二级外设地址,但是知道是可能几个值之一的情况下可用
#include <linux/kernel.h> #include <linux/module.h> #include <linux/i2c.h>static const unsigned short addr_list[] = {0x68, //probe函数会在列表中寻找设备//.....I2C_CLIENT_END };static struct i2c_client *mpu6050_client; static int __init mpu6050_dev_init(void) {struct i2c_adapter *padp = NULL;struct i2c_board_info mpu6050_info = {""};strcpy(mpu6050_info.type,"mpu6050");padp = i2c_get_adapter(i2c通道编号);mpu6050_client = i2c_new_probed_device(padp,&mpu6050_info,addr_list,NULL);i2c_put_adapter(padp);if(mpu6050_client != NULL){return 0;}else{return -ENODEV;} } module_init(mpu6050_dev_init);static void __exit mpu6050_dev_exit(void) {i2c_unregister_device(mpu6050_client); } module_exit(mpu6050_dev_exit); MODULE_LICENSE("GPL");
7.3 示例: 第二步创建client和testapp应用层测试
根据7.2,实现mpu6050_client.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>static struct i2c_board_info mpu6050_info =
{I2C_BOARD_INFO("mpu6050",0x68) //mpu6050需要和驱动中名称一致,.name = "mpu6050",
};static struct i2c_client *gpmpu6050_client = NULL;static int __init mpu6050_client_init(void)
{struct i2c_adapter *padp = NULL;padp = i2c_get_adapter(5);gpmpu6050_client = i2c_new_device(padp,&mpu6050_info);i2c_put_adapter(padp);return 0;
}static void mpu6050_client_exit(void)
{i2c_unregister_device(gpmpu6050_client);
}module_init(mpu6050_client_init);
module_exit(mpu6050_client_exit);
MODULE_LICENSE("GPL");
testapp.c
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>#include <stdio.h>#include "mpu6050.h"int main(int argc,char *argv[])
{int fd = -1;union mpu6050_data data;if(argc < 2){printf("The argument is too few\n");return 1;}fd = open(argv[1],O_RDONLY);if(fd < 0){printf("open %s failed \n",argv[1]);return 2;}while(1){sleep(2);ioctl(fd,GET_ACCEL,&data);printf("Accel-x=0x%x\n",data.accel.x);printf("Accel-y=0x%x\n",data.accel.y);printf("Accel-z=0x%x\n",data.accel.z);ioctl(fd,GET_GYRO,&data);printf("Gyro-x=0x%x\n",data.gyro.x);printf("Gyro-y=0x%x\n",data.gyro.y);printf("Gyro-z=0x%x\n",data.gyro.z);ioctl(fd,GET_TEMP,&data);printf("Temp=0x%x\n",data.temp);printf("\n");}close(fd);fd = -1;return 0;
}
Makefile
ifeq ($(KERNELRELEASE),)ifeq ($(ARCH),arm)
KERNELDIR ?= /home/linux/Linux_4412/kernel/linux-3.14
ROOTFS ?= /opt/4412/rootfs
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
endif
PWD := $(shell pwd)modules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_installclean:rm -rf *.o *.ko .*.cmd *.mod.* modules.order Module.symvers .tmp_versionselseCONFIG_MODULE_SIG=n
obj-m += mpu6050_drv.oendif
编译并拷贝到跟文件系统
测试
7.4 示例:不明确二级外设地址的另一种方式编写
mpu6050_client_probe.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>static unsigned short mpu6050_addr_list[] =
{0x68, //可能是680x69, //可能是69I2C_CLIENT_END //结束符
};static struct i2c_client *gpmpu6050_client = NULL;static int __init mpu6050_client_init(void)
{struct i2c_adapter *padp = NULL;struct i2c_board_info mpu6050_info = {""};strcpy(mpu6050_info.type,"mpu6050");padp = i2c_get_adapter(5);gpmpu6050_client = i2c_new_probed_device(padp,&mpu6050_info,mpu6050_addr_list,NULL); //从列表中探测mpu6050_addr_list,NULL这里探测到后可以调用一个函数,如果不需要函数被调用填写NULLi2c_put_adapter(padp);if(gpmpu6050_client != NULL){return 0;}else{return -ENODEV;}
}static void mpu6050_client_exit(void)
{i2c_unregister_device(gpmpu6050_client);
}module_init(mpu6050_client_init);
module_exit(mpu6050_client_exit);
MODULE_LICENSE("GPL");
修改Makefile,添加编译并拷贝到跟文件目录中测试
八、I2C总线二级外设驱动开发之设备树匹配
mpu6050_drv.c(设备树版本)
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/io.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>#include "mpu6050.h"#define SMPLRT_DIV 0x19
#define CONFIG 0x1A
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48#define PWR_MGMT_1 0x6Bint major = 11;
int minor = 0;
int mpu6050_num = 1;struct mpu6050_dev
{struct cdev mydev;struct i2c_client *pclt;
};struct mpu6050_dev *pgmydev = NULL;int mpu6050_open(struct inode *pnode,struct file *pfile)
{pfile->private_data =(void *) (container_of(pnode->i_cdev,struct mpu6050_dev,mydev));return 0;
}int mpu6050_close(struct inode *pnode,struct file *pfile)
{return 0;
}int mpu6050_read_byte(struct i2c_client *pclt,unsigned char reg) //<----------------------
{int ret = 0;char txbuf[1] = {reg};char rxbuf[1] = {0};struct i2c_msg msg[2] = {{pclt->addr,0,1,txbuf},{pclt->addr,I2C_M_RD,1,rxbuf}};ret = i2c_transfer(pclt->adapter,msg,ARRAY_SIZE(msg)); //<----------------------匹配adapterif(ret < 0){printk("ret = %d,in mpu6050_read_byte\n",ret);return ret;}return rxbuf[0];
}int mpu6050_write_byte(struct i2c_client *pclt,unsigned char reg,unsigned char val) //<----------------------
{int ret = 0;char txbuf[2] = {reg,val};struct i2c_msg msg[1] = {{pclt->addr,0,2,txbuf},};ret = i2c_transfer(pclt->adapter,msg,ARRAY_SIZE(msg)); //<----------------------匹配adapterif(ret < 0){printk("ret = %d,in mpu6050_write_byte\n",ret);return ret;}return 0;
}long mpu6050_ioctl(struct file *pfile,unsigned int cmd,unsigned long arg) //<----------------------
{struct mpu6050_dev *pmydev = (struct mpu6050_dev *)pfile->private_data;union mpu6050_data data;switch(cmd){case GET_ACCEL:data.accel.x = mpu6050_read_byte(pmydev->pclt,ACCEL_XOUT_L);data.accel.x = mpu6050_read_byte(pmydev->pclt,ACCEL_XOUT_H) << 8;data.accel.y = mpu6050_read_byte(pmydev->pclt,ACCEL_YOUT_L);data.accel.y = mpu6050_read_byte(pmydev->pclt,ACCEL_YOUT_H) << 8;data.accel.z = mpu6050_read_byte(pmydev->pclt,ACCEL_ZOUT_L);data.accel.z = mpu6050_read_byte(pmydev->pclt,ACCEL_ZOUT_H) << 8;break;case GET_GYRO:data.gyro.x = mpu6050_read_byte(pmydev->pclt,GYRO_XOUT_L);data.gyro.x = mpu6050_read_byte(pmydev->pclt,GYRO_XOUT_H) << 8;data.gyro.y = mpu6050_read_byte(pmydev->pclt,GYRO_YOUT_L);data.gyro.y = mpu6050_read_byte(pmydev->pclt,GYRO_YOUT_H) << 8;data.gyro.z = mpu6050_read_byte(pmydev->pclt,GYRO_ZOUT_L);data.gyro.z = mpu6050_read_byte(pmydev->pclt,GYRO_ZOUT_H) << 8;break;case GET_TEMP:data.temp = mpu6050_read_byte(pmydev->pclt,TEMP_OUT_L);data.temp = mpu6050_read_byte(pmydev->pclt,TEMP_OUT_H) << 8;break;default:return -EINVAL; //内核中无效命令}if(copy_to_user((void *)arg,&data,sizeof(data))) //读到用户空间中{return -EFAULT; //内核中 错误}return sizeof(data);
}void init_mpu6050(struct i2c_client *pclt)
{mpu6050_write_byte(pclt,PWR_MGMT_1,0x00);mpu6050_write_byte(pclt,SMPLRT_DIV,0x07);mpu6050_write_byte(pclt,CONFIG,0x06);mpu6050_write_byte(pclt,GYRO_CONFIG,0xF8);mpu6050_write_byte(pclt,ACCEL_CONFIG,0x19);
}struct file_operations myops = {.owner = THIS_MODULE,.open = mpu6050_open,.release = mpu6050_close,.unlocked_ioctl = mpu6050_ioctl,
};static int mpu6050_probe(struct i2c_client *pclt,const struct i2c_device_id *pid)
{int ret = 0;dev_t devno = MKDEV(major,minor);/*申请设备号*/ret = register_chrdev_region(devno,mpu6050_num,"mpu6050");if(ret){ret = alloc_chrdev_region(&devno,minor,mpu6050_num,"mpu6050");if(ret){printk("get devno failed\n");return -1;}major = MAJOR(devno);//容易遗漏,注意}pgmydev = (struct mpu6050_dev *)kmalloc(sizeof(struct mpu6050_dev),GFP_KERNEL);if(NULL == pgmydev){unregister_chrdev_region(devno,mpu6050_num);printk("kmalloc failed\n");return -1;}memset(pgmydev,0,sizeof(struct mpu6050_dev)); //这里的memset并非c库的函数,而是内核自己实现的memset函数pgmydev->pclt = pclt; //<-------------------------------获得client对象/*给struct cdev对象指定操作函数集*/ cdev_init(&pgmydev->mydev,&myops);/*将struct cdev对象添加到内核对应的数据结构里*/pgmydev->mydev.owner = THIS_MODULE;cdev_add(&pgmydev->mydev,devno,mpu6050_num);init_mpu6050(pgmydev->pclt); //<------------------------初始化设备return 0;
}static int mpu6050_remove(struct i2c_client *pclt)
{dev_t devno = MKDEV(major,minor);cdev_del(&pgmydev->mydev);unregister_chrdev_region(devno,mpu6050_num);kfree(pgmydev);pgmydev = NULL;return 0;
}struct of_device_id mpu6050_dt[] = //<---------------------设备树匹配
{{.compatible = "invensense,mpu6050"}, //与dts匹配{} //表示结束符
};struct i2c_device_id mpu6050_ids[] =
{{"mpu6050",0}, //0代表驱动程序使用的ID{}
};struct i2c_driver mpu6050_driver =
{.driver = {.name = "mpu6050",.owner = THIS_MODULE,.of_match_table = mpu6050_dt, //<---------------------设备树匹配},.probe = mpu6050_probe,.remove = mpu6050_remove,.id_table = mpu6050_ids,
};#if 0
static int __init mpu6050_driver_init(void)
{ return i2c_add_driver(&mpu6050_driver);
}
static void __exit mpu6050_driver_exit(void)
{i2c_del_driver(&mpu6050_driver);return;
}module_init(mpu6050_init);
module_exit(mpu6050_exit);#else
module_i2c_driver(mpu6050_driver);
#endifMODULE_LICENSE("GPL");
编译复制到根文件系统
编译设备树,复制到tftpboot目录
测试,不需要client,仅设备树即可配对