2. RTC Operation Guide

2.1. Module Introduction

RTC (real time clock) is a hardware clock that provides and records time for the system. If the RTC is powered by battery, the RTC will continue to count and maintain the time information when the processor is powered down or hibernated.

The Linux kernel uses the RTC as a time and date maintainer. When the Linux system is booted, the kernel reads the RTC time to initialize the system (software) clock for time synchronization. The kernel can also write the time and date back to the RTC when needed.

2.2. Counting Clock Frequency

The RTC’s count clock uses a 32.768KHz clock and operates based on a 32-bit additive counter that provides a second count. The maximum counting time is:

2 ^ 32 seconds = 49710 days = 136 years

2.3. Operation Preperation

The operation preparation of RTC is as follows:

  • Use the kernel released by the SDK

  • Insert module: insmod cv180x_rtc.ko/ cv181x_rtc.ko

2.4. Usage

2.4.1. Ioct Control RTC

The application layer can access RTC through ioctl, and the device node is /dev/rtc0.

The usage is as follow:

int ioctl(int fd, ind cmd);

Function description of ioctl commands:

Command

Description

RTC_ALM_READand

Read alarm time

RTC_ALM_SET

Set alarm time

RTC_RD_TIME

Read time and date

RTC_SET_TIME

Set time and date

RTC_PIE_ON

Turn on RTC global interrupt

RTC_PIE_OFF

Turn off RTC global interrupt

RTC_AIE_ON

Enable RTC alarm interrupt

RTC_AIE_OFF

Disable RTC alarm interrupt

RTC_UIE_ON

Enable RTC update interrupt

RTC_UIE_OFF

Disable RTC update interrupt

RTC_IRQP_SET

Set interrupt frequency

2.4.2. Example of ioctl Usage:

static const char default_rtc[] = "/dev/rtc0";
struct rtc_time rtc_tm;
int fd;

fd = open(rtc, O_RDONLY);
if (fd ==  -1) {
   perror(rtc);
   exit(errno);
}

The RTC time can be obtained by the following commands:

/* Read the RTC time/date */
retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
if (retval == -1) {
   perror("RTC_RD_TIME ioctl");
   exit(errno);
}
fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
      rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
      rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);

The RTC alarm time can be set by the following commands:

retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
if (retval == -1) {
   perror("RTC_RD_TIME ioctl");
   exit(errno);
}

2.4.3. Structure

  • rtc_time

struct rtc_time {
   int tm_sec;
   int tm_min;
   int tm_hour;
   int tm_mday;
   int tm_mon;
   int tm_year;
   int tm_wday;
   int tm_yday;
   int tm_isdst;
};

tm_mday:The date of the month, the value range is [1,31]

tm_wday:The day of the week, Sunday is 0, Monday is 1, and so on

tm_yday:The day of the year, the value range is [0,365], where 0 represents January 1st, 1 represents January 2nd, and so on

tm_isdst:Determine whether it is daylight saving time, 1 is daylight saving time; 0 is not daylight saving time