[I2C]pca9555應用程式層測試代碼

來源:互聯網
上載者:User

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


注意點: 如果在設定I2C_SLAVE的時候,提示device_busy,可以使用I2C_SLAVE_FORCE, 在驅動裡面二者對應同一個case語句 應用程式層可以調用介面:i2c_smbus_write_word_data(fd, __, __);和i2c_smbus_read_word_data(fd,__);

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

問題如下

1. 應用程式中直接fd控制代碼是整個I2C0匯流排的檔案控制代碼,而只是在set地址的時候,將I2C address設定下去,後面操作該晶片還是操作全域I2C0匯流排上這個檔案,並沒有指定去操作該晶片,這其中是怎麼做到的。這樣的話,如果在一個main程式中, 操作同一匯流排上不同i2c裝置,而檔案控制代碼是同一個,這要如何操作。

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

驅動方面

首先配置I2C核心驅動,將pca9555的源碼built-in進入(這雷根據需要可能要配thermal的驅動),然後在devicetree中根據pca9555硬體I2C地址配置節點。

測試源碼

// I2C test program for a PCA9555#include <stdint.h>#include <stdlib.h>#include <stdio.h>#include <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 handleint g_i2cFile;// open the Linux devicevoid i2cOpen(){    g_i2cFile = open("/dev/i2c-0", O_RDWR);    if (g_i2cFile < 0) {        perror("i2cOpen");        exit(1);    }}// close the Linux devicevoid i2cClose(){    close(g_i2cFile);}// set the I2C slave address for all subsequent I2C device transfersvoid i2cSetAddress(int address){    if (ioctl(g_i2cFile, I2C_SLAVE, address) < 0) {        perror("i2cSetAddress");        exit(1);    }}// write a 16 bit value to a register pair// write low byte of value to register reg,// and high byte of value to register reg+1void 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 16 bit value from a register pairuint16_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 is 1,// otherwise set it to outputvoid pca9555SetInputDirection(uint16_t direction){    pca9555WriteRegisterPair(6, direction);}// set the IO port outputsvoid pca9555SetOutput(uint16_t value){    pca9555WriteRegisterPair(2, value);}// read the IO port inputsuint16_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 15, 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 100 ms for next animation step        usleep(100000);    }    // close Linux I2C device    i2cClose();    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.