3

I have a Raspberry Pi model B with Raspbian and a Busware SD0 cape.
The SD0 has an RTC that can be read via the I2C bus. I want set the time and date of the Raspberry Pi because it is not always connected to the internet so the time can't be set via NTP.

Is it possible to use I2C without using "external" libraries like WiringPi (like you can use the UART via a Linux kernel module)?

2
  • Please be aware that I think the Linux kernel supports some I2C RTC chips. I am not sure if the DS1338 is one of the supported ones though.
    – joan
    Jul 15 '15 at 16:29
  • 1
    (Cheers @joan). The DS1338 is mentioned as compatible in the source for the DS1307 driver. This driver is in Raspbian. You could try sudo modprobe rtc-ds1307; then I guess check hwclock.
    – goldilocks
    Jul 15 '15 at 18:48
6

The kernel has an API for SMBus/I2C. You just have to include a couple of headers:

#include <linux/i2c-dev.h>
#include <sys/ioctl.h>

There's no library that needs linking. I've used this to write C++ based interfaces to various I2C sensors, I'm sure it can be made to work with an RTC. The API isn't the complicated part, it's figuring out how to use it in relation to a datasheet.

I did notice that sometimes that using a read() or write() on the file descriptor in a manner that should have duplicated an i2c_smbus function did not produce the same result. I mention this because if one method does not seem to work, try the other.

3

There is no need to use a third party library to access I2C.

Just search for Linux I2C to find code examples.

Here is a Pi example of mine to read an ADXL345 accelerometer via I2C. Note, this code defaults to bus 0.

1
  • 1
    +1 for mentioning bus 0. I wasted almost 2 days once for making a driver work. :( All I had to do was to change a 0 to 1. :) Jul 15 '15 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.