7-i2C總線_mpu6050驅(qū)動編程

i2c總線協(xié)議:
  • 歷史:
    • 是飛利浦公司在80年代丰刊,為研發(fā)音視頻模塊的通訊而產(chǎn)生坡慌。因其具有的諸多優(yōu)點(diǎn),而沿用至今藻三。
  • 優(yōu)點(diǎn):
    • 1.簡單性
    • 2.有效性
    • 3.多主控性。
i2c總線的信號:
  • 1.起始信號:當(dāng)scl保持高電平期間跪者,sda由高到低跳變棵帽,稱為起始信號。
  • 2.結(jié)束信號:當(dāng)scl保持高電平期間渣玲,sda由低到高跳變逗概,稱為結(jié)束信號。
  • 3.數(shù)據(jù)位: 當(dāng)scl保持高電平期間忘衍,sda保持電平穩(wěn)定有效性逾苫,稱為有效數(shù)據(jù)位。
  • 3.應(yīng)答信號:當(dāng)scl保持高電平期間枚钓,當(dāng)發(fā)送一個字節(jié)的數(shù)據(jù)后铅搓,必須要有對方的應(yīng)答信號。
i2c總線使用幾大原則:
  • a -- 在SCL=1(高電平)時,SDA千萬別忽悠!!!否則,SDA下跳則"判罰"為"起始信號S",SDA上跳則"判罰"為"停止信號P".
  • b -- 在SCL=0(低電平)時,SDA隨便忽悠!!!(可別忽悠過火到SCL跳高)
  • c -- 每個字節(jié)后應(yīng)該由對方回送一個應(yīng)答信號ACK做為對方在線的標(biāo)志.一般要由雙方協(xié)議簽定.
  • d -- SCL必須由主機(jī)發(fā)送,否則天下大亂
  • e -- 首字節(jié)是"片選信號",即7位從機(jī)地址加1位方向(讀寫)控制.從機(jī)收到(聽到)自己的地址才能發(fā)送應(yīng)答信號(必須應(yīng)答!!!)表示自己在線.其他地址的從機(jī)不允許忽悠!!!(當(dāng)然群呼可以忽悠但只能聽不許說話)
  • f -- 讀寫是站在主機(jī)的立場上定義的."讀"是主機(jī)接收從機(jī)數(shù)據(jù),"寫"是主機(jī)發(fā)送數(shù)據(jù)給從機(jī).
  • g -- 7位IIC總線可以掛接127個不同地址的IIC設(shè)備,0號"設(shè)備"作為群呼地址.10位IIC總線可以掛接更多的10位IIC設(shè)備.
i2c總線的框架:
  • 1.總線驅(qū)動層:主要數(shù)據(jù)結(jié)構(gòu)是i2c_adapter和i2c_algorithm搀捷,總線驅(qū)動層完成總線控制器設(shè)備的軟件實(shí)現(xiàn)星掰,和i2c總線通信的方法等
  • 2.i2c核心層:主要提供api給總線驅(qū)動層和設(shè)備驅(qū)動層,完成總線驅(qū)動層和設(shè)備驅(qū)動層之間的通信和匹配嫩舟。
  • 3.設(shè)備驅(qū)動層:主要數(shù)據(jù)結(jié)構(gòu)是i2c_client和i2c_driver氢烘,完成從設(shè)備的硬件和軟件框架功能的實(shí)現(xiàn)。
主要數(shù)據(jù)結(jié)構(gòu):
  • 1.i2c_adapter:是描述i2c控制器的數(shù)據(jù)結(jié)構(gòu)
  • 2.i2c_algorithm: 是描述i2c控制器的通信方法家厌,通俗講就是i2c控制器的驅(qū)動部分
  • 3.i2c_client: 是描述總線上從設(shè)備的硬件部分的數(shù)據(jù)結(jié)構(gòu)
  • 4.i2c_driver: 是描述總線上從設(shè)備的驅(qū)動部分的數(shù)據(jù)結(jié)構(gòu)
  • 5.i2c_msg: 是在i2c總線上傳輸?shù)臄?shù)據(jù)的格式
主要使用函數(shù):
  • 1.i2c_transfer:完成i2c總線上播玖,主從設(shè)備之間的信號或者數(shù)據(jù)的發(fā)送和接收。

/*
 * 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 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   */
    struct rt_mutex bus_lock;

    int timeout;            /* in jiffies */
    int retries;
    struct device dev;      /* the adapter device */

    int nr;
    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;
};

