// File: ds3231.cpp // Read and display index register 2 from a DS3231 RTC on the I2C bus // Author: Jim Merkle #include // printf() #include // open() #include // open() #include // open() #include // ioctl() #include // errno #include // strerror() #include // close() #include // struct i2c_msg #include // struct i2c_rdwr_ioctl_data int main(int argc, char * argv[]) { struct i2c_msg ds3231msg[2]; // declare our two i2c_msg array unsigned char ucIndex = 2; unsigned char ucData = 0; // Load up transmit msg ds3231msg[0].addr = 0x68; ds3231msg[0].flags = 0; ds3231msg[0].len = sizeof(ucIndex); ds3231msg[0].buf = &ucIndex; // Load up receive msg ds3231msg[1].addr = 0x68; ds3231msg[1].flags = I2C_M_RD; ds3231msg[1].len = sizeof(ucData); ds3231msg[1].buf = &ucData; // Load up i2c_rdwr_ioctl_data struct i2c_rdwr_ioctl_data i2c_data; // declare our i2c_rdwr_ioctl_data structure i2c_data.msgs = ds3231msg; i2c_data.nmsgs = 2; // Open file descriptor to I2C bus int fd = open("/dev/i2c-1",O_RDWR); if(fd<0) { printf("Failed to open i2c-1."); return -1; } // With our file descriptor, perform I2C message transfers int result = ioctl(fd,I2C_RDWR,&i2c_data); if(result < 0) { printf("ioctl error: %s\n", strerror(errno)); return -1; } // We have read a data byte from index 2... Display it printf("Index 2: 0x%02X\n",ucData); close(fd); return 0; }