Linux Drive I²c subsystem mpu6050 device driver

Source: Internet
Author: User

The following is a simple drive implementation of the mpu6050, mpu6050 is the I²c interface of the 6-axis sensor, can be registered as a character device to the kernel, the code is running the environment is the 3.4.2 kernel, 4.3.2 version of the compilation chain, 12.04 version of Ubuntu, the hardware environment is the jz2440 Development Board;

According to the previous analysis of the I²c drive frame, the mpu6050 drive is mainly to achieve the peripheral side of the drive, mainly registered peripherals to the I²C bus, and the peripheral terminal registered to the I²C bus including device and driver two parts registered to the I²c bus, the use of separate design ideas, see the code for details:

Device registered to the I²C bus:

#include <linux/kernel.h>#include<linux/module.h>#include<linux/platform_device.h>#include<linux/i2c.h>#include<linux/err.h>#include<linux/regmap.h>#include<linux/slab.h>Static ConstUnsigned ShortAddr_list[] = {0x50,0x68, i2c_client_end};Static structI2c_board_info Mpu6050_info ={. Type="mpu6050",};Static structI2c_client *mpu6050_client;Static intMpu6050dev_init (void ){    structI2c_adapter *I2c_adap; I2c_adap= I2c_get_adapter (0 ); Mpu6050_client= I2c_new_probed_device (I2c_adap, &Mpu6050_info, Addr_list, NULL);    I2c_put_adapter (I2C_ADAP); if(mpu6050_client)return 0; Else        return-Enodev;}Static voidMpu6050dev_exit (void) {i2c_unregister_device (mpu6050_client);} Module_init (Mpu6050dev_init); Module_exit (Mpu6050dev_exit); Module_license ("GPL");

Note: In order to implement dynamic loading of the driver module, using the I2c_new_probed_device function, the function of the key role is to register the device before the bus, will determine whether the device's address is true or not, or it can be said that the device is real, you can also use the I2c_new_ The device function is to load an appliance to the bus, but the function is similar to the total Force property of the I²c bus, forcing the thought of the loaded device address to be true, or forcing the current device to exist, and calling the i2c_get_ when the mpu6050 is associated with the I²C adapter. Adapter (0) function, because s3c2440 has only one i²c adapter, so the parameters of the function directly write 0, that is, the first adapter;

Next is the device driver loaded into the I²C bus:

#include <linux/kernel.h>#include<linux/module.h>#include<linux/platform_device.h>#include<linux/i2c.h>#include<linux/err.h>#include<linux/regmap.h>#include<linux/slab.h>#include<linux/fs.h>#include<asm/uaccess.h>#include<linux/cdev.h>Static intMpu6050_open (structInode *inode,structFile *file);Staticssize_t Mpu6050_write (structFile *file,Const Char__user *buf, size_t, loff_t *offset);Staticssize_t Mpu6050_read (structFile *file,Char__user *buf, size_t size, loff_t *offset);Static intMpu6050drv_probe (structI2c_client *i2c_client,Const structI2C_DEVICE_ID *i2c_device_id);Static intMpu6050drv_remove (structI2c_client *i2c_client);Static structi2c_device_id mpu6050drv_id ={. Name="mpu6050",. Driver_data=0x68,};Static structI2c_driver Mpu6050drv ={. Driver={. Name="mympu6050",. Owner=This_module,},. Probe=mpu6050drv_probe,. Remove=mpu6050drv_remove,. Id_table= &mpu6050drv_id,};/*character device related*/intMajor =0;Static structCdev Mpu6050_cdev;Static structFile_operations Mpu6050ops ={. Owner=this_module,. Read=Mpu6050_read,. Write=mpu6050_write,. Open=Mpu6050_open,};Static struct class*CLS;Staticssize_t Mpu6050_read (structFile *file,Char__user *buf, size_t size, loff_t *offset) {    return 0;}Staticssize_t Mpu6050_write (structFile *file,Const Char__user *buf, size_t size, loff_t *offset) {    return 0;}Static intMpu6050_open (structInode *inode,structFile *file) {    /*can do low-power switch, call Open, only enable the mpu6050 module, circuit no such function, so do not need to initialize this function*/    return 0;}Static intMpu6050drv_probe (structI2c_client *i2c_client,Const structI2C_DEVICE_ID *i2c_device_id) {dev_t Dev=0; PRINTK ("mpu6050drv_probe\r\n" );#if0/*Register mpu6050 as a character device to the kernel*/Major= Register_chrdev (0,"mpu6050", &mpu6050ops);#elseAlloc_chrdev_region (&dev,0,2,"mpu6050_region");//occupies 2 secondary device numbersCdev_init (&mpu6050_cdev, &mpu6050ops); Mpu6050_cdev.owner=This_module; Cdev_add (&mpu6050_cdev, Dev,2);//occupies 2 secondary device numbers#endifCLS= Class_create (This_module,"Mpu6050cls" ); Major=MAJOR (Dev); Device_create (CLS, NULL, MKDEV (Major,0), NULL,"mpu6050" );//device_create (CLS, NULL, MKDEV (major, 1), NULL, "mpu6050_2");    return 0;}Static intMpu6050drv_remove (structI2c_client *i2c_client) {PRINTK ("mpu6050drv_remove\r\n" ); Unregister_chrdev_region (0,1 ); Cdev_del (&Mpu6050_cdev); Device_destroy (CLS, MKDEV (Major,0 ) );//Device_destroy (CLS, MKDEV (Major, 1));Class_destroy (CLS); return 0;}Static intMpu6050_init (void) {I2c_add_driver (&mpu6050drv); return 0;}Static voidMpu6050_exit (void) {I2c_del_driver (&mpu6050drv);} Module_init (Mpu6050_init); Module_exit (Mpu6050_exit); Module_license ("GPL");

When the device is loaded into the I²C bus and the peripheral driver is loaded into the bus, I2ccore calls the match function, matching the name in the Id_table member of the mpu6050drv struct with the name of the loaded peripheral and binding if it is consistent. Then call the peripheral driver probe function, so that the I²c driver is basically completed, the next is based on the specific situation of the peripheral processing, such as if the peripheral is mpu6050, then as a character device registered to the kernel, and then write read and write functions, these are not the work of the i²c-driven framework, But the category of the character device driver;

This driver does not specifically to implement the mpu6050 read and write process, the specific code can borrow the bare metal version of the code, this example is only for the I²c drive frame reference.

Linux Drive I²c subsystem mpu6050 device driver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.