/**
 * struct i2c_algorithm - represent I2C transfer method
 * @master_xfer: Issue a set of i2c transactions to the given I2C adapter
 *   defined by the msgs array, with num messages available to transfer via
 *   the adapter specified by adap.
 * @smbus_xfer: Issue smbus transactions to the given I2C adapter. If this
 *   is not present, then the bus layer will try and convert the SMBus calls
 *   into I2C transfers instead.
 * @functionality: Return the flags that this algorithm/adapter pair supports
 *   from the I2C_FUNC_* flags.
 *
 * 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.
 *
 * The return codes from the @master_xfer field should indicate the type of
 * error code that occured during the transfer, as documented in the kernel
 * Documentation file Documentation/i2c/fault-codes.
 */

struct i2c_algorithm {
    /* If an adapter algorithm can't do I2C-level access, set master_xfer
       to NULL. If an adapter algorithm can do SMBus access, set
       smbus_xfer. If set to NULL, the SMBus protocol is simulated
       using common I2C messages */
    /* master_xfer should return the number of messages successfully
       processed, or a negative value on error */
    int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
               int num);
int i2c_transfer(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 *);
};



/**
 * struct i2c_client - represent an I2C slave device
 * @flags: I2C_CLIENT_TEN indicates the device uses a ten bit chip address;
 *  I2C_CLIENT_PEC indicates it uses SMBus Packet Error Checking
 * @addr: Address used on the I2C bus connected to the parent adapter.
 * @name: Indicates the type of the device, usually a chip name that's
 *  generic enough to hide second-sourcing and compatible revisions.
 * @adapter: manages the bus segment hosting this I2C device
 * @dev: Driver model device node for the slave.
 * @irq: indicates the IRQ generated by this device (if any)
 * @detected: member of an i2c_driver.clients list or i2c-core's
 *  userspace_devices list
 *
 * An i2c_client identifies a single device (i.e. chip) connected to an
 * i2c bus. The behaviour exposed to Linux is defined by the driver
 * managing the device.
 */

struct i2c_client {
    unsigned short flags;       /* div., see below      */
    unsigned short addr;        /* chip address - NOTE: 7bit    */
                    /* addresses are stored in the  */
                    /* _LOWER_ 7 bits       */
    char name[I2C_NAME_SIZE];
    struct i2c_adapter *adapter;    /* the adapter we sit on    */
    struct device dev;      /* the device structure     */
    int irq;            /* irq issued by device     */
    struct list_head detected;
};


/**
 * struct i2c_driver - represent an I2C device driver
 * @class: What kind of i2c device we instantiate (for detect)
 * @attach_adapter: Callback for bus addition (deprecated)
 * @probe: Callback for device binding
 * @remove: Callback for device unbinding
 * @shutdown: Callback for device shutdown
 * @suspend: Callback for device suspend
 * @resume: Callback for device resume
 * @alert: Alert callback, for example for the SMBus alert protocol
 * @command: Callback for bus-wide signaling (optional)
 * @driver: Device driver model driver
 * @id_table: List of I2C devices supported by this driver
 * @detect: Callback for device detection
 * @address_list: The I2C addresses to probe (for detect)
 * @clients: List of detected clients we created (for i2c-core use only)
 *
 * The driver.owner field should be set to the module owner of this driver.
 * The driver.name field should be set to the name of this driver.
 *
 * For automatic device detection, both @detect and @address_list must
 * be defined. @class should also be set, otherwise only devices forced
 * with module parameters will be created. The detect function must
 * fill at least the name field of the i2c_board_info structure it is
 * handed upon successful detection, and possibly also the flags field.
 *
 * If @detect is missing, the driver will still work fine for enumerated
 * devices. Detected devices simply won't be supported. This is expected
 * for the many I2C/SMBus devices which can't be detected reliably, and
 * the ones which can always be enumerated in practice.
 *
 * The i2c_client structure which is handed to the @detect callback is
 * not a real i2c_client. It is initialized just enough so that you can
 * call i2c_smbus_read_byte_data and friends on it. Don't do anything
 * else with it. In particular, calling dev_dbg and friends on it is
 * not allowed.
 */
struct i2c_driver {
    unsigned int class;

    /* Notifies the driver that a new bus has appeared. You should avoid
     * using this, it will be removed in a near future.
     */
    int (*attach_adapter)(struct i2c_adapter *) __deprecated;

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

    /* driver model interfaces that don't relate to enumeration  */
    void (*shutdown)(struct i2c_client *);
    int (*suspend)(struct i2c_client *, pm_message_t mesg);
    int (*resume)(struct i2c_client *);

