S32 SDK
rtc_hal.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
33 #include "rtc_hal.h"
34 
35 /*******************************************************************************
36  * Code
37  ******************************************************************************/
38 
39 /*FUNCTION**********************************************************************
40  *
41  * Function Name : RTC_HAL_Init
42  * Description : This function initializes the RTC instance
43  * Return STATUS_SUCCESS if the operation was successful,
44  * STATUS_ERROR if at least one register is locked.
45  * Implements : RTC_HAL_Init_Activity
46  *END**************************************************************************/
48 {
49  status_t statusCode = STATUS_SUCCESS;
50 
51  /* Check if the registers are locked */
52  if ((base->LR & 0xFFu) != 0xFFu)
53  {
54  statusCode = STATUS_ERROR;
55  }
56  else
57  {
58  /* Set all registers to default values, except for RTC IER */
59  /* Disable all interrupts */
60  base->IER = 0UL;
61  /* Clear all flags and disable the counter */
62  base->SR = 0UL;
63  /* Set Time Seconds Registers to 1 to avoid triggering Time
64  * Invalid Interrupt
65  */
66  base->TSR = 1UL;
67  /* Clear Time Prescaler Register */
68  base->TPR = 0UL;
69  /* Clear Time Alarm Register */
70  base->TAR = 0UL;
71  /* Set Configuration Register to reset value */
72  base->CR = 0UL;
73  /* Set Lock Register to default value */
74  base->LR = 0xFFUL;
75 
76  /* Check if the configuration was successful */
77  if (RTC_HAL_GetTimeInvalidFlag(base) == true)
78  {
79  statusCode = STATUS_ERROR;
80  }
81  }
82  /* Return the exit code */
83  return statusCode;
84 }
85 
86 
87 /*FUNCTION**********************************************************************
88  *
89  * Function Name : RTC_HAL_Enable
90  * Description : This function enables the RTC counter
91  * Return STATUS_SUCCESS if the operation was successful, STATUS_ERROR
92  * if the counter is enabled.
93  * Implements : RTC_HAL_Enable_Activity
94  *END**************************************************************************/
96 {
97  status_t statusCode = STATUS_SUCCESS;
98  bool isCounterEnabled;
99 
100  /* Get the flags signaling if the counter is enabled */
101  isCounterEnabled = RTC_HAL_GetTimeCounterEnable(base);
102 
103  /* Check if the RTC counter is enabled */
104  if (isCounterEnabled)
105  {
106  statusCode = STATUS_ERROR;
107  }
108  else
109  {
110  /* Enable oscillator and seconds counter */
111  RTC_HAL_SetTimeCounterEnable(base, true);
112  }
113  /* Return the exit code */
114  return statusCode;
115 }
116 
117 /*FUNCTION**********************************************************************
118  *
119  * Function Name : RTC_HAL_Disable
120  * Description : This function disables the RTC counter
121  * Return STATUS_SUCCESS if the operation was successful, STATUS_ERROR
122  * if the counter was not disabled.
123  * Implements : RTC_HAL_Disable_Activity
124  *END**************************************************************************/
126 {
127  if (RTC_HAL_GetTimeCounterEnable(base) == true)
128  {
129  RTC_HAL_SetTimeCounterEnable(base, false);
130  }
131 
132  /* Read TCE bit to check if the counter is really disabled and return the
133  * corresponding result.
134  * - Error if the timer is still enabled (The register can be locked)
135  * - Success if the timer is disabled
136  */
138 }
139 
140 /*FUNCTION**********************************************************************
141  *
142  * Function Name : RTC_HAL_SetTimeSecondsRegister
143  * Description : This function along with SetTimePrescalerRegister will help
144  * you set the starting time at a specified value.
145  * The write will fail if the Time Counter is enabled and will return
146  * STATUS_ERROR, otherwise the return will be STATUS_SUCCESS
147  * Return : STATUS_SUCCESS if the write is succeeded or STATUS_ERROR if
148  * the counter is enabled.
149  * Implements : RTC_HAL_SetTimeSecondsRegister_Activity
150  *END**************************************************************************/
151 status_t RTC_HAL_SetTimeSecondsRegister(RTC_Type * const base, uint32_t seconds)
152 {
153  status_t statusCode = STATUS_SUCCESS;
154 
155  if (RTC_HAL_GetTimeCounterEnable(base) == true)
156  {
157  statusCode = STATUS_ERROR;
158  }
159  else
160  {
161  base->TSR = seconds;
162  statusCode = STATUS_SUCCESS;
163  }
164  /* Return the exit code */
165  return statusCode;
166 }
167 
168 /*FUNCTION**********************************************************************
169  *
170  * Function Name : RTC_HAL_SetTimePrescalerRegister
171  * Description : This function along with SetTimeSecondsRegister will help you set
172  * the starting time at a specified value.
173  * The write will fail if the Time Counter is enabled and will return
174  * STATUS_ERROR, otherwise the return will be STATUS_SUCCESS
175  * Return : STATUS_SUCCESS if the write is succeeded or STATUS_ERROR if
176  * the counter is enabled.
177  * Implements : RTC_HAL_SetTimePrescalerRegister_Activity
178  *END**************************************************************************/
180 {
181  status_t statusCode = STATUS_SUCCESS;
182 
183  if (RTC_HAL_GetTimeCounterEnable(base) == true)
184  {
185  statusCode = STATUS_ERROR;
186  }
187  else
188  {
189  uint32_t tmp = base->TPR;
190  tmp &= ~(RTC_TPR_TPR_MASK);
191  tmp |= RTC_TPR_TPR(value);
192  base->TPR = tmp;
193  statusCode = STATUS_SUCCESS;
194  }
195  /* Return the exit code */
196  return statusCode;
197 }
198 
199 /*FUNCTION**********************************************************************
200  *
201  * Function Name : RTC_HAL_ConfigureRegisterLock
202  * Description : This method will allow you to lock the registers. It will return
203  * STATUS_SUCCESS if the lock was successful or if the register
204  * was already locked, STATUS_ERROR if the Lock Register is
205  * already locked or if the registerToConfig parameter is not a
206  * valid register.
207  * Return : Status of the operation
208  * Implements : RTC_HAL_ConfigureRegisterLock_Activity
209  *END**************************************************************************/
211 {
212  status_t statusCode = STATUS_SUCCESS;
213 
214  /* Check if the Lock Register is already locked,
215  * if true, any other register lock status cannot
216  * be modified.
217  */
218  if (RTC_HAL_GetLockRegisterLock(base) == true)
219  {
220  statusCode = STATUS_ERROR;
221  }
222  else
223  {
224  /* If the Lock Register is not locked we can
225  * configure the register lock.
226  */
227  switch (registerToConfig)
228  {
229  case RTC_LOCK_REG_LOCK:
231  break;
232  case RTC_STATUS_REG_LOCK:
234  break;
235  case RTC_CTRL_REG_LOCK:
237  break;
238  case RTC_TCL_REG_LOCK:
240  break;
241  default:
242  /* If the register is not recognized, return error */
243  statusCode = STATUS_ERROR;
244  break;
245  }
246  }
247  /* Return the exit code */
248  return statusCode;
249 }
250 
251 /*FUNCTION**********************************************************************
252  *
253  * Function Name : RTC_HAL_IsRegisterLocked
254  * Description : This method will get the register lock status
255  * Return : True if the register is locked, false if not
256  * Implements : RTC_HAL_IsRegisterLocked_Activity
257  *END**************************************************************************/
259 {
260  bool state = false;
261 
262  switch (reg)
263  {
264  case RTC_LOCK_REG_LOCK:
265  state = RTC_HAL_GetLockRegisterLock(base);
266  break;
267  case RTC_CTRL_REG_LOCK:
268  state = RTC_HAL_GetControlRegisterLock(base);
269  break;
270  case RTC_STATUS_REG_LOCK:
271  state = RTC_HAL_GetStatusRegisterLock(base);
272  break;
273  case RTC_TCL_REG_LOCK:
274  state = RTC_HAL_GetTimeCompensationLock(base);
275  break;
276  default:
277  /* This statement should not be reached */
278  break;
279  }
280  /* Return the exit code */
281  return state;
282 }
283 
284 /*FUNCTION**********************************************************************
285  *
286  * Function Name : RTC_HAL_ConfigureClockOut
287  * Description : This method will allow you to configure the RTC Clock out pin.
288  * It will return STATUS_SUCCESS if the configuration was successful
289  * STATUS_ERROR if the Control Register is locked.
290  * Return : Status of the operation
291  * Implements : RTC_HAL_ConfigureClockOut_Activity
292  *END**************************************************************************/
294 {
295  status_t statusCode = STATUS_SUCCESS;
296 
297  /*
298  * Check if the Control Register is already locked,
299  * if true, clock out configuration cannot be modified.
300  */
301  if (RTC_HAL_GetControlRegisterLock(base) == true)
302  {
303  statusCode = STATUS_ERROR;
304  }
305  else
306  {
307  switch (config)
308  {
309  case RTC_CLKOUT_DISABLED:
310  /* Disable the clock out pin */
311  base->CR &= ~RTC_CR_CPE_MASK;
312  break;
313  case RTC_CLKOUT_SRC_TSIC:
314  /* Select clock out source as Time Seconds Interrupt and enable the pin */
315  base->CR &= ~(RTC_CR_CPE_MASK | RTC_CR_CPS_MASK);
316  base->CR |= (RTC_CR_CPE(1U) | RTC_CR_CPS(0U));
317  break;
319  /* Select clock out source as the 32 KHz clock and enable the pin */
320  base->CR &= ~(RTC_CR_CPE_MASK | RTC_CR_CPS_MASK);
321  base->CR |= (RTC_CR_CPE(1U) | RTC_CR_CPS(1U));
322  break;
323  default:
324  /* This statement should not be reached */
325  break;
326  }
327  }
328  /* Return the exit code */
329  return statusCode;
330 }
331 
332 /*******************************************************************************
333  * EOF
334  ******************************************************************************/
status_t RTC_HAL_ConfigureClockOut(RTC_Type *const base, rtc_clk_out_config_t config)
This function configures the Clock Out pin source.
Definition: rtc_hal.c:293
__IO uint32_t TAR
Definition: S32K144.h:8985
__IO uint32_t IER
Definition: S32K144.h:8990
static void RTC_HAL_LockRegisterLock(RTC_Type *const base)
Lock the Lock Register.
Definition: rtc_hal.h:618
status_t RTC_HAL_Disable(RTC_Type *const base)
Disable RTC instance counter.
Definition: rtc_hal.c:125
#define RTC_CR_CPS(x)
Definition: S32K144.h:9073
rtc_clk_out_config_t
RTC CLKOUT pin configuration Implements : rtc_clk_out_config_t_Class.
Definition: rtc_hal.h:68
__IO uint32_t TPR
Definition: S32K144.h:8984
#define RTC_CR_CPS_MASK
Definition: S32K144.h:9070
#define RTC_CR_CPE_MASK
Definition: S32K144.h:9078
__IO uint32_t CR
Definition: S32K144.h:8987
static bool RTC_HAL_GetLockRegisterLock(const RTC_Type *const base)
Get the Lock Register Lock state.
Definition: rtc_hal.h:634
static void RTC_HAL_ControlRegisterLock(RTC_Type *const base)
Lock the Control Register.
Definition: rtc_hal.h:698
static bool RTC_HAL_GetTimeInvalidFlag(const RTC_Type *const base)
Get Time Invalid flag.
Definition: rtc_hal.h:598
__IO uint32_t LR
Definition: S32K144.h:8989
static void RTC_HAL_StatusRegisterLock(RTC_Type *const base)
Lock the Status Register.
Definition: rtc_hal.h:650
static void RTC_HAL_SetTimeCounterEnable(RTC_Type *const base, bool enable)
Enable or disable the Time counter.
Definition: rtc_hal.h:523
status_t RTC_HAL_ConfigureRegisterLock(RTC_Type *const base, rtc_lock_register_select_t registerToConfig)
This function configures register lock status.
Definition: rtc_hal.c:210
#define RTC_CR_CPE(x)
Definition: S32K144.h:9081
__IO uint32_t SR
Definition: S32K144.h:8988
#define RTC_TPR_TPR(x)
Definition: S32K144.h:9034
__IO uint32_t TSR
Definition: S32K144.h:8983
#define RTC_TPR_TPR_MASK
Definition: S32K144.h:9031
status_t RTC_HAL_Enable(RTC_Type *const base)
Enable RTC instance counter.
Definition: rtc_hal.c:95
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
bool RTC_HAL_IsRegisterLocked(const RTC_Type *const base, rtc_lock_register_select_t reg)
This function gets register lock status.
Definition: rtc_hal.c:258
static bool RTC_HAL_GetControlRegisterLock(const RTC_Type *const base)
Get the Control Register Lock state.
Definition: rtc_hal.h:682
status_t RTC_HAL_SetTimeSecondsRegister(RTC_Type *const base, uint32_t seconds)
Set Time Seconds Register.
Definition: rtc_hal.c:151
static bool RTC_HAL_GetTimeCounterEnable(const RTC_Type *const base)
Get the Time Counter Enable value.
Definition: rtc_hal.h:540
static bool RTC_HAL_GetTimeCompensationLock(const RTC_Type *const base)
Get the TimeCompensation Register Lock state.
Definition: rtc_hal.h:714
status_t RTC_HAL_Init(RTC_Type *const base)
Initialize RTC instance.
Definition: rtc_hal.c:47
static bool RTC_HAL_GetStatusRegisterLock(const RTC_Type *const base)
Get the Status Register Lock state.
Definition: rtc_hal.h:666
static void RTC_HAL_TimeCompensationLock(RTC_Type *const base)
Lock the TimeCompensation Register.
Definition: rtc_hal.h:731
rtc_lock_register_select_t
RTC register lock Implements : rtc_lock_register_select_t_Class.
Definition: rtc_hal.h:89
status_t RTC_HAL_SetTimePrescalerRegister(RTC_Type *const base, uint16_t value)
Set Time Prescaler Register.
Definition: rtc_hal.c:179