当前位置:首页 > 单片机 > 单片机
[导读]#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* i2c controller state *///i2c控制器状态enum s

#include
#include

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include
#include

#include
#include

/* i2c controller state */
//i2c控制器状态
enum s3c24xx_i2c_state {
STATE_IDLE,
STATE_START,
STATE_READ,
STATE_WRITE,
STATE_STOP
};

struct s3c24xx_i2c {
spinlock_tlock;
/*
在数据发送函数s3c24xx_i2c_doxfer中wait_event_timeout,
在数据发送完成函数s3c24xx_i2c_master_complete中wake_up
*/
wait_queue_head_twait;
unsigned intsuspended:1;

struct i2c_msg*msg;//管理收发数据的结构体
unsigned intmsg_num;//待收发msg的数目
unsigned intmsg_idx;//当前正在处理的msg在msg_num个msg中的序号
//每个msg都管理一段内存,msg_ptr是这段内存空间的偏移。
unsigned intmsg_ptr;
//在数据写入IICDS后的等待时间,在数据写入前SCL保持低电平,写入后
//释放,这个状态的建立需要一定的时间。
unsigned inttx_setup;
unsigned intirq;

enum s3c24xx_i2c_statestate;
//保存上一次通过clk_get_rate(i2c->clk);获取的频率。
//当本次获取的频率与clkrate不等时说明系统时钟改变。
unsigned longclkrate;

void __iomem*regs;
struct clk*clk;
struct device*dev;
struct resource*ioarea;//
struct i2c_adapteradap;//适配器结构体,这整个结构体都是对它的包装。

#ifdef CONFIG_CPU_FREQ
struct notifier_blockfreq_transition;//通知链
#endif
};

/* default platform data removed, dev should always carry data. */

/* s3c24xx_i2c_is2440()
*
* return true is this is an s3c2440
*/
//如果是s3c2440则返回1
static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c)
{
struct platform_device *pdev = to_platform_device(i2c->dev);

return !strcmp(pdev->name, "s3c2440-i2c");
}

/* s3c24xx_i2c_master_complete
*
* complete the message and wake up the caller, using the given return code,
* or zero to mean ok.
*/

static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
{
dev_dbg(i2c->dev, "master_complete %dn", ret);

i2c->msg_ptr = 0;
i2c->msg = NULL;
i2c->msg_idx++;
i2c->msg_num = 0;
if (ret)
i2c->msg_idx = ret;
//msg_num个msg都处理完则唤醒等待队列
wake_up(&i2c->wait);
}
//应答禁能
static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
}
//应答使能
static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
}

/* irq enable/disable functions */
//中断禁止
static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
}
//中断使能
static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
{
unsigned long tmp;

tmp = readl(i2c->regs + S3C2410_IICCON);
writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
}


/* 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;//主机读
} else
stat |= 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 DSn", 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);//延时等待SCL被释放

dev_dbg(i2c->dev, "iiccon, %08lxn", iiccon);
writel(iiccon, i2c->regs + S3C2410_IICCON);

stat |= S3C2410_IICSTAT_START;
writel(stat, i2c->regs + S3C2410_IICSTAT);//发送起始信号
}

static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
{
unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);

dev_dbg(i2c->dev, "STOPn");

/* stop the transfer */
iicstat &= ~S3C2410_IICSTAT_START;
writel(iicstat, i2c->regs + S3C2410_IICSTAT);

i2c->state = STATE_STOP;
//数据发送结束,唤醒等待队列
s3c24xx_i2c_master_complete(i2c, ret);
s3c24xx_i2c_disable_irq(i2c);
}

/* helper functions to determine the current state in the set of
* messages we are sending */

/* is_lastmsg()
*
* returns TRUE if the current message is the last in the set
*/
//msg_idx为当前msg在msg_num个发送msg中的偏移,
static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
{
return i2c->msg_idx >= (i2c->msg_num - 1);
}

/* is_msglast
*
* returns TRUE if we this is the last byte in the current message
*/
//msg_ptr为在msg中的len个数据中的偏移
static inline int is_msglast(struct s3c24xx_i2c *i2c)
{
return i2c->msg_ptr == i2c->msg->len-1;
}

/* is_msgend
*
* returns TRUE if we reached the end of the current message
*/
//当前msg中的数据处理完
static inline int is_msgend(struct s3c24xx_i2c *i2c)
{
return i2c->msg_ptr >= i2c->msg->len;
}

/* i2s_s3c_irq_nextbyte
*
* process an interrupt and work out what to do
*/