    /* Alert callback, for example for the SMBus alert protocol.
     * The format and meaning of the data value depends on the protocol.
     * For the SMBus alert protocol, there is a single bit of data passed
     * as the alert response's low bit ("event flag").
     */
    void (*alert)(struct i2c_client *, unsigned int data);

    /* a ioctl like command that can be used to perform specific functions
     * with the device.
     */
    int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

    struct device_driver driver;
    const struct i2c_device_id *id_table;

    /* Device detection callback for automatic device creation */
    int (*detect)(struct i2c_client *, struct i2c_board_info *);
    const unsigned short *address_list;
    struct list_head clients;
};

struct i2c_device_id {
    char name[I2C_NAME_SIZE];
    kernel_ulong_t driver_data; /* Data private to the driver */
};


/**
 * module_i2c_driver() - Helper macro for registering a I2C driver
 * @__i2c_driver: i2c_driver struct
 *
 * Helper macro for I2C drivers which do not do anything special in module
 * init/exit. This eliminates a lot of boilerplate. Each module may only
 * use this macro once, and calling it replaces module_init() and module_exit()
 */
#define module_i2c_driver(__i2c_driver)     module_driver(__i2c_driver, i2c_add_driver, i2c_del_driver)




/**
 * struct i2c_msg - an I2C transaction segment beginning with START
 * @addr: Slave address, either seven or ten bits.  When this is a ten
 *  bit address, I2C_M_TEN must be set in @flags and the adapter
 *  must support I2C_FUNC_10BIT_ADDR.
 * @flags: I2C_M_RD is handled by all adapters.  No other flags may be
 *  provided unless the adapter exported the relevant I2C_FUNC_*
 *  flags through i2c_check_functionality().
 * @len: Number of data bytes in @buf being read from or written to the
 *  I2C slave address.  For read transactions where I2C_M_RECV_LEN
 *  is set, the caller guarantees that this buffer can hold up to
 *  32 bytes in addition to the initial length byte sent by the
 *  slave (plus, if used, the SMBus PEC); and this value will be
 *  incremented by the number of block data bytes received.
 * @buf: The buffer into which data is read, or from which it's written.
 *
 * An i2c_msg is the low level representation of one segment of an I2C
 * transaction.  It is visible to drivers in the @i2c_transfer() procedure,
 * to userspace from i2c-dev, and to I2C adapter drivers through the
 * @i2c_adapter.@master_xfer() method.
 *
 * Except when I2C "protocol mangling" is used, all I2C adapters implement
 * the standard rules for I2C transactions.  Each transaction begins with a
 * START.  That is followed by the slave address, and a bit encoding read
 * versus write.  Then follow all the data bytes, possibly including a byte
 * with SMBus PEC.  The transfer terminates with a NAK, or when all those
 * bytes have been transferred and ACKed.  If this is the last message in a
 * group, it is followed by a STOP.  Otherwise it is followed by the next
 * @i2c_msg transaction segment, beginning with a (repeated) START.
 *
 * Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then
 * passing certain @flags may have changed those standard protocol behaviors.
 * Those flags are only for use with broken/nonconforming slaves, and with
 * adapters which are known to support the specific mangling options they
 * need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR).
 */
struct i2c_msg {
    __u16 addr; /* slave address            */
    __u16 flags;
#define I2C_M_TEN       0x0010  /* this is a ten bit chip address */
#define I2C_M_RD        0x0001  /* read data, from slave to master */
#define I2C_M_STOP      0x8000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NOSTART       0x4000  /* if I2C_FUNC_NOSTART */
#define I2C_M_REV_DIR_ADDR  0x2000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_IGNORE_NAK    0x1000  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_NO_RD_ACK     0x0800  /* if I2C_FUNC_PROTOCOL_MANGLING */
#define I2C_M_RECV_LEN      0x0400  /* length will be first received byte */
    __u16 len;      /* msg length               */
   __u8 *buf;      /* pointer to msg data          */
};

/***********************************************************************************
/**
 * i2c_transfer - execute a single or combined I2C message
 * @adap: Handle to I2C bus
 * @msgs: One or more messages to execute before STOP is issued to
 *  terminate the operation; each message begins with a START.
 * @num: Number of messages to be executed.
 *
 * Returns negative errno, else the number of messages executed.
 *
 * Note that there is no requirement that each message be sent to
 * the same slave address, although that is the most common model.
 */

int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)

  • 函數(shù)功能:完成i2c總線上饭于,主從設(shè)備之間的信號或者數(shù)據(jù)的發(fā)送和接收蜀踏。
  • 返回值:成功為發(fā)送的msg個數(shù),失敗返回負(fù)數(shù)
  • 參數(shù)1: 就是i2c總線所依賴的i2c控制器
  • 參數(shù)2: 要傳輸?shù)臄?shù)據(jù)格式msg
  • 參數(shù)3: 呀傳輸?shù)膍sg的個數(shù)

