Linux I2C read/write instance

Source: Internet
Author: User

During this time, a capacitive touch screen was created. After experiment, we finally tried it out, because we didn't have this one in the past, and we have accumulated a lot of experience. There are two key points to illustrate:

(1) read/write functions of I2C devices. The sequence is as follows:

Based on the features, I did not use the SMBus read/write function, but I still use the I2C read/write function to handle it:

Static struct i2c_client * g_client; // client registered with the I2C Device

Static int gw8158_read_register_packet (u8 bufp [13]) // read 13 bytes and use Array
{
Int ret = 0, Count = 0;
U8 buffer [13];
Unsigned long flags;
Spinlock_t * Lock = get_i2c_lock (); // gets the I2C spin lock.
If (g_client = NULL) | (Lock = NULL ))
Return-1;

Spin_lock_irqsave (lock, flags); // spin lock for I2C operations
Ret = i2c_master_recv (g_client, buffer, 13); // The device address is written by default. Because there is no sub-address, you can directly receive
Spin_unlock_irqrestore (lock, flags );
If (Ret> 0)
{
For (COUNT = 0; count <13; count ++)
Bufp [count] = buffer [count]; // If read succeeds, return the register value
Printk ("gw8158_read_register_packet OK/N ");
}
Return ret;
}
Static int gw8158_write_register_byte (const u8 subaddress, u8 bufp)
{
Int ret;
U8 buffer [2];
Unsigned long flags;
Spinlock_t * Lock = get_i2c_lock ();
If (g_client = NULL) | (Lock = NULL ))
Return-1;

Buffer [0] = subaddress;
Buffer [1] = bufp;
Spin_lock_irqsave (lock, flags );
If (i2c_master_send (g_client, buffer, 1) <0) // write the subaddress once. This write only verifies whether the communication is successful. Because there is no data
Return-1; // It does not make sense if the write is successful. The real write control will not be affected.

Ret = i2c_master_send (g_client, buffer, 2); // write the subaddress and data only once.
Spin_unlock_irqrestore (lock, flags );
If (Ret> 0)
Printk ("gw8158_write_register_byte OK/N ");
Return ret;
}
(2) reset part.

It is a high reset, so there must be a sequence from low to high, but it cannot always be high, otherwise it will always reset, it has to be pulled down. As follows:

Pxa3xx_outgpio_set_level (mfp_ts_gw8158_rst, gpio_level_low );
Mdelay (30 );
Pxa3xx_outgpio_set_level (mfp_ts_gw8158_rst, gpio_level_high );
Mdelay (20 );
Pxa3xx_outgpio_set_level (mfp_ts_gw8158_rst, gpio_level_low );
Mdelay (1000 );

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.