[i2c]pca9555 Application Layer test code

Source: Internet
Author: User
Tags case statement

Https://www.cnblogs.com/aaronLinux/p/6896573.html


Note: If you set the I2c_slave, the prompt device_busy, you can use the I2c_slave_force, in the driver which corresponds to the same Case statement application layer can call the interface: I2c_smbus_write_ Word_data (FD, __, __); and I2c_smbus_read_word_data (fd,__);

Share : https://stackoverflow.com/questions/9974592/i2c-slave-ioctl-purpose

The questions are as follows :

1. The direct FD handle in the application is the file handle of the entire I2C0 bus, but only when the set address is set, the I2C addresses are installed, followed by the operation of the chip or operation of the global I2C0 bus on this file, and do not specify to operate the chip, which is how to do it. In this case, if you operate a different I2C device on the same bus in a main program, and the file handle is the same, how does it work?

--------------------------------------------------------------------------------------------

Drive Aspect :

First configure the I2C kernel driver, the pca9555 source built-in into (where possible with the thermal driver), and then in Devicetree according to the PCA9555 hardware I2C address configuration node.

Test Source :

I2C test program for a PCA9555 #include <stdint.h> #include <stdlib.h> #include <stdio.h> #include & lt;unistd.h> #include <linux/i2c-dev.h> #include <sys/types.h> #include <sys/stat.h> #include

<sys/ioctl.h> #include <fcntl.h>//I2C Linux device handle int g_i2cfile;
    Open the Linux device void I2copen () {g_i2cfile = open ("/dev/i2c-0", O_RDWR);
        if (G_i2cfile < 0) {perror ("I2copen");
    Exit (1);

}//Close the Linux device void I2cclose () {close (g_i2cfile);} Set the I2C slave address to all subsequent I2C device transfers void i2csetaddress (int address) {if IOCTL (G_I2C
        File, I2c_slave, address) < 0) {perror ("i2csetaddress");
    Exit (1); }//write a bit value to a register pair//write low byte of value to register Reg,//And high byte of value to R
    Egister reg+1 void Pca9555writeregisterpair (uint8_t reg, uint16_t value) {uint8_t data[3]; Data[0] = reg;
    DATA[1] = value & 0xFF;
    DATA[2] = (value >> 8) & 0xFF;
    if (write (G_i2cfile, data, 3)!= 3) {perror ("Pca9555setregisterpair");
    }//read a bit value from a register pair uint16_t Pca9555readregisterpair (uint8_t reg) {uint8_t data[3];
    Data[0] = reg;
    if (write (G_i2cfile, data, 1)!= 1) {perror ("Pca9555readregisterpair set Register");
    } if (read (G_i2cfile, data, 2)!= 2) {perror ("Pca9555readregisterpair read value"); return Data[0] |
(Data[1] << 8); //Set IO ports to input, if the corresponding direction bit are 1,//otherwise set it to output void pca9555setinputdi

Rection (uint16_t direction) {Pca9555writeregisterpair (6, direction);}

Set the IO port outputs void Pca9555setoutput (uint16_t value) {Pca9555writeregisterpair (2, value);}

Read the IO port inputs uint16_t Pca9555getinput () {return Pca9555readregisterpair (0);}
int main (int argc, char** argv){//test output value int v = 3;

    Direction of the LED animation int directionleft = 1;

    Open Linux I2C device I2copen ();

    Set address of the PCA9555 i2csetaddress (0x20);

    Set input for IO pin, rest output pca9555setinputdirection (1 << 15);
        LED Animation Loop while (1) {//If button is pressed, invert output int xor;
        if (Pca9555getinput () & 0x8000) {xor = 0;
        else {xor = 0xFFFF;

        }//Set current output pca9555setoutput (v ^ xor);
        Animate LED position if (directionleft) {v <<= 1;
        else {v >>= 1;
        } if (v = = 0x6000) {directionleft = 0;
        } if (v = = 3) {directionleft = 1;
    //Wait MS for next animation step Usleep (100000); }//Close Linux I2C device I2cclose ();
return 0; }

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.