I²c Bus

Source: Internet
Author: User
Tags data structures int size mutex

Two-wire serial bus developed by Philips
GPIO analog I²c driver has its own set of transmission algorithms. Gpio analog i²c is to consume CPU resources, and the use of the I²C chip is not the CPU resources

features
Fewer interface lines, simple control mode, small package form, high communication rate

features a serial data line SDA, a serial clock line SCL It is a true multi-host bus, and if two or more hosts are initialized at the same time, data transmission can be prevented by conflict detection and arbitration of the serial 8 Bit rate of two-way data transfer in standard mode up to 100kbit/s, fast mode up to 400kbit/s, high-speed mode up to 3.4mbit/s connected to the same bus IC number only by the bus maximum capacitance 400pF limit

Architecture I²c Core Framework
Provides a definition of the core data structure and related interface functions to implement the I²C adapter. Driver and device-driven registration, logoff management
Implementation of I2C-CORE.C and I2C-DEV.C I²c bus drivers in the/DRIVERS/I2C directory
Define the I2c_adapter data structure describing the specific I²c bus adapter, implement the I²c bus communication method on the specific i²c adapter, and describe it by the I2C_ALGORITHM data structure. Via the I²c-bus-driven code, we can control the start bit, stop bit, read-write period and read-write from the device, generate ACK, etc.
Implement the Busses folder under the/DRIVERS/I2C directory. For example: The Linux i²c Gpio bus driver is the I2C_GPIO.C;I2C bus algorithm under the/DRIVERS/I2C Directory Algos folder. For example, the Linux i²c Gpio bus driver algorithm is implemented in the I2C_ALGO_BIT.C i²c device driver
Implementation of the specific I²C hardware driver. The I²C device driver communicates with the CPU via the I²C adapter. It mainly contains i2c_driver and i2c_client data structure, i2c_driver structure corresponding to a set of specific driving methods, such as: Probe, remove, suspend, etc., need to declare themselves; i2c_ The client data structure is automatically generated by the kernel based on the specific device registration information
Implement the Chips folder in the/DRIVERS/I2C directory

Device Connection Diagram

Waveform Diagram

Start signal: When the SCL is high, the SDA jumps from high to low, indicating that data will begin to be transferred
End signal: When the SCL is high, SDA jumps from low to high, indicating the end of the transfer data

i2c_client

struct i2c_client
{
    unsigned short flags;        Flag bit
    unsigned short addr;         The address of the device, low 7 bits for the chip address
    Char name[i2c_name_size];    The name of the device, up to a maximum of 20 bytes of
    struct i2c_adapter *adapter;//adapter I2c_adapter attached, adapter indicating the bus
    struct i2c_driver *driver;   The driver that points to the device
    struct device dev;           Device structural body
    int IRQ;                     Interrupt number of the device application
    struct List_head list;       Connect to all devices on the bus
    struct list_head detected;   The equipment linked list has been found to be
    struct completion released;  Whether the completed amount has been released
};

Address Code

1101000x
Read address: 11010001 = 0xd1
Write address: 11010000 = 0xd0
device Address: 01101000 = 0x68//high-fill 0 address code

I2c_driver

struct I2c_driver {int id;//driver identification ID unsigned int class;//driver type int (*attach_adapter) (struct i2c_adapter *); function int (*detach_adapter) (struct i2c_adapter *) Called when the adapter is detected; function int (*detach_client) (struct i2c_client *) Called when the adapter is unloaded __deprecated; Functions called when uninstalling a device/* the following is a new type of driver-driven function that supports the dynamic insertion and extraction of IIC devices. If you do not want to support only implement the above 3.
Do not implement the above 3. Either implement the following 5. Cannot define */INT (*probe) (struct i2c_client *, const struct I2C_DEVICE_ID *) at the same time; New type device probe function int (*remove) (struct i2c_client *); remove function void (*shutdown) (struct i2c_client *) for the new type of device; Close the IIC device int (*suspend) (struct i2c_client *, pm_messge_t MESG); Suspending the IIC device int (*resume) (struct i2c_client *); Recover IIC Device int (*command) (struct i2c_client *client, unsigned int cmd, void *arg); Use commands to make the device perform special functions. Similar to the IOCTL () function struct Devcie_driver driver; Device driven structural body const struct I2C_DEVICE_ID *id_table; Device ID table int (*detect) (struct i2c_client *, int kind, struct i2c_board_info *); callback function for automatic detection device const struct I2C_CLIENT_ADDRESS_DATA *address_Data The address range where the device is located struct List_head clients; Device pointing to drive support};

I2c_adapter

