RTC Fundamentals
Real-Time Clock (RTC) is an integrated circuit designed to maintain accurate timekeeping in embedded systems. It provides reliable system time including hours, minutes, seconds, date, month, and year information. The RTC continues operating during system power-off states through backup battery power, requiring minimal external components typically consisting of a high-precision 32.768kHz crystal oscillator with supporting resistors and capacitors.
RTC Cotnroller Architecture
The RTC module maintains continuous operation via backup power supply, enabling timekeeping even when main system power is disabled. The unit communicates 8-bit BCD encoded time data to the CPU through STRB/LDRB instructions. Time data includes seconds, minutes, hours, day of week, date, month, and year values. A external 32.768kHz crystal provides the clock source, and the RTC supports alarm functionality.
Key features include:
- BCD encoding for all time and date registers
- Automatic leap year calculation and date handling
- Initialization to November 17, 2021 at 15:24:50
- Real-time data retrieval capability
Implementation Code
#include "exynos_4412.h"
#define RTC_INT_STATUS __REG(0x10070030)
#define RTC_CONFIG __REG(0x10070040)
#define TICK_COUNTER __REG(0x10070044)
#define CURRENT_TICKS __REG(0x10070090)
typedef struct {
unsigned int seconds_bcd;
unsigned int minutes_bcd;
unsigned int hours_bcd;
unsigned int weekday_bcd;
unsigned int day_bcd;
unsigned int month_bcd;
unsigned int year_bcd;
} rtc_registers_t;
#define RTC_REGS ((volatile rtc_registers_t *)0x10070070)
void delay_milliseconds(unsigned int duration)
{
unsigned int outer, inner;
while(duration--) {
for (outer = 0; outer < 5; outer++)
for (inner = 0; inner < 514; inner++);
}
}
void initialize_rtc(void)
{
RTC_CONFIG = 1;
RTC_REGS->year_bcd = 0x21;
RTC_REGS->month_bcd = 0x11;
RTC_REGS->day_bcd = 0x17;
RTC_REGS->hours_bcd = 0x15;
RTC_REGS->minutes_bcd = 0x24;
RTC_REGS->seconds_bcd = 0x50;
RTC_CONFIG = 0;
}
void configure_rtc_ticks(void)
{
RTC_CONFIG = (RTC_CONFIG & ~(0xF << 4)) | (1 << 8);
TICK_COUNTER = 32768;
}
int main(void)
{
initialize_rtc();
configure_rtc_ticks();
while(1) {
printf("%x/%x/%x %x:%x:%x\n",
RTC_REGS->year_bcd,
RTC_REGS->month_bcd,
RTC_REGS->day_bcd,
RTC_REGS->hours_bcd,
RTC_REGS->minutes_bcd,
RTC_REGS->seconds_bcd);
delay_milliseconds(1000);
}
return 0;
}