zoukankan      html  css  js  c++  java
  • mini2440 linuxi2c驱动

    #include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/delay.h> #include <linux/mutex.h> #include <linux/sysfs.h> #include <linux/mod_devicetable.h> #include <linux/log2.h> #include <linux/bitops.h> #include <linux/jiffies.h> #include <linux/i2c.h> #include <linux/i2c/at24.h> #include <linux/fs.h> #include <asm/uaccess.h>

    static struct i2c_client  *at24_client;

    static int major=0;

    static struct class  *cls;

    static const struct i2c_device_id at24_ids[] = {  

     {"at24",8},    {}, };

    MODULE_DEVICE_TABLE(i2c,at24_ids);

    static ssize_t at24_read(struct file *file, char __user *buf, size_t len, loff_t *offset)

    {  unsigned char address;  

    unsigned char data;

     struct i2c_msg msg[2];

     int ret;

     if(len!=1)   return -EINVAL;  copy_from_user(&address,buf,1);  msg[0].addr=at24_client->addr;  msg[0].buf=&address;  msg[0].len=1;  msg[0].flags=0;  msg[1].addr=at24_client->addr;  msg[1].buf=&data;  msg[1].len=1;  msg[1].flags =I2C_M_RD;  ret=i2c_transfer(at24_client->adapter,msg,2);  if(ret==2)   {    copy_to_user(buf,&data,1);    return 1;   }  else   return -EIO;  printk("read ! ");  return 0; } static ssize_t at24_write(struct file *file, const char __user *buf, size_t len, loff_t *offset) {  unsigned char val[2];  struct i2c_msg msg[1];  int ret;  if(len!=2)   return -EINVAL;  copy_from_user(val,buf,2);  msg[0].addr=at24_client->addr;  msg[0].buf=val;  msg[0].len=2;  msg[0].flags=0;  ret=i2c_transfer(at24_client->adapter,msg,1);  if(ret==1)  {   return 2;  }  else   return -EIO;  printk("write ! ");  return 0; }

    static struct file_operations fop={  .owner =THIS_MODULE,  .write  =at24_write,  .read   =at24_read, }; static int at24_probe(struct i2c_client *client, const struct i2c_device_id *ids) {  printk("probe ok! ");  at24_client=client;  major=register_chrdev(0,"at24",&fop);  cls=class_create(THIS_MODULE,"at24");  device_create(cls,NULL,MKDEV(major,0),NULL,"at24");  return 0; } static int at24_remove(struct i2c_client *client) {  kfree(at24_client);  unregister_chrdev(major,"at24");  device_destroy(cls,MKDEV(major,0));  class_destroy(cls);  printk("remove ok! ");  return 0; }

    static struct i2c_driver at24cx_driver = {  .driver = {   .name = "at24",   .owner = THIS_MODULE,  },  .probe = at24_probe,  .remove = at24_remove,  .id_table =at24_ids,   };

    static int at24cx_init(void) {  return i2c_add_driver(&at24cx_driver); }

    static void at24cx_exit(void) {  i2c_del_driver(&at24cx_driver); } module_init(at24cx_init); module_exit(at24cx_exit); MODULE_LICENSE("GPL");

    static struct at24_platform_data at24_i2c_info = {  

    .byte_len = 8,  .page_size = 8, };

    static struct i2c_board_info at24_i2c_devices[] =

    {   {         I2C_BOARD_INFO("at24", 0x50),     

        .platform_data = &at24_i2c_info,          },

    };

  • 相关阅读:
    《Dubbo》接入与调用流程
    SpringBoot自定义配置实现与解析
    Docker使用笔记记录
    Elasticsearch(一):介绍与安装
    索引的建立与使用规范
    RocketMQ(一):介绍与安装
    NETCORE使用MINIO的PRESIGNEDPUTOBJECTASYNC方法,实现通过浏览器直接上传图片到MINIO服务器
    Minio 使用.NET + Vue 实现断点续传、秒传
    VS Code中小程序与Vue常用插件合集(前端合集)
    [瞎想笔记] 浅浅浅谈期望
  • 原文地址:https://www.cnblogs.com/luxiaolai/p/4316889.html
Copyright © 2011-2022 走看看