Linux I2C device驱动

本文详细介绍了Linux内核中I2C设备驱动的注册过程、数据读写操作,以及I2C总线驱动的工作原理。从获取I2C适配器到创建设备和驱动,再到实际的数据传输,阐述了驱动开发的关键步骤。同时强调了I2C交易的异步特性,不适合在中断服务例程中使用。
摘要由CSDN通过智能技术生成

零. Linux I2C驱动框架

 一. I2C device Driver in Linux Kernel注册过程

  1. Get the I2C adapter.
  2. Create the oled_i2c_board_info structure and create a device using that.
  3. Create the i2c_device_id for your slave device and register that.
  4. Create the i2c_driver structure and add that to the I2C subsystem.
  5. Now the driver is ready. So you can transfer the data between master and slave.
  6. Once you are done, then remove the device.

1.Get the I2C adapter

You can check that using the command ls -al /sys/bus/i2c/devices/.

        struct i2c_adapter *i2c_get_adapter(int nr);

2.Create the board info(是一个结构体)

struct i2c_board_info {
  char type[I2C_NAME_SIZE];
  unsigned short flags;
  unsigned short addr;
  void * platform_data;
  struct dev_archdata * archdata;
  struct device_node * of_node;
  struct fwnode_handle * fwnode;
  int irq;
};

You can use I2C_BOARD_INFO macro to initialize the essential fields of struct i2c_board_info.

I2C_BOARD_INFO ( dev_type, dev_addr);

dev_type – identifies the device type
dev_addr – the device’s address on the bus

3.Create Device

struct i2c_client * i2c_new_device ( struct i2c_adapter * adap, struct i2c_board_info const * info);

*adap – Adapter structure that we got from i2c_get_adapter()
*info – Board info structure that we have created

This will return the i2c_client structure. We can use this client for our future transfers.

4.Create the device id(一个结构)

struct i2c_device_id {
  char name[I2C_NAME_SIZE];
  kernel_ulong_t driver_data;
};

4.Create the i2c_driver(一个结构)

struct i2c_driver {
  unsigned int class;
  int (* attach_adapter) (struct i2c_adapter *);
  int (* probe) (struct i2c_client *, const struct i2c_device_id *);
  int (* remove) (struct i2c_client *);
  void (* shutdown) (struct i2c_client *);
  void (* alert) (struct i2c_client *, unsigned int data);
  int (* command) (struct i2c_client *client, unsigned i
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值