Linux驱动开发(使用I2C总线设备驱动模型编写AT24C02驱动程序)

简介: Linux驱动开发(使用I2C总线设备驱动模型编写AT24C02驱动程序)

前言

本篇文章将讲解如何使用I2C总线设备驱动模型编写AT24C02驱动程序。

一、I2C总线设备驱动模型

I2C设备模型驱动程序是一种新的I2C设备驱动模型,引入了设备树(Device Tree)这一机制,可以在I2C设备和相应的Linux设备节点之间建立关联。在I2C设备模型中,所有I2C设备节点共用一个I2C设备模型驱动程序,不需要为每个I2C设备节点编写独立的设备驱动程序。

下图来自百问网:

在i2c总线下分别有i2c_client和i2c_driver。i2c_client就是硬件设备(比如本篇文章用到的AT24C02),i2c_driver就是我们需要编写的驱动程序。

i2c_client由设备树提供。

i2c_driver是我们自己编写的驱动程序,里面提供了probe函数,当驱动和设备树中的compatible属性匹配后调用probe函数。


二、设备树编写

因为我使用的AT24C02是挂载在i2c1这根总线上的所有需要在i2c1这个节点下添加at24c02这个子节点。

reg属性代表的是AT24C02的设备地址。

&i2c1 {
    clock-frequency = <100000>;
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_i2c1>;
    status = "okay";
        at24c02 {
                compatible = "my,at24c02";
                reg = <0x50>;
        };
};

三、驱动程序编写

1.提供i2c_driver结构体变量并且注册

这里和之前编写的驱动程序的思路都是一样的提供一个driver结构体变量并且将其注册。

这里和之前最大的不同就是需要在i2c_driver结构体变量中提供id_table成员。

在内核源码中发现缺少了probe函数或者缺少了id_table成员都是无法进行正确的匹配的。

static const struct of_device_id at24c02_of_match[] = {
  {.compatible = "my,at24c02"},
  {}
};
static const struct i2c_device_id at24c02_ids[] = {
  { "xxxxyyy",  (kernel_ulong_t)NULL },
  { /* END OF LIST */ }
};
static struct i2c_driver at24c02_drv = {
  .driver = {
    .name = "myat24c02",
    .of_match_table = at24c02_of_match,
  },
  .probe = at24c02_probe,
  .remove = at24c02_remove,
  .id_table = at24c02_ids,
};
/* 2. 在入口函数注册platform_driver */
static int __init at24c02_init(void)
{
    int err;
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    return i2c_add_driver(&at24c02_drv);
  return err;
}
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 *     卸载platform_driver
 */
static void __exit at24c02_exit(void)
{
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    i2c_del_driver(&at24c02_drv);
}
/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */
module_init(at24c02_init);
module_exit(at24c02_exit);
MODULE_LICENSE("GPL");

2.注册file_operations结构体

这里我们使用ioctl来操作AT24C02,ioctl既可以读又可以写,可以对read和write函数进行替换。

/* 定义自己的file_operations结构体                                              */
static struct file_operations at24c02_fops = {
  .owner   = THIS_MODULE,
  .unlocked_ioctl    = at24c02_chrdev_ioctl,
};
static int at24c02_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
  at24c02_client = client;
  /* 注册file_operations  */
  major = register_chrdev(0, "100ask_at24c02", &at24c02_fops);  /* /dev/at24c02 */
  at24c02_class = class_create(THIS_MODULE, "100ask_at24c02_class");
  if (IS_ERR(at24c02_class)) {
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    unregister_chrdev(major, "100ask_at24c02");
    return PTR_ERR(at24c02_class);
  }
  device_create(at24c02_class, NULL, MKDEV(major, 0), NULL, "100ask_at24c02"); /* /dev/100ask_at24c02 */
  return 0;
}
static int at24c02_remove(struct i2c_client *client)
{
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
  device_destroy(at24c02_class, MKDEV(major, 0));
  class_destroy(at24c02_class);
  unregister_chrdev(major, "100ask_at24c02");
  return 0;
}

3.操作AT24C02

根据AT24C02的数据手册我们可以清楚的知道如何去读写AT24C02。

写AT24C02时序:

写AT24C02时需要发送设备地址和需要写入的数据,写入数据的地址,只需要构造一个msg消息即可。

读AT24C02时序:

