Infineon AN220190 User Manual

Please read the Important Notice and Warnings at the end of this document 002-20190 Rev. *D
www.infineon.com page 1 of 47 2020-12-15
AN220190

About this document

Scope and purpose
This application note describes how to use the real-time clock (RTC) in Traveo II family MCUs.
Intended audience
This document is intended for anyone who uses the RTC of the Traveo II family.

Table of contents

About this document ....................................................................................................................... 1
Table of contents ............................................................................................................................ 1
1 Introduction .......................................................................................................................... 2
1.1 Features of RTC ....................................................................................................................................... 2
2 Operation Overview ............................................................................................................... 3
2.1 Basic RTC Settings ................................................................................................................................... 4
2.1.1 Use Case ............................................................................................................................................. 4
2.1.2 Initializing the RTC Function .............................................................................................................. 4
2.1.3 Configuring the RTC Function ............................................................................................................ 6
2.1.4 Example Program to Configure RTC Function in Driver Part .......................................................... 10
2.2 Reading TIME and DATE ........................................................................................................................ 16
2.2.1 Use Case ........................................................................................................................................... 16
2.2.2 Reading the RTC Values ................................................................................................................... 17
2.2.3 Items Read for RTC Value ................................................................................................................. 18
2.3 Updating TIME and DATE ...................................................................................................................... 20
2.3.1 Use Case ........................................................................................................................................... 20
2.3.2 Updating the RTC Values (For Example, Adjusting DST) ................................................................ 21
2.3.3 Example Program to Update RTC Values in Driver Part.................................................................. 23
2.4 Calibrating WCO .................................................................................................................................... 24
2.4.1 Use Case ........................................................................................................................................... 24
2.4.2 Capturing the WCO Waveform ......................................................................................................... 24
2.4.3 Example Program to Capture WCO Waveform in Driver Part ......................................................... 29
2.5 Measuring the Difference Between the WCO and an Ideal Waveform................................................. 30
2.5.1 Example Program to Measuring the WCO Waveform in Driver Part ............................................... 34
2.5.2 Confirming the Accuracy of the WCO Frequency after Calibration ................................................ 36
2.5.3 Example Program to Confirm Accuracy of WCO Frequency after Calibration in Driver Part ........ 40
Glossary ....................................................................................................................................... 42
Related Documents ........................................................................................................................ 44
Revision history............................................................................................................................. 46
2 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Introduction

1 Introduction

The RTC in Traveo II family has four features; RTC, Alarm, Calibration, and Backup registers.
The RTC feature keeps track of the current time, year, month, date, day-of-week, hour, minute, and second accurately. The alarm feature can generate interrupts to the CPU with programable setting. The calibration feature can correct a frequency error of the WCO and Low-power external crystal oscillator (LPECO). The backup registers can keep user data in any power mode.
This application note:
Explains how to update the time of the RTC registers
Describes how to read the time from the RTC registers
Explains the functions of RTC in series
Shows how to set up the RTC function and calibrate the WCO
To understand the functionality described and terminology used in this application note, see the Real-Time Clock chapter of the Architecture Technical Reference Manual (TRM).

1.1 Features of RTC

The following are the features of RTC:
Fully-featured RTC function
Year/Month/Date, Day-of-Week, Hour: Minute: Second fields (All fields are integer values)
Supports both 12-hour and 24-hour formats
Automatic leap-year correction until 2400
Configurable alarm function
Alarm on Month/Date, Day-of-Week, Hour: Minute: Second fields
Two independent alarms
Calibration for 32.768-kHz WCO and 4-MHz to 8-MHz LPECO
Calibration waveform output
Supports 512 Hz, 1 Hz, and 2 Hz
Backup registers
Note: See the device-specific datasheet to confirm whether LPECO is present.
3 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview

2 Operation Overview

