This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
i2c [2016/01/10 21:25] – created bullar | i2c [2016/01/10 21:31] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 10: | Line 10: | ||
</ | </ | ||
+ | Example program (part of retest) | ||
+ | <file c i2c.h> | ||
+ | /* | ||
+ | * i2c.h | ||
+ | * | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | #ifndef I2C_H_ | ||
+ | #define I2C_H_ | ||
+ | |||
+ | #include < | ||
+ | |||
+ | void i2cOpen(uint8_t devno); | ||
+ | void i2cClose(); | ||
+ | void i2cSetAddress(uint8_t address); | ||
+ | uint8_t i2cReadData8(uint8_t reg); | ||
+ | int16_t i2cReadData16(uint8_t reg); | ||
+ | void i2cWriteData8(uint8_t reg, uint8_t value); | ||
+ | void i2cWriteData16(uint8_t reg, uint16_t value); | ||
+ | void i2cDetectDevice(void); | ||
+ | void i2cReadRCValues(void); | ||
+ | |||
+ | #endif /* I2C_H_ */ | ||
+ | </ | ||
+ | |||
+ | |||
+ | <file c i2c.c> | ||
+ | /* | ||
+ | * i2c.c | ||
+ | * | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | // I2C Linux device handle | ||
+ | int g_i2cFile; | ||
+ | |||
+ | // | ||
+ | // open the Linux device | ||
+ | void i2cOpen(uint8_t devno) | ||
+ | { | ||
+ | char device[20]; | ||
+ | snprintf(device, | ||
+ | g_i2cFile = open(device, | ||
+ | if (g_i2cFile < 0) { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // close the Linux device | ||
+ | void i2cClose() | ||
+ | { | ||
+ | close(g_i2cFile); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // set the I2C slave address for all subsequent I2C device transfers | ||
+ | void i2cSetAddress(uint8_t address) | ||
+ | { | ||
+ | if (ioctl(g_i2cFile, | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // read 8-bit value from register reg | ||
+ | uint8_t i2cReadData8(uint8_t reg) { | ||
+ | |||
+ | uint8_t | ||
+ | |||
+ | if (write(g_i2cFile, | ||
+ | perror(" | ||
+ | return 0; | ||
+ | } | ||
+ | if (read(g_i2cFile, | ||
+ | perror(" | ||
+ | return 0; | ||
+ | } | ||
+ | return data; | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // read a 16 bit value from a register pair | ||
+ | // read high byte of value from register reg, | ||
+ | // and low byte of value from register reg+1 | ||
+ | int16_t i2cReadData16(uint8_t reg) { | ||
+ | |||
+ | uint8_t | ||
+ | |||
+ | if (write(g_i2cFile, | ||
+ | perror(" | ||
+ | return 0; | ||
+ | } | ||
+ | if (read(g_i2cFile, | ||
+ | perror(" | ||
+ | return 0; | ||
+ | } | ||
+ | return data[1] | (data[0] << 8); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // write a 8 bit value to a register reg | ||
+ | void i2cWriteData8(uint8_t reg, uint8_t value) | ||
+ | { | ||
+ | uint8_t data[2]; | ||
+ | data[0] = reg; | ||
+ | data[1] = value; | ||
+ | if (write(g_i2cFile, | ||
+ | perror(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // write a 16 bit value to a register pair | ||
+ | // write high byte of value to register reg, | ||
+ | // and low byte of value to register reg+1 | ||
+ | void i2cWriteData16(uint8_t reg, uint16_t value) | ||
+ | { | ||
+ | uint8_t data[3]; | ||
+ | data[0] = reg; | ||
+ | data[1] = (value >> 8) & 0xff; | ||
+ | data[2] = value & 0xff; | ||
+ | if (write(g_i2cFile, | ||
+ | perror(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // try to detect both i2c devices | ||
+ | // MPU-6050 has a fixed ID of 0x68 at reg 0x75 | ||
+ | // RC-Mixer has a fixed ID of 0x34 at reg 0x1F | ||
+ | void i2cDetectDevice(void) { | ||
+ | |||
+ | uint16_t result; | ||
+ | |||
+ | // MPU-6050 | ||
+ | i2cOpen(2); | ||
+ | i2cSetAddress(MPU6050_ADDRESS); | ||
+ | result = i2cReadData8(MPU6050_RA_WHO_AM_I); | ||
+ | printf(" | ||
+ | if (result == 0x68) printf(" | ||
+ | else printf(" | ||
+ | i2cClose(); | ||
+ | |||
+ | // RC-Mixer | ||
+ | i2cOpen(1); | ||
+ | i2cSetAddress(0x34); | ||
+ | result = i2cReadData8(31); | ||
+ | printf(" | ||
+ | if (result == 0x34) printf(" | ||
+ | else printf(" | ||
+ | i2cClose(); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | // get RC values from RC-Mixer | ||
+ | void i2cReadRCValues(void) { | ||
+ | |||
+ | uint16_t result; | ||
+ | int i; | ||
+ | |||
+ | // RC-Mixer | ||
+ | i2cOpen(1); | ||
+ | i2cSetAddress(0x34); | ||
+ | |||
+ | printf(" | ||
+ | printf(" | ||
+ | for(i=0; | ||
+ | result = i2cReadData16(i*2); | ||
+ | printf(" | ||
+ | } | ||
+ | printf(" | ||
+ | i2cClose(); | ||
+ | } | ||
+ | </ |