1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_rtc.h"
10 
11 /*
12  * $Coverage Justification Reference$
13  *
14  * $Justification rtc_c_ref_1$
15  * Month is always under 13 and checked by assert.
16  *
17  * $Justification rtc_c_ref_2$
18  * Can't simulate tamper in unit test environment.
19  *
20  */
21 
22 /*******************************************************************************
23  * Definitions
24  ******************************************************************************/
25 
26 /* Component ID definition, used by tools. */
27 #ifndef FSL_COMPONENT_ID
28 #define FSL_COMPONENT_ID "platform.drivers.rtc"
29 #endif
30 
31 #define SECONDS_IN_A_DAY    (86400U)
32 #define SECONDS_IN_A_HOUR   (3600U)
33 #define SECONDS_IN_A_MINUTE (60U)
34 #define DAYS_IN_A_YEAR      (365U)
35 #define YEAR_RANGE_START    (1970U)
36 #define YEAR_RANGE_END      (2099U)
37 
38 /*******************************************************************************
39  * Prototypes
40  ******************************************************************************/
41 /*!
42  * @brief Checks whether the date and time passed in is valid
43  *
44  * @param datetime Pointer to structure where the date and time details are stored
45  *
46  * @return Returns false if the date & time details are out of range; true if in range
47  */
48 static bool RTC_CheckDatetimeFormat(const rtc_datetime_t *datetime);
49 
50 /*!
51  * @brief Converts time data from datetime to seconds
52  *
53  * @param datetime Pointer to datetime structure where the date and time details are stored
54  *
55  * @return The result of the conversion in seconds
56  */
57 static uint32_t RTC_ConvertDatetimeToSeconds(const rtc_datetime_t *datetime);
58 
59 /*!
60  * @brief Converts time data from seconds to a datetime structure
61  *
62  * @param seconds  Seconds value that needs to be converted to datetime format
63  * @param datetime Pointer to the datetime structure where the result of the conversion is stored
64  */
65 static void RTC_ConvertSecondsToDatetime(uint32_t seconds, rtc_datetime_t *datetime);
66 
67 /*******************************************************************************
68  * Code
69  ******************************************************************************/
RTC_CheckDatetimeFormat(const rtc_datetime_t * datetime)70 static bool RTC_CheckDatetimeFormat(const rtc_datetime_t *datetime)
71 {
72     assert(NULL != datetime);
73 
74     /* Table of days in a month for a non leap year. First entry in the table is not used,
75      * valid months start from 1
76      */
77     uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
78 
79     /* Check year, month, hour, minute, seconds */
80     if ((datetime->year < YEAR_RANGE_START) || (datetime->year > YEAR_RANGE_END) || (datetime->month > 12U) ||
81         (datetime->month < 1U) || (datetime->hour >= 24U) || (datetime->minute >= 60U) || (datetime->second >= 60U))
82     {
83         /* If not correct then error*/
84         return false;
85     }
86 
87     /* Adjust the days in February for a leap year */
88     if ((((datetime->year & 3U) == 0U) && (datetime->year % 100U != 0U)) || (datetime->year % 400U == 0U))
89     {
90         daysPerMonth[2] = 29U;
91     }
92 
93     /* Check the validity of the day */
94     if ((datetime->day > daysPerMonth[datetime->month]) || (datetime->day < 1U))
95     {
96         return false;
97     }
98 
99     return true;
100 }
101 
RTC_ConvertDatetimeToSeconds(const rtc_datetime_t * datetime)102 static uint32_t RTC_ConvertDatetimeToSeconds(const rtc_datetime_t *datetime)
103 {
104     assert(NULL != datetime);
105 
106     /* Number of days from begin of the non Leap-year*/
107     /* Number of days from begin of the non Leap-year*/
108     uint16_t monthDays[] = {0U, 0U, 31U, 59U, 90U, 120U, 151U, 181U, 212U, 243U, 273U, 304U, 334U};
109     uint32_t seconds;
110 
111     /* Compute number of days from 1970 till given year*/
112     seconds = ((uint32_t)datetime->year - 1970U) * DAYS_IN_A_YEAR;
113     /* Add leap year days */
114     seconds += (((uint32_t)datetime->year / 4U) - (1970U / 4U));
115     /* Add number of days till given month*/
116     seconds += monthDays[datetime->month];
117     /* Add days in given month. We subtract the current day as it is
118      * represented in the hours, minutes and seconds field*/
119     seconds += ((uint32_t)datetime->day - 1U);
120     /* For leap year if month less than or equal to Febraury, decrement day counter*/
121     if ((0U == (datetime->year & 3U)) && (datetime->month <= 2U))
122     {
123         seconds--;
124     }
125 
126     seconds = (seconds * SECONDS_IN_A_DAY) + ((uint32_t)datetime->hour * SECONDS_IN_A_HOUR) +
127               ((uint32_t)datetime->minute * SECONDS_IN_A_MINUTE) + datetime->second;
128 
129     return seconds;
130 }
131 
RTC_ConvertSecondsToDatetime(uint32_t seconds,rtc_datetime_t * datetime)132 static void RTC_ConvertSecondsToDatetime(uint32_t seconds, rtc_datetime_t *datetime)
133 {
134     assert(NULL != datetime);
135 
136     uint32_t x;
137     uint32_t secondsRemaining, days;
138     uint16_t daysInYear;
139     /* Table of days in a month for a non leap year. First entry in the table is not used,
140      * valid months start from 1
141      */
142     uint8_t daysPerMonth[] = {0U, 31U, 28U, 31U, 30U, 31U, 30U, 31U, 31U, 30U, 31U, 30U, 31U};
143 
144     /* Start with the seconds value that is passed in to be converted to date time format */
145     secondsRemaining = seconds;
146 
147     /* Calcuate the number of days, we add 1 for the current day which is represented in the
148      * hours and seconds field
149      */
150     days = secondsRemaining / SECONDS_IN_A_DAY + 1U;
151 
152     /* Update seconds left*/
153     secondsRemaining = secondsRemaining % SECONDS_IN_A_DAY;
154 
155     /* Calculate the datetime hour, minute and second fields */
156     datetime->hour   = (uint8_t)(secondsRemaining / SECONDS_IN_A_HOUR);
157     secondsRemaining = secondsRemaining % SECONDS_IN_A_HOUR;
158     datetime->minute = (uint8_t)(secondsRemaining / 60U);
159     datetime->second = (uint8_t)(secondsRemaining % SECONDS_IN_A_MINUTE);
160 
161     /* Calculate year */
162     daysInYear     = DAYS_IN_A_YEAR;
163     datetime->year = YEAR_RANGE_START;
164     while (days > daysInYear)
165     {
166         /* Decrease day count by a year and increment year by 1 */
167         days -= daysInYear;
168         datetime->year++;
169 
170         /* Adjust the number of days for a leap year */
171         if (0U != (datetime->year & 3U))
172         {
173             daysInYear = DAYS_IN_A_YEAR;
174         }
175         else
176         {
177             daysInYear = DAYS_IN_A_YEAR + 1U;
178         }
179     }
180 
181     /* Adjust the days in February for a leap year */
182     if (0U == (datetime->year & 3U))
183     {
184         daysPerMonth[2] = 29U;
185     }
186 
187     /*
188      * $Branch Coverage Justification$
189      * (x > 12U) not covered. $ref rtc_c_ref_1$.
190      */
191     for (x = 1U; x <= 12U; x++)
192     {
193         if (days <= daysPerMonth[x])
194         {
195             datetime->month = (uint8_t)x;
196             break;
197         }
198         else
199         {
200             days -= daysPerMonth[x];
201         }
202     }
203 
204     datetime->day = (uint8_t)days;
205 }
206 
207 /*!
208  * brief Ungates the RTC clock and configures the peripheral for basic operation.
209  *
210  * This function issues a software reset if the timer invalid flag is set.
211  *
212  * note This API should be called at the beginning of the application using the RTC driver.
213  *
214  * param base   RTC peripheral base address
215  * param config Pointer to the user's RTC configuration structure.
216  */
RTC_Init(RTC_Type * base,const rtc_config_t * config)217 void RTC_Init(RTC_Type *base, const rtc_config_t *config)
218 {
219     assert(NULL != config);
220 
221     uint32_t reg;
222 
223 #if defined(RTC_CLOCKS)
224 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
225     CLOCK_EnableClock(kCLOCK_Rtc0);
226 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
227 #endif /* RTC_CLOCKS */
228 
229     /* Issue a software reset if timer is invalid */
230     if ((uint32_t)kRTC_TimeInvalidFlag == (RTC_GetStatusFlags(base) & (uint32_t)kRTC_TimeInvalidFlag))
231     {
232         RTC_Reset(base);
233     }
234 
235     reg = base->CR;
236 /* Setup the update mode and supervisor access mode */
237 #if !(defined(FSL_FEATURE_RTC_HAS_NO_CR_OSCE) && FSL_FEATURE_RTC_HAS_NO_CR_OSCE)
238     reg &= ~(RTC_CR_UM_MASK | RTC_CR_SUP_MASK);
239     reg |= RTC_CR_UM(config->updateMode) | RTC_CR_SUP(config->supervisorAccess);
240 #else
241     reg &= ~RTC_CR_UM_MASK;
242     reg |= RTC_CR_UM(config->updateMode);
243 #endif
244 
245 #if defined(FSL_FEATURE_RTC_HAS_WAKEUP_PIN_SELECTION) && FSL_FEATURE_RTC_HAS_WAKEUP_PIN_SELECTION
246     /* Setup the wakeup pin select */
247     reg &= ~(RTC_CR_WPS_MASK);
248     reg |= RTC_CR_WPS(config->wakeupSelect);
249 #endif /* FSL_FEATURE_RTC_HAS_WAKEUP_PIN */
250     base->CR = reg;
251 
252     /* Configure the RTC time compensation register */
253     base->TCR = (RTC_TCR_CIR(config->compensationInterval) | RTC_TCR_TCR(config->compensationTime));
254 
255 #if defined(FSL_FEATURE_RTC_HAS_TSIC) && FSL_FEATURE_RTC_HAS_TSIC
256     /* Configure RTC timer seconds interrupt to be generated once per second */
257     base->IER &= ~(RTC_IER_TSIC_MASK | RTC_IER_TSIE_MASK);
258 #endif
259 }
260 
261 /*!
262  * brief Fills in the RTC config struct with the default settings.
263  *
264  * The default values are as follows.
265  * code
266  *    config->wakeupSelect = false;
267  *    config->updateMode = false;
268  *    config->supervisorAccess = false;
269  *    config->compensationInterval = 0;
270  *    config->compensationTime = 0;
271  * endcode
272  * param config Pointer to the user's RTC configuration structure.
273  */
RTC_GetDefaultConfig(rtc_config_t * config)274 void RTC_GetDefaultConfig(rtc_config_t *config)
275 {
276     assert(NULL != config);
277 
278     /* Initializes the configure structure to zero. */
279     (void)memset(config, 0, sizeof(*config));
280 
281     /* Wakeup pin will assert if the RTC interrupt asserts or if the wakeup pin is turned on */
282     config->wakeupSelect = false;
283     /* Registers cannot be written when locked */
284     config->updateMode = false;
285     /* Non-supervisor mode write accesses are not supported and will generate a bus error */
286     config->supervisorAccess = false;
287     /* Compensation interval used by the crystal compensation logic */
288     config->compensationInterval = 0;
289     /* Compensation time used by the crystal compensation logic */
290     config->compensationTime = 0;
291 }
292 
293 /*!
294  * brief Sets the RTC date and time according to the given time structure.
295  *
296  * The RTC counter must be stopped prior to calling this function because writes to the RTC
297  * seconds register fail if the RTC counter is running.
298  *
299  * param base     RTC peripheral base address
300  * param datetime Pointer to the structure where the date and time details are stored.
301  *
302  * return kStatus_Success: Success in setting the time and starting the RTC
303  *         kStatus_InvalidArgument: Error because the datetime format is incorrect
304  */
RTC_SetDatetime(RTC_Type * base,const rtc_datetime_t * datetime)305 status_t RTC_SetDatetime(RTC_Type *base, const rtc_datetime_t *datetime)
306 {
307     assert(NULL != datetime);
308 
309     /* Return error if the time provided is not valid */
310     if (!(RTC_CheckDatetimeFormat(datetime)))
311     {
312         return kStatus_InvalidArgument;
313     }
314 
315     /* Set time in seconds */
316     base->TSR = RTC_ConvertDatetimeToSeconds(datetime);
317 
318     return kStatus_Success;
319 }
320 
321 /*!
322  * brief Gets the RTC time and stores it in the given time structure.
323  *
324  * param base     RTC peripheral base address
325  * param datetime Pointer to the structure where the date and time details are stored.
326  */
RTC_GetDatetime(RTC_Type * base,rtc_datetime_t * datetime)327 void RTC_GetDatetime(RTC_Type *base, rtc_datetime_t *datetime)
328 {
329     assert(NULL != datetime);
330 
331     uint32_t seconds = 0;
332 
333     seconds = base->TSR;
334     RTC_ConvertSecondsToDatetime(seconds, datetime);
335 }
336 
337 /*!
338  * brief Sets the RTC alarm time.
339  *
340  * The function checks whether the specified alarm time is greater than the present
341  * time. If not, the function does not set the alarm and returns an error.
342  *
343  * param base      RTC peripheral base address
344  * param alarmTime Pointer to the structure where the alarm time is stored.
345  *
346  * return kStatus_Success: success in setting the RTC alarm
347  *         kStatus_InvalidArgument: Error because the alarm datetime format is incorrect
348  *         kStatus_Fail: Error because the alarm time has already passed
349  */
RTC_SetAlarm(RTC_Type * base,const rtc_datetime_t * alarmTime)350 status_t RTC_SetAlarm(RTC_Type *base, const rtc_datetime_t *alarmTime)
351 {
352     assert(NULL != alarmTime);
353 
354     uint32_t alarmSeconds = 0;
355     uint32_t currSeconds  = 0;
356 
357     /* Return error if the alarm time provided is not valid */
358     if (!(RTC_CheckDatetimeFormat(alarmTime)))
359     {
360         return kStatus_InvalidArgument;
361     }
362 
363     alarmSeconds = RTC_ConvertDatetimeToSeconds(alarmTime);
364 
365     /* Get the current time */
366     currSeconds = base->TSR;
367 
368     /* Return error if the alarm time has passed */
369     if (alarmSeconds < currSeconds)
370     {
371         return kStatus_Fail;
372     }
373 
374     /* Set alarm in seconds*/
375     base->TAR = alarmSeconds;
376 
377     return kStatus_Success;
378 }
379 
380 /*!
381  * brief Returns the RTC alarm time.
382  *
383  * param base     RTC peripheral base address
384  * param datetime Pointer to the structure where the alarm date and time details are stored.
385  */
RTC_GetAlarm(RTC_Type * base,rtc_datetime_t * datetime)386 void RTC_GetAlarm(RTC_Type *base, rtc_datetime_t *datetime)
387 {
388     assert(NULL != datetime);
389 
390     uint32_t alarmSeconds = 0;
391 
392     /* Get alarm in seconds  */
393     alarmSeconds = base->TAR;
394 
395     RTC_ConvertSecondsToDatetime(alarmSeconds, datetime);
396 }
397 
398 /*!
399  * brief Enables the selected RTC interrupts.
400  *
401  * param base RTC peripheral base address
402  * param mask The interrupts to enable. This is a logical OR of members of the
403  *             enumeration ::rtc_interrupt_enable_t
404  */
RTC_EnableInterrupts(RTC_Type * base,uint32_t mask)405 void RTC_EnableInterrupts(RTC_Type *base, uint32_t mask)
406 {
407     uint32_t tmp32 = 0U;
408 
409     /* RTC_IER */
410     if (0U != ((uint32_t)kRTC_TimeInvalidInterruptEnable & mask))
411     {
412         tmp32 |= RTC_IER_TIIE_MASK;
413     }
414     if (0U != ((uint32_t)kRTC_TimeOverflowInterruptEnable & mask))
415     {
416         tmp32 |= RTC_IER_TOIE_MASK;
417     }
418     if (0U != ((uint32_t)kRTC_AlarmInterruptEnable & mask))
419     {
420         tmp32 |= RTC_IER_TAIE_MASK;
421     }
422     if (0U != ((uint32_t)kRTC_SecondsInterruptEnable & mask))
423     {
424         tmp32 |= RTC_IER_TSIE_MASK;
425     }
426 #if defined(FSL_FEATURE_RTC_HAS_MONOTONIC) && (FSL_FEATURE_RTC_HAS_MONOTONIC)
427     if (0U != ((uint32_t)kRTC_MonotonicOverflowInterruptEnable & mask))
428     {
429         tmp32 |= RTC_IER_MOIE_MASK;
430     }
431 #endif /* FSL_FEATURE_RTC_HAS_MONOTONIC */
432     base->IER |= tmp32;
433 
434 #if (defined(FSL_FEATURE_RTC_HAS_TIR) && FSL_FEATURE_RTC_HAS_TIR)
435     tmp32 = 0U;
436 
437     /* RTC_TIR */
438     if (0U != ((uint32_t)kRTC_TestModeInterruptEnable & mask))
439     {
440         tmp32 |= RTC_TIR_TMIE_MASK;
441     }
442     if (0U != ((uint32_t)kRTC_FlashSecurityInterruptEnable & mask))
443     {
444         tmp32 |= RTC_TIR_FSIE_MASK;
445     }
446 #if (defined(FSL_FEATURE_RTC_HAS_TIR_TPIE) && FSL_FEATURE_RTC_HAS_TIR_TPIE)
447     if (0U != ((uint32_t)kRTC_TamperPinInterruptEnable & mask))
448     {
449         tmp32 |= RTC_TIR_TPIE_MASK;
450     }
451 #endif /* FSL_FEATURE_RTC_HAS_TIR_TPIE */
452 #if (defined(FSL_FEATURE_RTC_HAS_TIR_SIE) && FSL_FEATURE_RTC_HAS_TIR_SIE)
453     if (0U != ((uint32_t)kRTC_SecurityModuleInterruptEnable & mask))
454     {
455         tmp32 |= RTC_TIR_SIE_MASK;
456     }
457 #endif /* FSL_FEATURE_RTC_HAS_TIR_SIE */
458 #if (defined(FSL_FEATURE_RTC_HAS_TIR_LCIE) && FSL_FEATURE_RTC_HAS_TIR_LCIE)
459     if (0U != ((uint32_t)kRTC_LossOfClockInterruptEnable & mask))
460     {
461         tmp32 |= RTC_TIR_LCIE_MASK;
462     }
463 #endif /* FSL_FEATURE_RTC_HAS_TIR_LCIE */
464     base->TIR |= tmp32;
465 #endif /* FSL_FEATURE_RTC_HAS_TIR */
466 }
467 
468 /*!
469  * brief Disables the selected RTC interrupts.
470  *
471  * param base RTC peripheral base address
472  * param mask The interrupts to enable. This is a logical OR of members of the
473  *             enumeration ::rtc_interrupt_enable_t
474  */
RTC_DisableInterrupts(RTC_Type * base,uint32_t mask)475 void RTC_DisableInterrupts(RTC_Type *base, uint32_t mask)
476 {
477     uint32_t tmp32 = 0U;
478 
479     /* RTC_IER */
480     if (0U != ((uint32_t)kRTC_TimeInvalidInterruptEnable & mask))
481     {
482         tmp32 |= RTC_IER_TIIE_MASK;
483     }
484     if (0U != ((uint32_t)kRTC_TimeOverflowInterruptEnable & mask))
485     {
486         tmp32 |= RTC_IER_TOIE_MASK;
487     }
488     if (0U != ((uint32_t)kRTC_AlarmInterruptEnable & mask))
489     {
490         tmp32 |= RTC_IER_TAIE_MASK;
491     }
492     if (0U != ((uint32_t)kRTC_SecondsInterruptEnable & mask))
493     {
494         tmp32 |= RTC_IER_TSIE_MASK;
495     }
496 #if defined(FSL_FEATURE_RTC_HAS_MONOTONIC) && (FSL_FEATURE_RTC_HAS_MONOTONIC)
497     if (0U != ((uint32_t)kRTC_MonotonicOverflowInterruptEnable & mask))
498     {
499         tmp32 |= RTC_IER_MOIE_MASK;
500     }
501 #endif /* FSL_FEATURE_RTC_HAS_MONOTONIC */
502     base->IER &= (uint32_t)(~tmp32);
503 
504 #if (defined(FSL_FEATURE_RTC_HAS_TIR) && FSL_FEATURE_RTC_HAS_TIR)
505     tmp32 = 0U;
506 
507     /* RTC_TIR */
508     if (0U != ((uint32_t)kRTC_TestModeInterruptEnable & mask))
509     {
510         tmp32 |= RTC_TIR_TMIE_MASK;
511     }
512     if (0U != ((uint32_t)kRTC_FlashSecurityInterruptEnable & mask))
513     {
514         tmp32 |= RTC_TIR_FSIE_MASK;
515     }
516 #if (defined(FSL_FEATURE_RTC_HAS_TIR_TPIE) && FSL_FEATURE_RTC_HAS_TIR_TPIE)
517     if (0U != ((uint32_t)kRTC_TamperPinInterruptEnable & mask))
518     {
519         tmp32 |= RTC_TIR_TPIE_MASK;
520     }
521 #endif /* FSL_FEATURE_RTC_HAS_TIR_TPIE */
522 #if (defined(FSL_FEATURE_RTC_HAS_TIR_SIE) && FSL_FEATURE_RTC_HAS_TIR_SIE)
523     if (0U != ((uint32_t)kRTC_SecurityModuleInterruptEnable & mask))
524     {
525         tmp32 |= RTC_TIR_SIE_MASK;
526     }
527 #endif /* FSL_FEATURE_RTC_HAS_TIR_SIE */
528 #if (defined(FSL_FEATURE_RTC_HAS_TIR_LCIE) && FSL_FEATURE_RTC_HAS_TIR_LCIE)
529     if (0U != ((uint32_t)kRTC_LossOfClockInterruptEnable & mask))
530     {
531         tmp32 |= RTC_TIR_LCIE_MASK;
532     }
533 #endif /* FSL_FEATURE_RTC_HAS_TIR_LCIE */
534     base->TIR &= (uint32_t)(~tmp32);
535 #endif /* FSL_FEATURE_RTC_HAS_TIR */
536 }
537 
538 /*!
539  * brief Gets the enabled RTC interrupts.
540  *
541  * param base RTC peripheral base address
542  *
543  * return The enabled interrupts. This is the logical OR of members of the
544  *         enumeration ::rtc_interrupt_enable_t
545  */
RTC_GetEnabledInterrupts(RTC_Type * base)546 uint32_t RTC_GetEnabledInterrupts(RTC_Type *base)
547 {
548     uint32_t tmp32 = 0U;
549 
550     /* RTC_IER */
551     if (0U != (RTC_IER_TIIE_MASK & base->IER))
552     {
553         tmp32 |= (uint32_t)kRTC_TimeInvalidInterruptEnable;
554     }
555     if (0U != (RTC_IER_TOIE_MASK & base->IER))
556     {
557         tmp32 |= (uint32_t)kRTC_TimeOverflowInterruptEnable;
558     }
559     if (0U != (RTC_IER_TAIE_MASK & base->IER))
560     {
561         tmp32 |= (uint32_t)kRTC_AlarmInterruptEnable;
562     }
563     if (0U != (RTC_IER_TSIE_MASK & base->IER))
564     {
565         tmp32 |= (uint32_t)kRTC_SecondsInterruptEnable;
566     }
567 #if defined(FSL_FEATURE_RTC_HAS_MONOTONIC) && (FSL_FEATURE_RTC_HAS_MONOTONIC)
568     if (0U != (RTC_IER_MOIE_MASK & base->IER))
569     {
570         tmp32 |= (uint32_t)kRTC_MonotonicOverflowInterruptEnable;
571     }
572 #endif /* FSL_FEATURE_RTC_HAS_MONOTONIC */
573 
574 #if (defined(FSL_FEATURE_RTC_HAS_TIR) && FSL_FEATURE_RTC_HAS_TIR)
575     /* RTC_TIR */
576     if (0U != (RTC_TIR_TMIE_MASK & base->TIR))
577     {
578         tmp32 |= (uint32_t)kRTC_TestModeInterruptEnable;
579     }
580     if (0U != (RTC_TIR_FSIE_MASK & base->TIR))
581     {
582         tmp32 |= (uint32_t)kRTC_FlashSecurityInterruptEnable;
583     }
584 #if (defined(FSL_FEATURE_RTC_HAS_TIR_TPIE) && FSL_FEATURE_RTC_HAS_TIR_TPIE)
585     if (0U != (RTC_TIR_TPIE_MASK & base->TIR))
586     {
587         tmp32 |= (uint32_t)kRTC_TamperPinInterruptEnable;
588     }
589 #endif /* FSL_FEATURE_RTC_HAS_TIR_TPIE */
590 #if (defined(FSL_FEATURE_RTC_HAS_TIR_SIE) && FSL_FEATURE_RTC_HAS_TIR_SIE)
591     if (0U != (RTC_TIR_SIE_MASK & base->TIR))
592     {
593         tmp32 |= (uint32_t)kRTC_SecurityModuleInterruptEnable;
594     }
595 #endif /* FSL_FEATURE_RTC_HAS_TIR_SIE */
596 #if (defined(FSL_FEATURE_RTC_HAS_TIR_LCIE) && FSL_FEATURE_RTC_HAS_TIR_LCIE)
597     if (0U != (RTC_TIR_LCIE_MASK & base->TIR))
598     {
599         tmp32 |= (uint32_t)kRTC_LossOfClockInterruptEnable;
600     }
601 #endif /* FSL_FEATURE_RTC_HAS_TIR_LCIE */
602 #endif /* FSL_FEATURE_RTC_HAS_TIR */
603 
604     return tmp32;
605 }
606 
607 /*!
608  * brief Gets the RTC status flags.
609  *
610  * param base RTC peripheral base address
611  *
612  * return The status flags. This is the logical OR of members of the
613  *         enumeration ::rtc_status_flags_t
614  */
RTC_GetStatusFlags(RTC_Type * base)615 uint32_t RTC_GetStatusFlags(RTC_Type *base)
616 {
617     uint32_t tmp32 = 0U;
618 
619     /* RTC_SR */
620     if (0U != (RTC_SR_TIF_MASK & base->SR))
621     {
622         tmp32 |= (uint32_t)kRTC_TimeInvalidFlag;
623     }
624     if (0U != (RTC_SR_TOF_MASK & base->SR))
625     {
626         tmp32 |= (uint32_t)kRTC_TimeOverflowFlag;
627     }
628     if (0U != (RTC_SR_TAF_MASK & base->SR))
629     {
630         tmp32 |= (uint32_t)kRTC_AlarmFlag;
631     }
632 #if defined(FSL_FEATURE_RTC_HAS_MONOTONIC) && (FSL_FEATURE_RTC_HAS_MONOTONIC)
633     if (0U != (RTC_SR_MOF_MASK & base->SR))
634     {
635         tmp32 |= (uint32_t)kRTC_MonotonicOverflowFlag;
636     }
637 #endif /* FSL_FEATURE_RTC_HAS_MONOTONIC */
638 #if (defined(FSL_FEATURE_RTC_HAS_SR_TIDF) && FSL_FEATURE_RTC_HAS_SR_TIDF)
639     /*
640      * $Branch Coverage Justification$
641      * (0U != (RTC_SR_TIDF_MASK & base->SR)) not covered. $ref rtc_c_ref_2$.
642      */
643     if (0U != (RTC_SR_TIDF_MASK & base->SR))
644     {
645         /*
646         * $Line Coverage Justification$
647         * $ref rtc_c_ref_2$.
648         */
649         tmp32 |= (uint32_t)kRTC_TamperInterruptDetectFlag;
650     }
651 #endif /* FSL_FEATURE_RTC_HAS_SR_TIDF */
652 
653 #if (defined(FSL_FEATURE_RTC_HAS_TDR) && FSL_FEATURE_RTC_HAS_TDR)
654     /* RTC_TDR */
655     /*
656      * $Branch Coverage Justification$
657      * (0U != (RTC_TDR_TMF_MASK & base->TDR)) not covered. $ref rtc_c_ref_2$.
658      */
659     if (0U != (RTC_TDR_TMF_MASK & base->TDR))
660     {
661         /*
662         * $Line Coverage Justification$
663         * $ref rtc_c_ref_2$.
664         */
665         tmp32 |= (uint32_t)kRTC_TestModeFlag;
666     }
667     if (0U != (RTC_TDR_FSF_MASK & base->TDR))
668     {
669         tmp32 |= (uint32_t)kRTC_FlashSecurityFlag;
670     }
671 #if (defined(FSL_FEATURE_RTC_HAS_TDR_TPF) && FSL_FEATURE_RTC_HAS_TDR_TPF)
672     /*
673      * $Branch Coverage Justification$
674      * (0U != (RTC_TDR_TPF_MASK & base->TDR)) not covered. $ref rtc_c_ref_2$.
675      */
676     if (0U != (RTC_TDR_TPF_MASK & base->TDR))
677     {
678         /*
679         * $Line Coverage Justification$
680         * $ref rtc_c_ref_2$.
681         */
682         tmp32 |= (uint32_t)kRTC_TamperPinFlag;
683     }
684 #endif /* FSL_FEATURE_RTC_HAS_TDR_TPF */
685 #if (defined(FSL_FEATURE_RTC_HAS_TDR_STF) && FSL_FEATURE_RTC_HAS_TDR_STF)
686     /*
687      * $Branch Coverage Justification$
688      * (0U != (RTC_TDR_STF_MASK & base->TDR)) not covered. $ref rtc_c_ref_2$.
689      */
690     if (0U != (RTC_TDR_STF_MASK & base->TDR))
691     {
692         /*
693         * $Line Coverage Justification$
694         * $ref rtc_c_ref_2$.
695         */
696         tmp32 |= (uint32_t)kRTC_SecurityTamperFlag;
697     }
698 #endif /* FSL_FEATURE_RTC_HAS_TDR_STF */
699 #if (defined(FSL_FEATURE_RTC_HAS_TDR_LCTF) && FSL_FEATURE_RTC_HAS_TDR_LCTF)
700     /*
701      * $Branch Coverage Justification$
702      * (0U != (RTC_TDR_LCTF_MASK & base->TDR)) not covered. $ref rtc_c_ref_2$.
703      */
704     if (0U != (RTC_TDR_LCTF_MASK & base->TDR))
705     {
706         /*
707         * $Line Coverage Justification$
708         * $ref rtc_c_ref_2$.
709         */
710         tmp32 |= (uint32_t)kRTC_LossOfClockTamperFlag;
711     }
712 #endif /* FSL_FEATURE_RTC_HAS_TDR_LCTF */
713 #endif /* FSL_FEATURE_RTC_HAS_TDR */
714 
715     return tmp32;
716 }
717 
718 /*!
719  * brief  Clears the RTC status flags.
720  *
721  * param base RTC peripheral base address
722  * param mask The status flags to clear. This is a logical OR of members of the
723  *             enumeration ::rtc_status_flags_t
724  */
RTC_ClearStatusFlags(RTC_Type * base,uint32_t mask)725 void RTC_ClearStatusFlags(RTC_Type *base, uint32_t mask)
726 {
727     /* The alarm flag is cleared by writing to the TAR register */
728     if (0U != (mask & (uint32_t)kRTC_AlarmFlag))
729     {
730         base->TAR = 0U;
731     }
732 
733     /* The timer overflow flag is cleared by initializing the TSR register.
734      * The time counter should be disabled for this write to be successful
735      */
736     if (0U != (mask & (uint32_t)kRTC_TimeOverflowFlag))
737     {
738         base->TSR = 1U;
739     }
740 
741     /* The timer overflow flag is cleared by initializing the TSR register.
742      * The time counter should be disabled for this write to be successful
743      */
744     if (0U != (mask & (uint32_t)kRTC_TimeInvalidFlag))
745     {
746         base->TSR = 1U;
747     }
748 
749 #if (defined(FSL_FEATURE_RTC_HAS_TDR) && FSL_FEATURE_RTC_HAS_TDR)
750     /* To clear, write logic one to this flag after exiting from all test modes */
751     /*
752      * $Branch Coverage Justification$
753      * (0U != ((uint32_t)kRTC_TestModeFlag & mask)) not covered. $ref rtc_c_ref_2$.
754      */
755     if (0U != ((uint32_t)kRTC_TestModeFlag & mask))
756     {
757         /*
758         * $Line Coverage Justification$
759         * $ref rtc_c_ref_2$.
760         */
761         base->TDR = RTC_TDR_TMF_MASK;
762     }
763     /* To clear, write logic one to this flag after flash security is enabled */
764     /*
765      * $Branch Coverage Justification$
766      * (0U != ((uint32_t)kRTC_FlashSecurityFlag & mask)) not covered. $ref rtc_c_ref_2$.
767      */
768     if (0U != ((uint32_t)kRTC_FlashSecurityFlag & mask))
769     {
770         /*
771         * $Line Coverage Justification$
772         * $ref rtc_c_ref_2$.
773         */
774         base->TDR = RTC_TDR_FSF_MASK;
775     }
776 #if (defined(FSL_FEATURE_RTC_HAS_TDR_TPF) && FSL_FEATURE_RTC_HAS_TDR_TPF)
777     /* To clear, write logic one to the corresponding flag after that tamper pin negates */
778     /*
779      * $Branch Coverage Justification$
780      * (0U != ((uint32_t)kRTC_TamperPinFlag & mask)) not covered. $ref rtc_c_ref_2$.
781      */
782     if (0U != ((uint32_t)kRTC_TamperPinFlag & mask))
783     {
784         /*
785         * $Line Coverage Justification$
786         * $ref rtc_c_ref_2$.
787         */
788         base->TDR = RTC_TDR_TPF_MASK;
789     }
790 #endif /* FSL_FEATURE_RTC_HAS_TDR_TPF */
791 #if (defined(FSL_FEATURE_RTC_HAS_TDR_STF) && FSL_FEATURE_RTC_HAS_TDR_STF)
792     /* To clear, write logic one to this flag after security module has negated its tamper detect */
793     /*
794      * $Branch Coverage Justification$
795      * (0U != ((uint32_t)kRTC_SecurityTamperFlag & mask)) not covered. $ref rtc_c_ref_2$.
796      */
797     if (0U != ((uint32_t)kRTC_SecurityTamperFlag & mask))
798     {
799         /*
800         * $Line Coverage Justification$
801         * $ref rtc_c_ref_2$.
802         */
803         base->TDR = RTC_TDR_STF_MASK;
804     }
805 #endif /* FSL_FEATURE_RTC_HAS_TDR_STF */
806 #if (defined(FSL_FEATURE_RTC_HAS_TDR_LCTF) && FSL_FEATURE_RTC_HAS_TDR_LCTF)
807     /* To clear, write logic one to this flag after loss of clock negates */
808     /*
809      * $Branch Coverage Justification$
810      * (0U != ((uint32_t)kRTC_LossOfClockTamperFlag & mask)) not covered. $ref rtc_c_ref_2$.
811      */
812     if (0U != ((uint32_t)kRTC_LossOfClockTamperFlag & mask))
813     {
814         /*
815         * $Line Coverage Justification$
816         * $ref rtc_c_ref_2$.
817         */
818         base->TDR = RTC_TDR_LCTF_MASK;
819     }
820 #endif /* FSL_FEATURE_RTC_HAS_TDR_LCTF */
821 #endif /* FSL_FEATURE_RTC_HAS_TDR */
822 }
823 
824 #if defined(FSL_FEATURE_RTC_HAS_MONOTONIC) && (FSL_FEATURE_RTC_HAS_MONOTONIC)
825 
826 /*!
827  * brief Reads the values of the Monotonic Counter High and Monotonic Counter Low and returns
828  *        them as a single value.
829  *
830  * param base    RTC peripheral base address
831  * param counter Pointer to variable where the value is stored.
832  */
RTC_GetMonotonicCounter(RTC_Type * base,uint64_t * counter)833 void RTC_GetMonotonicCounter(RTC_Type *base, uint64_t *counter)
834 {
835     uint64_t u64temp;
836 
837     assert(NULL != counter);
838 
839     u64temp  = (uint64_t)base->MCLR;
840     *counter = (((uint64_t)base->MCHR << 32U) | u64temp);
841 }
842 
843 /*!
844  * brief Writes values Monotonic Counter High and Monotonic Counter Low by decomposing
845  *        the given single value. The Monotonic Overflow Flag in RTC_SR is cleared due to the API.
846  *
847  * param base    RTC peripheral base address
848  * param counter Counter value
849  */
RTC_SetMonotonicCounter(RTC_Type * base,uint64_t counter)850 void RTC_SetMonotonicCounter(RTC_Type *base, uint64_t counter)
851 {
852     /* Prepare to initialize the register with the new value written */
853     base->MER &= ~RTC_MER_MCE_MASK;
854 
855     base->MCHR = (uint32_t)((counter) >> 32);
856     base->MCLR = (uint32_t)(counter);
857 }
858 
859 /*!
860  * brief Increments the Monotonic Counter by one.
861  *
862  * Increments the Monotonic Counter (registers RTC_MCLR and RTC_MCHR accordingly) by setting
863  * the monotonic counter enable (MER[MCE]) and then writing to the RTC_MCLR register. A write to the
864  * monotonic counter low that causes it to overflow also increments the monotonic counter high.
865  *
866  * param base RTC peripheral base address
867  *
868  * return kStatus_Success: success
869  *         kStatus_Fail: error occurred, either time invalid or monotonic overflow flag was found
870  */
RTC_IncrementMonotonicCounter(RTC_Type * base)871 status_t RTC_IncrementMonotonicCounter(RTC_Type *base)
872 {
873     if (0U != (base->SR & (RTC_SR_MOF_MASK | RTC_SR_TIF_MASK)))
874     {
875         return kStatus_Fail;
876     }
877 
878     /* Prepare to switch to increment mode */
879     base->MER |= RTC_MER_MCE_MASK;
880     /* Write anything so the counter increments*/
881     base->MCLR = 1U;
882 
883     return kStatus_Success;
884 }
885 
886 #endif /* FSL_FEATURE_RTC_HAS_MONOTONIC */
887