8

As I understand RTC is needed to keep track of time. However, Raspberry Pi does not have that and use NTP servers to synchronize its time.

All that is fine, but how does Pi know that one millisecond or one second has elapsed. How is Pi aware of the passing of time?

  • RTC is really there (with a battery) for when the Pi (or any processor) is powered off. – Octopus May 15 '13 at 4:59
  • The Pi has an internal clock that gets set by NTP whenever it is on the Internet, as you said. It is software that keeps track of it and the subsequent passing of time, not an absolute RTC. The standard Linux commands can retrieve that information whenever you need. So, to answer your question, all it "knows" is the time when it was set most recently then how many ticks since then. Here is an interesting article on the subject. : blog.remibergsma.com/2013/05/12/… – SDsolar Oct 29 '16 at 18:14
16

RTC operation

What RaspberryPi lack is a battery backed RTC (Real Time Clock). It's device that can work independently from main computer and keep track of a passing time even when it's off (this is why it has its own battery). It may work in very simple way - it has a known frequency oscillator connected and on every pulse of the oscillator, it will increment its internal timer/counter. Since the frequency is know, it's easy to calculate the time passed based on the counter. The device will also have a battery so that it can continue working even when external power is off.

Some RTC devices may be more sophisticated. They can, for example, create an interrupt in programmable frequency or in alarm mode (set to a certain time in the future or to certain time each day etc). It is not relevant to keeping system time and is optional.

How RTC is used

A computer can ask RTC what time it is (or more precisely - what is its counter value) at boot time and set its internal date/time based on this value. From now on, however, a computer won't ask RTC for time every second/milisecond/microsecond. Instead, it will run its own clock (called system time), usually using its own timers/counters. Just like in RTC, timer/counter is clocked with a known frequency so it's easy to calculate the time passed.

You may force your system to synchronize its clock with RTC device (in both directions) but it won't happen until requested. Some systems are configured to store system time in RTC when shutting down or after NTP synchronization, for example.

Lack of RTC

RaspberryPi, like many cheap single board computers, does not have external RTC device. This means that it can't ask for current time on boot. But as already mentioned - this is not a problem if we can get date/time from other source (like NTP). The only disadvantage is that unlike RTC, you can't ask NTP on early boot (since you first need network connection).

So to directly answer your question - no matter if RaspberryPi (or any other computer) has RTC or not, it will keep track of time using internal timer/counter device which is available in almost every computer system and is required to run almost any kind of operating system.

Time in Linux

Userspace applications in Linux can determinate so called calendar time (which is a time in real world) using gettimeofday() function which then calls clock_gettime() system call. What happens now is a little bit complicated due to many advanced features that Linux kernel has (like namespaces etc). But the basics is that kernel maintains its time based on jiffies. Its actually just a variable inside of the kernel (usually 64bit wide) that counting number of ticks since the system started.

Ticks are generated by hardware timer by means of interrupts. jiffies value is incremented on each such interrupt. Hardware timer is configured to produce such interrupts at regular intervals. The interval is configured at boot time according to a value of HZ kernel config parameter.

At any given time, kernel knows how many ticks where generated from boot (jiffies variable), it knows how many such ticks are generated each second (HZ config option) so it can easily calculate how much time passed from the last boot.

So to sum up - kernel asks RTC about calendar time (also called wall clock) on boot so it knows what was the actual time when system started. It then adds jiffies/HZ value to this each time a userspace application asks what time it is using clock_gettime system call.

Hardware timer/counter device

Hardware timer/counter is a very simple device. It has a counter register that counts its clock ticks. Clock ticks are usually created by some external oscillator (an electronic circuit that produces repetitive signal) and are of known frequency (usually ranging from couple of kHz to hundreds MHz). This means that we can easily calculate how much time passed by dividing the counter value by the frequency of oscillator.

Timer device can be programmed to do various things - it can count upwards and downwards, compare counter register to some value. For example it can create an external signal and start counting from beginning when counter register is at some value. This way you can configure timer device to create such external signal at constant interval that is much lower frequency than oscillator. This signal can be then used as an interrupt event for a CPU.

Note that oscillator could be used directly instead of timer device. What differentiates timer/counter device from oscillator is that it can be programmed. So you can think of timer/counter device as a much more sophisticated oscillator.

  • It would have been more helpful if more details would have been provided about the internal timer. – AppleGrew May 15 '13 at 8:01
  • @AppleGrew: What details are you interested in? – Krzysztof Adamski May 15 '13 at 8:03
  • Can you name the timers you mentioned? So that I can read more about them. When I google "internal CPU timer", do not seem to get anything relevant. I am not sure what exactly I hope to learn from that, but I feel like I still lack some clarity. – AppleGrew May 16 '13 at 8:49
  • @AppleGrew: I've added some more background information to my question. Hope this helps. – Krzysztof Adamski May 16 '13 at 9:56
  • BTW you might want to fix the typo - 'suck' in 3rd para of 'Time in Linux' section. – AppleGrew May 17 '13 at 8:32
1

The CPU clock is derived from a crystal. Typically you can expect 10-20 parts per million for the clock accuracy (about 5-10 minutes per year)

0

Anyway to answer the question in a much easier fashion along with instructions to add a RTC to your PI, I will insert a link below. Depending on whether you just want a reliable method besides using the inet/ntp way to tell time or need a very precise method I would recommend going with the "Chronodot" RTC. Otherwise the breakout board RTC would work fine and is very easy to get working all you need to setup is I2C to have the PI communicate with the RTC. Link Info/materials

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.