0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

I2C子系統(tǒng)幾個(gè)主要的結(jié)構(gòu)體

麥辣雞腿堡 ? 來(lái)源:嵌入式Linux系統(tǒng)開發(fā) ? 作者:嵌入式Linux系統(tǒng)開 ? 2023-07-22 16:04 ? 次閱讀

I2C Data Structure

我們要搞懂一個(gè) Linux 子系統(tǒng),必須研究它的數(shù)據(jù)結(jié)構(gòu),搞懂每個(gè)結(jié)構(gòu)體存儲(chǔ)了什么東西,才能梳理清楚該子系統(tǒng)的架構(gòu)。

I2C 子系統(tǒng)有幾個(gè)主要的結(jié)構(gòu)體:

I2C 控制器:i2c_adapter、i2c_algorithm、mtk_i2c

I2C 設(shè)備驅(qū)動(dòng):i2c_client、i2c_driver

I2C 傳輸:i2c_msg

i2c_adapter:i2c-core 層描述一個(gè) I2C 控制器,假如一個(gè)芯片有 8 路 I2C bus,則有 8 個(gè) i2c_adapter。請(qǐng)?jiān)敿?xì)看博主對(duì) code 的注釋說(shuō)明。

struct i2c_adapter {
 struct module *owner;
 unsigned int class; /* 該 I2C bus 支持哪些類型的從設(shè)備 */
 const struct i2c_algorithm *algo; /* the algorithm to access the bus */
 void *algo_data;

 /* data fields that are valid for all devices */
 const struct i2c_lock_operations *lock_ops;
 struct rt_mutex bus_lock;
 struct rt_mutex mux_lock;

 int timeout;/* 超過(guò)該時(shí)間無(wú)法重發(fā) */
 int retries;/* I2C發(fā)送失敗重試次數(shù) */
 struct device dev;  /* the adapter device */
 unsigned long locked_flags; /* owned by the I2C core */
#define I2C_ALF_IS_SUSPENDED  0
#define I2C_ALF_SUSPEND_REPORTED 1

 int nr;/*I2C bus id*/
 char name[48];
 struct completion dev_released;

 struct mutex userspace_clients_lock;
 struct list_head userspace_clients;

 struct i2c_bus_recovery_info *bus_recovery_info;
 const struct i2c_adapter_quirks *quirks;

 struct irq_domain *host_notify_domain;
 struct regulator *bus_regulator;
};

i2c_algorithm:I2C 傳輸函數(shù)合集,其中 master_xfer 是真正的傳輸函數(shù) ,芯片原廠寫 I2C 控制器驅(qū)動(dòng)時(shí)必須實(shí)現(xiàn)。functionality 函數(shù)會(huì)返回該 I2C 控制器支持什么通信協(xié)議,也需要實(shí)現(xiàn),其他的函數(shù)即便 Linux 規(guī)定了,芯片原廠也可以不實(shí)現(xiàn),因?yàn)椴怀S谩?/p>

struct i2c_algorithm {
 int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,int num);
 int (*master_xfer_atomic)(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);
 int (*smbus_xfer_atomic)(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 *adap);

#if IS_ENABLED(CONFIG_I2C_SLAVE)
 int (*reg_slave)(struct i2c_client *client);
 int (*unreg_slave)(struct i2c_client *client);
#endif
};

MTK 只實(shí)現(xiàn)了其中兩個(gè)圖片

圖片

i2c_client:描述設(shè)備信息

struct i2c_client {
 unsigned short flags;/* I2C 傳輸標(biāo)志位如下*/
#define I2C_CLIENT_PEC  0x04 /* Use Packet Error Checking */
#define I2C_CLIENT_TEN  0x10 /* we have a ten bit chip address */
/* Must equal I2C_M_TEN below */
#define I2C_CLIENT_SLAVE 0x20 /* we are the slave */
#define I2C_CLIENT_HOST_NOTIFY 0x40 /* We want to use I2C host notify */
#define I2C_CLIENT_WAKE  0x80 /* for board_info; true iff can wake */
#define I2C_CLIENT_SCCB  0x9000 /* Use Omnivision SCCB protocol */
     /* Must match I2C_M_STOP|IGNORE_NAK */

