# linux下mpu6050驱动
## 视频讲解
[video(video-ASCejqRf-1703404139750)(type-bilibili)(url-https://player.bilibili.com/player.html?aid=249033128)(image-https://img-blog.csdnimg.cn/img_convert/a69a48a0a2735563aee5aeee5517e4ca.jpeg)(title-【Linux】imx6下编写mpu6050(i2c)驱动流程(上))]
[video(video-vluUgmhx-1703404177670)(type-bilibili)(url-https://player.bilibili.com/player.html?aid=761596537)(image-https://img-blog.csdnimg.cn/img_convert/e3c5572f172a5e4bff20f6f642b10e0b.jpeg)(title-【Linux】imx6下编写mpu6050(i2c)驱动流程(下))]
## 代码
- 先上代码:
- [github仓库链接](https://github.com/darboy/mpu6050-linux)
- [gitee仓库链接](https://gitee.com/darboy/mpu6050-linux)
## 设备树
在i2c的控制器下追加该设备
```c
&i2c2 {
clock_frequency = <100000>;
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_i2c2>;
status = "okay";
mpu6050:mpu6050@68 {
compatible = "dar,mpu6050";
reg = <0x68>;
status = "okay";/* 如果需要改为okay */
};
};
```
## Makefile
- 修改内核目录KERNELDIR,xxx为用户
- source编译环境后,可直接make或讲CC更换为指定的交叉编译器即可
```c
KERNELDIR := /home/xxx/Project/linux/kernal_source/linux-imx-rel_imx_4.1.15_2.1.0_ga_alientek/
CURRENT_PATH := $(shell pwd)
obj-m := mpu6050.o
build: kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
date
$(CC) mpu6050Demo.c -o mpu6050Demo -Wall -pthread -O2
install:
scp mpu6050Demo mpu6050.ko root@192.168.192.5:/home/root
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
rm mpu6050Demo -rf
```
## 编译
```powershell
cd ./mpu6050-linux
source /opt/fsl-imx-x11/4.1.15-2.1.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
make
make install
```
## 测试
1. 登录开发板
2. mpu6050Demo为测试程序,将会读取6轴数据
3. mpu6050.ko为模块驱动,通过insmod加载,匹配设备树后,在/dev中生成mpu6050设备
4. 执行如下命令
```powershell
ssh root@192.168.192.5
insmod mpu6050.ko
chmod +x mpu6050Demo
./mpu6050Demp
```
加载驱动
![777504](/data/attachment/forum/202401/19/092626pxf5nebzf1wztff8.png?rand=3050.1778313107798)
mpu6050设备已经加载
程序