benzeman
Posts: 108
Joined: Fri Apr 20, 2012 12:04 pm
Contact: Website

RTC Software

Thu May 17, 2012 10:29 am

So, after the London Raspberry Pi Faces Meetup, I've ordered a cover for my raspberry pi, as well as a breadboard, RTC chip (1307), and all the other random stuff you need (cables, crystal, smoothing capacitor, battery, etc).

I plan to connect the RTC to the Raspberry Pi over I2C. I understand it won't work automatically, but I'll need to write a short program which will run at startup, and which I hope will look something like this:

Code: Select all

Can I get the time from an NTP server?
If Yes {
  Set RPi clock to time from NTP server
  Set RTC clock to time from NTP server
  echo "Set time to xx:xx:xx using NTP server"
}
If No {
  Can I get the time from the RTC?
  If Yes {
    Set RPi clock to time from RTC
    echo "Set time to xx:xx:xx using RTC"
  }
  If No {
    echo "Unable to set time from NTP server or RTC. Please check internet connection and / or RTC."
  }
}

Two questions:

1) What would be the "best" language to write this in? I want to be able to add the program fairly easily to any OS image I want to use.
2) How would I go about writing this? Any hints would be greatly appreciated!

Many thanks,

Ben

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RTC Software

Thu May 17, 2012 1:28 pm

benzeman wrote:1) What would be the "best" language to write this in? I want to be able to add the program fairly easily to any OS image I want to use.
Probably BASH, as that's the language used by the rest of the Linux startup-scripts.
Or you could code it in C which would be much harder, but allow it to execute faster, which might get you slightly better time-precision?
2) How would I go about writing this? Any hints would be greatly appreciated!
You'll probably want to investigate the date / ntpdate / hwclock commands, and this kernel module http://cateee.net/lkddb/web-lkddb/RTC_DRV_DS1307.html
Good luck, I'm sure it's something many people will find useful 8-)