The RTC function keeps track of the current time accurately up to seconds and therefore, can be used as an accurate time source in automotive applications. This application note describes clock screens in vehicles as a sample use case. The time on the clock screen is updated accurately and periodically.
Figure 1 shows the RTC function block diagram and an example of the user system.
Clock Screen In a Vehicle
WCO
Traveo II
Traveo II Device
CPU
AM
RTC Function
RTC Field
Alarm Field
Config Field
RAM
User Buffer
WCO Logic
PORT
Interrupt
/ /
Tues
RTC_CAL Pin
AHB-Lite Bus
Use Register
RTC Field
Alarm Field
Config Field
RTC Register
V
DDD
Calibration Waveform
Figure 1 Example Structure of the User System using RTC Function
The RTC function block consists of user registers, RTC registers, and WCO and LPECO. The RTC function interfaces with the CPU and other sub-systems via the AHB-Lite bus interface. The WCO or LPECO can generate the required clock with the help of an external crystal or external clock inputs. The RTC function has a programmable alarm function, which can generate interrupts to the CPU.
For updating and displaying the current time on the clock screen of vehicles, the RTC function is generally used with periodic interrupts. The CPU and the RTC function are connected via an AHB-Lite interface, which provides firmware access interface.
The CPU reads the current time from the RTC function, stores the real-time data in the user buffers, which are local variables assigned in RAM, and outputs the real-time data to the external clock screen. Moreover, the RTC function can also output a calibration waveform.
User registers and the RTC registers have RTC fields, Alarm fields, and Config fields. Software can access the user registers. A specific writing operation updates the RTC registers with the user registers. A specific read operation copies data from the RTC registers to the user registers. See the Architecture TRM and Register TRM for the details on RTC fields, Alarm fields, and Config fields.
The WCO device and logic, the LPECO and logic, and the RTC function run on VDDD, which is a continuous power supply. Therefore, the RTC function runs in all power modes, and the function continuously keeps track of the current time.
There are four operations that keep track of the current time:
1. Initializing RTC function including interrupts
2. Reading time and date
3. Updating time and date
4. Calibrating WCO
4 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview
Before using the RTC function, an initialization routine needs to be executed. The initializing operation includes the alarm interrupt setting. The interrupt is caused by the alarm function which has fields corresponding to the RTC field registers. Basic RTC Settings provides the example setting for generating interrupts every 30 seconds with two alarm functions.
To display the current time on the clock screen of a vehicle, the CPU reads a current time from the RTC field registers. Reading TIME and DATE provides an example to read the current time from the RTC field.
There are instances where you might have to update the RTC fields registers, for example for Daylight Saving Time (DST). For DST, the Hour field needs to be updated. Updating TIME and DATE provides an example to the update the current time of the RTC field.
You can use ILO, CLK_LF, WCO, or LPECO as a source clock for the RTC function. However, WCO or LPECO is recommended as it is more accurate. Calibrating WCO provides an example to calibrate the time of the RTC function.

2.1 Basic RTC Settings

This section describes the operation of the RTC function with the following assumptions.
This section also explains how to configure the RTC based on a use case using the Sample Driver Library (SDL) provided by Cypress. The code snippets in this application note are part of SDL. See Other References for the SDL.
SDL basically has a configuration part and a driver part. The configuration part mainly configures the parameter values for the desired operation. The driver part configures each register based on the parameter values in the configuration part.

2.1.1 Use Case

This section explains an example of the RTC function using the following use case. This use case shows how to initialize items such as input clock, time, and date to enable the RTC function. Also, this use case enables the ALARM function. When the ALARM setting time and RTC time match, an interrupt is generated.
Use case:
Source clock: Internal Low-speed oscillator (ILO)
Setting year: 2019
Setting month: August
Setting date: 21
Setting day of week: Monday
Setting time: 12:00:00
Setting HR mode: 24HR
ALARM1: Every 0 seconds
IRQ number for ALARM1: 3

2.1.2 Initializing the RTC Function

