1 /***************************************************************************//**
2  * @file
3  * @brief Real Time Counter (RTC) peripheral API
4  * @author Energy Micro AS
5  * @version 3.0.0
6  *******************************************************************************
7  * @section License
8  * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
9  *******************************************************************************
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute it
13  * freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must not
16  *    claim that you wrote the original software.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  *    misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
22  * obligation to support this Software. Energy Micro AS is providing the
23  * Software "AS IS", with no express or implied warranties of any kind,
24  * including, but not limited to, any implied warranties of merchantability
25  * or fitness for any particular purpose or warranties against infringement
26  * of any proprietary rights of a third party.
27  *
28  * Energy Micro AS will not be liable for any consequential, incidental, or
29  * special damages, or any other relief, or for any claim by any third party,
30  * arising from your use of this Software.
31  *
32  ******************************************************************************/
33 #ifndef __EM_RTC_H
34 #define __EM_RTC_H
35 
36 #include <stdbool.h>
37 #include "em_part.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /***************************************************************************//**
44  * @addtogroup EM_Library
45  * @{
46  ******************************************************************************/
47 
48 /***************************************************************************//**
49  * @addtogroup RTC
50  * @{
51  ******************************************************************************/
52 
53 /*******************************************************************************
54  *******************************   STRUCTS   ***********************************
55  ******************************************************************************/
56 
57 /** RTC initialization structure. */
58 typedef struct
59 {
60   bool enable;   /**< Start counting when init completed. */
61   bool debugRun; /**< Counter shall keep running during debug halt. */
62   bool comp0Top; /**< Use compare register 0 as max count value. */
63 } RTC_Init_TypeDef;
64 
65 /** Suggested default config for RTC init structure. */
66 #define RTC_INIT_DEFAULT                                       \
67   { true,    /* Start counting when init done */               \
68     false,   /* Disable updating during debug halt */          \
69     true     /* Restart counting from 0 when reaching COMP0 */ \
70   }
71 
72 
73 /*******************************************************************************
74  *****************************   PROTOTYPES   **********************************
75  ******************************************************************************/
76 
77 uint32_t RTC_CompareGet(unsigned int comp);
78 void RTC_CompareSet(unsigned int comp, uint32_t value);
79 
80 /***************************************************************************//**
81  * @brief
82  *   Get RTC counter value.
83  *
84  * @return
85  *   Current RTC counter value.
86  ******************************************************************************/
RTC_CounterGet(void)87 __STATIC_INLINE uint32_t RTC_CounterGet(void)
88 {
89   return(RTC->CNT);
90 }
91 
92 void RTC_CounterReset(void);
93 void RTC_Enable(bool enable);
94 void RTC_FreezeEnable(bool enable);
95 void RTC_Init(const RTC_Init_TypeDef *init);
96 
97 /***************************************************************************//**
98  * @brief
99  *   Clear one or more pending RTC interrupts.
100  *
101  * @param[in] flags
102  *   RTC interrupt sources to clear. Use a set of interrupt flags OR-ed
103  *   together to clear multiple interrupt sources for the RTC module
104  *   (RTC_IFS_nnn).
105  ******************************************************************************/
RTC_IntClear(uint32_t flags)106 __STATIC_INLINE void RTC_IntClear(uint32_t flags)
107 {
108   RTC->IFC = flags;
109 }
110 
111 
112 /***************************************************************************//**
113  * @brief
114  *   Disable one or more RTC interrupts.
115  *
116  * @param[in] flags
117  *   RTC interrupt sources to disable. Use a set of interrupt flags OR-ed
118  *   together to disable multiple interrupt sources for the RTC module
119  *   (RTC_IFS_nnn).
120  ******************************************************************************/
RTC_IntDisable(uint32_t flags)121 __STATIC_INLINE void RTC_IntDisable(uint32_t flags)
122 {
123   RTC->IEN &= ~(flags);
124 }
125 
126 
127 /***************************************************************************//**
128  * @brief
129  *   Enable one or more RTC interrupts.
130  *
131  * @note
132  *   Depending on the use, a pending interrupt may already be set prior to
133  *   enabling the interrupt. Consider using RTC_IntClear() prior to enabling
134  *   if such a pending interrupt should be ignored.
135  *
136  * @param[in] flags
137  *   RTC interrupt sources to enable. Use a set of interrupt flags OR-ed
138  *   together to set multiple interrupt sources for the RTC module
139  *   (RTC_IFS_nnn).
140  ******************************************************************************/
RTC_IntEnable(uint32_t flags)141 __STATIC_INLINE void RTC_IntEnable(uint32_t flags)
142 {
143   RTC->IEN |= flags;
144 }
145 
146 
147 /***************************************************************************//**
148  * @brief
149  *   Get pending RTC interrupt flags.
150  *
151  * @note
152  *   The event bits are not cleared by the use of this function.
153  *
154  * @return
155  *   Pending RTC interrupt sources. Returns a set of interrupt flags OR-ed
156  *   together for multiple interrupt sources in the RTC module (RTC_IFS_nnn).
157  ******************************************************************************/
RTC_IntGet(void)158 __STATIC_INLINE uint32_t RTC_IntGet(void)
159 {
160   return(RTC->IF);
161 }
162 
163 
164 /***************************************************************************//**
165  * @brief
166  *   Set one or more pending RTC interrupts from SW.
167  *
168  * @param[in] flags
169  *   RTC interrupt sources to set to pending. Use a set of interrupt flags
170  *   OR-ed together to set multiple interrupt sources for the RTC module
171  *   (RTC_IFS_nnn).
172  ******************************************************************************/
RTC_IntSet(uint32_t flags)173 __STATIC_INLINE void RTC_IntSet(uint32_t flags)
174 {
175   RTC->IFS = flags;
176 }
177 
178 void RTC_Reset(void);
179 
180 /** @} (end addtogroup RTC) */
181 /** @} (end addtogroup EM_Library) */
182 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif /* __EM_RTC_H */
188