读AT24C02时首先需要写入设备地址,再去读取指定要读取数据的地址。 然后再发起一次操作,指定设备地址,指定读取数据保存的地址。一共需要构造两个msg消息。

#define IOC_AT24C02_READ  100
#define IOC_AT24C02_WRITE 101
/* 主设备号                                                                 */
static int major = 0;
static struct class *at24c02_class;
struct i2c_client *at24c02_client;
static long at24c02_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
  unsigned char addr;
  unsigned char data;
  unsigned int ker_buf[2];
  unsigned int *usr_buf = (unsigned int *)arg;
  unsigned char byte_buf[2];
  struct i2c_msg msgs[2];
  copy_from_user(ker_buf, usr_buf, 8);
  addr = ker_buf[0];
  switch (cmd)
  {
    case IOC_AT24C02_READ:
    {
      /* 读AT24C02 */
      msgs[0].addr  = at24c02_client->addr;
      msgs[0].flags = 0; /* 写 */
      msgs[0].len   = 1;
      msgs[0].buf   = &addr;
      msgs[1].addr  = at24c02_client->addr;
      msgs[1].flags = I2C_M_RD; /* 读 */
      msgs[1].len   = 1;
      msgs[1].buf   = &data;
      i2c_transfer(at24c02_client->adapter, msgs, 2);
      ker_buf[1] = data;
      copy_to_user(usr_buf, ker_buf, 8);
      break;
    }
    case IOC_AT24C02_WRITE:
    {
      /* 写AT24C02 */
      byte_buf[0] = addr;
      byte_buf[1] = ker_buf[1];
      msgs[0].addr  = at24c02_client->addr;
      msgs[0].flags = 0; /* 写 */
      msgs[0].len   = 2;
      msgs[0].buf   = byte_buf;
      i2c_transfer(at24c02_client->adapter, msgs, 1);
      mdelay(20);
      break;
    }
  }
  return 0;
}

完整代码:

#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/mod_devicetable.h>
#include <linux/log2.h>
#include <linux/bitops.h>
#include <linux/jiffies.h>
#include <linux/of.h>
#include <linux/acpi.h>
#include <linux/i2c.h>
#include <asm/uaccess.h>
#define IOC_AT24C02_READ  100
#define IOC_AT24C02_WRITE 101
/* 主设备号                                                                 */
static int major = 0;
static struct class *at24c02_class;
struct i2c_client *at24c02_client;
static long at24c02_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
  unsigned char addr;
  unsigned char data;
  unsigned int ker_buf[2];
  unsigned int *usr_buf = (unsigned int *)arg;
  unsigned char byte_buf[2];
  struct i2c_msg msgs[2];
  copy_from_user(ker_buf, usr_buf, 8);
  addr = ker_buf[0];
  switch (cmd)
  {
    case IOC_AT24C02_READ:
    {
      /* 读AT24C02 */
      msgs[0].addr  = at24c02_client->addr;
      msgs[0].flags = 0; /* 写 */
      msgs[0].len   = 1;
      msgs[0].buf   = &addr;
      msgs[1].addr  = at24c02_client->addr;
      msgs[1].flags = I2C_M_RD; /* 读 */
      msgs[1].len   = 1;
      msgs[1].buf   = &data;
      i2c_transfer(at24c02_client->adapter, msgs, 2);
      ker_buf[1] = data;
      copy_to_user(usr_buf, ker_buf, 8);
      break;
    }
    case IOC_AT24C02_WRITE:
    {
      /* 写AT24C02 */
      byte_buf[0] = addr;
      byte_buf[1] = ker_buf[1];
      msgs[0].addr  = at24c02_client->addr;
      msgs[0].flags = 0; /* 写 */
      msgs[0].len   = 2;
      msgs[0].buf   = byte_buf;
      i2c_transfer(at24c02_client->adapter, msgs, 1);
      mdelay(20);
      break;
    }
  }
  return 0;
}
/* 定义自己的file_operations结构体                                              */
static struct file_operations at24c02_fops = {
  .owner   = THIS_MODULE,
  .unlocked_ioctl    = at24c02_chrdev_ioctl,
};
static int at24c02_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
  at24c02_client = client;
  /* 注册file_operations  */
  major = register_chrdev(0, "100ask_at24c02", &at24c02_fops);  /* /dev/at24c02 */
  at24c02_class = class_create(THIS_MODULE, "100ask_at24c02_class");
  if (IS_ERR(at24c02_class)) {
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    unregister_chrdev(major, "100ask_at24c02");
    return PTR_ERR(at24c02_class);
  }
  device_create(at24c02_class, NULL, MKDEV(major, 0), NULL, "100ask_at24c02"); /* /dev/100ask_at24c02 */
  return 0;
}
static int at24c02_remove(struct i2c_client *client)
{
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
  device_destroy(at24c02_class, MKDEV(major, 0));
  class_destroy(at24c02_class);
  unregister_chrdev(major, "100ask_at24c02");
  return 0;
}
static const struct of_device_id at24c02_of_match[] = {
  {.compatible = "my,at24c02"},
  {}
};
static const struct i2c_device_id at24c02_ids[] = {
  { "xxxxyyy",  (kernel_ulong_t)NULL },
  { /* END OF LIST */ }
};
static struct i2c_driver at24c02_drv = {
  .driver = {
    .name = "myat24c02",
    .of_match_table = at24c02_of_match,
  },
  .probe = at24c02_probe,
  .remove = at24c02_remove,
  .id_table = at24c02_ids,
};
/* 2. 在入口函数注册platform_driver */
static int __init at24c02_init(void)
{
    int err;
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    return i2c_add_driver(&at24c02_drv);
  return err;
}
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 *     卸载platform_driver
 */
