Linux-IIC驱动(2)-Linux下IIC子系统的介绍

2024-08-31 06:38

本文主要是介绍Linux-IIC驱动(2)-Linux下IIC子系统的介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

IIC子系统架构

 

Linux下IIC的架构模型大概可以分为3层:

第一层是IIC的从设备驱动,他包含图中的device driver和i2c-dev。device driver需要用户编写,i2c-dev由内核实现,包含了IIC设备的通用方法,但是用户不能直接使用这个驱动,需要编写一个用户层驱动,它们2个合起来才可以实现一个驱动程序。

 

第二层总线驱动,他又叫做控制器驱动,比如说芯片内部的IIC控制器的使用需要实现一个驱动程序,比如所需要往IIC总线上传输数据它需要什么方法,需要实现那些函数等等。它包含图中的i2c-adapter和adapter-agio。

 

第三层是i2c-core。它作为一个媒介关联了总线驱动和i2c驱动。第二个作用就是它还提供了一个IIC驱动的注册和注销。

IIC总线驱动

 

图中我们可以知道编写i2c驱动有2种实现方法。

第一种是用户自己完全写一个i2c的驱动程序。

另一种方法是使用i2c的通用驱动i2c-dev,然后自己设计一个i2c用户模式驱动。

 

 

对于第一种情况,应用程序读写i2c设备的流程是比较明了的。

 

对于第二种情况的读写流程稍微有点复杂,我们先来介绍另一个比较重要的地方,就是i2c控制器驱动,i2c adapter/algorithm。

它是直接操作i2c设备的。里面包含2部分的东西,一个是adapter,适配器的意思,另一个是algorithm,算法的意思。

 

 

我们先来分析adapter,打开Linux源代码,搜索i2c_adapter

/** i2c_adapter is the structure used to identify a physical i2c bus along* with the access algorithms necessary to access it.*/
struct i2c_adapter {struct module *owner;unsigned int id;unsigned int class;		  /* classes to allow probing for */const struct i2c_algorithm *algo; /* the algorithm to access the bus */void *algo_data;/* data fields that are valid for all devices	*/u8 level; 			/* nesting level for lockdep */struct mutex bus_lock;int timeout;			/* in jiffies */int retries;struct device dev;		/* the adapter device */int nr;char name[48];struct completion dev_released;
};


在Linux中每一个i2c适配器或者说控制器都会有一个i2c_adapter来描述一个i2c控制器并确定i2c设备的访问方法。

里面有一个成员

const struct i2c_algorithm *algo; /* the algorithm to access the bus */

 

看看它的定义:

/** The following structs are for those who like to implement new bus drivers:* i2c_algorithm is the interface to a class of hardware solutions which can* be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584* to name two of the most common.*/
struct i2c_algorithm {/* If an adapter algorithm can't do I2C-level access, set master_xferto NULL. If an adapter algorithm can do SMBus access, setsmbus_xfer. If set to NULL, the SMBus protocol is simulatedusing common I2C messages *//* master_xfer should return the number of messages successfullyprocessed, or a negative value on error */int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,int num);int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,unsigned short flags, char read_write,u8 command, int size, union i2c_smbus_data *data);/* To determine what the adapter supports */u32 (*functionality) (struct i2c_adapter *);
};


这个结构里面主要是一些函数指针,最重要的要数master_xfer了。这个函数主要来实现需要在i2c总线上传输数据的方法。比如说CPU需要通过i2c总线往一个i2c设备发送一些数据,这时候就可以使用i2c控制器里面实现的传输方法,比如说xxx_transfer来实现对i2c设备的读写。这样就把i2c数据的传输给封装起来了。

 

 

我们接下来分析一下2440上i2c控制器驱动的实现,它的实现在i2c-s3c2410.c文件中实现。

 

先来分析一下模块初始化和退出函数:

MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);static struct platform_driver s3c24xx_i2c_driver = {.probe		= s3c24xx_i2c_probe,.remove		= s3c24xx_i2c_remove,.id_table	= s3c24xx_driver_ids,.driver		= {.owner	= THIS_MODULE,.name	= "s3c-i2c",.pm	= S3C24XX_DEV_PM_OPS,},
};static int __init i2c_adap_s3c_init(void)
{return platform_driver_register(&s3c24xx_i2c_driver);
}
subsys_initcall(i2c_adap_s3c_init);static void __exit i2c_adap_s3c_exit(void)
{platform_driver_unregister(&s3c24xx_i2c_driver);
}
module_exit(i2c_adap_s3c_exit);	.probe		= s3c24xx_i2c_probe,.remove		= s3c24xx_i2c_remove,.id_table	= s3c24xx_driver_ids,.driver		= {.owner	= THIS_MODULE,.name	= "s3c-i2c",.pm	= S3C24XX_DEV_PM_OPS,},
};static int __init i2c_adap_s3c_init(void)
{return platform_driver_register(&s3c24xx_i2c_driver);
}
subsys_initcall(i2c_adap_s3c_init);static void __exit i2c_adap_s3c_exit(void)
{platform_driver_unregister(&s3c24xx_i2c_driver);
}
module_exit(i2c_adap_s3c_exit);

 