 unsigned short addr;  /* chip address - NOTE: 7bit */
     /* addresses are stored in the */
     /* _LOWER_ 7 bits  */
 char name[I2C_NAME_SIZE];
 struct i2c_adapter *adapter;/* 所處的那一路 I2C bus */
 struct device dev;  /* the device structure  */
 int init_irq;   /* irq set at initialization */
 int irq;   /* irq issued by device  */
 struct list_head detected;
#if IS_ENABLED(CONFIG_I2C_SLAVE)
 i2c_slave_cb_t slave_cb; /* callback for slave mode */
#endif
 void *devres_group_id;  /* ID of probe devres group */
};

i2c_driver:普通驅(qū)動(dòng)工程師寫驅(qū)動(dòng)時(shí),必須實(shí)現(xiàn)其中的 probe 函數(shù)和 remove 函數(shù),其余的函數(shù)一般用不到。

struct i2c_driver {
 unsigned int class;

 /* Standard driver model interfaces */
 int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);
 int (*remove)(struct i2c_client *client);

 int (*probe_new)(struct i2c_client *client);
 void (*shutdown)(struct i2c_client *client);
 void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,unsigned int data);
 int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

 struct device_driver driver;
 const struct i2c_device_id *id_table;

 int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
 const unsigned short *address_list;
 struct list_head clients;
};

mtk_i2c:MTK 平臺(tái)用該結(jié)構(gòu)體表示 I2C 控制器,定義在/kernel-5.10/drivers/i2c/busses/i2c-mt65xx.c

struct mtk_i2c {
 struct i2c_adapter adap; /* i2c host adapter */
 struct device *dev;
 struct completion msg_complete;

 /* set in i2c probe */
 void __iomem *base;  /* i2c base addr */
 void __iomem *pdmabase;  /* dma base address*/
 struct clk *clk_main;  /* main clock for i2c bus */
 struct clk *clk_dma;  /* DMA clock for i2c via DMA */
 struct clk *clk_pmic;  /* PMIC clock for i2c from PMIC */
 bool have_pmic;   /* can use i2c pins from PMIC */
 bool use_push_pull;  /* IO config push-pull mode */

 u16 irq_stat;   /* interrupt status */
 unsigned int clk_src_div;
 unsigned int speed_hz;  /* The speed in transfer */
 enum mtk_trans_op op;
 u16 timing_reg;
 u16 high_speed_reg;
 unsigned char auto_restart;
 bool ignore_restart_irq;
 const struct mtk_i2c_compatible *dev_comp;
};

i2c_msg:I2C 讀寫時(shí),必須填充 i2c_msg。

標(biāo)志位:寫為 0 ,讀為 I2C_M_RD,其他的 flag 大家可以參考。

