[I 2 C]pca 9555(i 2 c-gpio拡張)アプリケーション層テストコード
11225 ワード
注意点:設定I 2 C_SLAVEの場合は、デバイス_busy、I 2 C_を使用できますSLAVE_FORCEは、駆動中に両者が同じcase文 に対応するアプリケーション層はインタフェースを呼び出すことができる:i 2 c_smbus_write_word_data(fd, __, __);とi 2 c_smbus_read_word_data(fd,__);
共有: https://stackoverflow.com/questions/9974592/i2c-slave-ioctl-purpose
問題は次のとおりです.
1.アプリケーション内の直接fdハンドルはI 2 C 0バス全体のファイルハンドルであり、setアドレスのときにI 2 C addressを設定するだけで、後でチップを操作するか、グローバルI 2 C 0バス上のファイルを操作するか、チップを操作する指定はありません.この中でどのようにしてできますか.そうすると、1つのmainプログラムで同じバス上の異なるi 2 cデバイスを操作し、ファイルハンドルが同じであれば、どのように操作しますか?
--------------------------------------------------------------------------------------------
ドライブ:
まずI 2 Cカーネルドライバを構成し、pca 9555のソースコードbuilt-inを(ここでは必要に応じてthermalのドライバを割り当てることができる)、次いでdevicetreeでpca 9555ハードウェアI 2 Cアドレスに基づいてノードを構成する.
テストソース:
転載先:https://www.cnblogs.com/aaronLinux/p/6896573.html
共有:
問題は次のとおりです.
1.アプリケーション内の直接fdハンドルはI 2 C 0バス全体のファイルハンドルであり、setアドレスのときにI 2 C addressを設定するだけで、後でチップを操作するか、グローバルI 2 C 0バス上のファイルを操作するか、チップを操作する指定はありません.この中でどのようにしてできますか.そうすると、1つのmainプログラムで同じバス上の異なるi 2 cデバイスを操作し、ファイルハンドルが同じであれば、どのように操作しますか?
--------------------------------------------------------------------------------------------
ドライブ:
まずI 2 Cカーネルドライバを構成し、pca 9555のソースコードbuilt-inを(ここでは必要に応じてthermalのドライバを割り当てることができる)、次いでdevicetreeでpca 9555ハードウェアI 2 Cアドレスに基づいてノードを構成する.
テストソース:
// I2C test program for a PCA9555
#include
#include
#include
#include
#include
#include
#include
#include
#include
// 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 for all subsequent I2C device transfers
void 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+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 16 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 is 1,
// otherwise set it to output
void pca9555SetInputDirection(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 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;
}
転載先:https://www.cnblogs.com/aaronLinux/p/6896573.html