Page 219
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
void checkI2CBus();
int main(int argc, char **argv)
{
int i2cfd = open("/dev/i2c-1", O_RDWR);
ioctl(i2cfd, I2C_SLAVE, 0x40);
char buf[3] = {0xE3};
write(i2cfd, buf, 1);
int result = read(i2cfd, buf, 3);
uint8_t msb = buf[0];
uint8_t lsb = buf[1];
uint8_t check = buf[2];
printf("msb %d \n\rlsb %d \n\rchecksum %d \n\r", msb, lsb, check);
}
Page 222
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
void checkI2CBus();
int i2cReadRegister(int i2cfd, uint8_t slaveaddr, uint8_t reg, uint8_t *buf, int len);
int main(int argc, char **argv)
{
checkI2CBus();
int i2cfd = open("/dev/i2c-1", O_RDWR);
char buf2[1] = {0};
i2cReadRegister(i2cfd, 0x40, 0xE7, buf2, 1);
printf("%d \n\r", buf2[0]);
return (EXIT_SUCCESS);
}