1 /*
2 * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef NRF_RTC_H
33 #define NRF_RTC_H
34
35 #include <nrfx.h>
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42 * @defgroup nrf_rtc_hal RTC HAL
43 * @{
44 * @ingroup nrf_rtc
45 * @brief Hardware access layer for managing the Real Time Counter (RTC) peripheral.
46 */
47
48 /**
49 * @brief Macro for getting the number of compare channels available
50 * in a given RTC instance.
51 */
52
53 #define NRF_RTC_CC_CHANNEL_COUNT(id) NRFX_CONCAT_3(RTC, id, _CC_NUM)
54
55 #define RTC_INPUT_FREQ 32768 /**< Input frequency of the RTC instance. */
56
57 /**
58 * @brief Macro for converting expected frequency to prescaler setting.
59 */
60 #define RTC_FREQ_TO_PRESCALER(FREQ) (uint16_t)(((RTC_INPUT_FREQ) / (FREQ)) - 1)
61
62 /**< Macro for wrapping values to RTC capacity. */
63 #define RTC_WRAP(val) ((val) & RTC_COUNTER_COUNTER_Msk)
64
65 #define RTC_CHANNEL_INT_MASK(ch) ((uint32_t)(NRF_RTC_INT_COMPARE0_MASK) << (ch))
66 #define RTC_CHANNEL_EVENT_ADDR(ch) (nrf_rtc_event_t)((NRF_RTC_EVENT_COMPARE_0) + (ch) * sizeof(uint32_t))
67 /**
68 * @enum nrf_rtc_task_t
69 * @brief RTC tasks.
70 */
71 typedef enum
72 {
73 /*lint -save -e30*/
74 NRF_RTC_TASK_START = offsetof(NRF_RTC_Type,TASKS_START), /**< Start. */
75 NRF_RTC_TASK_STOP = offsetof(NRF_RTC_Type,TASKS_STOP), /**< Stop. */
76 NRF_RTC_TASK_CLEAR = offsetof(NRF_RTC_Type,TASKS_CLEAR), /**< Clear. */
77 NRF_RTC_TASK_TRIGGER_OVERFLOW = offsetof(NRF_RTC_Type,TASKS_TRIGOVRFLW),/**< Trigger overflow. */
78 /*lint -restore*/
79 } nrf_rtc_task_t;
80
81 /**
82 * @enum nrf_rtc_event_t
83 * @brief RTC events.
84 */
85 typedef enum
86 {
87 /*lint -save -e30*/
88 NRF_RTC_EVENT_TICK = offsetof(NRF_RTC_Type,EVENTS_TICK), /**< Tick event. */
89 NRF_RTC_EVENT_OVERFLOW = offsetof(NRF_RTC_Type,EVENTS_OVRFLW), /**< Overflow event. */
90 NRF_RTC_EVENT_COMPARE_0 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[0]), /**< Compare 0 event. */
91 NRF_RTC_EVENT_COMPARE_1 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[1]), /**< Compare 1 event. */
92 NRF_RTC_EVENT_COMPARE_2 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[2]), /**< Compare 2 event. */
93 NRF_RTC_EVENT_COMPARE_3 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[3]) /**< Compare 3 event. */
94 /*lint -restore*/
95 } nrf_rtc_event_t;
96
97 /**
98 * @enum nrf_rtc_int_t
99 * @brief RTC interrupts.
100 */
101 typedef enum
102 {
103 NRF_RTC_INT_TICK_MASK = RTC_INTENSET_TICK_Msk, /**< RTC interrupt from tick event. */
104 NRF_RTC_INT_OVERFLOW_MASK = RTC_INTENSET_OVRFLW_Msk, /**< RTC interrupt from overflow event. */
105 NRF_RTC_INT_COMPARE0_MASK = RTC_INTENSET_COMPARE0_Msk, /**< RTC interrupt from compare event on channel 0. */
106 NRF_RTC_INT_COMPARE1_MASK = RTC_INTENSET_COMPARE1_Msk, /**< RTC interrupt from compare event on channel 1. */
107 NRF_RTC_INT_COMPARE2_MASK = RTC_INTENSET_COMPARE2_Msk, /**< RTC interrupt from compare event on channel 2. */
108 NRF_RTC_INT_COMPARE3_MASK = RTC_INTENSET_COMPARE3_Msk /**< RTC interrupt from compare event on channel 3. */
109 } nrf_rtc_int_t;
110
111 /**@brief Function for setting a compare value for a channel.
112 *
113 * @param[in] p_rtc Pointer to the peripheral registers structure.
114 * @param[in] ch Channel.
115 * @param[in] cc_val Compare value to set.
116 */
117 __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_rtc, uint32_t ch, uint32_t cc_val);
118
119 /**@brief Function for returning the compare value for a channel.
120 *
121 * @param[in] p_rtc Pointer to the peripheral registers structure.
122 * @param[in] ch Channel.
123 *
124 * @return COMPARE[ch] value.
125 */
126 __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_rtc, uint32_t ch);
127
128 /**@brief Function for enabling interrupts.
129 *
130 * @param[in] p_rtc Pointer to the peripheral registers structure.
131 * @param[in] mask Interrupt mask to be enabled.
132 */
133 __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_rtc, uint32_t mask);
134
135 /**@brief Function for disabling interrupts.
136 *
137 * @param[in] p_rtc Pointer to the peripheral registers structure.
138 * @param[in] mask Interrupt mask to be disabled.
139 */
140 __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_rtc, uint32_t mask);
141
142 /**@brief Function for checking if interrupts are enabled.
143 *
144 * @param[in] p_rtc Pointer to the peripheral registers structure.
145 * @param[in] mask Mask of interrupt flags to check.
146 *
147 * @return Mask with enabled interrupts.
148 */
149 __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_rtc, uint32_t mask);
150
151 /**@brief Function for returning the status of currently enabled interrupts.
152 *
153 * @param[in] p_rtc Pointer to the peripheral registers structure.
154 *
155 * @return Value in INTEN register.
156 */
157 __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_rtc);
158
159 /**@brief Function for checking if an event is pending.
160 *
161 * @param[in] p_rtc Pointer to the peripheral registers structure.
162 * @param[in] event Address of the event.
163 *
164 * @return Mask of pending events.
165 */
166 __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
167
168 /**@brief Function for clearing an event.
169 *
170 * @param[in] p_rtc Pointer to the peripheral registers structure.
171 * @param[in] event Event to clear.
172 */
173 __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
174
175 /**@brief Function for returning a counter value.
176 *
177 * @param[in] p_rtc Pointer to the peripheral registers structure.
178 *
179 * @return Counter value.
180 */
181 __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_rtc);
182
183 /**@brief Function for setting a prescaler value.
184 *
185 * @param[in] p_rtc Pointer to the peripheral registers structure.
186 * @param[in] val Value to set the prescaler to.
187 */
188 __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_rtc, uint32_t val);
189
190 /**@brief Function for returning the address of an event.
191 *
192 * @param[in] p_rtc Pointer to the peripheral registers structure.
193 * @param[in] event Requested event.
194 *
195 * @return Address of the requested event register.
196 */
197 __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
198
199 /**@brief Function for returning the address of a task.
200 *
201 * @param[in] p_rtc Pointer to the peripheral registers structure.
202 * @param[in] task Requested task.
203 *
204 * @return Address of the requested task register.
205 */
206 __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task);
207
208 /**@brief Function for starting a task.
209 *
210 * @param[in] p_rtc Pointer to the peripheral registers structure.
211 * @param[in] task Requested task.
212 */
213 __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task);
214
215 /**@brief Function for enabling events.
216 *
217 * @param[in] p_rtc Pointer to the peripheral registers structure.
218 * @param[in] mask Mask of event flags to enable.
219 */
220 __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_rtc, uint32_t mask);
221
222 /**@brief Function for disabling an event.
223 *
224 * @param[in] p_rtc Pointer to the peripheral registers structure.
225 * @param[in] event Requested event.
226 */
227 __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_rtc, uint32_t event);
228
229 #ifndef SUPPRESS_INLINE_IMPLEMENTATION
230
nrf_rtc_cc_set(NRF_RTC_Type * p_rtc,uint32_t ch,uint32_t cc_val)231 __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_rtc, uint32_t ch, uint32_t cc_val)
232 {
233 p_rtc->CC[ch] = cc_val;
234 }
235
nrf_rtc_cc_get(NRF_RTC_Type * p_rtc,uint32_t ch)236 __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_rtc, uint32_t ch)
237 {
238 return p_rtc->CC[ch];
239 }
240
nrf_rtc_int_enable(NRF_RTC_Type * p_rtc,uint32_t mask)241 __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_rtc, uint32_t mask)
242 {
243 p_rtc->INTENSET = mask;
244 }
245
nrf_rtc_int_disable(NRF_RTC_Type * p_rtc,uint32_t mask)246 __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_rtc, uint32_t mask)
247 {
248 p_rtc->INTENCLR = mask;
249 }
250
nrf_rtc_int_is_enabled(NRF_RTC_Type * p_rtc,uint32_t mask)251 __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_rtc, uint32_t mask)
252 {
253 return (p_rtc->INTENSET & mask);
254 }
255
nrf_rtc_int_get(NRF_RTC_Type * p_rtc)256 __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_rtc)
257 {
258 return p_rtc->INTENSET;
259 }
260
nrf_rtc_event_pending(NRF_RTC_Type * p_rtc,nrf_rtc_event_t event)261 __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
262 {
263 return *(volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event);
264 }
265
nrf_rtc_event_clear(NRF_RTC_Type * p_rtc,nrf_rtc_event_t event)266 __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
267 {
268 *((volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event)) = 0;
269 #if __CORTEX_M == 0x04
270 volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event));
271 (void)dummy;
272 #endif
273 }
274
nrf_rtc_counter_get(NRF_RTC_Type * p_rtc)275 __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_rtc)
276 {
277 return p_rtc->COUNTER;
278 }
279
nrf_rtc_prescaler_set(NRF_RTC_Type * p_rtc,uint32_t val)280 __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_rtc, uint32_t val)
281 {
282 NRFX_ASSERT(val <= (RTC_PRESCALER_PRESCALER_Msk >> RTC_PRESCALER_PRESCALER_Pos));
283 p_rtc->PRESCALER = val;
284 }
rtc_prescaler_get(NRF_RTC_Type * p_rtc)285 __STATIC_INLINE uint32_t rtc_prescaler_get(NRF_RTC_Type * p_rtc)
286 {
287 return p_rtc->PRESCALER;
288 }
289
nrf_rtc_event_address_get(NRF_RTC_Type * p_rtc,nrf_rtc_event_t event)290 __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
291 {
292 return (uint32_t)p_rtc + event;
293 }
294
nrf_rtc_task_address_get(NRF_RTC_Type * p_rtc,nrf_rtc_task_t task)295 __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task)
296 {
297 return (uint32_t)p_rtc + task;
298 }
299
nrf_rtc_task_trigger(NRF_RTC_Type * p_rtc,nrf_rtc_task_t task)300 __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task)
301 {
302 *(__IO uint32_t *)((uint32_t)p_rtc + task) = 1;
303 }
304
nrf_rtc_event_enable(NRF_RTC_Type * p_rtc,uint32_t mask)305 __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_rtc, uint32_t mask)
306 {
307 p_rtc->EVTENSET = mask;
308 }
nrf_rtc_event_disable(NRF_RTC_Type * p_rtc,uint32_t mask)309 __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_rtc, uint32_t mask)
310 {
311 p_rtc->EVTENCLR = mask;
312 }
313 #endif
314
315 /** @} */
316
317 #ifdef __cplusplus
318 }
319 #endif
320
321 #endif /* NRF_RTC_H */
322