/** Simple I2C example to read the first address of a device in C * Written by Derek Molloy for the book "Exploring BeagleBone: Tools and * Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014 * ISBN 9781118935125. Please see the file README.md in the repository root * directory for copyright and GNU GPLv3 license information. */ #include #include #include #include #include // Small macro to display value in hexadecimal with 2 places #define DEVID 0x00 #define BUFFER_SIZE 40 int main(){ int file; printf("Starting the ADXL345 test application\n"); if((file=open("/dev/i2c-1", O_RDWR)) < 0){ perror("failed to open the bus\n"); return 1; } if(ioctl(file, I2C_SLAVE, 0x53) < 0){ perror("Failed to connect to the sensor\n"); return 1; } char writeBuffer[1] = {0x00}; if(write(file, writeBuffer, 1)!=1){ perror("Failed to reset the read address\n"); return 1; } char readBuffer[BUFFER_SIZE]; if(read(file, readBuffer, BUFFER_SIZE)!=BUFFER_SIZE){ perror("Failed to read in the buffer\n"); return 1; } printf("The Device ID is: 0x%02x\n", readBuffer[DEVID]); close(file); return 0; }