一、修改设备树
不要使用插件设备树更改pinmux,因为在很多板子上都无效,BeagleBone AI也不例外。首先对设备树作如下更改:
- 将pinmux@1400节点的cape_pins_default子节点内的CTRL_CORE_PAD_VIN1A_D20(0x4a003544 - 0x4a000000 - 0x2000 - 0x1400 = 0x00000144)、CTRL_CORE_PAD_GPIO6_15(0x4a00368C - 0x4a000000 - 0x2000 - 0x1400 = 0x0000028c)、CTRL_CORE_PAD_GPIO6_14(0x4A003688 - 0x4a000000 - 0x2000 - 0x1400 = 0x00000288)三个寄存器的值分别改为0x0001000f(PIN_OUTPUT | MUX_MODE15)、0x00060009(PIN_OUTPUT_PULLUP | INPUT_EN | MUX_MODE9)、0x00060009(PIN_OUTPUT_PULLUP | INPUT_EN | MUX_MODE9);
- 关闭AE2管脚是因为它和i2c3_sda对应的E21管脚在BeagleBone AI上是连在一起的,这个可以通过看板子的原理图和芯片的数据手册知道;
- 将i2c3对应的i2c@48060000节点的status属性值从"disabled"更改为"okay";
- 在i2c@48060000节点内添加clock-frequency = <0x00061a80>;属性,这个属性指定了i2c的速度为400kHz;
- 在i2c@48060000节点内添加i2c_mpu6050@68节点及其内容。
如下为更改后的am5729-beagleboneai.dts文件:
...
ocp {
compatible = "ti,dra7-l3-noc", "simple-bus";
ranges = <0x00000000 0x00000000 0x00000000 0xc0000000>;
ti,hwmods = "l3_main_1", "l3_main_2";
reg = <0x00000000 0x44000000 0x00000000 0x01000000 0x00000000 0x45000000 0x00000000 0x00001000>;
interrupts-extended = <0x00000001 0x00000000 0x00000004 0x00000004 0x00000008 0x00000000 0x0000000a 0x00000004>;
phandle = <0x00000139>;
l4@4a000000 {
compatible = "ti,dra7-l4-cfg", "simple-bus";
ranges = <0x00000000 0x4a000000 0x0022c000>;
phandle = <0x0000013a>;
scm@2000 {
compatible = "ti,dra7-scm-core", "simple-bus";
reg = <0x00002000 0x00002000>;
ranges = <0x00000000 0x00002000 0x00002000>;
phandle = <0x0000013b>;
...
pinmux@1400 {
compatible = "ti,dra7-padconf", "pinctrl-single";
reg = <0x00001400 0x00000468>;
interrupt-controller;
pinctrl-single,register-width = <0x00000020>;
pinctrl-single,function-mask = <0x3fffffff>;
phandle = <0x0000013f>;
...
cape_pins_default {
pinctrl-single,pins = <
...
0x00000144 0x0001000f /* vin1a_d20.off */
0x0000028c 0x00060009 /* gpio6_15.i2c3_scl */
0x00000288 0x00060009 /* gpio6_14.i2c3_sda */
...
>;
phandle = <0x00000137>;
};
...
};
...
};
...
};
...
i2c@48060000 {
compatible = "ti,omap4-i2c";
reg = <0x48060000 0x00000100>;
interrupts = <0x00000000 0x00000038 0x00000004>;
ti,hwmods = "i2c3";
status = "okay";
clock-frequency = <0x00061a80>;
phandle = <0x000001db>;
i2c_mpu6050@68 {
compatible = "charming,i2c_mpu6050";
reg = <0x68>;
status = "okay";
};
};
...
};
...
执行如下命令编译设备树文件:
dtc -I dts -O dtb -o am5729-beagleboneai.dtb am5729-beagleboneai.dts
然后将编译好的am5729-beagleboneai.dtb文件替换掉开发板根文件系统中的/boot/dtbs/4.14.108-ti-r143/am5729-beagleboneai.dtb 文件。
二、编写程序
i2c_mpu6050.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/i2c.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.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 0x6B
#define WHO_AM_I 0x75
#define DEV_NAME "I2C3_mpu6050"
#define DEV_CNT (1)
static dev_t mpu6050_devno;
static struct cdev mpu6050_chr_dev;
struct class *class_mpu6050;
struct device *device_mpu6050;
struct device_node *mpu6050_device_node;
struct i2c_client *mpu6050_client = NULL;
static int i2c_write_mpu6050(struct i2c_client *mpu6050_client, u8 address, u8 data)
{
int error = 0;
u8 write_data[2];
struct i2c_msg send_msg;
write_data[0] = address;
write_data[1] = data;
send_msg.addr = mpu6050_client->addr;
send_msg.flags = 0;
send_msg.buf = write_data;
send_msg.len = 2;
error = i2c_transfer(mpu6050_client->adapter, &send_msg, 1);
if (error != 1)
{
printk(KERN_DEBUG "\n i2c_transfer error \n");
return -1;
}
return 0;
}
static int i2c_read_mpu6050(struct i2c_client *mpu6050_client, u8 address, void *data, u32 length)
{
int error = 0;
u8 address_data = address;
struct i2c_msg mpu6050_msg[2];
mpu6050_msg[0].addr = mpu6050_client->addr;
mpu6050_msg[0].flags = 0;
mpu6050_msg[0].buf = &address_data;
mpu6050_msg[0].len = 1;
mpu6050_msg[1].addr = mpu6050_client->addr;
mpu6050_msg[1].flags = I2C_M_RD;
mpu6050_msg[1].buf = data;
mpu6050_msg[1].len = length;
error = i2c_transfer(mpu6050_client->adapter, mpu6050_msg, 2);
if (error != 2)
{
printk(KERN_DEBUG "\n i2c_read_mpu6050 error \n");
return -1;
}
return 0;
}
static int mpu6050_init(void)
{
int error = 0;
error += i2c_write_mpu6050(mpu6050_client, PWR_MGMT_1, 0X00);
error += i2c_write_mpu6050(mpu6050_client, SMPLRT_DIV, 0X07);
error += i2c_write_mpu6050(mpu6050_client, CONFIG, 0X06);
error += i2c_write_mpu6050(mpu6050_client, ACCEL_CONFIG, 0X01);
if (error < 0)
{
printk(KERN_DEBUG "\n mpu6050_init error \n");
return -1;
}
return 0;
}
static int mpu6050_open(struct inode *inode, struct file *filp)
{
mpu6050_init();
return 0;
}
static ssize_t mpu6050_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
char data_H;
char data_L;
int error;
short mpu6050_result[6];
i2c_read_mpu6050(mpu6050_client, ACCEL_XOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, ACCEL_XOUT_L, &data_L, 1);
mpu6050_result[0] = data_H << 8;
mpu6050_result[0] += data_L;
i2c_read_mpu6050(mpu6050_client, ACCEL_YOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, ACCEL_YOUT_L, &data_L, 1);
mpu6050_result[1] = data_H << 8;
mpu6050_result[1] += data_L;
i2c_read_mpu6050(mpu6050_client, ACCEL_ZOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, ACCEL_ZOUT_L, &data_L, 1);
mpu6050_result[2] = data_H << 8;
mpu6050_result[2] += data_L;
i2c_read_mpu6050(mpu6050_client, GYRO_XOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, GYRO_XOUT_L, &data_L, 1);
mpu6050_result[3] = data_H << 8;
mpu6050_result[3] += data_L;
i2c_read_mpu6050(mpu6050_client, GYRO_YOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, GYRO_YOUT_L, &data_L, 1);
mpu6050_result[4] = data_H << 8;
mpu6050_result[4] += data_L;
i2c_read_mpu6050(mpu6050_client, GYRO_ZOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, GYRO_ZOUT_L, &data_L, 1);
mpu6050_result[5] = data_H << 8;
mpu6050_result[5] += data_L;
error = copy_to_user(buf, mpu6050_result, cnt);
if(error != 0)
{
printk("copy_to_user error!");
return -1;
}
return 0;
}
static int mpu6050_release(struct inode *inode, struct file *filp)
{
return 0;
}
static struct file_operations mpu6050_chr_dev_fops =
{
.owner = THIS_MODULE,
.open = mpu6050_open,
.read = mpu6050_read,
.release = mpu6050_release,
};
static int mpu6050_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int ret = -1;
printk(KERN_EMERG "\t match successed \n");
ret = alloc_chrdev_region(&mpu6050_devno, 0, DEV_CNT, DEV_NAME);
if (ret < 0)
{
printk("fail to alloc mpu6050_devno\n");
goto alloc_err;
}
mpu6050_chr_dev.owner = THIS_MODULE;
cdev_init(&mpu6050_chr_dev, &mpu6050_chr_dev_fops);
ret = cdev_add(&mpu6050_chr_dev, mpu6050_devno, DEV_CNT);
if (ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
class_mpu6050 = class_create(THIS_MODULE, DEV_NAME);
device_mpu6050 = device_create(class_mpu6050, NULL, mpu6050_devno, NULL, DEV_NAME);
mpu6050_client = client;
return 0;
add_err:
unregister_chrdev_region(mpu6050_devno, DEV_CNT);
printk("\n error! \n");
alloc_err:
return -1;
}
static int mpu6050_remove(struct i2c_client *client)
{
device_destroy(class_mpu6050, mpu6050_devno);
class_destroy(class_mpu6050);
cdev_del(&mpu6050_chr_dev);
unregister_chrdev_region(mpu6050_devno, DEV_CNT);
return 0;
}
static const struct i2c_device_id gtp_device_id[] = {
{"charming,i2c_mpu6050", 0},
{}};
static const struct of_device_id mpu6050_of_match_table[] = {
{.compatible = "charming,i2c_mpu6050"},
{}};
struct i2c_driver mpu6050_driver = {
.probe = mpu6050_probe,
.remove = mpu6050_remove,
.id_table = gtp_device_id,
.driver = {
.name = "charming,i2c_mpu6050",
.owner = THIS_MODULE,
.of_match_table = mpu6050_of_match_table,
},
};
static int __init mpu6050_driver_init(void)
{
int ret;
pr_info("mpu6050_driver_init\n");
ret = i2c_add_driver(&mpu6050_driver);
return ret;
}
static void __exit mpu6050_driver_exit(void)
{
pr_info("mpu6050_driver_exit\n");
i2c_del_driver(&mpu6050_driver);
}
module_init(mpu6050_driver_init);
module_exit(mpu6050_driver_exit);
MODULE_LICENSE("GPL");
app.c:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
short resive_data[6];
int fd = open("/dev/I2C3_mpu6050", O_RDWR);
if(fd < 0)
{
printf("open file : %s failed !\n", argv[0]);
return -1;
}
int error = read(fd,resive_data,12);
if(error < 0)
{
printf("write file error! \n");
close(fd);
}
printf("AX=%d, AY=%d, AZ=%d ",(int)resive_data[0],(int)resive_data[1],(int)resive_data[2]);
printf(" GX=%d, GY=%d, GZ=%d \n \n",(int)resive_data[3],(int)resive_data[4],(int)resive_data[5]);
error = close(fd);
if(error < 0)
{
printf("close file error! \n");
}
return 0;
}
Makefile:
KERNEL_DIR=/opt/ti-processor-sdk-linux-am57xx-evm-05.03.00.07/board-support/linux-4.14.79+gitAUTOINC+e669d52447-ge669d52447
ARCH=arm
CROSS_COMPILE=/opt/ti-processor-sdk-linux-am57xx-evm-05.03.00.07/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-
export ARCH CROSS_COMPILE
obj-m := i2c_mpu6050.o
all:app
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
app:
${CROSS_COMPILE}gcc app.c -o App
.PHONE:clean copy
clean:
rm *.o *.order *.symvers *.mod.c
delete:
rm App i2c_mpu6050.ko
执行如下命令编译驱动程序和应用程序:
make
make clean
三、运行程序
执行如下命令加载i2c_mpu6050.ko模块,并且执行App:
debian@beaglebone:~$ sudo insmod i2c_mpu6050.ko
[sudo] password for debian:
[ 112.814058] match successed
Message from syslogd@beaglebone at Mar 22 06:01:50 ...
kernel:[ 112.814058] match successed
debian@beaglebone:~$ chmod +x ./App
debian@beaglebone:~$ sudo ./App
AX=206, AY=14350, AZ=7764 GX=-246, GY=-126, GZ=-29
debian@beaglebone:~$ sudo ./App
AX=212, AY=14332, AZ=7772 GX=-302, GY=-126, GZ=-29
|