7-i2C總線_mpu6050驅動編程

i2c總線協議:
  • 歷史:
    • 是飛利浦公司在80年代,爲研發音視頻模塊的通訊而產生。因其具有的諸多優點,而沿用至今。
  • 優點:
    • 1.簡單性
    • 2.有效性
    • 3.多主控性。
i2c總線的信號:
  • 1.起始信號:當scl保持高電平期間,sda由高到低跳變,稱爲起始信號。
  • 2.結束信號:當scl保持高電平期間,sda由低到高跳變,稱爲結束信號。
  • 3.數據位: 當scl保持高電平期間,sda保持電平穩定有效性,稱爲有效數據位。
  • 3.應答信號:當scl保持高電平期間,當發送一個字節的數據後,必須要有對方的應答信號。
i2c總線使用幾大原則:
  • a – 在SCL=1(高電平)時,SDA千萬別忽悠!!!否則,SDA下跳則”判罰”爲”起始信號S”,SDA上跳則”判罰”爲”停止信號P”.
  • b – 在SCL=0(低電平)時,SDA隨便忽悠!!!(可別忽悠過火到SCL跳高)
  • c – 每個字節後應該由對方回送一個應答信號ACK做爲對方在線的標誌.一般要由雙方協議簽定.
  • d – SCL必須由主機發送,否則天下大亂
  • e – 首字節是”片選信號”,即7位從機地址加1位方向(讀寫)控制.從機收到(聽到)自己的地址才能發送應答信號(必須應答!!!)表示自己在線.其他地址的從機不允許忽悠!!!(當然羣呼可以忽悠但只能聽不許說話)
  • f – 讀寫是站在主機的立場上定義的.”讀”是主機接收從機數據,”寫”是主機發送數據給從機.
  • g – 7位IIC總線可以掛接127個不同地址的IIC設備,0號”設備”作爲羣呼地址.10位IIC總線可以掛接更多的10位IIC設備.
i2c總線的框架:
  • 1.總線驅動層:主要數據結構是i2c_adapter和i2c_algorithm,總線驅動層完成總線控制器設備的軟件實現,和i2c總線通信的方法等
  • 2.i2c核心層:主要提供api給總線驅動層和設備驅動層,完成總線驅動層和設備驅動層之間的通信和匹配。
  • 3.設備驅動層:主要數據結構是i2c_client和i2c_driver,完成從設備的硬件和軟件框架功能的實現。
主要數據結構:
  • 1.i2c_adapter:是描述i2c控制器的數據結構
  • 2.i2c_algorithm: 是描述i2c控制器的通信方法,通俗講就是i2c控制器的驅動部分
  • 3.i2c_client: 是描述總線上從設備的硬件部分的數據結構
  • 4.i2c_driver: 是描述總線上從設備的驅動部分的數據結構
  • 5.i2c_msg: 是在i2c總線上傳輸的數據的格式
主要使用函數:
  • 1.i2c_transfer:完成i2c總線上,主從設備之間的信號或者數據的發送和接收。

/*
 * 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)```
* 函數功能:完成i2c總線上,主從設備之間的信號或者數據的發送和接收。
* 返回值:成功爲發送的msg個數,失敗返回負數
* 參數1: 就是i2c總線所依賴的i2c控制器
* 參數2: 要傳輸的數據格式msg
* 參數3:  呀傳輸的msg的個數
***





<div class="se-preview-section-delimiter"></div>

###### mpu6050驅動編程:
* 1.確定需求:  fs4412開發板所相連的mpu6050器件工作起來。並在應用層採集到數據。
* 2.查看原理圖和數據手冊,得知,mpu6050與exynos4412的i2c通道5相連,mpu6050的從地址是0x68
    * 還有mpu6050的重要的控制寄存器和數據寄存器。
* 3.完成設備樹的書寫:描述兩個重要設備信息:  i2c5通道和從地址0x68
* 4.完成i2c_driver的框架編程。
* 5.完成封裝兩個重要功能函數,分別完成從mpu6050數據寄存器中讀取數據和向mpu6050控制寄存器中寫入數據的
    功能。
* 6.完成字符設備8步流程,並且完成自動創建設備節點。
* 7.完成mpu6050控制寄存器的初始化。
* 8.完成file_operations中ioctl函數的實現。主要實現三種傳感器的數據讀取,並傳遞到應用層。
* 9.完成頭文件,頭文件中有寄存器的地址,ioctl中傳遞到應用層數據類型的定義,cmd格式的封裝
* 10.完成測試程序,併到開發板上運行測試。

***





<div class="se-preview-section-delimiter"></div>

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 軸加速度感應器的測量值 */

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 軸陀螺儀感應器的測量值 */

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 地址寄存器(默認數值 0x68,只讀) */

“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章