Real Time Clock

From Teknologisk videncenter
Jump to: navigation, search
RTC chip with 32,278 KHz X-tal.

A RTC or Real Time Clock is a build in time clock in electronic equipment such as an PC or an embedded processor. The RTC often has a battery power backup and the time are retained using a precision X-tal to minimize drift of time.

DS1307 RTC chip

A popular stand alone RTC chip is the DS1307 used by example by the popular Arduino[1] embedded system.

In the picture to the right you can see a DS1307 chip driven by the 32,768 KHz X-tal. The reason for chosing 32768 Hz is that 215 = 32768. The DS1307 contains a counter that repeatedly counts to 215. Each time the counter reaches 32768 the time clock counts one second forward. Battery power backup is provided by the battery just visible in top right corner.

Beside the RTC the DS1307 contains 112 Bytes of RAM often used for configuration and password information. The time and contents of RAM are lost if the chip losses power. (Loss of battery and external power)

Motorola MC146818

The most used RTC design was the Motorola MC146818 chip introduced in the PC/AT in 1984[2]. Today the RTC in is an integrated part of the PC mainboard chipset, but still emulating the MC146818.

MC146818 memory map

The MC146818 content are read- and writeable by the CPU using the assembly IN and OUT instructions. The RTC is located at port number 0x70 and 0x71. Port 0x70 is used to transfer the address of the intended register using the OUT instruction. After transferring the register address to port 0x70, it is possible to read the requested register using the IN instruction or write to the register using the OUT instruction.

MC146818 memory map, showing clock time registers
 ; Assembly source code example to PC compatible MC146818 RTC
 ; Reading the day of the month to x86 AL register.
 MOV   AL,7    ; move 7 to the AL register 
                ;(Day of month address in MC1468818 is 7)
 OUTB  70h      ; Transfer content of AL register to port number 0x70
 INB   71h      ; Read content of port number 0x71.
                ;AL register now contains the day of the month.

Problem with MC146818

The MC146818 keeps track of time, date and year, but does not have a century register. This lead to big problems called the year 2000 or Y2K problem[3] when date shifted from 31st December 1999 to 1st January 2000. Some operating systems reported the date as 1st January 1900.

Century problem workaround

To resolve the problem with shift of century, it was decided to use a byte of RAM in the MC146818 for the century. Register address 0x32 holds the century byte. The century byte do not count automatically, but should be incremented manually by the operating system after century rollover.

Still a problem

In modern day RTC's there are still no automatic century register, but a Century Rollover bit[4] is set if the year register changes from 99 to 00. The operating system must increment the MC146818 register at 0x32 manually.

Modern day PC mainboards

In modern day mainboards the RTC is an integrated part of the chipset, but still emulating the MC146818. By example the popular desktop chipset from Intel P55 express chipset[5]

The battery backuped RAM typically contains the BIOS settings and optional passwords.

P55 express chipset

The P55 data sheets[6] RTC introduction states

P55 data sheet excerpt

The chipset contains a Motorola MC146818B-compatible real-time clock with 256 bytes of battery-backed RAM. The real-time clock performs two key functions: keeping track of the time of day and storing system data, even when the system is powered down. The RTC operates on a 32,768 KHz crystal and a 3 V battery.

The RTC also supports two lockable memory ranges. By setting bits in the configuration space, two 8-byte ranges can be locked to read and write accesses. This prevents unauthorized reading of passwords or other system security information.

The RTC also supports a date alarm that allows for scheduling a wake up event up to 30 days in advance, rather than just 24 hours in advance.