Hardware Abstraction Layer (HAL)
All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
RTC (Real-Time Clock)

General Description

High level interface for interacting with the real-time clock (RTC).

The real time clock provides tracking of the current time and date, as well as the ability to trigger a callback at a specific time in the future.

Features

Quick Start

Initialise the RTC using the PDL. Set the current time and date using mtb_hal_rtc_write.
See Snippet 1: Initialize RTC, write and read current time and date to initialize RTC, read and write current date and time to the RTC peripheral.

Code snippets

Snippet 1: Initialize RTC, write and read current time and date

The following code sets the current date and time using mtb_hal_rtc_write. The current date and time is read from the RTC using mtb_hal_rtc_read. The time structure tm , contains the calendar date and time which are broken down into its components. This structure is declared in standard C library time.h which is included by HAL.

// Structure tm stores years since 1900
#define TM_YEAR_BASE (1900u)
cy_rslt_t result;
mtb_hal_rtc_t my_rtc;
char buffer[80];
int mday = 3, month = 3, year = 2020; // Day of month, month and year
int hours = 8, minutes = 10, seconds = 10; // Hours, minutes and seconds
int wday = 3; // Days from Sunday. Sunday is 0, Monday is 1 and so on.
int dst = 0; // Daylight Savings. 0 - Disabled, 1 - Enabled
// Setting the tm structure as 08 HRS:10 MIN:10 SEC ; 3rd March 2020; Wednesday ; DST off
struct tm new_date_time =
{
.tm_sec = seconds,
.tm_min = minutes,
.tm_hour = hours,
.tm_mday = mday,
.tm_mon = month - 1,
.tm_year = year - TM_YEAR_BASE,
.tm_wday = wday,
.tm_isdst = dst
};
struct tm current_date_time = {0};
/* initialize the RTC using the PDL at first */
result = mtb_hal_rtc_setup(&my_rtc, &config);
// Update the current time and date to the RTC peripheral
result = mtb_hal_rtc_write(&my_rtc, &new_date_time);
// Get the current time and date from the RTC peripheral
result = mtb_hal_rtc_read(&my_rtc, &current_date_time);
if (CY_RSLT_SUCCESS == result)
{
// strftime() is a C library function which can be used to format date and time
// into string. It comes under the header file "time.h".
// (See http://www.cplusplus.com/reference/ctime/strftime/)
strftime(buffer, sizeof(buffer), "%c", &current_date_time);
// Print the buffer in serial terminal to view the current date and time
}
RTC object.
Definition: mtb_hal_hw_types_rtc.h:54
cy_rslt_t mtb_hal_rtc_read(mtb_hal_rtc_t *obj, struct tm *time)
Get the current time and date from the RTC peripheral.
Definition: mtb_hal_rtc.c:149
cy_rslt_t mtb_hal_rtc_write(mtb_hal_rtc_t *obj, const struct tm *time)
Write the specified time and date to the RTC peripheral.
Definition: mtb_hal_rtc.c:169
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:457
#define CY_RSLT_SUCCESS
cy_rslt_t return value indicating success
Definition: cy_result.h:484

API Reference

 RTC HAL Results
 RTC specific return codes.
 

Functions

cy_rslt_t mtb_hal_rtc_read (mtb_hal_rtc_t *obj, struct tm *time)
 Get the current time and date from the RTC peripheral. More...
 
cy_rslt_t mtb_hal_rtc_write (mtb_hal_rtc_t *obj, const struct tm *time)
 Write the specified time and date to the RTC peripheral. More...
 

Function Documentation

◆ mtb_hal_rtc_read()

cy_rslt_t mtb_hal_rtc_read ( mtb_hal_rtc_t obj,
struct tm *  time 
)

Get the current time and date from the RTC peripheral.

Parameters
[in]objRTC object
[out]timeThe current time (see: https://en.cppreference.com/w/cpp/chrono/c/tm)
Returns
The status of the read request

◆ mtb_hal_rtc_write()

cy_rslt_t mtb_hal_rtc_write ( mtb_hal_rtc_t obj,
const struct tm *  time 
)

Write the specified time and date to the RTC peripheral.

Parameters
[in]objRTC object
[in]timeThe time to be set (see: https://en.cppreference.com/w/cpp/chrono/c/tm). Specify the local time to set to the RTC.
Returns
The status of the write request