If both the NTP and RTC methods fail, you could use this as a final fallback: http://groups.google.com/group/bifferbo ... 61c31850dd
(you'll obviously need to change the /dev/sda1 part!)

arm2
Posts: 253
Joined: Thu Dec 15, 2011 3:46 pm

Re: RTC Software

Thu May 17, 2012 8:00 pm

Noooooo don't use a DS1307 it needs 5V and if you connect it to 5V then the I2C lines will be 5V which can blow your Pi!

benzeman
Posts: 108
Joined: Fri Apr 20, 2012 12:04 pm
Contact: Website

Re: RTC Software

Thu May 17, 2012 8:18 pm

arm2 wrote:Noooooo don't use a DS1307 it needs 5V and if you connect it to 5V then the I2C lines will be 5V which can blow your Pi!
5v I2C lines is a problem? I thought I2C was up to 5v? Crumbs...

I've got the chip in a breadboard, what do I need to do?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RTC Software

Thu May 17, 2012 11:31 pm

According to http://www.maxim-ic.com/datasheet/index.mvp/id/2688 the DS1307 needs a 5V supply.
So you either need a I2C-compatible (i.e. bi-directional) level-converter to interface the 5V i2c of the RTC to the 3.3V i2c of the Rpi, or you could get a RTC that works with a 3.3V supply, e.g. http://www.maxim-ic.com/datasheet/index.mvp/id/3128

benzeman
Posts: 108
Joined: Fri Apr 20, 2012 12:04 pm
Contact: Website

Re: RTC Software

Fri May 18, 2012 7:10 am

Well, I've just ordered a free sample of a DS1340Z-3+ - this works on 3V3 I think...

benzeman
Posts: 108
Joined: Fri Apr 20, 2012 12:04 pm
Contact: Website

Re: RTC Software

Fri May 18, 2012 7:43 am

arm2 wrote:Noooooo don't use a DS1307 it needs 5V and if you connect it to 5V then the I2C lines will be 5V which can blow your Pi!
Just been told I have to connect the I2C lines myself, and that connecting them to 3.3v shouldn't be an issue by the shop owner. Thoughts?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RTC Software

Fri May 18, 2012 10:42 am

I've only ever used 3v3 i2c myself (not on the Raspi yet though), so I'm not claiming to be any kind of expert, but this throws up lots and lots of info: http://www.google.com/search?q=i2c+5v+3v
With the scarcity of Raspis, do you really want to risk frying any of it's GPIO lines? :?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RTC Software

Fri May 18, 2012 4:18 pm


User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13006
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: RTC Software

Fri May 18, 2012 5:21 pm

Just use (4K7) pullups to 3V3, and all will be fine, no level translators needed.
i2c devices all operate with open collector outputs, so no I2C device will ever force its supply voltage on the bus, and all I2C devices will accept 3V3 as logical high (generally everything above 2.0 Volt).

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RTC Software

Fri May 18, 2012 5:26 pm

mahjongg: forgive my naivety / newbie-ness, but if that's the case how come there's so much talk "on the internet" about i2c level converters/conversion? :? :oops:

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13006
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: RTC Software

Fri May 18, 2012 5:59 pm

Good question!

Actually I don't really know why, except that there are a few other tradeoffs:

*These work with high voltage sections with voltages above 5V, and the circuit protects the low voltage section from potential failures in the high voltage section.
*5V and 3V3 I2C sections can be logically isolated from each other.
*it also works in principle with devices that work with less than 2V (1V8) . Trying to use pullups to 1V8 would cause problems with 3V3 and 5V devices that expect 2V high levels.
*the 3V3 and 5V sections can have optimal pullup strengths, so that raising flanks of the signals are faster.

perhaps Philips thought that pullups to 3V3 would be too weak for 5V devices, and 3V3 devices would be too weak for pullups strong enough to compensate for the lower pullup voltage.

In any case, I have seen products that used 3V3 and 5V I2C logic without conversion circuits, but maybe only at the lower speeds (that RTC's normally use). perhaps not at 300KHz speeds.

The "canonical" document from philips about bus translation can be found here:
http://ics.nxp.com/support/documents/in ... n97055.pdf

selsinork
Posts: 151
Joined: Mon Apr 16, 2012 8:31 am

Re: RTC Software

Fri May 18, 2012 6:50 pm

Sooner or later it's a question of reliability. All sorts of things work just fine for someone doing it as a hobby with two devices on the bus. A company making and selling millions of some device will trade off cost of adding the level shifters vs cost of returns when it doesn't work or is just unreliable.

In the I2C spec, http://www.nxp.com/documents/user_manual/UM10204.pdf section 3.1.2 you'll see that logic levels are referenced to supply voltage: "VIL is 0.3VDD and VIH is 0.7VDD".
So it's likely difficult to get a chip manufacturer to agree to "generally everything above 2.0 Volt" as being seen as logic high on a device being supplied with 5v when you ask them why it doesn't work. They'll just point you at the spec and ask you what 0.7x5v is.
Now reality is that most things run at 3.3v today, likely 1.8v or less sooner than later. So doubtful that they're still designing any 5v only stuff, and you'll find it all just works.

Then you're next problems come, dust off your EE transmission line theory and delve into problems caused by bus capacitance at a given frequency, realise that something that works at 100kHz may not at 3.4MHz. Section 7 of the spec gives hints and details of active pullups etc.

There's probably books to be written on the theory and tradeoffs to be made with I2C today. The original protocol is some 30+ years old and technology has moved on, but I2C is still tied to the limitations of the original concept in a lot of ways.

Anyway, hope that might give you some food for thought on why there's so much talk of level shifters for I2C.

benzeman
Posts: 108
Joined: Fri Apr 20, 2012 12:04 pm
Contact: Website

Re: RTC Software

Sun May 20, 2012 7:00 am

OK, well I'll puck up some 4K7 resistors from school on Monday, now does anyone have any idea how I'd go about writing the driver for it? ;)

selsinork
Posts: 151
Joined: Mon Apr 16, 2012 8:31 am

Re: RTC Software

Sun May 20, 2012 8:49 am

benzeman wrote:now does anyone have any idea how I'd go about writing the driver for it? ;)
There are in-kernel drivers for most of the common RTC chips, but you'll need to recompile. Look under drivers/rtc.

There's also a couple of i2c drivers been written:
gpio based https://github.com/FrankBuss/linux.git
bsc based https://github.com/bootc/linux/tree/rpi-i2cspi

along with some details of how to get all the pieces to talk to each other here
http://www.bootc.net/archives/2012/05/1 ... /#more-439

User avatar
TheCrazyInventor
Posts: 58
Joined: Sun Mar 04, 2012 11:39 am
Location: The Netherlands
Contact: Website

Re: RTC Software

Sun May 20, 2012 2:40 pm

Get yourself a DS1337. Fantastic little I2C RTC. The I2C communication works down to 1.8 volts (time keeping will work all the way to 1.2 volts), so the 3v3 from the pi is not a problem. I use it in a small pocket watch I made which runs off an ultracap. It runs off of it for many weeks as the whole clock only draws 600nA (yes, nano amps) in sleep mode. Talking to the thing is easy, this is all you need:

Code: Select all

void rtc_getAllRegisters(void) {
	unsigned char i;

	//connect to rtc
	i2c_WriteTo(0xD0);
	
	//start at 0th memory index
	i2c_PutByte(0);
	
	//send read command
	i2c_ReadFrom(0xD0);
	
	//read every register and store it
	for(i=0;i<14;i++) {
		ds1337Reg[i] = i2c_GetByte(I2C_MORE);
	}
	
	ds1337Reg[i + 1] = i2c_GetByte(I2C_LAST);
	
	//end communication
	i2c_Stop();
}

void rtc_setTime(unsigned char hours, unsigned char minutes, unsigned char seconds) {
	//turn the integers in the weird DS1337 format
	hours = ((hours / 10) << 4) + (hours - (hours / 10 * 10));
	minutes = ((minutes / 10) << 4) + (minutes - (minutes / 10 * 10));
	seconds = ((seconds / 10) << 4) + (seconds - (seconds / 10 * 10));

	i2c_WriteTo(0xD0);
	i2c_PutByte(0);
	i2c_PutByte(seconds);
	i2c_PutByte(minutes);
	i2c_PutByte(hours);

	i2c_Stop();
}

void rtc_setDate(unsigned char day, unsigned char month, unsigned char year) {
	//turn the integers in the weird DS1337 format
	day = ((day / 10) << 4) + (day - (day / 10 * 10));
	month = ((month / 10) << 4) + (month - (month / 10 * 10));
	year = ((year / 10) << 4) + (year - (year / 10 * 10));

	i2c_WriteTo(0xD0);
	i2c_PutByte(4);
	i2c_PutByte(day);
	i2c_PutByte(month);
	i2c_PutByte(year);

	i2c_Stop();
}
ds1337Reg[] now contains all register values from the RTC.
Now, I don't have a pi yet and I have no idea how to drive the I2C lines, but this is how I do it on a microcontroller. The DS1337 is really simple and easy to use, though. I plan on sticking one on my pi as well. :)
"Anything that can possibly go wrong, does" -M

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RTC Software

Sun May 20, 2012 5:32 pm

LOL, it's not "weird DS1337 format", it's Binary-Coded Decimal (BCD) http://en.wikipedia.org/wiki/Binary-coded_decimal

User avatar
TheCrazyInventor
Posts: 58
Joined: Sun Mar 04, 2012 11:39 am
Location: The Netherlands
Contact: Website

Re: RTC Software

Sun May 20, 2012 6:38 pm

Yeah, I know. :) But I still thought that was weird. Why not save the values as regular binary values? Now I have to do all kind of weird conversions to get the numbers I want.
"Anything that can possibly go wrong, does" -M

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: RTC Software

Sun May 20, 2012 7:03 pm

The wikipedia article explains why ;-)
It's probably a mixture of legacy, and the fact that i2c devices are also used by super-low-power devices which don't have "integers" that your high-level languages have.

You'd probably benefit from adding bcd2int() and int2bcd() macros to your code :)

benzeman
Posts: 108
Joined: Fri Apr 20, 2012 12:04 pm
Contact: Website

Re: RTC Software

Mon May 21, 2012 10:14 am

Well, I've got my 4K7's - I don't really want to buy another chip if I can avoid it.

Return to “General discussion”