static void __exit at24c02_exit(void)
{
  printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    i2c_del_driver(&at24c02_drv);
}
/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */
module_init(at24c02_init);
module_exit(at24c02_exit);
MODULE_LICENSE("GPL");

四、应用程序编写

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define IOC_AT24C02_READ  100
#define IOC_AT24C02_WRITE 101
/*
 * at24c02_test /dev/myat24c02 r 10
 * at24c02_test /dev/myat24c02 w 10 123
 */
int main(int argc, char **argv)
{
  int fd;
  int buf[2];
  if ((argc != 4) && (argc != 5))
  {
    printf("Usage: %s <dev> r <addr>\n", argv[0]);
    printf("       %s <dev> w <addr> <val>\n", argv[0]);
    return -1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd < 0)
  {
    printf(" can not open %s\n", argv[1]);
    return -1;
  }
  if (argv[2][0] == 'r')
  {
    buf[0] = strtoul(argv[3], NULL, 0);
    ioctl(fd, IOC_AT24C02_READ, buf);
    printf("Read addr 0x%x, get data 0x%x\n", buf[0], buf[1]);
  }
  else
  {
    buf[0] = strtoul(argv[3], NULL, 0);
    buf[1] = strtoul(argv[4], NULL, 0);
    ioctl(fd, IOC_AT24C02_WRITE, buf);
  }
  return 0;
}

五、上机测试

装载驱动后进入/sys/bus/i2c/devices目录下找到我们自己编写的驱动程序:

进入0-0050目录使用cat命令查看具体信息:

根据信息可以得知驱动程序装载成功。

进行at24c02的读写操作:

读写测试通过。


总结

本篇文章主要讲解了i2C总线设备驱动模型编写AT24C02驱动程序,这里大家主要需要掌握的就是i2C总线设备驱动这个模型,只要掌握好了这个模型那么剩下的就是裸机的操作了。


相关文章
|
7月前
|
监控 Linux 开发者
理解Linux操作系统内核中物理设备驱动(phy driver)的功能。
综合来看,物理设备驱动在Linux系统中的作用是至关重要的,它通过与硬件设备的紧密配合,为上层应用提供稳定可靠的通信基础设施。开发一款优秀的物理设备驱动需要开发者具备深厚的硬件知识、熟练的编程技能以及对Linux内核架构的深入理解,以确保驱动程序能在不同的硬件平台和网络条件下都能提供最优的性能。
403 0
|
缓存 安全 Linux
Linux 五种IO模型
Linux 五种IO模型
|
Java Linux API
Linux设备驱动开发详解2
Linux设备驱动开发详解
284 6
|
消息中间件 算法 Unix
Linux设备驱动开发详解1
Linux设备驱动开发详解
343 5
|
Linux API
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
|
5月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
629 1
二、Linux文本处理与文件操作核心命令
|
5月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
404 137
|
5月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
958 57
|
4月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
877 2