1 /**
2  * @file xmc1_rtc.c
3  * @date 2016-03-09
4  *
5  * @cond
6  *********************************************************************************************************************
7  * XMClib v2.1.24 - XMC Peripheral Driver Library
8  *
9  * Copyright (c) 2015-2019, Infineon Technologies AG
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without modification,are permitted provided that the
13  * following conditions are met:
14  *
15  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
16  * disclaimer.
17  *
18  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
19  * disclaimer in the documentation and/or other materials provided with the distribution.
20  *
21  * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * To improve the quality of the software, users are encouraged to share modifications, enhancements or bug fixes with
33  * Infineon Technologies AG dave@infineon.com).
34  *********************************************************************************************************************
35  *
36  * Change History
37  * --------------
38  *
39  * 2015-02-20:
40  *     - Initial <br>
41  *
42  * 2015-05-20:
43  *     - XMC_RTC_Init() function is modified
44  *       by adding RTC running condition check
45  *
46  * 2016-03-09:
47  *     - Optimize write only registers
48  *
49  * @endcond
50  *
51  */
52 
53 
54 /**
55  *
56  * @brief RTC driver for XMC microcontroller family.
57  *
58  */
59 
60 /*********************************************************************************************************************
61  * HEADER FILES
62  *********************************************************************************************************************/
63 #include <xmc_rtc.h>
64 
65 #if UC_FAMILY == XMC1
66 #include <xmc_scu.h>
67 
68 /*********************************************************************************************************************
69  * API IMPLEMENTATION
70  *********************************************************************************************************************/
71 /*
72  * Initialize the RTC peripheral
73  */
XMC_RTC_Init(const XMC_RTC_CONFIG_t * const config)74 XMC_RTC_STATUS_t XMC_RTC_Init(const XMC_RTC_CONFIG_t *const config)
75 {
76   if (XMC_RTC_IsRunning() == false)
77   {
78     if (XMC_RTC_IsEnabled() == false)
79     {
80       XMC_RTC_Enable();
81 	}
82 
83     XMC_RTC_SetPrescaler(config->prescaler);
84 
85 	while ((XMC_SCU_GetMirrorStatus() & (SCU_GENERAL_MIRRSTS_RTC_TIM0_Msk | SCU_GENERAL_MIRRSTS_RTC_TIM1_Msk)) != 0U)
86 	{
87       /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
88     }
89     RTC->TIM0 = config->time.raw0;
90     RTC->TIM1 = config->time.raw1;
91 
92     while ((XMC_SCU_GetMirrorStatus() & (SCU_GENERAL_MIRRSTS_RTC_ATIM0_Msk | SCU_GENERAL_MIRRSTS_RTC_ATIM1_Msk)) != 0U)
93     {
94       /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
95     }
96     RTC->ATIM0 = config->alarm.raw0;
97     RTC->ATIM1 = config->alarm.raw1;
98   }
99   return XMC_RTC_STATUS_OK;
100 }
101 
102 /*
103  * Ungates a clock node for RTC
104  */
XMC_RTC_Enable(void)105 void XMC_RTC_Enable(void)
106 {
107   XMC_SCU_CLOCK_UngatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_RTC);
108 }
109 
110 /*
111  * Gates a clock node for RTC
112  */
XMC_RTC_Disable(void)113 void XMC_RTC_Disable(void)
114 {
115   XMC_SCU_CLOCK_GatePeripheralClock(XMC_SCU_PERIPHERAL_CLOCK_RTC);
116 }
117 
118 /*
119  * Suspends RTC function during CPU HALT mode
120  */
XMC_RTC_SetDebugMode(const XMC_RTC_DEBUG_MODE_t debug_mode)121 void XMC_RTC_SetDebugMode(const XMC_RTC_DEBUG_MODE_t debug_mode)
122 {
123   uint32_t regval;
124   regval = (RTC->CTR & (uint32_t)~RTC_CTR_SUS_Msk);
125   RTC->CTR = (regval | (uint32_t)debug_mode);
126 }
127 
128 /*
129  * Enable RTC periodic and alarm event(s)
130  */
XMC_RTC_EnableEvent(const uint32_t event)131 void XMC_RTC_EnableEvent(const uint32_t event)
132 {
133   RTC->MSKSR |= event;
134 }
135 
136 /*
137  * Disable RTC periodic and alarm event(s)
138  */
XMC_RTC_DisableEvent(const uint32_t event)139 void XMC_RTC_DisableEvent(const uint32_t event)
140 {
141   RTC->MSKSR &= ~event;
142 }
143 
144 /*
145  * Clear RTC periodic and alarm event(s)
146  */
XMC_RTC_ClearEvent(const uint32_t event)147 void XMC_RTC_ClearEvent(const uint32_t event)
148 {
149   RTC->CLRSR = event;
150 }
151 
152 /*
153  * Checks RTC peripheral is enabled for programming to its registers
154  */
XMC_RTC_IsEnabled(void)155 bool XMC_RTC_IsEnabled(void)
156 {
157   return !XMC_SCU_CLOCK_IsPeripheralClockGated(XMC_SCU_PERIPHERAL_CLOCK_RTC);
158 }
159 
160 #endif /* UC_FAMILY == XMC1 */
161