The following example initializes the RTC function after the power on reset. Once the RTC function is initialized, there is no need to reinitialize the RTC function even if the device has switched power modes.
Figure 2 shows an example flow to configure the basic RTC settings.
5 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview
Start
Set Time and Date Parameters
in Temporary Structure
(1)
(2)
(3)
(4)
(5)
(6)
(7)
Set WRITE bit for Writing
ALARM1 Time and Date
Set WRITE Bit for Writing Time
and Date
Set Time and Date in the
BACKUP register
Set ALM1_TIME and
ALM1_DATE
Clear WRITE Bit
Set ALARM1 Time and Date
Parameters in Temporary
Structure
End
(8)
Clear WRITE Bit
(9)
Select Source Clock as ILO
Set Interrupt Mask
(10)
Set Interrupt
Clear Interrupt
(11)
(12)
Start
End
Clear Interrupt Flag for ALARM1
Interrupt routineMain routine
(13)
Figure 2 Example Flow to Configure Basic RTC Settings and Interrupt Routine
6 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview
The following procedure shows the basic setup procedure for the RTC function:
1. Set interrupt.
2. Select the source clock as ILO:
Write BACKUP_CTL.CLK_SEL = CY_RTC_CLK_SRC_ILO_0 (ILO)
3. Set the time and date parameters in temporary structure:
TIME parameter (Second=0, minute=0, hour=12, HR mode=0(24HR), day of week=1(Monday)) DATE parameter (date=21, month=8, year=19 (2019))
4. Set the WRITE bit for writing the time and date:
Write BACKUP_RTC_RW.WRITE = ‘1’
5. Set the time and date for the BACKUP_RTC_TIME and BACKUP_RTC_DATE registers:
Write BACKUP_RTC_TIME = time parameter Write BACKUP_RTC_DATE = date parameter
6. Clear the WRITE bit to update the date and time in the RTC registers:
Write BACKUP_RTC_RW.WRITE = ‘0’
7. Clear the interrupt.
8. Set the interrupt mask.
9. Set the ALARM1 time and date parameters in temporary structure.
10. Set the WRITE bit for writing the ALARM1 time and the date.
11. Set the ALARM1 time and date for the BACKUP_ALM1_TIME and BACKUP_ALM1_DATE registers:
When the RTC fields and the alarm fields match, an interrupt occurs. Write BACKUP_ALM1_TIME = ALARM1 time parameter (0 second) Write BACKUP_ALM1_DATE = ALARM1 date parameter (No ALARM in Date parameter)
12. Clear the WRITE bit to update the date and time in the RTC registers:
Write BACKUP_RTC_RW.WRITE = ‘0’
13. When the ALARM1 interrupt occurs, clear the interrupt flag.
The alarm function generates an interrupt when the RTC fields and the alarm fields match.
If you want to set the alarm every 30 seconds, you need to set two alarms.
For details on the interrupt setting procedure, see the Interrupt and Fault Report Structure section in AN219842 [5].

2.1.3 Configuring the RTC Function

