1 /**
2  * @file xmc4_rtc.c
3  * @date 2017-08-04
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  * 2016-03-09:
43  *     - Optimize write only registers
44  *
45  * 2017-08-04:
46  *     - Changed XMC_RTC_EnableHibernationWakeUp() and XMC_RTC_DisableHibernationWakeUpcheck()
47  *       Check SCU_MIRRSTS to ensure that no transfer over serial interface is pending to the RTC_CTR register
48  *
49  * @endcond
50  *
51  */
52 
53 /**
54  * @brief RTC driver for XMC microcontroller family.
55  *
56  */
57 
58 /*********************************************************************************************************************
59  * HEADER FILES
60  *********************************************************************************************************************/
61 #include <xmc_rtc.h>
62 
63 #if UC_FAMILY == XMC4
64 #include <xmc_scu.h>
65 
66 /*********************************************************************************************************************
67  * API IMPLEMENTATION
68  *********************************************************************************************************************/
69 
70 /*
71  * Enables RTC peripheral for programming its registers
72  */
XMC_RTC_Enable(void)73 void XMC_RTC_Enable(void)
74 {
75   XMC_SCU_HIB_EnableHibernateDomain();
76 }
77 
78 /*
79  * Disables RTC peripheral for programming its registers
80  */
XMC_RTC_Disable(void)81 void XMC_RTC_Disable(void)
82 {
83   /*
84    * Empty because disabling the hibernate
85    * domain is not done intentionally.
86    */
87 }
88 
89 /*
90  * Checks RTC peripheral is enabled for programming to its registers
91  */
XMC_RTC_IsEnabled(void)92 bool XMC_RTC_IsEnabled(void)
93 {
94   return XMC_SCU_HIB_IsHibernateDomainEnabled();
95 }
96 
97 /*
98  * Initialize the RTC peripheral
99  */
XMC_RTC_Init(const XMC_RTC_CONFIG_t * const config)100 XMC_RTC_STATUS_t XMC_RTC_Init(const XMC_RTC_CONFIG_t *const config)
101 {
102   if (XMC_RTC_IsRunning() == false)
103   {
104     if (XMC_SCU_HIB_IsHibernateDomainEnabled() == false)
105     {
106       XMC_SCU_HIB_EnableHibernateDomain();
107     }
108 
109     XMC_RTC_SetPrescaler(config->prescaler);
110 
111     while ((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_TIM0_Msk) != 0U)
112     {
113       /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
114     }
115     RTC->TIM0 = config->time.raw0;
116 
117     while ((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_TIM1_Msk) != 0U)
118     {
119       /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
120     }
121     RTC->TIM1 = config->time.raw1;
122 
123     while ((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_ATIM0_Msk) != 0U)
124     {
125       /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
126     }
127     RTC->ATIM0 = config->alarm.raw0;
128 
129     while ((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_ATIM1_Msk) != 0U)
130     {
131       /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
132     }
133     RTC->ATIM1 = config->alarm.raw1;
134   }
135   return XMC_RTC_STATUS_OK;
136 }
137 
138 /*
139  * Enable RTC periodic and alarm event(s)
140  */
XMC_RTC_EnableEvent(const uint32_t event)141 void XMC_RTC_EnableEvent(const uint32_t event)
142 {
143   while ((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_MSKSR_Msk) != 0U)
144   {
145     /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
146   }
147   RTC->MSKSR |= event;
148 }
149 
150 /*
151  * Disable RTC periodic and alarm event(s)
152  */
XMC_RTC_DisableEvent(const uint32_t event)153 void XMC_RTC_DisableEvent(const uint32_t event)
154 {
155   while ((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_MSKSR_Msk) != 0U)
156   {
157     /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
158   }
159   RTC->MSKSR &= ~event;
160 }
161 
162 /*
163  * Clear RTC periodic and alarm event(s)
164  */
XMC_RTC_ClearEvent(const uint32_t event)165 void XMC_RTC_ClearEvent(const uint32_t event)
166 {
167   while ((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_CLRSR_Msk) != 0U)
168   {
169     /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
170   }
171   RTC->CLRSR = event;
172 }
173 
XMC_RTC_EnableHibernationWakeUp(const uint32_t event)174 void XMC_RTC_EnableHibernationWakeUp(const uint32_t event)
175 {
176   while((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_CTR_Msk) != 0U)
177   {
178     /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
179   }
180   RTC->CTR |= event;
181 }
182 
XMC_RTC_DisableHibernationWakeUp(const uint32_t event)183 void XMC_RTC_DisableHibernationWakeUp(const uint32_t event)
184 {
185   while((XMC_SCU_GetMirrorStatus() & SCU_GENERAL_MIRRSTS_RTC_CTR_Msk) != 0U)
186   {
187     /* check SCU_MIRRSTS to ensure that no transfer over serial interface is pending */
188   }
189   RTC->CTR &= ~event;
190 }
191 
192 #endif /* UC_FAMILY == XMC4 */
193