1 /*
2  * Copyright (c) 2014 - 2024, 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 getting the Phase A pin selection.
275  *
276  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
277  *
278  * @return Phase A pin selection.
279  */
280 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg);
281 
282 /**
283  * @brief Function for getting the Phase B pin selection.
284  *
285  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
286  *
287  * @return Phase B pin selection.
288  */
289 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg);
290 
291 /**
292  * @brief Function for getting the LED pin selection.
293  *
294  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
295  *
296  * @return LED pin selection.
297  */
298 NRF_STATIC_INLINE uint32_t nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg);
299 
300 /**
301  * @brief Function for setting the specified QDEC task.
302  *
303  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
304  * @param[in] task  QDEC task to be triggered.
305  */
306 NRF_STATIC_INLINE void nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg, nrf_qdec_task_t task);
307 
308 /**
309  * @brief Function for retrieving the address of a QDEC task register.
310  *
311  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
312  * @param[in] task  QDEC task to get its address.
313  *
314  * @return Address of the specified QDEC task.
315  */
316 NRF_STATIC_INLINE uint32_t nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,
317                                                      nrf_qdec_task_t       task);
318 
319 /**
320  * @brief Function for clearing the specified QDEC event.
321  *
322  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
323  * @param[in] event QDEC event to be cleared.
324  */
325 NRF_STATIC_INLINE void nrf_qdec_event_clear(NRF_QDEC_Type * p_reg, nrf_qdec_event_t event);
326 
327 /**
328  * @brief Function for getting the state of the specified QDEC event.
329  *
330  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
331  * @param[in] event QDEC event to be checked.
332  *
333  * @return State of the specified QDEC event.
334  */
335 NRF_STATIC_INLINE bool nrf_qdec_event_check(NRF_QDEC_Type const * p_reg, nrf_qdec_event_t event);
336 
337 /**
338  * @brief Function for retrieving the address of the specified QDEC event register.
339  *
340  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
341  * @param[in] event QDEC event to get its address.
342  *
343  * @return Address of the specified QDEC event.
344  */
345 NRF_STATIC_INLINE uint32_t nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,
346                                                       nrf_qdec_event_t      event);
347 
348 /**
349  * @brief Function for setting QDEC shortcuts.
350  *
351  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
352  * @param[in] mask  Mask of QDEC shortcuts to be set.
353  */
354 NRF_STATIC_INLINE void nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg, uint32_t mask);
355 
356 /**
357  * @brief Function for clearing shortcuts of the QDEC by mask.
358  *
359  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
360  * @param[in] mask  Mask of QDEC shortcuts to be cleared.
361  */
362 NRF_STATIC_INLINE void nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg, uint32_t mask);
363 
364 /**
365  * @brief Function for converting return value of the @ref nrf_qdec_sampleper_get function
366  *        to microseconds.
367  *
368  * @param[in] sampleper The sampling period.
369  *
370  * @return Period in microseconds.
371  */
372 NRF_STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper);
373 
374 /**
375  * @brief Function for setting value of the QDEC sampling period.
376  *
377  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
378  * @param[in] sampleper The sampling period.
379  */
380 NRF_STATIC_INLINE void nrf_qdec_sampleper_set(NRF_QDEC_Type *      p_reg,
381                                               nrf_qdec_sampleper_t sampleper);
382 
383 /**
384  * @brief Function for retrieving value of the QDEC sampling period.
385  *
386  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
387  *
388  * @return Sampling period.
389  */
390 NRF_STATIC_INLINE nrf_qdec_sampleper_t nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg);
391 
392 /**
393  * @brief Function for retrieving value of the QDEC SAMPLE register.
394  *
395  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
396  *
397  * @return Value of the SAMPLE register.
398  */
399 NRF_STATIC_INLINE int32_t nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg);
400 
401 /**
402  * @brief Function for retrieving value of the QDEC ACC register.
403  *
404  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
405  *
406  * @return Value of the ACC register.
407  */
408 NRF_STATIC_INLINE int32_t nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg);
409 
410 /**
411  * @brief Function for retrieving value of the QDEC ACCREAD register.
412  *
413  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
414  *
415  * @return Value of the ACCREAD register.
416  */
417 NRF_STATIC_INLINE int32_t nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg);
418 
419 /**
420  * @brief Function for retrieving value of the QDEC ACCDBL register.
421  *
422  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
423  *
424  * @return Value of the ACCDBL register.
425  */
426 NRF_STATIC_INLINE uint32_t nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg);
427 
428 /**
429  * @brief Function for retrieving value of the QDEC ACCDBLREAD register.
430  *
431  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
432  *
433  * @return Value of the ACCDBLREAD register.
434  */
435 NRF_STATIC_INLINE uint32_t nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg);
436 
437 /**
438  * @brief Function for setting delay time between setting LED active state and start sampling.
439  *
440  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
441  * @param[in] time_us Delay time (in microseconds) between setting LED active state
442  *                    and start sampling.
443  */
444 NRF_STATIC_INLINE void nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg, uint32_t time_us);
445 
446 /**
447  * @brief Function for retrieving how long the LED is switched on before sampling.
448  *
449  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
450  *
451  * @return The gap in time in microseconds between switched LED to active state and start sampling.
452  */
453 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg);
454 
455 /**
456  * @brief Function for setting the report period (in samples).
457  *
458  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
459  * @param[in] reportper The number of samples.
460  */
461 NRF_STATIC_INLINE void nrf_qdec_reportper_set(NRF_QDEC_Type *      p_reg,
462                                               nrf_qdec_reportper_t reportper);
463 
464 /**
465  * @brief Function for retrieving the report period.
466  *
467  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
468  *
469  * @return The report period.
470  */
471 NRF_STATIC_INLINE nrf_qdec_reportper_t nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg);
472 
473 /**
474  * @brief Function for retrieving the value of QDEC SAMPLEPER register.
475  *
476  * @param[in] reportper Reportper to be converted to amount of samples per report.
477  *
478  * @return Number of samples per report.
479  */
480 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(nrf_qdec_reportper_t reportper);
481 
482 /**
483  * @brief Function for setting the active level for the LED.
484  *
485  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
486  * @param[in] pol   Level of the active signal of the LED.
487  */
488 NRF_STATIC_INLINE void nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg, nrf_qdec_ledpol_t pol);
489 
490 /**
491  * @brief Function for retrieving the active level for the LED.
492  *
493  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
494  *
495  * @return Level of the active signal of the LED.
496  */
497 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg);
498 
499 
500 #ifndef NRF_DECLARE_ONLY
501 
nrf_qdec_enable(NRF_QDEC_Type * p_reg)502 NRF_STATIC_INLINE void nrf_qdec_enable(NRF_QDEC_Type * p_reg)
503 {
504     p_reg->ENABLE = NRF_QDEC_ENABLE;
505 }
506 
nrf_qdec_disable(NRF_QDEC_Type * p_reg)507 NRF_STATIC_INLINE void nrf_qdec_disable(NRF_QDEC_Type * p_reg)
508 {
509     p_reg->ENABLE = NRF_QDEC_DISABLE;
510 }
511 
nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg)512 NRF_STATIC_INLINE uint32_t nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg)
513 {
514     return p_reg->ENABLE;
515 }
516 
nrf_qdec_int_enable(NRF_QDEC_Type * p_reg,uint32_t mask)517 NRF_STATIC_INLINE void nrf_qdec_int_enable(NRF_QDEC_Type * p_reg, uint32_t mask)
518 {
519     p_reg->INTENSET = mask; // writing 0 has no effect
520 }
521 
nrf_qdec_int_disable(NRF_QDEC_Type * p_reg,uint32_t mask)522 NRF_STATIC_INLINE void nrf_qdec_int_disable(NRF_QDEC_Type * p_reg, uint32_t mask)
523 {
524     p_reg->INTENCLR = mask; // writing 0 has no effect
525 }
526 
nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg,uint32_t mask)527 NRF_STATIC_INLINE uint32_t nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg, uint32_t mask)
528 {
529     return p_reg->INTENSET & mask; // when read this register will return the value of INTEN.
530 }
531 
nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg)532 NRF_STATIC_INLINE void nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg)
533 {
534     p_reg->DBFEN = NRF_QDEC_DBFEN_ENABLE;
535 }
536 
nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg)537 NRF_STATIC_INLINE void nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg)
538 {
539     p_reg->DBFEN = NRF_QDEC_DBFEN_DISABLE;
540 }
541 
nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg)542 NRF_STATIC_INLINE uint32_t nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg)
543 {
544     return p_reg->DBFEN;
545 }
546 
nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,uint32_t phase_a_pin,uint32_t phase_b_pin,uint32_t led_pin)547 NRF_STATIC_INLINE void nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,
548                                          uint32_t        phase_a_pin,
549                                          uint32_t        phase_b_pin,
550                                          uint32_t        led_pin)
551 {
552 #if defined(QDEC_PSEL_A_CONNECT_Pos)
553     p_reg->PSEL.A = phase_a_pin;
554 #else
555     p_reg->PSELA = phase_a_pin;
556 #endif
557 
558 #if defined(QDEC_PSEL_B_CONNECT_Pos)
559     p_reg->PSEL.B = phase_b_pin;
560 #else
561     p_reg->PSELB = phase_b_pin;
562 #endif
563 
564 #if defined(QDEC_PSEL_LED_CONNECT_Pos)
565     p_reg->PSEL.LED = led_pin;
566 #else
567     p_reg->PSELLED = led_pin;
568 #endif
569 }
570 
nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg)571 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg)
572 {
573 #if defined(QDEC_PSEL_A_CONNECT_Pos)
574     return p_reg->PSEL.A;
575 #else
576     return p_reg->PSELA;
577 #endif
578 }
579 
nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg)580 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg)
581 {
582 #if defined(QDEC_PSEL_B_CONNECT_Pos)
583     return p_reg->PSEL.B;
584 #else
585     return p_reg->PSELB;
586 #endif
587 }
588 
nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg)589 NRF_STATIC_INLINE uint32_t nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg)
590 {
591 #if defined(QDEC_PSEL_LED_CONNECT_Pos)
592     return p_reg->PSEL.LED;
593 #else
594     return p_reg->PSELLED;
595 #endif
596 }
597 
nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg,nrf_qdec_task_t task)598 NRF_STATIC_INLINE void nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg, nrf_qdec_task_t task)
599 {
600     *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)task) ) = 1;
601 }
602 
nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,nrf_qdec_task_t task)603 NRF_STATIC_INLINE uint32_t nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,
604                                                      nrf_qdec_task_t       task)
605 {
606     return nrf_task_event_address_get(p_reg, task);
607 }
608 
nrf_qdec_event_clear(NRF_QDEC_Type * p_reg,nrf_qdec_event_t event)609 NRF_STATIC_INLINE void nrf_qdec_event_clear(NRF_QDEC_Type * p_reg, nrf_qdec_event_t event)
610 {
611     *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)event) ) = 0;
612     nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
613 }
614 
nrf_qdec_event_check(NRF_QDEC_Type const * p_reg,nrf_qdec_event_t event)615 NRF_STATIC_INLINE bool nrf_qdec_event_check(NRF_QDEC_Type const * p_reg, nrf_qdec_event_t event)
616 {
617     return nrf_event_check(p_reg, event);
618 }
619 
nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,nrf_qdec_event_t event)620 NRF_STATIC_INLINE uint32_t nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,
621                                                       nrf_qdec_event_t      event)
622 {
623     return nrf_task_event_address_get(p_reg, event);
624 }
625 
nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg,uint32_t mask)626 NRF_STATIC_INLINE void nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg, uint32_t mask)
627 {
628     p_reg->SHORTS |= mask;
629 }
630 
nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg,uint32_t mask)631 NRF_STATIC_INLINE void nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg, uint32_t mask)
632 {
633     p_reg->SHORTS &= ~mask;
634 }
635 
nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper)636 NRF_STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper)
637 {
638     return (1 << (7 + sampleper));
639 }
640 
nrf_qdec_sampleper_set(NRF_QDEC_Type * p_reg,nrf_qdec_sampleper_t sampleper)641 NRF_STATIC_INLINE void nrf_qdec_sampleper_set(NRF_QDEC_Type *      p_reg,
642                                               nrf_qdec_sampleper_t sampleper)
643 {
644     p_reg->SAMPLEPER = sampleper;
645 }
646 
nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg)647 NRF_STATIC_INLINE nrf_qdec_sampleper_t nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg)
648 {
649     return (nrf_qdec_sampleper_t)(p_reg->SAMPLEPER);
650 }
651 
nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg)652 NRF_STATIC_INLINE int32_t nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg)
653 {
654     return p_reg->SAMPLE;
655 }
656 
nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg)657 NRF_STATIC_INLINE int32_t nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg)
658 {
659     return p_reg->ACC;
660 }
661 
nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg)662 NRF_STATIC_INLINE int32_t nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg)
663 {
664     return p_reg->ACCREAD;
665 }
666 
nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg)667 NRF_STATIC_INLINE uint32_t nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg)
668 {
669     return p_reg->ACCDBL;
670 }
671 
nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg)672 NRF_STATIC_INLINE uint32_t nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg)
673 {
674     return p_reg->ACCDBLREAD;
675 }
676 
nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg,uint32_t time_us)677 NRF_STATIC_INLINE void nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg, uint32_t time_us)
678 {
679     p_reg->LEDPRE = time_us;
680 }
681 
nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg)682 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg)
683 {
684     return p_reg->LEDPRE;
685 }
686 
nrf_qdec_reportper_set(NRF_QDEC_Type * p_reg,nrf_qdec_reportper_t reportper)687 NRF_STATIC_INLINE void nrf_qdec_reportper_set(NRF_QDEC_Type *      p_reg,
688                                               nrf_qdec_reportper_t reportper)
689 {
690     p_reg->REPORTPER = reportper;
691 }
692 
nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg)693 NRF_STATIC_INLINE nrf_qdec_reportper_t nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg)
694 {
695     return (nrf_qdec_reportper_t)(p_reg->REPORTPER);
696 }
697 
nrf_qdec_reportper_to_value(nrf_qdec_reportper_t reportper)698 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(nrf_qdec_reportper_t reportper)
699 {
700     return (reportper == NRF_QDEC_REPORTPER_10) ? 10 : reportper * 40;
701 }
702 
nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg,nrf_qdec_ledpol_t pol)703 NRF_STATIC_INLINE void nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg, nrf_qdec_ledpol_t pol)
704 {
705     p_reg->LEDPOL = pol;
706 }
707 
nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg)708 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg)
709 {
710     return p_reg->LEDPOL;
711 }
712 
713 #endif // NRF_DECLARE_ONLY
714 
715 /** @} */
716 
717 #ifdef __cplusplus
718 }
719 #endif
720 
721 #endif // NRF_QDEC_H__
722