Table 1 lists the parameters and Table 2 lists the functions of the configuration part in SDL for RTC and ALARM
settings.
7 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview
Table 1 List of RTC and ALARM Setting Parameters
Parameters
Description
Value
RTC_config.sec
Calendar seconds, 0-59
RTC_INITIAL_DATE_SEC = 0ul
RTC_config.min
Calendar minutes, 0-59
RTC_INITIAL_DATE_MIN = 0ul
RTC_config.hour
Calendar hours, value depending on 12/24HR mode
RTC_INITIAL_DATE_HOUR = 12ul
RTC_config.hrMode
Select 12/24HR mode: 1=12HR, 0=24HR CY_RTC_12_HOURS = 1ul, CY_RTC_24_HOURS = 0ul
RTC_INITIAL_DATE_HOUR_FORMAT = CY_RTC_24_HOURS
RTC_config.dayOfWeek
Calendar Day of the week, 1-7 I You can define the values, but it is
recommended to set 1=Monday.
RTC_INITIAL_DATE_DOW = 1ul
RTC_config.date
Calendar Day of the Month, 1-31 Automatic Leap Year Correction
RTC_INITIAL_DATE_DOM = 21ul
RTC_config.month
Calendar Month, 1-12
RTC_INITIAL_DATE_MONTH = 8ul
RTC_config.year
Calendar year, 0-99
RTC_INITIAL_DATE_YEAR = 19ul
alarm.sec
Alarm seconds, 0-59
0ul
alarm.sec_en
Alarm second enable: 0=ignore, 1=match
CY_RTC_ALARM_ENABLE = 1ul
alarm.min
Alarm minutes, 0-59
0ul
alarm.min_en
Alarm minutes enable: 0=ignore, 1=match
CY_RTC_ALARM_DISABLE = 0ul
alarm.hour
Alarm hours, value depending on 12/24HR mode
0ul
alarm.hour_en
Alarm hour enable: 0=ignore, 1=match
CY_RTC_ALARM_DISABLE = 0ul
alarm.dayOfWeek
Calendar Day of the week, 1-7 You can define the values, but it is
recommended to set 1=Monday
1ul
alarm.dayOfWeek_en
Alarm Day of the Week enable: 0=ignore, 1=match
CY_RTC_ALARM_DISABLE = 0ul
alarm.date
Calendar Day of the Month, 1-31 Automatic Leap Year Correction
1ul
alarm.date_en
Alarm Day of the Month enable: 0=ignore, 1=match
CY_RTC_ALARM_DISABLE = 0ul
alarm.month
Alarm Month, 1-12
1ul
alarm.month_en
Alarm Month enable: 0=ignore, 1=match
CY_RTC_ALARM_DISABLE = 0ul
alarm.alm_en
Master enable for alarm 1. 0: Alarm 1 is disabled. Fields for date and
time are ignored. 1: Alarm 1 is enabled. If none of the date
and time fields are enabled, this alarm triggers once every second.
CY_RTC_ALARM_ENABLE = 0ul
8 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview
Table 2 List of RTC and ALARM Setting Functions
Functions
Description
Value
Cy_Rtc_clock_source (clock_source)
Set the RTC input clock source Clock source: Input clock
source
CY_RTC_CLK_SRC_ILO_0
Cy_Rtc_Init (*config)
Initialize the RTC driver and return the RTC register address
config: RTC configuration structure address
&RTC_config
Cy_Rtc_SetDateAndTime (*dateTime)
Set the time and date values to the RTC_TIME and RTC_DATE registers.
dateTime: RTC configuration structure address
config
Cy_Rtc_ConstructTimeDate (*timeDate, time, date)
Returns Integer time and Integer date in the format used in APIs from individual elements passed.
timeDate: Structure address of time and date
time: Time configuration structure address for the RTC_TIME register set
date: Date configuration structure address for the RTC_TIME register set
dateTime, &tmpTime, &tmpDate
Cy_Rtc_ClearInterrupt (interruptMask)
Clear the RTC interrupt interruptMask: The bit mask of
interrupt to clear
CY_RTC_INTR_ALARM1=0x1ul
Cy_Rtc_SetInterruptMask (interruptMask)
Set the RTC interrupt interruptMask: The bit mask of
interrupt to set
CY_RTC_INTR_ALARM1=0x1ul
Cy_Rtc_SetAlarmDateAndTime (alarmDateTime, alarmIndex)
Sets alarm time and date values into the ALMx_TIME and ALMx_DATE registers
alarmDateTime: The alarm configuration structure
alarmIndex: The alarm index to be configured
&alarm, CY_RTC_ALARM_1=0ul
Cy_Rtc_ConstructAlarmTimeDate (*alarmDateTime, *alarmTime,
*alarmDate)
Returns the Integer time and Integer date in the format used in APIs from individual elements passed for alarm
alarmDateTime: Structure address of alarm time and date
alarmDateTime, &tmpAlarmTime, &tmpAlarmDate
9 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview Functions
Description
Value
alarmTime: Alarm time configuration structure address for the ALMx_TIME register set
alarmDate: Alarm date configuration structure address for the ALMx_DATE register set
_VAL2FLD (field, value)
Mask and shift a bit field value for use in a register bit range.
Field: Name of the register bit field.
Value: Value of the bit field. This parameter is interpreted as an uint32_t type.
-
Code Listing 1 shows an example program of configuration part for the RTC function.
The following description will help you understand the register notation of the driver part of SDL:
BACKUP->unRTC_TIME register is the BACKUP_RTC_TIME register mentioned in the Register TRM. Other
registers are also described in the same manner.
See cyip_backup_v3.h under hdr/rev_x/ip for more information on the union and structure representation of registers.
Code Listing 1 Example Program to Configure RTC
cy_stc_rtc_config_t const RTC_config = { /* Initiate time and date */ .sec = RTC_INITIAL_DATE_SEC, .min = RTC_INITIAL_DATE_MIN, .hour = RTC_INITIAL_DATE_HOUR, .hrMode = RTC_INITIAL_DATE_HOUR_FORMAT, .dayOfWeek = RTC_INITIAL_DATE_DOW, .date = RTC_INITIAL_DATE_DOM, .month = RTC_INITIAL_DATE_MONTH, .year = RTC_INITIAL_DATE_YEAR, }; cy_stc_rtc_alarm_t const alarm = { .sec = 0ul, .sec_en = CY_RTC_ALARM_ENABLE, .min = 0ul, .min_en = CY_RTC_ALARM_DISABLE, .hour = 0ul, .hour_en = CY_RTC_ALARM_DISABLE, .dayOfWeek = 1ul,
.dayOfWeek_en = CY_RTC_ALARM_DISABLE,
.date = 1ul, .date_en = CY_RTC_ALARM_DISABLE, .month = 1ul, .month_en = CY_RTC_ALARM_DISABLE, .alm_en = CY_RTC_ALARM_ENABLE };
int main(void) { SystemInit();
__enable_irq(); /* Enable global interrupts. */
Cy_GPIO_Pin_Init(USER_LED_PORT, USER_LED_PIN, &user_led_port_pin_cfg);
Configure initial time and date parameters. See Table 1.
Configure ALARM1 time and date parameters. See Table 1 .
10 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview Code Listing 1 Example Program to Configure RTC
cy_stc_sysint_irq_t irq_cfg = (cy_stc_sysint_irq_t) { .sysIntSrc = srss_interrupt_backup_IRQn, .intIdx = CPUIntIdx0_IRQn, .isEnabled = true, }; Cy_SysInt_InitIRQ(&irq_cfg); Cy_SysInt_SetSystemIrqVector(irq_cfg.sysIntSrc, RTC_Handler); NVIC_SetPriority(CPUIntIdx0_IRQn, 3ul); NVIC_ClearPendingIRQ(CPUIntIdx0_IRQn); NVIC_EnableIRQ(CPUIntIdx0_IRQn);
/* Set the ILO_0 as the clock source to the RTC block */
Cy_Rtc_clock_source(CY_RTC_CLK_SRC_ILO_0);
/* Wait for alarm to be set */ while(Cy_Rtc_Init(&RTC_config) != CY_RET_SUCCESS);
/* Clear any pending interrupts */
Cy_Rtc_ClearInterrupt(CY_RTC_INTR_ALARM1);
/*Configures the source (Alarm1) that trigger the interrupts */ Cy_Rtc_SetInterruptMask(CY_RTC_INTR_ALARM1);
/* Wait for alarm to be set */
while(Cy_Rtc_SetAlarmDateAndTime(&alarm,CY_RTC_ALARM_1) != CY_RET_SUCCESS);
for(;;) { Cy_Rtc_GetDateAndTime(&Read_DateTime); } }
Code Listing 2 shows an example program of the RTC interrupt routine for alarm1.
Code Listing 2 Example Program of RTC Interrupt Routine for Alarm1
void Cy_Rtc_Alarm1Interrupt(void) { /* Clear any pending interrupts */ Cy_Rtc_ClearInterrupt(CY_RTC_INTR_ALARM1); }