mpu6050驅(qū)動編程:
  • 1.確定需求: fs4412開發(fā)板所相連的mpu6050器件工作起來镰绎。并在應(yīng)用層采集到數(shù)據(jù)脓斩。
  • 2.查看原理圖和數(shù)據(jù)手冊,得知畴栖,mpu6050與exynos4412的i2c通道5相連随静,mpu6050的從地址是0x68
    • 還有mpu6050的重要的控制寄存器和數(shù)據(jù)寄存器。
  • 3.完成設(shè)備樹的書寫:描述兩個重要設(shè)備信息: i2c5通道和從地址0x68
  • 4.完成i2c_driver的框架編程。
  • 5.完成封裝兩個重要功能函數(shù)燎猛,分別完成從mpu6050數(shù)據(jù)寄存器中讀取數(shù)據(jù)和向mpu6050控制寄存器中寫入數(shù)據(jù)的
    功能恋捆。
  • 6.完成字符設(shè)備8步流程,并且完成自動創(chuàng)建設(shè)備節(jié)點(diǎn)重绷。
  • 7.完成mpu6050控制寄存器的初始化沸停。
  • 8.完成file_operations中ioctl函數(shù)的實(shí)現(xiàn)。主要實(shí)現(xiàn)三種傳感器的數(shù)據(jù)讀取昭卓,并傳遞到應(yīng)用層愤钾。
  • 9.完成頭文件,頭文件中有寄存器的地址候醒,ioctl中傳遞到應(yīng)用層數(shù)據(jù)類型的定義能颁,cmd格式的封裝
  • 10.完成測試程序,并到開發(fā)板上運(yùn)行測試倒淫。

#define SMPLRT_DIV 0x19 //采樣率分頻伙菊,典型值: 0x07(125Hz) */
#define CONFIG 0x1A // 低通濾波頻率,典型值: 0x06(5Hz) */
#define GYRO_CONFIG 0x1B // 陀螺儀自檢及測量范圍敌土,典型值: 0x18(不自檢镜硕,2000deg/s) */
#define ACCEL_CONFIG 0x1C // 加速計自檢、測量范圍及高通濾波頻率返干,典型值: 0x01(不自檢兴枯, 2G, 5Hz) */
#define ACCEL_XOUT_H 0x3B // 存儲最近的 X 軸犬金、 Y 軸念恍、 Z 軸加速度感應(yīng)器的測量值 */
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41 // 存儲的最近溫度傳感器的測量值 */
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43 // 存儲最近的 X 軸、 Y 軸晚顷、 Z 軸陀螺儀感應(yīng)器的測量值 */
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B // 電源管理峰伙,典型值: 0x00(正常啟用) */
#define WHO_AM_I 0x75 //IIC 地址寄存器(默認(rèn)數(shù)值 0x68,只讀) */
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末该默,一起剝皮案震驚了整個濱河市瞳氓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌栓袖,老刑警劉巖匣摘,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異裹刮,居然都是意外死亡音榜,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門捧弃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赠叼,“玉大人擦囊,你說我怎么就攤上這事∽彀欤” “怎么了瞬场?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長涧郊。 經(jīng)常有香客問我贯被,道長,這世上最難降的妖魔是什么妆艘? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任彤灶,我火速辦了婚禮,結(jié)果婚禮上批旺,老公的妹妹穿的比我還像新娘枢希。我一直安慰自己,他們只是感情好朱沃,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著茅诱,像睡著了一般逗物。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上瑟俭,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天翎卓,我揣著相機(jī)與錄音,去河邊找鬼摆寄。 笑死失暴,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的微饥。 我是一名探鬼主播逗扒,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼欠橘!你這毒婦竟也來了矩肩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤肃续,失蹤者是張志新(化名)和其女友劉穎黍檩,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體始锚,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡刽酱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了瞧捌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片棵里。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出衍慎,到底是詐尸還是另有隱情转唉,我是刑警寧澤,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布稳捆,位于F島的核電站赠法,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏乔夯。R本人自食惡果不足惜砖织,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望末荐。 院中可真熱鬧侧纯,春花似錦、人聲如沸甲脏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽块请。三九已至娜氏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間墩新,已是汗流浹背贸弥。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留海渊,地道東北人绵疲。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像臣疑,于是被迫代替她去往敵國和親盔憨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容