struct I2c_adapter
{
    struct module *owner;//module count
    unsigned int id;//alogorithm type, defined in I2c_id.h
    unsigned int class; Allowable probe driver type
    const struct I2C_ALGORITHM *algo;//driver for adapter
    void *algo_data;//private data to adapter, different
    usage method depending on situation Int (*client_register) (struct i2c_client *); The device client registers with an
    int (*client_unregister (struct i2c_client *);//U8 level is called when the device client logs off
    ;                                                         
    struct Mutex bus_lock; When the bus is operated, the bus lock struct mutex clist_lock;/////list
    operation mutex
    int timeout;//timeout
    int retries;//Retry Count
    struct device dev; The device structure that points to the adapter
    int nr;                                                          
    struct List_head clients; Link List of devices connected to the bus
    Char name[48];//adapter name
    struct completion dev_released;///For synchronization of the completion amount
};

I2c_algorithm

struct I2c_algorithm
{
    int (*master_xfer) (struct i2c_adapter *adap, struct  i2c_msg *msg, int num); 
        A transfer function pointer to a function that implements the IIC Bus communication protocol to determine the adapter supports those transport type
    int (*smbus_xfer) (struct i2c_adapter *adap, U16 addr, unsigned short Flags, char Read_write, U8 command, int size,
        Union i2c_smbus_data *data); The/*smbus-mode transfer function pointer points to a function that implements the SMBus Bus communication protocol.
        SMBus and IIC can be software-compatible, so a function is provided, but it is generally assigned a value of null*/
    u32 (*functionality) (struct i2c_adapter *);//Returns the features supported by the adapter
};

i2c_msg

struct i2c_msg
{
    __u16 addr;//iic device address. This field indicates that an adapter can interact with multiple IIC devices after gaining control of the bus
    __U16 flags;//message type flag      
    __u16 len;//message byte length
    __u8 *buf;//buffer point to message data
};

I2c_adapter and I2c_algorithm
The i2c_adapter corresponds to a physical adapter, while the i2c_algorithm corresponds to a set of communication methods. An I²C adapter requires the communication functions provided in I2c_algorithm to control the adapter to generate a specific access cycle. I2c_adapter without i2c_algorithm can do nothing, so the i2c_adapter contains pointers to the i2c_algorithm they use
The key function in I2c_algorithm Master_xfer () is used to generate the signals required for the I²c access cycle, in i2c_msg units

I2c_driver and I2c_client
The i2c_driver corresponds to a set of drive methods, which are purely auxiliary data structures that do not correspond to any physical entities. The i2c_client corresponds to a real physical device, and each i²c device needs a i2c_client to describe it. I2c_client is generally included in the private information structure of the I²C character device
I2c_driver is associated with i2c_client at the moment when the I2c_driver Attach_adapter () function is run. Attach_adapter () detects the physical device and, when determining the presence of a client, points the adapter pointer of the I2c_client data structure used by that client to the corresponding I2c_adapter
The driver pointer points to the I2c_driver and calls the I2c_adapter Client_register () function. The opposite process occurs when the I2c_driver detach_client () function is called

I2c_adpater and I2c_client
The relationship between I2c_adpater and I2c_client is consistent with the adapter and device in the hardware system, that is, i2c_client is attached to I2c_adpater. Because multiple i²c devices can be attached to an adapter, a i2c_adpater can also be attached to multiple i2c_client, and the i2c_adpater includes a linked list of i2c_client attached to it

SMBus
SMBus is a subset of I²c I2c_smbus_read_byte
Reads a byte from the device (does not define a position offset, using the offset of the previously initiated command). No significance, no base address i2c_smbus_write_byte
Writes a byte from the device (using an offset from a previously initiated command) I2c_smbus_write_quick
Send a bit i2c_smbus_read_byte_data to the device
Reads a byte from the device at the specified offset. The first msg is used to transmit a read base address, and the second MSG is used to read the data i2c_smbus_write_byte_data
Writes a byte to the device at the specified offset. Transmitting two MSG I2c_smbus_read_word_data
Reads two bytes from the device at the specified offset i2c_smbus_write_word_data
Writes two bytes to the device at a specified offset i2c_smbus_read_block_data
Reads a piece of data from the device at the specified offset i2c_smbus_write_block_data
Writes a piece of data to the device at the specified offset (<= 32 bytes)

The above series of functions is ultimately called the I2c_smbus_xfer () function

/** * I2c_smbus_xfer-execute SMBus Protocol operations * @adapter: Handle to I²c bus * @addr: Address of SMBus slave On that bus * @flags: i2c_client_* flags (usually zero or i2c_client_pec) * @read_write: I2c_smbus_read or I2c_smbus_wri TE * @command: Byte interpreted by slave, for protocols which use such bytes * @protocol: SMBus protocol operation to ex Ecute, such as I2c_smbus_proc_call * @data: Data to is read or written * * This executes an SMBUS protocol operation, a
 nd returns a negative * errno code else Zero on success.  */S32 I2c_smbus_xfer (struct i2c_adapter *adapter, U16 addr, unsigned short flags, char read_write, U8 command,
    int protocol, Union i2c_smbus_data *data) {unsigned long orig_jiffies;
    int try;

    S32 Res;
     /* If enabled, the following tracepoints is conditional on * read_write and protocol.
    */Trace_smbus_write (adapter, addr, flags, Read_write, command, Protocol, data); Trace_smBus_read (adapter, addr, flags, Read_write, Command, protocol); Flags &= I2c_m_ten | I2c_client_pec |

    I2C_CLIENT_SCCB;

        if (adapter->algo->smbus_xfer) {I2c_lock_adapter (adapter);
        /* Retry automatically on Arbitration loss */orig_jiffies = jiffies; for (res = 0, try = 0; try <= adapter->retries; try++) {res = Adapter->algo->smbus_xfer (Adapter,
            addr, flags, Read_write, command, Protocol, data);
            if (res! =-eagain) break;
        if (Time_after (jiffies, orig_jiffies + adapter->timeout)) break;

        } i2c_unlock_adapter (adapter);
        if (res! =-eopnotsupp | |!adapter->algo->master_xfer) goto trace; /* * Fall back to i2c_smbus_xfer_emulated if the adapter doesn ' t * implement native support for the Smbu s operation.
         */} res = i2c_smbus_xfer_emulated (adapter, addr, flags, Read_write, command, pro

Tocol, data); Trace:/* If enabled, the reply tracepoint is conditional on read_write.
    */trace_smbus_reply (adapter, addr, flags, Read_write, command, Protocol, data);

    Trace_smbus_result (adapter, addr, flags, Read_write, command, Protocol, RES);
return res; } export_symbol (I2c_smbus_xfer);

The first is to determine whether the host controller supports Smbus_xfer transmission, but usually the I²C host controller is not supported, so call the i2c_smbus_xfer_emulated () function directly

Reference: http://blog.chinaunix.net/uid-27041925-id-3631304.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.