New Linux2.6 I2C Driver Model Example
来源:互联网 发布:淘宝跳失率怎么算 编辑:程序博客网 时间:2024/05/29 10:24
- //////////////////////////////////////////////////////////////////////////
- // COPYRIGHT NOTICE
- // Copyright (c) 2012, 华中科技大学 卢俊(版权声明)
- // All rights reserved.
- //
- /// @file mydev.c
- /// @brief i2c driver示例代码
- ///
- /// Linux2.6.32 new i2c driver model example
- ///
- /// @version 1.0
- /// @author lujun
- /// @E-mail lujun.hust@gmail.com
- /// @date 2012/08/24
- //
- //
- // 修订说明:
- //////////////////////////////////////////////////////////////////////////
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/slab.h>
- #include <linux/init.h>
- #include <linux/list.h>
- #include <linux/i2c.h>
- #include <linux/i2c-dev.h>
- #include <linux/smp_lock.h>
- #include <linux/jiffies.h>
- #include <asm/uaccess.h>
- #include <linux/delay.h>
- #define I2C_DEV_NAME "MyDevice" //这个名字要跟board_info中的名字一致,才会与I2C_Client匹配
- static struct i2c_device_id my_id[] = {
- {I2C_DEV_NAME,0}, //作为i2c client 与 driver 匹配的关键词
- {}
- };
- MODULE_DEVICE_TABLE(i2c, my_id);
- //I2c client对象指针,给本模块i2c read/write提供参数,在probe成功后获取
- static struct i2c_client *my_client;
- //新的I2C Client模型采用probe方式,老的接口采用的是attach_adapter
- static int my_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
- {
- //系统探测到了本模块的i2c设备,保存得到的i2c对象指针,供read/write参数
- my_client = client;
- return 0;
- }
- static int my_i2c_remove(struct i2c_client *client)
- {
- //释放I2C Client对象
- if( my_client != NULL ) {
- i2c_unregister_device(my_client);
- }
- return 0;
- }
- static struct i2c_driver my_driver = {
- .driver = {
- .name = "my i2c driver",
- .owner = THIS_MODULE,
- },
- .probe = my_i2c_probe,
- .remove = __devexit_p(my_i2c_remove),
- .id_table = my_id,
- };
- static int __init my_i2c_init(void)
- {
- //i2c_add_driver函数内部会根据my_driver提供的id_table在注册到系统中的boardinfo列表中找到匹配的client,然后调用my_driver的probe函数,将构造出来的i2c client传递过来
- return i2c_add_driver(&my_driver);
- }
- static void __exit my_i2c_exit(void)
- {
- //删除驱动,解除绑定
- i2c_del_driver(&my_driver);
- }
- int my_i2c_write( uint8_t reg,uint8_t data )
- {
- unsigned char buffer[2];
- buffer[0] = reg;
- buffer[1] = data;
- if( 2!= i2c_master_send(my_client,buffer,2) ) {
- printk( KERN_ERR "my i2c send fail! \n" );
- return -1;
- }
- return 0;
- }
- int my_i2c_read( uint8_t reg,uint8_t *data )
- {
- // write reg addr
- if( 1!= i2c_master_send(my_client,®,1) ) {
- printk( KERN_ERR "my i2c recv fail! \n" );
- return -1;
- }
- // wait
- msleep(10);
- // read
- if( 1!= i2c_master_recv(my_client,data,1) ) {
- printk( KERN_ERR "my i2c recv fail! \n" );
- return -1;
- }
- return 0;
- }
- MODULE_DESCRIPTION("my i2c driver");
- MODULE_AUTHOR("Lujun @HUST");
- MODULE_LICENSE("GPL");
- module_init(my_i2c_init);
- module_exit(my_i2c_exit);
- static struct i2c_board_info __initdata i2c_info[] = {
- {
- I2C_BOARD_INFO("24c256", 0x50),
- .platform_data = &eeprom_info,
- },
- {
- I2C_BOARD_INFO("MyDevice", (0xbc>>1)),
- .platform_data = NULL,
- },
- };
- //这个函数将会使用info提供的信息建立一个i2c_client并与第一个参数指向的i2c_adapter绑定。返回的参数是一个i2c_client指针。
- struct i2c_client * i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info);
- //获取i2c_adapter的函数如下,它的参数是i2c总线编号。
- struct i2c_adapter* i2c_get_adapter(int id);
- //如何知道i2c总线编号呢
- ls /sys/class/i2c-adapter/
- //即可看到对应的I2C adapter编号,i2c-0代表编号为0,i2c-1代表编号为1
- //自定义boardinfo信息
- static struct i2c_board_info my_dev_info[] __initdata = {
- {
- I2C_BOARD_INFO(I2C_DEV_NAME,I2C_DEV_ADDR), //设备名称和地址(很关键)
- .platform_data= NULL, // 传递私有数据,赋值给client->dev->platform_data
- },
- };
- //ls /sys/class/i2c-adapter/得到的编号
- static int sys_adap_bus_num = 0;
- static int __init my_i2c_init(void)
- {
- struct i2c_adapter* adap = i2c_get_adapter(sys_adap_bus_num);
- if(adap==NULL) {
- printk("[LUJUN-DEBUG] i2c_get_adapter fail!\n");
- return -1;
- }
- //动态构造i2c client对象
- my_client = i2c_new_device(adap, &my_dev_info[0]);
- if( my_client==NULL ){
- printk("[LUJUN-DEBUG] i2c_new_device fail!\n");
- return -1;
- }
- //释放资源
- i2c_put_adapter(adap);
- //函数内部会找到匹配的driver和client,然后调用probe
- return i2c_add_driver(&my_driver);
- }
0 0
- New Linux2.6 I2C Driver Model Example
- I2C的Standard driver model驱动
- Linux2.6 I2C子系统分析
- linux2.6下内核I2C分析
- Xenomai: Hard Real Time Driver Example Tutorial with MMAP using the RTDM (Real Time Driver Model)
- i2c driver
- I2C DRIVER
- linux2.6内核SD Card Driver详细解析之一
- linux2.6内核SD Card Driver详细解析之一
- driver: linux2.6 内核模块导出函数实例(EXPORT_SYMBOL)
- driver: linux2.6 内核模块导出函数实例(EXPORT_SYMBOL)
- driver: linux2.6 内核模块导出函数实例(EXPORT_SYMBOL)
- linux2.6内核SD Card Driver详细解析之一
- driver: linux2.6 内核模块导出函数实例(EXPORT_SYMBOL)
- linux2.6内核SD Card Driver详细解析
- Driver development code example.
- Linux i2c bus driver
- i2c总线 && driver
- Js学习---妙味课堂1-4 (css + arguments)
- Js学习---妙味课堂1-5
- Js学习---妙味课堂2-1
- Js学习---妙味课堂2-2 (说一些DOM的基础知识)
- Js学习---妙味课堂2-3 (BOM基础)
- New Linux2.6 I2C Driver Model Example
- Js学习---妙味课堂3-1 (事件对象和事件冒泡)--- 获取鼠标+键盘事件
- CF---C. Mashmokh and Numbers
- CF---C. Unusual Product
- 算法竞赛入门经典---函数部分4(动态规划)
- 2014.4.10 Web主机服务器建立
- 背景颜色渐变 css3 ---- 转自:至尊宝的BLOG http://blog.sina.com.cn/zzbnie
- 妙味课堂:css学习--- 制作一个网页html+css过程
- Web --- 课堂Js学习总结 --- 14年4月22日