1 /*
2 * Copyright (c) 2014 - 2020, 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 /** @brief Macro for getting the number of compare channels available in a given RTC instance. */
49 #define NRF_RTC_CC_CHANNEL_COUNT(id) NRFX_CONCAT_3(RTC, id, _CC_NUM)
50
51 /** @brief Input frequency of the RTC instance. */
52 #define RTC_INPUT_FREQ 32768
53
54 /** @brief Macro for converting expected frequency to prescaler setting. */
55 #define RTC_FREQ_TO_PRESCALER(FREQ) (uint16_t)(((RTC_INPUT_FREQ) / (FREQ)) - 1)
56
57 /** @brief Macro for trimming values to the RTC bit width. */
58 #define RTC_WRAP(val) ((val) & RTC_COUNTER_COUNTER_Msk)
59
60 /** @brief Macro for creating the interrupt bitmask for the specified compare channel. */
61 #define RTC_CHANNEL_INT_MASK(ch) ((uint32_t)(NRF_RTC_INT_COMPARE0_MASK) << (ch))
62
63 /** @brief Macro for obtaining the compare event for the specified channel. */
64 #define RTC_CHANNEL_EVENT_ADDR(ch) (nrf_rtc_event_t)((NRF_RTC_EVENT_COMPARE_0) + (ch) * sizeof(uint32_t))
65
66
67 /** @brief RTC tasks. */
68 typedef enum
69 {
70 NRF_RTC_TASK_START = offsetof(NRF_RTC_Type,TASKS_START), /**< Start. */
71 NRF_RTC_TASK_STOP = offsetof(NRF_RTC_Type,TASKS_STOP), /**< Stop. */
72 NRF_RTC_TASK_CLEAR = offsetof(NRF_RTC_Type,TASKS_CLEAR), /**< Clear. */
73 NRF_RTC_TASK_TRIGGER_OVERFLOW = offsetof(NRF_RTC_Type,TASKS_TRIGOVRFLW),/**< Trigger overflow. */
74 } nrf_rtc_task_t;
75
76 /** @brief RTC events. */
77 typedef enum
78 {
79 NRF_RTC_EVENT_TICK = offsetof(NRF_RTC_Type,EVENTS_TICK), /**< Tick event. */
80 NRF_RTC_EVENT_OVERFLOW = offsetof(NRF_RTC_Type,EVENTS_OVRFLW), /**< Overflow event. */
81 NRF_RTC_EVENT_COMPARE_0 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[0]), /**< Compare 0 event. */
82 NRF_RTC_EVENT_COMPARE_1 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[1]), /**< Compare 1 event. */
83 NRF_RTC_EVENT_COMPARE_2 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[2]), /**< Compare 2 event. */
84 NRF_RTC_EVENT_COMPARE_3 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[3]) /**< Compare 3 event. */
85 } nrf_rtc_event_t;
86
87 /** @brief RTC interrupts. */
88 typedef enum
89 {
90 NRF_RTC_INT_TICK_MASK = RTC_INTENSET_TICK_Msk, /**< RTC interrupt from tick event. */
91 NRF_RTC_INT_OVERFLOW_MASK = RTC_INTENSET_OVRFLW_Msk, /**< RTC interrupt from overflow event. */
92 NRF_RTC_INT_COMPARE0_MASK = RTC_INTENSET_COMPARE0_Msk, /**< RTC interrupt from compare event on channel 0. */
93 NRF_RTC_INT_COMPARE1_MASK = RTC_INTENSET_COMPARE1_Msk, /**< RTC interrupt from compare event on channel 1. */
94 NRF_RTC_INT_COMPARE2_MASK = RTC_INTENSET_COMPARE2_Msk, /**< RTC interrupt from compare event on channel 2. */
95 NRF_RTC_INT_COMPARE3_MASK = RTC_INTENSET_COMPARE3_Msk /**< RTC interrupt from compare event on channel 3. */
96 } nrf_rtc_int_t;
97
98
99 /**
100 * @brief Function for setting a compare value for a channel.
101 *
102 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
103 * @param[in] ch Channel.
104 * @param[in] cc_val Compare value to be set.
105 */
106 NRF_STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_reg, uint32_t ch, uint32_t cc_val);
107
108 /**
109 * @brief Function for returning the compare value for a channel.
110 *
111 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
112 * @param[in] ch Channel.
113 *
114 * @return COMPARE[ch] value.
115 */
116 NRF_STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type const * p_reg, uint32_t ch);
117
118 /**
119 * @brief Function for enabling interrupts.
120 *
121 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
122 * @param[in] mask Interrupt mask to be enabled.
123 */
124 NRF_STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_reg, uint32_t mask);
125
126 /**
127 * @brief Function for disabling interrupts.
128 *
129 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
130 * @param[in] mask Interrupt mask to be disabled.
131 */
132 NRF_STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_reg, uint32_t mask);
133
134 /**
135 * @brief Function for checking if the specified interrupts are enabled.
136 *
137 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
138 * @param[in] mask Mask of interrupts to be checked.
139 *
140 * @return Mask of enabled interrupts.
141 */
142 NRF_STATIC_INLINE uint32_t nrf_rtc_int_enable_check(NRF_RTC_Type const * p_reg, uint32_t mask);
143
144 #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
145 /**
146 * @brief Function for setting the subscribe configuration for a given
147 * RTC task.
148 *
149 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
150 * @param[in] task Task for which to set the configuration.
151 * @param[in] channel Channel through which to subscribe events.
152 */
153 NRF_STATIC_INLINE void nrf_rtc_subscribe_set(NRF_RTC_Type * p_reg,
154 nrf_rtc_task_t task,
155 uint8_t channel);
156
157 /**
158 * @brief Function for clearing the subscribe configuration for a given
159 * RTC task.
160 *
161 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
162 * @param[in] task Task for which to clear the configuration.
163 */
164 NRF_STATIC_INLINE void nrf_rtc_subscribe_clear(NRF_RTC_Type * p_reg,
165 nrf_rtc_task_t task);
166
167 /**
168 * @brief Function for setting the publish configuration for a given
169 * RTC event.
170 *
171 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
172 * @param[in] event Event for which to set the configuration.
173 * @param[in] channel Channel through which to publish the event.
174 */
175 NRF_STATIC_INLINE void nrf_rtc_publish_set(NRF_RTC_Type * p_reg,
176 nrf_rtc_event_t event,
177 uint8_t channel);
178
179 /**
180 * @brief Function for clearing the publish configuration for a given
181 * RTC event.
182 *
183 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
184 * @param[in] event Event for which to clear the configuration.
185 */
186 NRF_STATIC_INLINE void nrf_rtc_publish_clear(NRF_RTC_Type * p_reg,
187 nrf_rtc_event_t event);
188 #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
189
190 /**
191 * @brief Function for retrieving the state of the RTC event.
192 *
193 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
194 * @param[in] event Event to be checked.
195 *
196 * @retval true The event has been generated.
197 * @retval false The event has not been generated.
198 */
199 NRF_STATIC_INLINE bool nrf_rtc_event_check(NRF_RTC_Type const * p_reg, nrf_rtc_event_t event);
200
201 /**
202 * @brief Function for clearing an event.
203 *
204 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
205 * @param[in] event Event to be cleared.
206 */
207 NRF_STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_reg, nrf_rtc_event_t event);
208
209 /**
210 * @brief Function for returning a counter value.
211 *
212 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
213 *
214 * @return Counter value.
215 */
216 NRF_STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type const * p_reg);
217
218 /**
219 * @brief Function for setting a prescaler value.
220 *
221 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
222 * @param[in] val Value to set the prescaler to.
223 */
224 NRF_STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_reg, uint32_t val);
225
226 /**
227 * @brief Function for getting a prescaler value.
228 *
229 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
230 *
231 * @return Prescaler value.
232 */
233 NRF_STATIC_INLINE uint32_t nrf_rtc_prescaler_get(NRF_RTC_Type const * p_reg);
234
235 /**
236 * @brief Function for returning the address of an event.
237 *
238 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
239 * @param[in] event Requested event.
240 *
241 * @return Address of the requested event register.
242 */
243 NRF_STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type const * p_reg,
244 nrf_rtc_event_t event);
245
246 /**
247 * @brief Function for returning the address of a task.
248 *
249 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
250 * @param[in] task Requested task.
251 *
252 * @return Address of the requested task register.
253 */
254 NRF_STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type const * p_reg,
255 nrf_rtc_task_t task);
256
257 /**
258 * @brief Function for starting a task.
259 *
260 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
261 * @param[in] task Requested task.
262 */
263 NRF_STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_reg, nrf_rtc_task_t task);
264
265 /**
266 * @brief Function for enabling events.
267 *
268 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
269 * @param[in] mask Mask of event flags to be enabled.
270 */
271 NRF_STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_reg, uint32_t mask);
272
273 /**
274 * @brief Function for disabling an event.
275 *
276 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
277 * @param[in] event Requested event.
278 */
279 NRF_STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_reg, uint32_t event);
280
281 /**
282 * @brief Function for getting the COMPARE event associated with the specified compare channel.
283 *
284 * @param[in] index Compare channel index.
285 *
286 * @return Requested COMPARE event.
287 */
288 NRF_STATIC_INLINE nrf_rtc_event_t nrf_rtc_compare_event_get(uint8_t index);
289
290 #ifndef NRF_DECLARE_ONLY
291
nrf_rtc_cc_set(NRF_RTC_Type * p_reg,uint32_t ch,uint32_t cc_val)292 NRF_STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_reg, uint32_t ch, uint32_t cc_val)
293 {
294 p_reg->CC[ch] = cc_val;
295 }
296
nrf_rtc_cc_get(NRF_RTC_Type const * p_reg,uint32_t ch)297 NRF_STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type const * p_reg, uint32_t ch)
298 {
299 return p_reg->CC[ch];
300 }
301
nrf_rtc_int_enable(NRF_RTC_Type * p_reg,uint32_t mask)302 NRF_STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_reg, uint32_t mask)
303 {
304 p_reg->INTENSET = mask;
305 }
306
nrf_rtc_int_disable(NRF_RTC_Type * p_reg,uint32_t mask)307 NRF_STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_reg, uint32_t mask)
308 {
309 p_reg->INTENCLR = mask;
310 }
311
nrf_rtc_int_enable_check(NRF_RTC_Type const * p_reg,uint32_t mask)312 NRF_STATIC_INLINE uint32_t nrf_rtc_int_enable_check(NRF_RTC_Type const * p_reg, uint32_t mask)
313 {
314 return p_reg->INTENSET & mask;
315 }
316
317 #if defined(DPPI_PRESENT)
nrf_rtc_subscribe_set(NRF_RTC_Type * p_reg,nrf_rtc_task_t task,uint8_t channel)318 NRF_STATIC_INLINE void nrf_rtc_subscribe_set(NRF_RTC_Type * p_reg,
319 nrf_rtc_task_t task,
320 uint8_t channel)
321 {
322 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
323 ((uint32_t)channel | RTC_SUBSCRIBE_START_EN_Msk);
324 }
325
nrf_rtc_subscribe_clear(NRF_RTC_Type * p_reg,nrf_rtc_task_t task)326 NRF_STATIC_INLINE void nrf_rtc_subscribe_clear(NRF_RTC_Type * p_reg,
327 nrf_rtc_task_t task)
328 {
329 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
330 }
331
nrf_rtc_publish_set(NRF_RTC_Type * p_reg,nrf_rtc_event_t event,uint8_t channel)332 NRF_STATIC_INLINE void nrf_rtc_publish_set(NRF_RTC_Type * p_reg,
333 nrf_rtc_event_t event,
334 uint8_t channel)
335 {
336 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
337 ((uint32_t)channel | RTC_PUBLISH_TICK_EN_Msk);
338 }
339
nrf_rtc_publish_clear(NRF_RTC_Type * p_reg,nrf_rtc_event_t event)340 NRF_STATIC_INLINE void nrf_rtc_publish_clear(NRF_RTC_Type * p_reg,
341 nrf_rtc_event_t event)
342 {
343 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
344 }
345 #endif // defined(DPPI_PRESENT)
346
nrf_rtc_event_check(NRF_RTC_Type const * p_reg,nrf_rtc_event_t event)347 NRF_STATIC_INLINE bool nrf_rtc_event_check(NRF_RTC_Type const * p_reg, nrf_rtc_event_t event)
348 {
349 return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
350 }
351
nrf_rtc_event_clear(NRF_RTC_Type * p_reg,nrf_rtc_event_t event)352 NRF_STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_reg, nrf_rtc_event_t event)
353 {
354 *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0;
355 nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
356 }
357
nrf_rtc_counter_get(NRF_RTC_Type const * p_reg)358 NRF_STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type const * p_reg)
359 {
360 return p_reg->COUNTER;
361 }
362
nrf_rtc_prescaler_set(NRF_RTC_Type * p_reg,uint32_t val)363 NRF_STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_reg, uint32_t val)
364 {
365 NRFX_ASSERT(val <= (RTC_PRESCALER_PRESCALER_Msk >> RTC_PRESCALER_PRESCALER_Pos));
366 p_reg->PRESCALER = val;
367 }
368
nrf_rtc_prescaler_get(NRF_RTC_Type const * p_reg)369 NRF_STATIC_INLINE uint32_t nrf_rtc_prescaler_get(NRF_RTC_Type const * p_reg)
370 {
371 return p_reg->PRESCALER;
372 }
373
nrf_rtc_event_address_get(NRF_RTC_Type const * p_reg,nrf_rtc_event_t event)374 NRF_STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type const * p_reg,
375 nrf_rtc_event_t event)
376 {
377 return (uint32_t)p_reg + event;
378 }
379
nrf_rtc_task_address_get(NRF_RTC_Type const * p_reg,nrf_rtc_task_t task)380 NRF_STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type const * p_reg,
381 nrf_rtc_task_t task)
382 {
383 return (uint32_t)p_reg + task;
384 }
385
nrf_rtc_task_trigger(NRF_RTC_Type * p_reg,nrf_rtc_task_t task)386 NRF_STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_reg, nrf_rtc_task_t task)
387 {
388 *(__IO uint32_t *)((uint32_t)p_reg + task) = 1;
389 }
390
nrf_rtc_event_enable(NRF_RTC_Type * p_reg,uint32_t mask)391 NRF_STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_reg, uint32_t mask)
392 {
393 p_reg->EVTENSET = mask;
394 }
395
nrf_rtc_event_disable(NRF_RTC_Type * p_reg,uint32_t mask)396 NRF_STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_reg, uint32_t mask)
397 {
398 p_reg->EVTENCLR = mask;
399 }
400
nrf_rtc_compare_event_get(uint8_t index)401 NRF_STATIC_INLINE nrf_rtc_event_t nrf_rtc_compare_event_get(uint8_t index)
402 {
403 return (nrf_rtc_event_t)NRFX_OFFSETOF(NRF_RTC_Type, EVENTS_COMPARE[index]);
404 }
405
406 #endif // NRF_DECLARE_ONLY
407
408 /** @} */
409
410 #ifdef __cplusplus
411 }
412 #endif
413
414 #endif /* NRF_RTC_H */
415