static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
{
unsigned long tmp;
unsigned char byte;
int ret = 0;

switch (i2c->state) {

case STATE_IDLE:
dev_err(i2c->dev, "%s: called in STATE_IDLEn", __func__);
goto out;
break;

case STATE_STOP:
dev_err(i2c->dev, "%s: called in STATE_STOPn", __func__);
s3c24xx_i2c_disable_irq(i2c);
goto out_ack;//如果当前为停止状态则清除中断挂起条件

case STATE_START://开始状态在开始数据传输函数s3c24xx_i2c_doxfer中设置
/* last thing we did was send a start condition on the
* bus, or started a new i2c message
*/

if (iicstat & S3C2410_IICSTAT_LASTBIT &&
!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
/* ack was not received... */
//没有收到应答信号则停止数据传输
dev_dbg(i2c->dev, "ack was not receivedn");
s3c24xx_i2c_stop(i2c, -ENXIO);
goto out_ack;
}
//其实信号成功发送后决定接下来要做的事情
if (i2c->msg->flags & I2C_M_RD)
i2c->state = STATE_READ;
else
i2c->state = STATE_WRITE;

/* terminate the transfer if there is nothing to do
* as this is used by the i2c probe to find devices. */
//如果当前msg是队列中的最后一个而且没有要发送的数据则停止
if (is_lastmsg(i2c) && i2c->msg->len == 0) {
s3c24xx_i2c_stop(i2c, 0);
goto out_ack;
}
//当前中断是起始信号成功发送,下一次中断则是进行数据接收,
//为接收数据准备可用的缓存
if (i2c->state == STATE_READ)
goto prepare_read;

/* fall through to the write state, as we will need to
* send a byte as well */

case STATE_WRITE:
/* we are writing data to the device... check for the
* end of the message, and if so, work out what to do
*/

if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
if (iicstat & S3C2410_IICSTAT_LASTBIT) {
dev_dbg(i2c->dev, "WRITE: No Ackn");

s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
goto out_ack;
}
}

retry_write:

if (!is_msgend(i2c)) {
byte = i2c->msg->buf[i2c->msg_ptr++];
//i2c->msg_ptr++表明它总是指向第一个未被发送的数据
writeb(byte, i2c->regs + S3C2410_IICDS);
//如果当前msg缓存中的数据没有全部发送则继续发送
/* delay after writing the byte to allow the
* data setup time on the bus, as writing the
* data to the register causes the first bit
* to appear on SDA, and SCL will change as
* soon as the interrupt is acknowledged */

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

    本文主要介绍的就是基于DM642的视频采集处理系统中I2C模块的正确初始化,以及通过I2C总线正确地对视频解码芯片SAA7115的寄存器读/写程序。   1 I

关键字: dm642 i2c 视频采集

    串行总线和并行总线相比具有结构简单、占用引脚少、成本低的优点。常见的串行总线有USB、IEEE1394、I2C等,其中I2C总线具有使用简单的特点,在单片机、串行E2P

关键字: vhdl i2c 串行总线

  引言   汽车的普及为人们的生活带来了方便,同时也给人们提出了一大难题——汽车防盗。本设计是为了解决以往汽车防盗产品的缺点和不足而开发的集成传感、报警和远程图

关键字: 数据采集 操作系统 s3c2410 汽车防盗系统

  引言   巡航控制系统(CCS)是20世纪60年代发展起来的,又称为恒速行驶系统。巡航控制系统工作时,ECU根据各种传感器输送来的信号判断汽车的运行状况,通过执行元件自动调节节气门的

关键字: ARM 三星 Linux ecu s3c2410 电子控制系统

  在过去一百年里(l906-2005),全球地表平均温度升高了0.74摄氏度,未来20年,每十年全球温度将会升高0.2摄氏度。气候变暖已成为不容忽视的、直接影响全球生产和生活问题。  

关键字: can总线 s3c2410

  在此设计的基于 GPRS 的远程安防监控系统,是采用的是摄像机传送视频信号经压缩编码后,通过内部总线传送到内置的 Web 服务器。用户在监控端可以直接通过浏览器观看 Web服务器上的摄像机视

关键字: GPRS s3c2410 安防监控

  I2C简介   I2C总线是由Philips公司开发的一种简单、双向二线制同步串行总线。它只需要两根线即可在连接于总线上的器件之间传送信息。主器件用于启动总线传送数据,并产生时钟以开

关键字: Arduino i2c

本项目开发了一个使用 Xilinx 公司的 SPARTAN3A-DSP FPGA 作为目标开发板,采集数字和模拟传感器数据,并将传感器数据采用 I2C 接口与上级无线传感器网络进行通信的环境监测

关键字: i2c 无线传感器网络 环境监测

2020年6月17日-国内领先的信号链芯片及其解决方案提供商苏州纳芯微电子股份有限公司(以下简称“纳芯微”)日前宣布推出五款I2C总线接口类芯片产品。

关键字: i2c 纳芯微 接口产品
关闭
关闭