2.1.4 Example Program to Configure RTC Function in Driver Part

Code Listing 3 to Code Listing 11 show an example program to configure the RTC in the driver part.
Code Listing 3 Example Program to Configure RTC Input Clock Source Set in Driver Part
void Cy_Rtc_clock_source(cy_en_rtc_clock_src_t clock_source) { BACKUP->unCTL.stcField.u2CLK_SEL = clock_source; }
(1) Set interrupt.
Select the source clock as ILO. See Code Listing 3.
Set the RTC initial time, date parameter. See Code Listing 4.
Clear interrupt. See Code Listing 8.
Set the interrupt mask. See Code Listing 9.
Set the ALARM1 time and date parameters. See Code Listing 10.
Interrupt routine for alarm1 function
(13) Clear the Interrupt flag for alarm1.
See Code Listing 8.
(2) Select the source clock as ILO
11 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview Code Listing 4 Example Program to Initialize RTC in Driver Part
cy_en_rtc_status_t Cy_Rtc_Init(cy_stc_rtc_config_t const *config) { cy_en_rtc_status_t retVal;
if(NULL != config) { retVal = Cy_Rtc_SetDateAndTime(config); } else { retVal = CY_RTC_INVALID_STATE; } return(retVal); }
Code Listing 5 Example Program to Set Date and Time Registers in Driver Part
cy_en_rtc_status_t Cy_Rtc_SetDateAndTime(cy_stc_rtc_config_t const *dateTime) { uint32_t tmpTime; uint32_t tmpDate; uint32_t tmpDaysInMonth; uint32_t interruptState;
cy_en_rtc_status_t retVal = CY_RTC_BAD_PARAM;
/* Check the input parameters valid ranges */ if((dateTime->month > 0u) && (dateTime->month <= CY_RTC_MONTHS_PER_YEAR) && (dateTime->year <= CY_RTC_MAX_YEAR)) { tmpDaysInMonth = Cy_Rtc_DaysInMonth(dateTime->month, (dateTime->year + CY_RTC_TWO_THOUSAND_YEARS));
/* Check if the date is in the valid range */ if((dateTime->date > 0u) && (dateTime->date <= tmpDaysInMonth)) { Cy_Rtc_ConstructTimeDate(dateTime, &tmpTime, &tmpDate);
/* The RTC AHB register can be updated only under condition that the
* Write bit is set and the RTC busy bit is cleared (CY_RTC_BUSY = 0). */
interruptState = Cy_SysLib_EnterCriticalSection(); retVal = Cy_Rtc_WriteEnable(CY_RTC_WRITE_ENABLED); if(retVal == CY_RTC_SUCCESS) { BACKUP->unRTC_TIME.u32Register = tmpTime; BACKUP->unRTC_DATE.u32Register = tmpDate;
/* Clear the RTC Write bit to finish RTC register update */ retVal = Cy_Rtc_WriteEnable(CY_RTC_WRITE_DISABLED); } Cy_SysLib_ExitCriticalSection(interruptState); } } return(retVal); }
This function sets the time, date parameters, WRITE bit. See Code Listing 5.
Set the initial time and date parameter in temporary structure. See Code Listing 6.
Set the WRITE bit for writing the time and date. See Code Listing 7.
(5) Set for BACKUP_RTC_TIME and BACKUP_RTC_DATE registers
Clear the WRITE bit. See Code Listing 7.
Check if configuration parameter values are valid
12 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview Code Listing 6 Example Program to Construct Time Date in Driver Part
static void Cy_Rtc_ConstructTimeDate(cy_stc_rtc_config_t const *timeDate, uint32_t *time, uint32_t *date) { uint32_t tmpTime; uint32_t tmpDate;
/* Prepare the RTC TIME value based on the structure obtained */ tmpTime = (_VAL2FLD(BACKUP_RTC_TIME_RTC_SEC, (timeDate->sec))); tmpTime |= (_VAL2FLD(BACKUP_RTC_TIME_RTC_MIN, (timeDate->min)));
/* Read the current hour mode to know how many hour bits to convert.
* In the 24-hour mode, the hour value is presented in [21:16] bits in the * Integer format. * In the 12-hour mode, the hour value is presented in [20:16] bits in the * Integer format and * bit [21] is present: 0 - AM; 1 - PM. */ if(timeDate->hrMode != CY_RTC_24_HOURS)
{ if(CY_RTC_AM != timeDate->amPm) { /* Set the PM bit */ tmpTime |= CY_RTC_BACKUP_RTC_TIME_RTC_PM; } else { /* Set the AM bit */ tmpTime &= ((uint32_t) ~CY_RTC_BACKUP_RTC_TIME_RTC_PM); } tmpTime |= BACKUP_RTC_TIME_CTRL_12HR_Msk; tmpTime |= (_VAL2FLD(BACKUP_RTC_TIME_RTC_HOUR, ((timeDate->hour) & ((uint32_t) ~CY_RTC_12HRS_PM_BIT)))); } else { tmpTime &= ((uint32_t) ~BACKUP_RTC_TIME_CTRL_12HR_Msk); tmpTime |= (_VAL2FLD(BACKUP_RTC_TIME_RTC_HOUR, (timeDate->hour))); } tmpTime |= (_VAL2FLD(BACKUP_RTC_TIME_RTC_DAY, (timeDate->dayOfWeek)));
/* Prepare the RTC Date value based on the structure obtained */
tmpDate = (_VAL2FLD(BACKUP_RTC_DATE_RTC_DATE,(timeDate->date)));
tmpDate |= (_VAL2FLD(BACKUP_RTC_DATE_RTC_MON, (timeDate->month))); tmpDate |= (_VAL2FLD(BACKUP_RTC_DATE_RTC_YEAR, (timeDate->year)));
/* Update the parameter values with prepared values */
*time = tmpTime;
*date = tmpDate; }
Set to Date parameters
(3) Set the initial time and date parameters in
temporary structure.
Set to Time parameters
13 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview Code Listing 7 Example Program to Write Enable in Driver Part
cy_en_rtc_status_t Cy_Rtc_WriteEnable(uint32_t writeEnable) { cy_en_rtc_status_t retVal = CY_RTC_INVALID_STATE;
if(writeEnable == CY_RTC_WRITE_ENABLED) { /* RTC Write bit set is possible only in condition that CY_RTC_BUSY bit = 0
* or RTC Read bit is not set */
if((CY_RTC_BUSY != Cy_Rtc_GetSyncStatus()) && (!_FLD2BOOL(BACKUP_RTC_RW_READ, BACKUP-> unRTC_RW.u32Register))) { BACKUP->unRTC_RW.u32Register |= BACKUP_RTC_RW_WRITE_Msk; retVal = CY_RTC_SUCCESS; } } else { /* Clearing Write Bit to complete write procedure */
BACKUP->unRTC_RW.u32Register &= ((uint32_t) ~BACKUP_RTC_RW_WRITE_Msk);
/* Delay to guarantee data write after clearing write bit */
Cy_SysLib_DelayUs(CY_RTC_DELAY_WRITE_US);
retVal = CY_RTC_SUCCESS; }
return(retVal); }
Code Listing 8 Example Program to Clear Interrupt in Driver Part
void Cy_Rtc_ClearInterrupt(uint32_t interruptMask) { BACKUP->unINTR.u32Register = interruptMask;
(void) BACKUP->unINTR.u32Register; }
Code Listing 9 Example Program to Set Interrupt mask in Driver Part
void Cy_Rtc_SetInterruptMask(uint32_t interruptMask) { BACKUP->unINTR_MASK.u32Register = interruptMask; }
(12) Clear the WRITE bit.
(4) Set the WRITE bit for writing the time and date.
(6) Clear the WRITE bit..
(10) Set the WRITE bit for writing
ALARM1 time and date.
(7) Clear interrupt
(8) Set interrupt mask
14 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview Code Listing 10 Example Program to Set Alarm Time and Date in Driver Part
cy_en_rtc_status_t Cy_Rtc_SetAlarmDateAndTime(cy_stc_rtc_alarm_t const *alarmDateTime, cy_en_rtc_alarm_t alarmIndex) { uint32_t tmpAlarmTime; uint32_t tmpAlarmDate; uint32_t tmpYear; uint32_t tmpDaysInMonth; uint32_t interruptState; cy_en_rtc_status_t retVal = CY_RTC_BAD_PARAM;
/* Read the current RTC time and date to validate the input parameters */ Cy_Rtc_SyncRegisters();
tmpYear = CY_RTC_TWO_THOUSAND_YEARS + (_FLD2VAL(BACKUP_RTC_DATE_RTC_YEAR, BACKUP->unRTC_DATE.u32Register));
/* Parameters validation */ if((alarmDateTime->month > 0u) && (alarmDateTime->month <= CY_RTC_MONTHS_PER_YEAR)) { tmpDaysInMonth = Cy_Rtc_DaysInMonth(alarmDateTime->month, tmpYear);
if((alarmDateTime->date > 0u) && (alarmDateTime->date <= tmpDaysInMonth)) { Cy_Rtc_ConstructAlarmTimeDate(alarmDateTime, &tmpAlarmTime, &tmpAlarmDate);
/* The RTC AHB register can be updated only under condition that the
* Write bit is set and the RTC busy bit is cleared (RTC_BUSY = 0). */
interruptState = Cy_SysLib_EnterCriticalSection(); retVal = Cy_Rtc_WriteEnable(CY_RTC_WRITE_ENABLED); if(CY_RTC_SUCCESS == retVal) {
/* Update the AHB RTC registers with formed values */
if(alarmIndex != CY_RTC_ALARM_2) { BACKUP->unALM1_TIME.u32Register = tmpAlarmTime; BACKUP->unALM1_DATE.u32Register = tmpAlarmDate; } else { BACKUP->unALM2_TIME.u32Register = tmpAlarmTime; BACKUP->unALM2_DATE.u32Register = tmpAlarmDate; }
/* Clear the RTC Write bit to finish RTC update */ retVal = Cy_Rtc_WriteEnable(CY_RTC_WRITE_DISABLED); } Cy_SysLib_ExitCriticalSection(interruptState); } } return(retVal); }
Set the ALARM1 time and date parameters in temporary structure. See Code Listing 11.
Set the WRITE bit for writing ALARM1 time and date. See Code Listing 7.
(11) Set the ALM1_TIME and
ALM1_DATE registers
Clear the WRITE bit. See Code Listing 7.
Check if configuration parameter values are valid
15 of 47 002-20190 Rev. *D 2020-12-15
How to Use RTC in Traveo II Family
Operation Overview Code Listing 11 Example Program to Construct Alarm Time Date in Driver Part
static void Cy_Rtc_ConstructAlarmTimeDate(cy_stc_rtc_alarm_t const *alarmDateTime, uint32_t *alarmTime, uint32_t *alarmDate) { uint32_t tmpAlarmTime; uint32_t tmpAlarmDate; uint32_t hourValue;
/* Prepare the RTC ALARM value based on the structure obtained */
tmpAlarmTime = (_VAL2FLD(BACKUP_ALM1_TIME_ALM_SEC, (alarmDateTime->sec))); tmpAlarmTime |= (_VAL2FLD(BACKUP_ALM1_TIME_ALM_SEC_EN, alarmDateTime->sec_en)); tmpAlarmTime |= (_VAL2FLD(BACKUP_ALM1_TIME_ALM_MIN, (alarmDateTime->min))); tmpAlarmTime |= (_VAL2FLD(BACKUP_ALM1_TIME_ALM_MIN_EN, alarmDateTime->min_en));
/* Read the current hour mode to know how many hour bits to convert.
* In the 24-hour mode, the hour value is presented in [21:16] bits in the * Integer format. * In the 12-hour mode, the hour value is presented in [20:16] bits in the * Integer format and bit [21] is present: 0 - AM; 1 - PM */
Cy_Rtc_SyncRegisters(); if(CY_RTC_24_HOURS != Cy_Rtc_GetHoursFormat()) { /* Convert the hour from the 24-hour mode into the 12-hour mode */
if(alarmDateTime->hour >= CY_RTC_HOURS_PER_HALF_DAY)
{ /* The current hour is more than 12 in the 24-hour mode. Set the PM
* bit and converting hour: hour = hour - 12 */ hourValue = (uint32_t) alarmDateTime->hour - CY_RTC_HOURS_PER_HALF_DAY;
hourValue = ((0u != hourValue) ? hourValue : CY_RTC_HOURS_PER_HALF_DAY); tmpAlarmTime |= CY_RTC_BACKUP_RTC_TIME_RTC_PM | (_VAL2FLD(BACKUP_ALM1_TIME_ALM_HOUR, (hourValue))); } else if(alarmDateTime->hour < 1u) { /* The current hour in the 24-hour mode is 0 which is equal to 12:00 AM */ tmpAlarmTime = (tmpAlarmTime & ((uint32_t) ~CY_RTC_BACKUP_RTC_TIME_RTC_PM)) | (_VAL2FLD(BACKUP_ALM1_TIME_ALM_HOUR, CY_RTC_HOURS_PER_HALF_DAY)); } else { /* The current hour is less than 12. Set the AM bit */ tmpAlarmTime = (tmpAlarmTime & ((uint32_t) ~CY_RTC_BACKUP_RTC_TIME_RTC_PM)) | (_VAL2FLD(BACKUP_ALM1_TIME_ALM_HOUR, (alarmDateTime->hour))); } tmpAlarmTime |= BACKUP_RTC_TIME_CTRL_12HR_Msk; } else { tmpAlarmTime |= (_VAL2FLD(BACKUP_ALM1_TIME_ALM_HOUR, (alarmDateTime->hour))); tmpAlarmTime &= ((uint32_t) ~BACKUP_RTC_TIME_CTRL_12HR_Msk); } tmpAlarmTime |= (_VAL2FLD(BACKUP_ALM1_TIME_ALM_HOUR_EN, alarmDateTime->hour_en)); tmpAlarmTime |= (_VAL2FLD(BACKUP_ALM1_TIME_ALM_DAY, (alarmDateTime->dayOfWeek))); tmpAlarmTime |= (_VAL2FLD(BACKUP_ALM1_TIME_ALM_DAY_EN, alarmDateTime->dayOfWeek_en));
/* Prepare the RTC ALARM DATE value based on the obtained structure */
tmpAlarmDate = (_VAL2FLD(BACKUP_ALM1_DATE_ALM_DATE, (alarmDateTime->date))); tmpAlarmDate |= (_VAL2FLD(BACKUP_ALM1_DATE_ALM_DATE_EN, alarmDateTime->date_en)); tmpAlarmDate |= (_VAL2FLD(BACKUP_ALM1_DATE_ALM_MON, (alarmDateTime->month))); tmpAlarmDate |= (_VAL2FLD(BACKUP_ALM1_DATE_ALM_MON_EN, alarmDateTime->month_en)); tmpAlarmDate |= (_VAL2FLD(BACKUP_ALM1_DATE_ALM_EN, alarmDateTime->alm_en));
/* Update the parameter values with prepared values */
*alarmTime = tmpAlarmTime; *alarmDate = tmpAlarmDate;
}
Set to ALARM Time parameters
Set ALARM date parameters
(9) Set the ALARM1 time and date parameter in temporary structure.
Loading...
+ 32 hidden pages