1 /*
2 * Copyright (c) 2014 - 2025, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33 #ifndef NRF_QDEC_H__
34 #define NRF_QDEC_H__
35
36 #include <nrfx.h>
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #if !defined(NRF_QDEC0) && defined(NRF_QDEC)
43 #define NRF_QDEC0 NRF_QDEC
44 #endif
45
46 /**
47 * @defgroup nrf_qdec_hal QDEC HAL
48 * @{
49 * @ingroup nrf_qdec
50 * @brief Hardware access layer for managing the Quadrature Decoder (QDEC) peripheral.
51 */
52
53 /**
54 * @brief This value can be provided as a parameter for the @ref nrf_qdec_pins_set
55 * function call to specify that a LED signal shall not be use by the QDEC and
56 * connected to a physical pin.
57 */
58 #define NRF_QDEC_PIN_NOT_CONNECTED 0xFFFFFFFF
59
60 #if defined(QDEC_TASKS_RDCLRACC_TASKS_RDCLRACC_Msk) || defined(__NRFX_DOXYGEN__)
61 /** @brief Presence of the RDCLRACC task. */
62 #define NRF_QDEC_HAS_TASK_RDCLRACC 1
63 #else
64 #define NRF_QDEC_HAS_TASK_RDCLRACC 0
65 #endif
66
67 #if defined(QDEC_TASKS_RDCLRDBL_TASKS_RDCLRDBL_Msk) || defined(__NRFX_DOXYGEN__)
68 /** @brief Presence of the RDCLRDBL task. */
69 #define NRF_QDEC_HAS_TASK_RDCLRDBL 1
70 #else
71 #define NRF_QDEC_HAS_TASK_RDCLRDBL 0
72 #endif
73
74 #if defined(QDEC_EVENTS_DBLRDY_EVENTS_DBLRDY_Msk) || defined(__NRFX_DOXYGEN__)
75 /** @brief Presence of the DBLRDY event. */
76 #define NRF_QDEC_HAS_EVENT_DBLRDY 1
77 #else
78 #define NRF_QDEC_HAS_EVENT_DBLRDY 0
79 #endif
80
81 #if defined(QDEC_EVENTS_STOPPED_EVENTS_STOPPED_Msk) || defined(__NRFX_DOXYGEN__)
82 /** @brief Presence of the STOPPED event. */
83 #define NRF_QDEC_HAS_EVENT_STOPPED 1
84 #else
85 #define NRF_QDEC_HAS_EVENT_STOPPED 0
86 #endif
87
88 /** @brief Reset value of LEDPRE register. */
89 #define NRF_QDEC_LEDPRE_DEFAULT 0x10
90
91 /** @brief QDEC tasks. */
92 typedef enum
93 {
94 NRF_QDEC_TASK_START = offsetof(NRF_QDEC_Type, TASKS_START), /**< Starting the quadrature decoder. */
95 NRF_QDEC_TASK_STOP = offsetof(NRF_QDEC_Type, TASKS_STOP), /**< Stopping the quadrature decoder. */
96 NRF_QDEC_TASK_READCLRACC = offsetof(NRF_QDEC_Type, TASKS_READCLRACC), /**< Reading and clearing ACC and ACCDBL registers. */
97 #if NRF_QDEC_HAS_TASK_RDCLRACC
98 NRF_QDEC_TASK_RDCLRACC = offsetof(NRF_QDEC_Type, TASKS_RDCLRACC), /**< Reading and clearing ACC register. */
99 #endif
100 #if NRF_QDEC_HAS_TASK_RDCLRDBL
101 NRF_QDEC_TASK_RDCLRDBL = offsetof(NRF_QDEC_Type, TASKS_RDCLRDBL) /**< Reading and clearing ACCDBL register. */
102 #endif
103 } nrf_qdec_task_t;
104
105 /** @brief QDEC events. */
106 typedef enum
107 {
108 NRF_QDEC_EVENT_SAMPLERDY = offsetof(NRF_QDEC_Type, EVENTS_SAMPLERDY), /**< Event generated for every new sample. */
109 NRF_QDEC_EVENT_REPORTRDY = offsetof(NRF_QDEC_Type, EVENTS_REPORTRDY), /**< Event generated for every new report. */
110 NRF_QDEC_EVENT_ACCOF = offsetof(NRF_QDEC_Type, EVENTS_ACCOF), /**< Event generated for every accumulator overflow. */
111 #if NRF_QDEC_HAS_EVENT_DBLRDY
112 NRF_QDEC_EVENT_DBLRDY = offsetof(NRF_QDEC_Type, EVENTS_DBLRDY), /**< Event generated for every double displacement(s) detected. */
113 #endif
114 #if NRF_QDEC_HAS_EVENT_STOPPED
115 NRF_QDEC_EVENT_STOPPED = offsetof(NRF_QDEC_Type, EVENTS_STOPPED) /**< Event generated for every QDEC stop. */
116 #endif
117 } nrf_qdec_event_t;
118
119 /** @brief QDEC shortcuts. */
120 typedef enum
121 {
122 NRF_QDEC_SHORT_REPORTRDY_READCLRACC_MASK = QDEC_SHORTS_REPORTRDY_READCLRACC_Msk, /**< Shortcut between REPORTRDY event and READCLRACC task. */
123 NRF_QDEC_SHORT_SAMPLERDY_STOP_MASK = QDEC_SHORTS_SAMPLERDY_STOP_Msk /**< Shortcut between SAMPLERDY event and STOP task. */
124 } nrf_qdec_short_mask_t;
125
126 /** @brief QDEC interrupts. */
127 typedef enum
128 {
129 NRF_QDEC_INT_SAMPLERDY_MASK = QDEC_INTENSET_SAMPLERDY_Msk, /**< Mask for enabling or disabling an interrupt on SAMPLERDY event. */
130 NRF_QDEC_INT_REPORTRDY_MASK = QDEC_INTENSET_REPORTRDY_Msk, /**< Mask for enabling or disabling an interrupt on REPORTRDY event. */
131 NRF_QDEC_INT_ACCOF_MASK = QDEC_INTENSET_ACCOF_Msk /**< Mask for enabling or disabling an interrupt on ACCOF event. */
132 } nrf_qdec_int_mask_t;
133
134 /** @brief States of the enable bit. */
135 typedef enum
136 {
137 NRF_QDEC_DISABLE = QDEC_ENABLE_ENABLE_Disabled, /**< Mask for disabling the QDEC periperal. When disabled, the QDEC decoder pins are not active. */
138 NRF_QDEC_ENABLE = QDEC_ENABLE_ENABLE_Enabled /**< Mask for enabling the QDEC periperal. When enabled, the QDEC pins are active. */
139 } nrf_qdec_enable_t;
140
141 /** @brief States of the debounce filter enable bit. */
142 typedef enum
143 {
144 NRF_QDEC_DBFEN_DISABLE = QDEC_DBFEN_DBFEN_Disabled, /**< Mask for disabling the debounce filter. */
145 NRF_QDEC_DBFEN_ENABLE = QDEC_DBFEN_DBFEN_Enabled /**< Mask for enabling the debounce filter. */
146 } nrf_qdec_dbfen_t;
147
148 /** @brief Active LED polarity. */
149 typedef enum
150 {
151 NRF_QDEC_LEPOL_ACTIVE_LOW = QDEC_LEDPOL_LEDPOL_ActiveLow, /**< QDEC LED active on output pin low. */
152 NRF_QDEC_LEPOL_ACTIVE_HIGH = QDEC_LEDPOL_LEDPOL_ActiveHigh /**< QDEC LED active on output pin high. */
153 } nrf_qdec_ledpol_t;
154
155 /** @brief Available sampling periods. */
156 typedef enum
157 {
158 NRF_QDEC_SAMPLEPER_128US = QDEC_SAMPLEPER_SAMPLEPER_128us, /**< QDEC sampling period 128 microseconds. */
159 NRF_QDEC_SAMPLEPER_256US = QDEC_SAMPLEPER_SAMPLEPER_256us, /**< QDEC sampling period 256 microseconds. */
160 NRF_QDEC_SAMPLEPER_512US = QDEC_SAMPLEPER_SAMPLEPER_512us, /**< QDEC sampling period 512 microseconds. */
161 NRF_QDEC_SAMPLEPER_1024US = QDEC_SAMPLEPER_SAMPLEPER_1024us, /**< QDEC sampling period 1024 microseconds. */
162 NRF_QDEC_SAMPLEPER_2048US = QDEC_SAMPLEPER_SAMPLEPER_2048us, /**< QDEC sampling period 2048 microseconds. */
163 NRF_QDEC_SAMPLEPER_4096US = QDEC_SAMPLEPER_SAMPLEPER_4096us, /**< QDEC sampling period 4096 microseconds. */
164 NRF_QDEC_SAMPLEPER_8192US = QDEC_SAMPLEPER_SAMPLEPER_8192us, /**< QDEC sampling period 8192 microseconds. */
165 NRF_QDEC_SAMPLEPER_16384US = QDEC_SAMPLEPER_SAMPLEPER_16384us /**< QDEC sampling period 16384 microseconds. */
166 } nrf_qdec_sampleper_t;
167
168 /** @brief Available report periods. */
169 typedef enum
170 {
171 #if defined(QDEC_REPORTPER_REPORTPER_1Smpl) || defined(__NRFX_DOXYGEN__)
172 NRF_QDEC_REPORTPER_1 = QDEC_REPORTPER_REPORTPER_1Smpl, /**< QDEC report period 1 sample. */
173 #endif
174 NRF_QDEC_REPORTPER_10 = QDEC_REPORTPER_REPORTPER_10Smpl, /**< QDEC report period 10 samples. */
175 NRF_QDEC_REPORTPER_40 = QDEC_REPORTPER_REPORTPER_40Smpl, /**< QDEC report period 40 samples. */
176 NRF_QDEC_REPORTPER_80 = QDEC_REPORTPER_REPORTPER_80Smpl, /**< QDEC report period 80 samples. */
177 NRF_QDEC_REPORTPER_120 = QDEC_REPORTPER_REPORTPER_120Smpl, /**< QDEC report period 120 samples. */
178 NRF_QDEC_REPORTPER_160 = QDEC_REPORTPER_REPORTPER_160Smpl, /**< QDEC report period 160 samples. */
179 NRF_QDEC_REPORTPER_200 = QDEC_REPORTPER_REPORTPER_200Smpl, /**< QDEC report period 200 samples. */
180 NRF_QDEC_REPORTPER_240 = QDEC_REPORTPER_REPORTPER_240Smpl, /**< QDEC report period 240 samples. */
181 NRF_QDEC_REPORTPER_280 = QDEC_REPORTPER_REPORTPER_280Smpl, /**< QDEC report period 280 samples. */
182 } nrf_qdec_reportper_t;
183
184 /**
185 * @brief Function for enabling QDEC.
186 *
187 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
188 */
189 NRF_STATIC_INLINE void nrf_qdec_enable(NRF_QDEC_Type * p_reg);
190
191 /**
192 * @brief Function for disabling QDEC.
193 *
194 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
195 */
196 NRF_STATIC_INLINE void nrf_qdec_disable(NRF_QDEC_Type * p_reg);
197
198 /**
199 * @brief Function for returning the enable state of QDEC.
200 *
201 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
202 *
203 * @return State of the register.
204 */
205 NRF_STATIC_INLINE uint32_t nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg);
206
207 /**
208 * @brief Function for enabling QDEC interrupts by mask.
209 *
210 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
211 * @param[in] mask Mask of interrupts to be enabled.
212 * Use @ref nrf_qdec_int_mask_t values for bit masking.
213 */
214 NRF_STATIC_INLINE void nrf_qdec_int_enable(NRF_QDEC_Type * p_reg, uint32_t mask);
215
216 /**
217 * @brief Function for disabling QDEC interrupts by mask.
218 *
219 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
220 * @param[in] mask Mask of interrupts to be disabled.
221 * Use @ref nrf_qdec_int_mask_t values for bit masking.
222 */
223 NRF_STATIC_INLINE void nrf_qdec_int_disable(NRF_QDEC_Type * p_reg, uint32_t mask);
224
225 /**
226 * @brief Function for checking if the specified interrupts are enabled.
227 *
228 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
229 * @param[in] mask Mask of interrupts to be checked.
230 * Use @ref nrf_qdec_int_mask_t values for bit masking.
231 *
232 * @return Mask of enabled interrupts.
233 */
234 NRF_STATIC_INLINE uint32_t nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg, uint32_t mask);
235
236 /**
237 * @brief Function for enabling the QDEC debouncing filter.
238 *
239 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
240 */
241 NRF_STATIC_INLINE void nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg);
242
243 /**
244 * @brief Function for disabling the QDEC debouncing filter.
245 *
246 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
247 */
248 NRF_STATIC_INLINE void nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg);
249
250 /**
251 * @brief Function for getting the state of the QDEC debouncing filter.
252 *
253 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
254 *
255 * @retval NRF_QDEC_DBFEN_DISABLE The debouncing filter is disabled.
256 * @retval NRF_QDEC_DBFEN_ENABLE The debouncing filter is enabled.
257 */
258 NRF_STATIC_INLINE uint32_t nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg);
259
260 /**
261 * @brief Function for configuring QDEC pins.
262 *
263 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
264 * @param[in] phase_a_pin Phase A pin number.
265 * @param[in] phase_b_pin Phase B pin number.
266 * @param[in] led_pin LED pin number.
267 */
268 NRF_STATIC_INLINE void nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,
269 uint32_t phase_a_pin,
270 uint32_t phase_b_pin,
271 uint32_t led_pin);
272
273 /**
274 * @brief Function for setting the Phase A pin.
275 *
276 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
277 * @param[in] pin Phase A pin number.
278 */
279 NRF_STATIC_INLINE void nrf_qdec_phase_a_pin_set(NRF_QDEC_Type * p_reg, uint32_t pin);
280
281 /**
282 * @brief Function for setting the Phase B pin.
283 *
284 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
285 * @param[in] pin Phase B pin number.
286 */
287 NRF_STATIC_INLINE void nrf_qdec_phase_b_pin_set(NRF_QDEC_Type * p_reg, uint32_t pin);
288
289 /**
290 * @brief Function for setting the LED pin.
291 *
292 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
293 * @param[in] pin LED pin number.
294 */
295 NRF_STATIC_INLINE void nrf_qdec_led_pin_set(NRF_QDEC_Type * p_reg, uint32_t pin);
296
297 /**
298 * @brief Function for getting the Phase A pin selection.
299 *
300 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
301 *
302 * @return Phase A pin selection.
303 */
304 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg);
305
306 /**
307 * @brief Function for getting the Phase B pin selection.
308 *
309 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
310 *
311 * @return Phase B pin selection.
312 */
313 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg);
314
315 /**
316 * @brief Function for getting the LED pin selection.
317 *
318 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
319 *
320 * @return LED pin selection.
321 */
322 NRF_STATIC_INLINE uint32_t nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg);
323
324 /**
325 * @brief Function for setting the specified QDEC task.
326 *
327 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
328 * @param[in] task QDEC task to be triggered.
329 */
330 NRF_STATIC_INLINE void nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg, nrf_qdec_task_t task);
331
332 /**
333 * @brief Function for retrieving the address of a QDEC task register.
334 *
335 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
336 * @param[in] task QDEC task to get its address.
337 *
338 * @return Address of the specified QDEC task.
339 */
340 NRF_STATIC_INLINE uint32_t nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,
341 nrf_qdec_task_t task);
342
343 /**
344 * @brief Function for clearing the specified QDEC event.
345 *
346 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
347 * @param[in] event QDEC event to be cleared.
348 */
349 NRF_STATIC_INLINE void nrf_qdec_event_clear(NRF_QDEC_Type * p_reg, nrf_qdec_event_t event);
350
351 /**
352 * @brief Function for getting the state of the specified QDEC event.
353 *
354 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
355 * @param[in] event QDEC event to be checked.
356 *
357 * @return State of the specified QDEC event.
358 */
359 NRF_STATIC_INLINE bool nrf_qdec_event_check(NRF_QDEC_Type const * p_reg, nrf_qdec_event_t event);
360
361 /**
362 * @brief Function for retrieving the address of the specified QDEC event register.
363 *
364 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
365 * @param[in] event QDEC event to get its address.
366 *
367 * @return Address of the specified QDEC event.
368 */
369 NRF_STATIC_INLINE uint32_t nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,
370 nrf_qdec_event_t event);
371
372 /**
373 * @brief Function for setting QDEC shortcuts.
374 *
375 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
376 * @param[in] mask Mask of QDEC shortcuts to be set.
377 */
378 NRF_STATIC_INLINE void nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg, uint32_t mask);
379
380 /**
381 * @brief Function for clearing shortcuts of the QDEC by mask.
382 *
383 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
384 * @param[in] mask Mask of QDEC shortcuts to be cleared.
385 */
386 NRF_STATIC_INLINE void nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg, uint32_t mask);
387
388 /**
389 * @brief Function for converting return value of the @ref nrf_qdec_sampleper_get function
390 * to microseconds.
391 *
392 * @param[in] sampleper The sampling period.
393 *
394 * @return Period in microseconds.
395 */
396 NRF_STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper);
397
398 /**
399 * @brief Function for setting value of the QDEC sampling period.
400 *
401 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
402 * @param[in] sampleper The sampling period.
403 */
404 NRF_STATIC_INLINE void nrf_qdec_sampleper_set(NRF_QDEC_Type * p_reg,
405 nrf_qdec_sampleper_t sampleper);
406
407 /**
408 * @brief Function for retrieving value of the QDEC sampling period.
409 *
410 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
411 *
412 * @return Sampling period.
413 */
414 NRF_STATIC_INLINE nrf_qdec_sampleper_t nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg);
415
416 /**
417 * @brief Function for retrieving value of the QDEC SAMPLE register.
418 *
419 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
420 *
421 * @return Value of the SAMPLE register.
422 */
423 NRF_STATIC_INLINE int32_t nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg);
424
425 /**
426 * @brief Function for retrieving value of the QDEC ACC register.
427 *
428 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
429 *
430 * @return Value of the ACC register.
431 */
432 NRF_STATIC_INLINE int32_t nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg);
433
434 /**
435 * @brief Function for retrieving value of the QDEC ACCREAD register.
436 *
437 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
438 *
439 * @return Value of the ACCREAD register.
440 */
441 NRF_STATIC_INLINE int32_t nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg);
442
443 /**
444 * @brief Function for retrieving value of the QDEC ACCDBL register.
445 *
446 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
447 *
448 * @return Value of the ACCDBL register.
449 */
450 NRF_STATIC_INLINE uint32_t nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg);
451
452 /**
453 * @brief Function for retrieving value of the QDEC ACCDBLREAD register.
454 *
455 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
456 *
457 * @return Value of the ACCDBLREAD register.
458 */
459 NRF_STATIC_INLINE uint32_t nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg);
460
461 /**
462 * @brief Function for setting delay time between setting LED active state and start sampling.
463 *
464 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
465 * @param[in] time_us Delay time (in microseconds) between setting LED active state
466 * and start sampling.
467 */
468 NRF_STATIC_INLINE void nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg, uint32_t time_us);
469
470 /**
471 * @brief Function for retrieving how long the LED is switched on before sampling.
472 *
473 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
474 *
475 * @return The gap in time in microseconds between switched LED to active state and start sampling.
476 */
477 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg);
478
479 /**
480 * @brief Function for setting the report period (in samples).
481 *
482 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
483 * @param[in] reportper The number of samples.
484 */
485 NRF_STATIC_INLINE void nrf_qdec_reportper_set(NRF_QDEC_Type * p_reg,
486 nrf_qdec_reportper_t reportper);
487
488 /**
489 * @brief Function for retrieving the report period.
490 *
491 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
492 *
493 * @return The report period.
494 */
495 NRF_STATIC_INLINE nrf_qdec_reportper_t nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg);
496
497 /**
498 * @brief Function for retrieving the value of QDEC SAMPLEPER register.
499 *
500 * @param[in] reportper Reportper to be converted to amount of samples per report.
501 *
502 * @return Number of samples per report.
503 */
504 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(nrf_qdec_reportper_t reportper);
505
506 /**
507 * @brief Function for setting the active level for the LED.
508 *
509 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
510 * @param[in] pol Level of the active signal of the LED.
511 */
512 NRF_STATIC_INLINE void nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg, nrf_qdec_ledpol_t pol);
513
514 /**
515 * @brief Function for retrieving the active level for the LED.
516 *
517 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
518 *
519 * @return Level of the active signal of the LED.
520 */
521 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg);
522
523
524 #ifndef NRF_DECLARE_ONLY
525
nrf_qdec_enable(NRF_QDEC_Type * p_reg)526 NRF_STATIC_INLINE void nrf_qdec_enable(NRF_QDEC_Type * p_reg)
527 {
528 p_reg->ENABLE = NRF_QDEC_ENABLE;
529 }
530
nrf_qdec_disable(NRF_QDEC_Type * p_reg)531 NRF_STATIC_INLINE void nrf_qdec_disable(NRF_QDEC_Type * p_reg)
532 {
533 p_reg->ENABLE = NRF_QDEC_DISABLE;
534 }
535
nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg)536 NRF_STATIC_INLINE uint32_t nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg)
537 {
538 return p_reg->ENABLE;
539 }
540
nrf_qdec_int_enable(NRF_QDEC_Type * p_reg,uint32_t mask)541 NRF_STATIC_INLINE void nrf_qdec_int_enable(NRF_QDEC_Type * p_reg, uint32_t mask)
542 {
543 p_reg->INTENSET = mask; // writing 0 has no effect
544 }
545
nrf_qdec_int_disable(NRF_QDEC_Type * p_reg,uint32_t mask)546 NRF_STATIC_INLINE void nrf_qdec_int_disable(NRF_QDEC_Type * p_reg, uint32_t mask)
547 {
548 p_reg->INTENCLR = mask; // writing 0 has no effect
549 }
550
nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg,uint32_t mask)551 NRF_STATIC_INLINE uint32_t nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg, uint32_t mask)
552 {
553 return p_reg->INTENSET & mask; // when read this register will return the value of INTEN.
554 }
555
nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg)556 NRF_STATIC_INLINE void nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg)
557 {
558 p_reg->DBFEN = NRF_QDEC_DBFEN_ENABLE;
559 }
560
nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg)561 NRF_STATIC_INLINE void nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg)
562 {
563 p_reg->DBFEN = NRF_QDEC_DBFEN_DISABLE;
564 }
565
nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg)566 NRF_STATIC_INLINE uint32_t nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg)
567 {
568 return p_reg->DBFEN;
569 }
570
nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,uint32_t phase_a_pin,uint32_t phase_b_pin,uint32_t led_pin)571 NRF_STATIC_INLINE void nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,
572 uint32_t phase_a_pin,
573 uint32_t phase_b_pin,
574 uint32_t led_pin)
575 {
576 #if defined(QDEC_PSEL_A_CONNECT_Pos)
577 p_reg->PSEL.A = phase_a_pin;
578 #else
579 p_reg->PSELA = phase_a_pin;
580 #endif
581
582 #if defined(QDEC_PSEL_B_CONNECT_Pos)
583 p_reg->PSEL.B = phase_b_pin;
584 #else
585 p_reg->PSELB = phase_b_pin;
586 #endif
587
588 #if defined(QDEC_PSEL_LED_CONNECT_Pos)
589 p_reg->PSEL.LED = led_pin;
590 #else
591 p_reg->PSELLED = led_pin;
592 #endif
593 }
594
nrf_qdec_phase_a_pin_set(NRF_QDEC_Type * p_reg,uint32_t pin)595 NRF_STATIC_INLINE void nrf_qdec_phase_a_pin_set(NRF_QDEC_Type * p_reg, uint32_t pin)
596 {
597 #if defined(QDEC_PSEL_A_CONNECT_Pos)
598 p_reg->PSEL.A = pin;
599 #else
600 p_reg->PSELA = pin;
601 #endif
602 }
603
nrf_qdec_phase_b_pin_set(NRF_QDEC_Type * p_reg,uint32_t pin)604 NRF_STATIC_INLINE void nrf_qdec_phase_b_pin_set(NRF_QDEC_Type * p_reg, uint32_t pin)
605 {
606 #if defined(QDEC_PSEL_B_CONNECT_Pos)
607 p_reg->PSEL.B = pin;
608 #else
609 p_reg->PSELB = pin;
610 #endif
611 }
612
nrf_qdec_led_pin_set(NRF_QDEC_Type * p_reg,uint32_t pin)613 NRF_STATIC_INLINE void nrf_qdec_led_pin_set(NRF_QDEC_Type * p_reg, uint32_t pin)
614 {
615 #if defined(QDEC_PSEL_LED_CONNECT_Pos)
616 p_reg->PSEL.LED = pin;
617 #else
618 p_reg->PSELLED = pin;
619 #endif
620 }
621
nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg)622 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg)
623 {
624 #if defined(QDEC_PSEL_A_CONNECT_Pos)
625 return p_reg->PSEL.A;
626 #else
627 return p_reg->PSELA;
628 #endif
629 }
630
nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg)631 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg)
632 {
633 #if defined(QDEC_PSEL_B_CONNECT_Pos)
634 return p_reg->PSEL.B;
635 #else
636 return p_reg->PSELB;
637 #endif
638 }
639
nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg)640 NRF_STATIC_INLINE uint32_t nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg)
641 {
642 #if defined(QDEC_PSEL_LED_CONNECT_Pos)
643 return p_reg->PSEL.LED;
644 #else
645 return p_reg->PSELLED;
646 #endif
647 }
648
nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg,nrf_qdec_task_t task)649 NRF_STATIC_INLINE void nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg, nrf_qdec_task_t task)
650 {
651 *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)task) ) = 1;
652 }
653
nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,nrf_qdec_task_t task)654 NRF_STATIC_INLINE uint32_t nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,
655 nrf_qdec_task_t task)
656 {
657 return nrf_task_event_address_get(p_reg, task);
658 }
659
nrf_qdec_event_clear(NRF_QDEC_Type * p_reg,nrf_qdec_event_t event)660 NRF_STATIC_INLINE void nrf_qdec_event_clear(NRF_QDEC_Type * p_reg, nrf_qdec_event_t event)
661 {
662 *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)event) ) = 0;
663 nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
664 }
665
nrf_qdec_event_check(NRF_QDEC_Type const * p_reg,nrf_qdec_event_t event)666 NRF_STATIC_INLINE bool nrf_qdec_event_check(NRF_QDEC_Type const * p_reg, nrf_qdec_event_t event)
667 {
668 return nrf_event_check(p_reg, event);
669 }
670
nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,nrf_qdec_event_t event)671 NRF_STATIC_INLINE uint32_t nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,
672 nrf_qdec_event_t event)
673 {
674 return nrf_task_event_address_get(p_reg, event);
675 }
676
nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg,uint32_t mask)677 NRF_STATIC_INLINE void nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg, uint32_t mask)
678 {
679 p_reg->SHORTS |= mask;
680 }
681
nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg,uint32_t mask)682 NRF_STATIC_INLINE void nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg, uint32_t mask)
683 {
684 p_reg->SHORTS &= ~mask;
685 }
686
nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper)687 NRF_STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper)
688 {
689 return (1 << (7 + sampleper));
690 }
691
nrf_qdec_sampleper_set(NRF_QDEC_Type * p_reg,nrf_qdec_sampleper_t sampleper)692 NRF_STATIC_INLINE void nrf_qdec_sampleper_set(NRF_QDEC_Type * p_reg,
693 nrf_qdec_sampleper_t sampleper)
694 {
695 p_reg->SAMPLEPER = sampleper;
696 }
697
nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg)698 NRF_STATIC_INLINE nrf_qdec_sampleper_t nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg)
699 {
700 return (nrf_qdec_sampleper_t)(p_reg->SAMPLEPER);
701 }
702
nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg)703 NRF_STATIC_INLINE int32_t nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg)
704 {
705 return p_reg->SAMPLE;
706 }
707
nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg)708 NRF_STATIC_INLINE int32_t nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg)
709 {
710 return p_reg->ACC;
711 }
712
nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg)713 NRF_STATIC_INLINE int32_t nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg)
714 {
715 return p_reg->ACCREAD;
716 }
717
nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg)718 NRF_STATIC_INLINE uint32_t nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg)
719 {
720 return p_reg->ACCDBL;
721 }
722
nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg)723 NRF_STATIC_INLINE uint32_t nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg)
724 {
725 return p_reg->ACCDBLREAD;
726 }
727
nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg,uint32_t time_us)728 NRF_STATIC_INLINE void nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg, uint32_t time_us)
729 {
730 p_reg->LEDPRE = time_us;
731 }
732
nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg)733 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg)
734 {
735 return p_reg->LEDPRE;
736 }
737
nrf_qdec_reportper_set(NRF_QDEC_Type * p_reg,nrf_qdec_reportper_t reportper)738 NRF_STATIC_INLINE void nrf_qdec_reportper_set(NRF_QDEC_Type * p_reg,
739 nrf_qdec_reportper_t reportper)
740 {
741 p_reg->REPORTPER = reportper;
742 }
743
nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg)744 NRF_STATIC_INLINE nrf_qdec_reportper_t nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg)
745 {
746 return (nrf_qdec_reportper_t)(p_reg->REPORTPER);
747 }
748
nrf_qdec_reportper_to_value(nrf_qdec_reportper_t reportper)749 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(nrf_qdec_reportper_t reportper)
750 {
751 return (reportper == NRF_QDEC_REPORTPER_10) ? 10 : reportper * 40;
752 }
753
nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg,nrf_qdec_ledpol_t pol)754 NRF_STATIC_INLINE void nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg, nrf_qdec_ledpol_t pol)
755 {
756 p_reg->LEDPOL = pol;
757 }
758
nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg)759 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg)
760 {
761 return p_reg->LEDPOL;
762 }
763
764 #endif // NRF_DECLARE_ONLY
765
766 /** @} */
767
768 #ifdef __cplusplus
769 }
770 #endif
771
772 #endif // NRF_QDEC_H__
773