这里主要通过平台驱动注册了一个设备。设备名叫做s3c24xx_i2c_driver。上面是s3c24xx_i2c_driver的定义,先来分析一些s3c24xx_i2c_probe函数。里面的一些变量赋值和其他一些简单的函数就不分析了,我们分析一个重要的函数s3c24xx_i2c_init。

 

/* s3c24xx_i2c_init** initialise the controller, set the IO lines and frequency
*/static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
{unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;struct s3c2410_platform_i2c *pdata;unsigned int freq;/* get the plafrom data */pdata = i2c->dev->platform_data;/* inititalise the gpio */if (pdata->cfg_gpio)pdata->cfg_gpio(to_platform_device(i2c->dev));/* write slave address */writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);writel(iicon, i2c->regs + S3C2410_IICCON);/* we need to work out the divisors for the clock... */if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {writel(0, i2c->regs + S3C2410_IICCON);dev_err(i2c->dev, "cannot meet bus frequency required\n");return -EINVAL;}/* todo - check that the i2c lines aren't being dragged anywhere */dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);return 0;
}


我们可以看到这个函数主要完成了一下的功能:

1、unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;使能中断和应答

2、pdata->cfg_gpio(to_platform_device(i2c->dev));初始化GPIO

3、writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);写从地址

4、if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {初始化时钟。

 

可以发现,这里的初始化代码和裸机的代码也是非常类似的。

初始化之后还调用了i2c_add_numbered_adapter函数来注册一个i2c适配器。

 

 

prob函数中有一个重要的赋值语句i2c->adap.algo    = &s3c24xx_i2c_algorithm;之前说过algorithm是用来实现i2c设备读写方法的。这里面包含2个函数,一个是s3c24xx_i2c_xfer,这个函数用来实现i2c设备的读写,他们依次调用s3c24xx_i2c_doxfer->s3c24xx_i2c_message_start,最终实现设备的读写,这个函数如下:

 

/* s3c24xx_i2c_message_start** put the start of a message onto the bus
*/static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,struct i2c_msg *msg)
{unsigned int addr = (msg->addr & 0x7f) << 1;unsigned long stat;unsigned long iiccon;stat = 0;stat |=  S3C2410_IICSTAT_TXRXEN;if (msg->flags & I2C_M_RD) {stat |= S3C2410_IICSTAT_MASTER_RX;addr |= 1;} elsestat |= S3C2410_IICSTAT_MASTER_TX;if (msg->flags & I2C_M_REV_DIR_ADDR)addr ^= 1;/* todo - check for wether ack wanted or not */s3c24xx_i2c_enable_ack(i2c);iiccon = readl(i2c->regs + S3C2410_IICCON);writel(stat, i2c->regs + S3C2410_IICSTAT);dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);writeb(addr, i2c->regs + S3C2410_IICDS);/* delay here to ensure the data byte has gotten onto the bus* before the transaction is started */ndelay(i2c->tx_setup);dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);writel(iiccon, i2c->regs + S3C2410_IICCON);stat |= S3C2410_IICSTAT_START;writel(stat, i2c->regs + S3C2410_IICSTAT);
}


我们对比2440数据手册中的IIC读写时序:

可以发现读写的流程和数据手册是一致的(肯定是一致的,不然就读写失败了)。

 

不过后面从ACK period开始就属于中断的内容了,我们需要找到i2c的中断处理函数,肯定是在prob函数里面找的,发现了这么一段话:

ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED,dev_name(&pdev->dev), i2c);


这里面注册了一个中断,s3c24xx_i2c_irq。中断处理程序如下:

/* s3c24xx_i2c_irq** top level IRQ servicing routine
*/static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
{struct s3c24xx_i2c *i2c = dev_id;unsigned long status;unsigned long tmp;status = readl(i2c->regs + S3C2410_IICSTAT);if (status & S3C2410_IICSTAT_ARBITR) {/* deal with arbitration loss */dev_err(i2c->dev, "deal with arbitration loss\n");}if (i2c->state == STATE_IDLE) {dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");tmp = readl(i2c->regs + S3C2410_IICCON);tmp &= ~S3C2410_IICCON_IRQPEND;writel(tmp, i2c->regs +  S3C2410_IICCON);goto out;}/* pretty much this leaves us with the fact that we've* transmitted or received whatever byte we last sent */i2s_s3c_irq_nextbyte(i2c, status);out:return IRQ_HANDLED;
}


前面都是一些错误处理,主要分析i2s_s3c_irq_nextbyte函数。里面的步骤完成就是剩下的i2c读写的流程。

更多Linux资料及视频教程点击这里

 

 

这篇关于Linux-IIC驱动(2)-Linux下IIC子系统的介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123175

相关文章

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.