I2C 單筆傳輸最大 64KB,len 的長(zhǎng)度博主在注釋中有說(shuō)明。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • I2C
    I2C
    +關(guān)注

    關(guān)注

    28

    文章

    1468

    瀏覽量

    122786
  • 結(jié)構(gòu)體
    +關(guān)注

    關(guān)注

    1

    文章

    127

    瀏覽量

    10812
  • 系統(tǒng)
    +關(guān)注

    關(guān)注

    1

    文章

    1005

    瀏覽量

    21273
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    Linux內(nèi)核中描述I2C的四個(gè)核心結(jié)構(gòu)

    I2C核心維護(hù)了i2c_bus結(jié)構(gòu),提供了I2C總線驅(qū)動(dòng)和設(shè)備驅(qū)動(dòng)的注冊(cè)、注銷方法,維護(hù)了I2C
    的頭像 發(fā)表于 09-04 09:35 ?1148次閱讀
    Linux內(nèi)核中描述<b class='flag-5'>I2C</b>的四個(gè)核心<b class='flag-5'>結(jié)構(gòu)</b><b class='flag-5'>體</b>

    linux I2C子系統(tǒng)的相關(guān)資料分享

    文章目錄linux I2C子系統(tǒng)框架在設(shè)備樹中添加從設(shè)備信息,mpu5060I2C driver 程序的編寫mpu6050 I2C程序具體實(shí)現(xiàn)linux
    發(fā)表于 02-10 06:06

    i2c總線ppt(I2C總線器件應(yīng)用)

    I2C總線器件應(yīng)用第一節(jié) I2C總線器件應(yīng)用概述I2C總線工作原理I2C總線系統(tǒng)結(jié)構(gòu)
    發(fā)表于 08-13 17:34 ?0次下載

    I2C總線應(yīng)用中的幾個(gè)問(wèn)題

    I2C總線應(yīng)用中的幾個(gè)問(wèn)題:i2c上拉電阻阻值的確定,PCB布局布線與抗干擾設(shè)計(jì),軟件模擬I2C時(shí)序,I2C 應(yīng)用中上拉電阻電源問(wèn)題。
    發(fā)表于 09-13 14:27 ?51次下載
    <b class='flag-5'>I2C</b>總線應(yīng)用中的<b class='flag-5'>幾個(gè)</b>問(wèn)題

    探討I2C總線應(yīng)用中的幾個(gè)問(wèn)題

    I2C應(yīng)用中的幾個(gè)問(wèn)題,I2C上拉電阻的確定,抗干擾設(shè)計(jì)
    發(fā)表于 02-25 14:48 ?4次下載

    I2C總線的結(jié)構(gòu)介紹

    內(nèi)容摘要:介紹了I2C總線的結(jié)構(gòu)、工作原理、數(shù)據(jù)傳輸方式,討論了基于I2C總線的多機(jī)通信軟硬件設(shè)計(jì),實(shí)現(xiàn)了程控交換多機(jī)通信調(diào)度指揮系統(tǒng)。
    發(fā)表于 02-29 15:44 ?6次下載

    Linux內(nèi)核中I2C子系統(tǒng)的整體視圖

    本文通過(guò)閱讀內(nèi)核代碼,來(lái)梳理一下I2C子系統(tǒng)的整體視圖。在開發(fā)I2C設(shè)備驅(qū)動(dòng)程序時(shí),往往缺乏對(duì)于系統(tǒng)整體的認(rèn)識(shí),沒有一個(gè)清晰的思路。所以從高層級(jí)來(lái)分析一下
    的頭像 發(fā)表于 12-31 10:40 ?2123次閱讀
    Linux內(nèi)核中<b class='flag-5'>I2C</b><b class='flag-5'>子系統(tǒng)</b>的整體視圖

    linux I2C子系統(tǒng)(及相關(guān)程序設(shè)計(jì)MPU6050)

    文章目錄linux I2C子系統(tǒng)框架在設(shè)備樹中添加從設(shè)備信息,mpu5060I2C driver 程序的編寫mpu6050 I2C程序具體實(shí)現(xiàn)linux
    發(fā)表于 12-06 13:36 ?9次下載
    linux <b class='flag-5'>I2C</b><b class='flag-5'>子系統(tǒng)</b>(及相關(guān)程序設(shè)計(jì)MPU6050)

    Linux驅(qū)動(dòng):I2C設(shè)備驅(qū)動(dòng)(基于Freescale i.MX6ULL平臺(tái)了解I2C的驅(qū)動(dòng)框架,順便寫個(gè)簡(jiǎn)陋的MPU6050驅(qū)動(dòng))

    文章目錄1、簡(jiǎn)介2I2C總線、設(shè)備和驅(qū)動(dòng)的結(jié)構(gòu)定義2.1 結(jié)構(gòu)定義--
    發(fā)表于 12-06 13:51 ?8次下載
    Linux驅(qū)動(dòng):<b class='flag-5'>I2C</b>設(shè)備驅(qū)動(dòng)(基于Freescale <b class='flag-5'>i</b>.MX6ULL平臺(tái)了解<b class='flag-5'>I2C</b>的驅(qū)動(dòng)框架,順便寫個(gè)簡(jiǎn)陋的MPU6050驅(qū)動(dòng))

    嵌入式內(nèi)核及驅(qū)動(dòng)開發(fā)-09IIC子系統(tǒng)框架使用(I2C協(xié)議和時(shí)序,I2C驅(qū)動(dòng)框架,I2C從設(shè)備驅(qū)動(dòng)開發(fā),MPU6050硬件連接

    文章目錄I2c協(xié)議和時(shí)序I2c介紹I2c硬件連接I2c總線的信號(hào)I2c總線寫時(shí)序I2c總線讀時(shí)序
    發(fā)表于 12-06 14:06 ?17次下載
    嵌入式內(nèi)核及驅(qū)動(dòng)開發(fā)-09IIC<b class='flag-5'>子系統(tǒng)</b>框架使用(<b class='flag-5'>I2C</b>協(xié)議和時(shí)序,<b class='flag-5'>I2C</b>驅(qū)動(dòng)框架,<b class='flag-5'>I2C</b>從設(shè)備驅(qū)動(dòng)開發(fā),MPU6050硬件連接

    硬件I2C與模擬I2C

    硬件I2C對(duì)應(yīng)芯片上的I2C外設(shè),有相應(yīng)I2C驅(qū)動(dòng)電路,其所使用的I2C管腳也是專用的,因而效率要遠(yuǎn)高于軟件模擬的I2C;一般也較為穩(wěn)定,但
    發(fā)表于 12-28 19:14 ?81次下載
    硬件<b class='flag-5'>I2C</b>與模擬<b class='flag-5'>I2C</b>

    I2C子系統(tǒng)debug的常見問(wèn)題

    常見問(wèn)題 1、同一條 i2c bus 上所有的外設(shè)的 i2c addr 要不同 1)相同 address 注冊(cè)時(shí)沖突 [ 2.059184 ][xxx]i2c i2c- 1 :Fail
    的頭像 發(fā)表于 07-22 14:52 ?860次閱讀

    I2C控制器驅(qū)動(dòng)介紹

    適配器與 I2C 設(shè)備進(jìn)行通信的方法。 I2C 總線驅(qū)動(dòng),或者說(shuō) I2C 適配器驅(qū)動(dòng)的主要工作就是初始化 i2c_adapter
    的頭像 發(fā)表于 07-22 15:38 ?1324次閱讀
    <b class='flag-5'>I2C</b>控制器驅(qū)動(dòng)介紹

    I2C子系統(tǒng)SW Architecture

    I2C SW Architecture 【driver 驅(qū)動(dòng)層】由普通驅(qū)動(dòng)工程師負(fù)責(zé),【i2c 核心層】由 Linux 提供,【i2c 核心層】以下由芯片原廠負(fù)責(zé)。 I2C
    的頭像 發(fā)表于 07-22 16:01 ?862次閱讀
    <b class='flag-5'>I2C</b><b class='flag-5'>子系統(tǒng)</b>SW Architecture

    I2C轉(zhuǎn)UART子系統(tǒng)設(shè)計(jì)

    電子發(fā)燒友網(wǎng)站提供《I2C轉(zhuǎn)UART子系統(tǒng)設(shè)計(jì).pdf》資料免費(fèi)下載
    發(fā)表于 08-28 10:27 ?0次下載
    <b class='flag-5'>I2C</b>轉(zhuǎn)UART<b class='flag-5'>子系統(tǒng)</b>設(shè)計(jì)