當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* i2c controller state *///i2c控制器狀態(tài)enum s

#include
#include

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

#include
#include

#include
#include

/* i2c controller state */
//i2c控制器狀態(tài)
enum s3c24xx_i2c_state {
STATE_IDLE,
STATE_START,
STATE_READ,
STATE_WRITE,
STATE_STOP
};

struct s3c24xx_i2c {
spinlock_tlock;
/*
在數(shù)據(jù)發(fā)送函數(shù)s3c24xx_i2c_doxfer中wait_event_timeout,
在數(shù)據(jù)發(fā)送完成函數(shù)s3c24xx_i2c_master_complete中wake_up
*/
wait_queue_head_twait;
unsigned intsuspended:1;

struct i2c_msg*msg;//管理收發(fā)數(shù)據(jù)的結(jié)構(gòu)體
unsigned intmsg_num;//待收發(fā)msg的數(shù)目
unsigned intmsg_idx;//當(dāng)前正在處理的msg在msg_num個(gè)msg中的序號(hào)
//每個(gè)msg都管理一段內(nèi)存,msg_ptr是這段內(nèi)存空間的偏移。
unsigned intmsg_ptr;
//在數(shù)據(jù)寫入IICDS后的等待時(shí)間,在數(shù)據(jù)寫入前SCL保持低電平,寫入后
//釋放,這個(gè)狀態(tài)的建立需要一定的時(shí)間。
unsigned inttx_setup;
unsigned intirq;

enum s3c24xx_i2c_statestate;
//保存上一次通過clk_get_rate(i2c->clk);獲取的頻率。
//當(dāng)本次獲取的頻率與clkrate不等時(shí)說明系統(tǒng)時(shí)鐘改變。
unsigned longclkrate;

void __iomem*regs;
struct clk*clk;
struct device*dev;
struct resource*ioarea;//
struct i2c_adapteradap;//適配器結(jié)構(gòu)體,這整個(gè)結(jié)構(gòu)體都是對(duì)它的包裝。

#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個(gè)msg都處理完則喚醒等待隊(duì)列
wake_up(&i2c->wait);
}
//應(yīng)答禁能
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);
}
//應(yīng)答使能
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
*/
//寫入從器件地址,并發(fā)送起始信號(hào)
static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
struct i2c_msg *msg)
{
unsigned int addr = (msg->addr & 0x7f) << 1;//最低位為讀寫標(biāo)志
unsigned long stat;
unsigned long iiccon;

stat = 0;
stat |= S3C2410_IICSTAT_TXRXEN;//數(shù)據(jù)收發(fā)使能

if (msg->flags & I2C_M_RD) {
stat |= S3C2410_IICSTAT_MASTER_RX;
addr |= 1;//主機(jī)讀
} else
stat |= S3C2410_IICSTAT_MASTER_TX;//主機(jī)寫

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);//延時(shí)等待SCL被釋放

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

stat |= S3C2410_IICSTAT_START;
writel(stat, i2c->regs + S3C2410_IICSTAT);//發(fā)送起始信號(hào)
}

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;
//數(shù)據(jù)發(fā)送結(jié)束,喚醒等待隊(duì)列
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為當(dāng)前msg在msg_num個(gè)發(fā)送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個(gè)數(shù)據(jù)中的偏移
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
*/
//當(dāng)前msg中的數(shù)據(jù)處理完
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;//如果當(dāng)前為停止?fàn)顟B(tài)則清除中斷掛起條件

case STATE_START://開始狀態(tài)在開始數(shù)據(jù)傳輸函數(shù)s3c24xx_i2c_doxfer中設(shè)置
/* 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... */
//沒有收到應(yīng)答信號(hào)則停止數(shù)據(jù)傳輸
dev_dbg(i2c->dev, "ack was not receivedn");
s3c24xx_i2c_stop(i2c, -ENXIO);
goto out_ack;
}
//其實(shí)信號(hào)成功發(fā)送后決定接下來(lái)要做的事情
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. */
//如果當(dāng)前msg是隊(duì)列中的最后一個(gè)而且沒有要發(fā)送的數(shù)據(jù)則停止
if (is_lastmsg(i2c) && i2c->msg->len == 0) {
s3c24xx_i2c_stop(i2c, 0);
goto out_ack;
}
//當(dāng)前中斷是起始信號(hào)成功發(fā)送,下一次中斷則是進(jìn)行數(shù)據(jù)接收,
//為接收數(shù)據(jù)準(zhǔn)備可用的緩存
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++表明它總是指向第一個(gè)未被發(fā)送的數(shù)據(jù)
writeb(byte, i2c->regs + S3C2410_IICDS);
//如果當(dāng)前msg緩存中的數(shù)據(jù)沒有全部發(fā)送則繼續(xù)發(fā)送
/* 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 */

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