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 #ifndef NRF_QDEC_H__
32 #define NRF_QDEC_H__
33 
34 #include <nrfx.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * @defgroup nrf_qdec_hal QDEC HAL
42  * @{
43  * @ingroup nrf_qdec
44  * @brief   Hardware access layer for managing the Quadrature Decoder (QDEC) peripheral.
45  */
46 
47 /**
48  * @brief This value can be provided as a parameter for the @ref nrf_qdec_pio_assign
49  *        function call to specify that a LED signal shall not be use by the QDEC and
50  *        connected to a physical pin.
51  */
52 #define NRF_QDEC_LED_NOT_CONNECTED  0xFFFFFFFF
53 
54 /** @brief QDEC tasks. */
55 typedef enum
56 {
57     NRF_QDEC_TASK_START      = offsetof(NRF_QDEC_Type, TASKS_START),     /**< Starting the quadrature decoder. */
58     NRF_QDEC_TASK_STOP       = offsetof(NRF_QDEC_Type, TASKS_STOP),      /**< Stopping the quadrature decoder. */
59     NRF_QDEC_TASK_READCLRACC = offsetof(NRF_QDEC_Type, TASKS_READCLRACC) /**< Reading and clearing ACC and ACCDBL registers. */
60 } nrf_qdec_task_t;
61 
62 /** @brief QDEC events. */
63 typedef enum
64 {
65     NRF_QDEC_EVENT_SAMPLERDY = offsetof(NRF_QDEC_Type, EVENTS_SAMPLERDY), /**< Event generated for every new sample.  */
66     NRF_QDEC_EVENT_REPORTRDY = offsetof(NRF_QDEC_Type, EVENTS_REPORTRDY), /**< Event generated for every new report.  */
67     NRF_QDEC_EVENT_ACCOF     = offsetof(NRF_QDEC_Type, EVENTS_ACCOF)      /**< Event generated for every accumulator overflow. */
68 } nrf_qdec_event_t;
69 
70 /** @brief QDEC shortcuts. */
71 typedef enum
72 {
73     NRF_QDEC_SHORT_REPORTRDY_READCLRACC_MASK = QDEC_SHORTS_REPORTRDY_READCLRACC_Msk, /**< Shortcut between REPORTRDY event and READCLRACC task.  */
74     NRF_QDEC_SHORT_SAMPLERDY_STOP_MASK       = QDEC_SHORTS_SAMPLERDY_STOP_Msk        /**< Shortcut between SAMPLERDY event and STOP task.  */
75 } nrf_qdec_short_mask_t;
76 
77 /** @brief QDEC interrupts. */
78 typedef enum
79 {
80     NRF_QDEC_INT_SAMPLERDY_MASK = QDEC_INTENSET_SAMPLERDY_Msk, /**< Mask for enabling or disabling an interrupt on SAMPLERDY event.  */
81     NRF_QDEC_INT_REPORTRDY_MASK = QDEC_INTENSET_REPORTRDY_Msk, /**< Mask for enabling or disabling an interrupt on REPORTRDY event.  */
82     NRF_QDEC_INT_ACCOF_MASK     = QDEC_INTENSET_ACCOF_Msk      /**< Mask for enabling or disabling an interrupt on ACCOF event.  */
83 } nrf_qdec_int_mask_t;
84 
85 /** @brief States of the enable bit. */
86 typedef enum
87 {
88     NRF_QDEC_DISABLE = QDEC_ENABLE_ENABLE_Disabled, /**< Mask for disabling the QDEC periperal. When disabled, the QDEC decoder pins are not active.  */
89     NRF_QDEC_ENABLE  = QDEC_ENABLE_ENABLE_Enabled   /**< Mask for enabling the QDEC periperal. When enabled, the QDEC pins are active. */
90 } nrf_qdec_enable_t;
91 
92 /** @brief States of the debounce filter enable bit. */
93 typedef enum
94 {
95     NRF_QDEC_DBFEN_DISABLE = QDEC_DBFEN_DBFEN_Disabled, /**< Mask for disabling the debounce filter.  */
96     NRF_QDEC_DBFEN_ENABLE  = QDEC_DBFEN_DBFEN_Enabled   /**< Mask for enabling the debounce filter.  */
97 } nrf_qdec_dbfen_t;
98 
99 /** @brief Active LED polarity. */
100 typedef enum
101 {
102     NRF_QDEC_LEPOL_ACTIVE_LOW  = QDEC_LEDPOL_LEDPOL_ActiveLow, /**< QDEC LED active on output pin low.  */
103     NRF_QDEC_LEPOL_ACTIVE_HIGH = QDEC_LEDPOL_LEDPOL_ActiveHigh /**< QDEC LED active on output pin high.  */
104 } nrf_qdec_ledpol_t;
105 
106 /** @brief Available sampling periods. */
107 typedef enum
108 {
109     NRF_QDEC_SAMPLEPER_128us   = QDEC_SAMPLEPER_SAMPLEPER_128us,  /**< QDEC sampling period 128 microseconds.  */
110     NRF_QDEC_SAMPLEPER_256us   = QDEC_SAMPLEPER_SAMPLEPER_256us,  /**< QDEC sampling period 256 microseconds.  */
111     NRF_QDEC_SAMPLEPER_512us   = QDEC_SAMPLEPER_SAMPLEPER_512us,  /**< QDEC sampling period 512 microseconds.  */
112     NRF_QDEC_SAMPLEPER_1024us  = QDEC_SAMPLEPER_SAMPLEPER_1024us, /**< QDEC sampling period 1024 microseconds.  */
113     NRF_QDEC_SAMPLEPER_2048us  = QDEC_SAMPLEPER_SAMPLEPER_2048us, /**< QDEC sampling period 2048 microseconds.  */
114     NRF_QDEC_SAMPLEPER_4096us  = QDEC_SAMPLEPER_SAMPLEPER_4096us, /**< QDEC sampling period 4096 microseconds.  */
115     NRF_QDEC_SAMPLEPER_8192us  = QDEC_SAMPLEPER_SAMPLEPER_8192us, /**< QDEC sampling period 8192 microseconds.  */
116     NRF_QDEC_SAMPLEPER_16384us = QDEC_SAMPLEPER_SAMPLEPER_16384us /**< QDEC sampling period 16384 microseconds.  */
117 } nrf_qdec_sampleper_t;
118 
119 /** @brief Available report periods. */
120 typedef enum
121 {
122     NRF_QDEC_REPORTPER_10  = QDEC_REPORTPER_REPORTPER_10Smpl,  /**< QDEC report period 10 samples.  */
123     NRF_QDEC_REPORTPER_40  = QDEC_REPORTPER_REPORTPER_40Smpl,  /**< QDEC report period 40 samples.  */
124     NRF_QDEC_REPORTPER_80  = QDEC_REPORTPER_REPORTPER_80Smpl,  /**< QDEC report period 80 samples.  */
125     NRF_QDEC_REPORTPER_120 = QDEC_REPORTPER_REPORTPER_120Smpl, /**< QDEC report period 120 samples. */
126     NRF_QDEC_REPORTPER_160 = QDEC_REPORTPER_REPORTPER_160Smpl, /**< QDEC report period 160 samples. */
127     NRF_QDEC_REPORTPER_200 = QDEC_REPORTPER_REPORTPER_200Smpl, /**< QDEC report period 200 samples. */
128     NRF_QDEC_REPORTPER_240 = QDEC_REPORTPER_REPORTPER_240Smpl, /**< QDEC report period 240 samples. */
129     NRF_QDEC_REPORTPER_280 = QDEC_REPORTPER_REPORTPER_280Smpl, /**< QDEC report period 280 samples. */
130     NRF_QDEC_REPORTPER_DISABLED                                /**< QDEC reporting disabled.        */
131 } nrf_qdec_reportper_t;
132 
133 /**
134  * @brief Function for enabling QDEC.
135  *
136  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
137  */
138 NRF_STATIC_INLINE void nrf_qdec_enable(NRF_QDEC_Type * p_reg);
139 
140 /**
141  * @brief Function for disabling QDEC.
142  *
143  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
144  */
145 NRF_STATIC_INLINE void nrf_qdec_disable(NRF_QDEC_Type * p_reg);
146 
147 /**
148  * @brief Function for returning the enable state of QDEC.
149  *
150  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
151  *
152  * @return State of the register.
153  */
154 NRF_STATIC_INLINE uint32_t nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg);
155 
156 /**
157  * @brief Function for enabling QDEC interrupts by mask.
158  *
159  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
160  * @param[in] mask  Mask of interrupts to be enabled.
161  */
162 NRF_STATIC_INLINE void nrf_qdec_int_enable(NRF_QDEC_Type * p_reg, uint32_t mask);
163 
164 /**
165  * @brief Function for disabling QDEC interrupts by mask.
166  *
167  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
168  * @param[in] mask  Mask of interrupts to be disabled.
169  */
170 NRF_STATIC_INLINE void nrf_qdec_int_disable(NRF_QDEC_Type * p_reg, uint32_t mask);
171 
172 /**
173  * @brief Function for checking if the specified interrupts are enabled.
174  *
175  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
176  * @param[in] mask  Mask of interrupts to be checked.
177  *
178  * @return Mask of enabled interrupts.
179  */
180 NRF_STATIC_INLINE uint32_t nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg, uint32_t mask);
181 
182 /**
183  * @brief Function for enabling the QDEC debouncing filter.
184  *
185  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
186  */
187 NRF_STATIC_INLINE void nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg);
188 
189 /**
190  * @brief Function for disabling the QDEC debouncing filter.
191  *
192  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
193  */
194 NRF_STATIC_INLINE void nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg);
195 
196 /**
197  * @brief Function for getting the state of the QDEC debouncing filter.
198  *
199  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
200  *
201  * @retval NRF_QDEC_DBFEN_DISABLE The debouncing filter is disabled.
202  * @retval NRF_QDEC_DBFEN_ENABLE  The debouncing filter is enabled.
203  */
204 NRF_STATIC_INLINE uint32_t nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg);
205 
206 /**
207  * @brief Function for configuring QDEC pins.
208  *
209  * @param[in] p_reg       Pointer to the structure of registers of the peripheral.
210  * @param[in] phase_a_pin Phase A pin number.
211  * @param[in] phase_b_pin Phase B pin number.
212  * @param[in] led_pin     LED pin number.
213  */
214 NRF_STATIC_INLINE void nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,
215                                          uint32_t        phase_a_pin,
216                                          uint32_t        phase_b_pin,
217                                          uint32_t        led_pin);
218 
219 /**
220  * @brief Function for assigning QDEC pins.
221  *
222  * @note This function is deprecated. Use @ref nrf_qdec_pins_set instead.
223  *
224  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
225  * @param[in] psela   Pin number.
226  * @param[in] pselb   Pin number.
227  * @param[in] pselled Pin number.
228  */
229 NRF_STATIC_INLINE void nrf_qdec_pio_assign(NRF_QDEC_Type * p_reg,
230                                            uint32_t        psela,
231                                            uint32_t        pselb,
232                                            uint32_t        pselled);
233 
234 /**
235  * @brief Function for getting the Phase A pin selection.
236  *
237  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
238  *
239  * @return Phase A pin selection.
240  */
241 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg);
242 
243 /**
244  * @brief Function for getting the Phase B pin selection.
245  *
246  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
247  *
248  * @return Phase B pin selection.
249  */
250 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg);
251 
252 /**
253  * @brief Function for getting the LED pin selection.
254  *
255  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
256  *
257  * @return LED pin selection.
258  */
259 NRF_STATIC_INLINE uint32_t nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg);
260 
261 /**
262  * @brief Function for setting the specified QDEC task.
263  *
264  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
265  * @param[in] task  QDEC task to be triggered.
266  */
267 NRF_STATIC_INLINE void nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg, nrf_qdec_task_t task);
268 
269 /**
270  * @brief Function for retrieving the address of a QDEC task register.
271  *
272  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
273  * @param[in] task  QDEC task to get its address.
274  *
275  * @return Address of the specified QDEC task.
276  */
277 NRF_STATIC_INLINE uint32_t nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,
278                                                      nrf_qdec_task_t       task);
279 
280 /**
281  * @brief Function for clearing the specified QDEC event.
282  *
283  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
284  * @param[in] event QDEC event to be cleared.
285  */
286 NRF_STATIC_INLINE void nrf_qdec_event_clear(NRF_QDEC_Type * p_reg, nrf_qdec_event_t event);
287 
288 /**
289  * @brief Function for getting the state of the specified QDEC event.
290  *
291  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
292  * @param[in] event QDEC event to be checked.
293  *
294  * @return State of the specified QDEC event.
295  */
296 NRF_STATIC_INLINE bool nrf_qdec_event_check(NRF_QDEC_Type const * p_reg, nrf_qdec_event_t event);
297 
298 /**
299  * @brief Function for retrieving the address of the specified QDEC event register.
300  *
301  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
302  * @param[in] event QDEC event to get its address.
303  *
304  * @return Address of the specified QDEC event.
305  */
306 NRF_STATIC_INLINE uint32_t nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,
307                                                       nrf_qdec_event_t      event);
308 
309 /**
310  * @brief Function for setting QDEC shortcuts.
311  *
312  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
313  * @param[in] mask  Mask of QDEC shortcuts to be set.
314  */
315 NRF_STATIC_INLINE void nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg, uint32_t mask);
316 
317 /**
318  * @brief Function for clearing shortcuts of the QDEC by mask.
319  *
320  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
321  * @param[in] mask  Mask of QDEC shortcuts to be cleared.
322  */
323 NRF_STATIC_INLINE void nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg, uint32_t mask);
324 
325 /**
326  * @brief Function for converting return value of the @ref nrf_qdec_sampleper_get function
327  *        to microseconds.
328  *
329  * @param[in] sampleper The sampling period.
330  *
331  * @return Period in microseconds.
332  */
333 NRF_STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper);
334 
335 /**
336  * @brief Function for setting value of the QDEC sampling period.
337  *
338  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
339  * @param[in] sampleper The sampling period.
340  */
341 NRF_STATIC_INLINE void nrf_qdec_sampleper_set(NRF_QDEC_Type *      p_reg,
342                                               nrf_qdec_sampleper_t sampleper);
343 
344 /**
345  * @brief Function for retrieving value of the QDEC sampling period.
346  *
347  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
348  *
349  * @return Sampling period.
350  */
351 NRF_STATIC_INLINE nrf_qdec_sampleper_t nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg);
352 
353 /**
354  * @brief Function for retrieving value of the QDEC SAMPLE register.
355  *
356  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
357  *
358  * @return Value of the SAMPLE register.
359  */
360 NRF_STATIC_INLINE int32_t nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg);
361 
362 /**
363  * @brief Function for retrieving value of the QDEC ACC register.
364  *
365  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
366  *
367  * @return Value of the ACC register.
368  */
369 NRF_STATIC_INLINE int32_t nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg);
370 
371 /**
372  * @brief Function for retrieving value of the QDEC ACCREAD register.
373  *
374  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
375  *
376  * @return Value of the ACCREAD register.
377  */
378 NRF_STATIC_INLINE int32_t nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg);
379 
380 /**
381  * @brief Function for retrieving value of the QDEC ACCDBL register.
382  *
383  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
384  *
385  * @return Value of the ACCDBL register.
386  */
387 NRF_STATIC_INLINE uint32_t nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg);
388 
389 /**
390  * @brief Function for retrieving value of the QDEC ACCDBLREAD register.
391  *
392  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
393  *
394  * @return Value of the ACCDBLREAD register.
395  */
396 NRF_STATIC_INLINE uint32_t nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg);
397 
398 /**
399  * @brief Function for setting delay time between setting LED active state and start sampling.
400  *
401  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
402  * @param[in] time_us Delay time (in microseconds) between setting LED active state
403  *                    and start sampling.
404  */
405 NRF_STATIC_INLINE void nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg, uint32_t time_us);
406 
407 /**
408  * @brief Function for retrieving how long the LED is switched on before sampling.
409  *
410  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
411  *
412  * @return The gap in time in microseconds between switched LED to active state and start sampling.
413  */
414 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg);
415 
416 /**
417  * @brief Function for setting the report period (in samples).
418  *
419  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
420  * @param[in] reportper The number of samples.
421  */
422 NRF_STATIC_INLINE void nrf_qdec_reportper_set(NRF_QDEC_Type *      p_reg,
423                                               nrf_qdec_reportper_t reportper);
424 
425 /**
426  * @brief Function for retrieving the report period.
427  *
428  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
429  *
430  * @return The report period.
431  */
432 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg);
433 
434 /**
435  * @brief Function for retrieving the value of QDEC SAMPLEPER register.
436  *
437  * @param[in] reportper Reportper to be converted to amount of samples per report.
438  *
439  * @return Number of samples per report.
440  */
441 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(uint32_t reportper);
442 
443 /**
444  * @brief Function for setting the active level for the LED.
445  *
446  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
447  * @param[in] pol   Level of the active signal of the LED.
448  */
449 NRF_STATIC_INLINE void nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg, nrf_qdec_ledpol_t pol);
450 
451 /**
452  * @brief Function for retrieving the active level for the LED.
453  *
454  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
455  *
456  * @return Level of the active signal of the LED.
457  */
458 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg);
459 
460 
461 #ifndef NRF_DECLARE_ONLY
462 
nrf_qdec_enable(NRF_QDEC_Type * p_reg)463 NRF_STATIC_INLINE void nrf_qdec_enable(NRF_QDEC_Type * p_reg)
464 {
465     p_reg->ENABLE = NRF_QDEC_ENABLE;
466 }
467 
nrf_qdec_disable(NRF_QDEC_Type * p_reg)468 NRF_STATIC_INLINE void nrf_qdec_disable(NRF_QDEC_Type * p_reg)
469 {
470     p_reg->ENABLE = NRF_QDEC_DISABLE;
471 }
472 
nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg)473 NRF_STATIC_INLINE uint32_t nrf_qdec_enable_get(NRF_QDEC_Type const * p_reg)
474 {
475     return p_reg->ENABLE;
476 }
477 
nrf_qdec_int_enable(NRF_QDEC_Type * p_reg,uint32_t mask)478 NRF_STATIC_INLINE void nrf_qdec_int_enable(NRF_QDEC_Type * p_reg, uint32_t mask)
479 {
480     p_reg->INTENSET = mask; // writing 0 has no effect
481 }
482 
nrf_qdec_int_disable(NRF_QDEC_Type * p_reg,uint32_t mask)483 NRF_STATIC_INLINE void nrf_qdec_int_disable(NRF_QDEC_Type * p_reg, uint32_t mask)
484 {
485     p_reg->INTENCLR = mask; // writing 0 has no effect
486 }
487 
nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg,uint32_t mask)488 NRF_STATIC_INLINE uint32_t nrf_qdec_int_enable_check(NRF_QDEC_Type const * p_reg, uint32_t mask)
489 {
490     return p_reg->INTENSET & mask; // when read this register will return the value of INTEN.
491 }
492 
nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg)493 NRF_STATIC_INLINE void nrf_qdec_dbfen_enable(NRF_QDEC_Type * p_reg)
494 {
495     p_reg->DBFEN = NRF_QDEC_DBFEN_ENABLE;
496 }
497 
nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg)498 NRF_STATIC_INLINE void nrf_qdec_dbfen_disable(NRF_QDEC_Type * p_reg)
499 {
500     p_reg->DBFEN = NRF_QDEC_DBFEN_DISABLE;
501 }
502 
nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg)503 NRF_STATIC_INLINE uint32_t nrf_qdec_dbfen_get(NRF_QDEC_Type const * p_reg)
504 {
505     return p_reg->DBFEN;
506 }
507 
nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,uint32_t phase_a_pin,uint32_t phase_b_pin,uint32_t led_pin)508 NRF_STATIC_INLINE void nrf_qdec_pins_set(NRF_QDEC_Type * p_reg,
509                                          uint32_t        phase_a_pin,
510                                          uint32_t        phase_b_pin,
511                                          uint32_t        led_pin)
512 {
513 #if defined(QDEC_PSEL_A_CONNECT_Pos)
514     p_reg->PSEL.A = phase_a_pin;
515 #else
516     p_reg->PSELA = phase_a_pin;
517 #endif
518 
519 #if defined(QDEC_PSEL_B_CONNECT_Pos)
520     p_reg->PSEL.B = phase_b_pin;
521 #else
522     p_reg->PSELB = phase_b_pin;
523 #endif
524 
525 #if defined(QDEC_PSEL_LED_CONNECT_Pos)
526     p_reg->PSEL.LED = led_pin;
527 #else
528     p_reg->PSELLED = led_pin;
529 #endif
530 }
531 
nrf_qdec_pio_assign(NRF_QDEC_Type * p_reg,uint32_t psela,uint32_t pselb,uint32_t pselled)532 NRF_STATIC_INLINE void nrf_qdec_pio_assign(NRF_QDEC_Type * p_reg,
533                                            uint32_t        psela,
534                                            uint32_t        pselb,
535                                            uint32_t        pselled)
536 {
537     nrf_qdec_pins_set(p_reg, psela, pselb, pselled);
538 }
539 
nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg)540 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_a_pin_get(NRF_QDEC_Type const * p_reg)
541 {
542 #if defined(QDEC_PSEL_A_CONNECT_Pos)
543     return p_reg->PSEL.A;
544 #else
545     return p_reg->PSELA;
546 #endif
547 }
548 
nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg)549 NRF_STATIC_INLINE uint32_t nrf_qdec_phase_b_pin_get(NRF_QDEC_Type const * p_reg)
550 {
551 #if defined(QDEC_PSEL_B_CONNECT_Pos)
552     return p_reg->PSEL.B;
553 #else
554     return p_reg->PSELB;
555 #endif
556 }
557 
nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg)558 NRF_STATIC_INLINE uint32_t nrf_qdec_led_pin_get(NRF_QDEC_Type const * p_reg)
559 {
560 #if defined(QDEC_PSEL_LED_CONNECT_Pos)
561     return p_reg->PSEL.LED;
562 #else
563     return p_reg->PSELLED;
564 #endif
565 }
566 
nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg,nrf_qdec_task_t task)567 NRF_STATIC_INLINE void nrf_qdec_task_trigger(NRF_QDEC_Type * p_reg, nrf_qdec_task_t task)
568 {
569     *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)task) ) = 1;
570 }
571 
nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,nrf_qdec_task_t task)572 NRF_STATIC_INLINE uint32_t nrf_qdec_task_address_get(NRF_QDEC_Type const * p_reg,
573                                                      nrf_qdec_task_t       task)
574 {
575     return (uint32_t)( (uint8_t *)p_reg + (uint32_t)task);
576 }
577 
nrf_qdec_event_clear(NRF_QDEC_Type * p_reg,nrf_qdec_event_t event)578 NRF_STATIC_INLINE void nrf_qdec_event_clear(NRF_QDEC_Type * p_reg, nrf_qdec_event_t event)
579 {
580     *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)event) ) = 0;
581     nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
582 }
583 
nrf_qdec_event_check(NRF_QDEC_Type const * p_reg,nrf_qdec_event_t event)584 NRF_STATIC_INLINE bool nrf_qdec_event_check(NRF_QDEC_Type const * p_reg, nrf_qdec_event_t event)
585 {
586     return (bool)*(volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)event);
587 }
588 
nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,nrf_qdec_event_t event)589 NRF_STATIC_INLINE uint32_t nrf_qdec_event_address_get(NRF_QDEC_Type const * p_reg,
590                                                       nrf_qdec_event_t      event)
591 {
592     return (uint32_t)( (uint8_t *)p_reg + (uint32_t)event);
593 }
594 
nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg,uint32_t mask)595 NRF_STATIC_INLINE void nrf_qdec_shorts_enable(NRF_QDEC_Type * p_reg, uint32_t mask)
596 {
597     p_reg->SHORTS |= mask;
598 }
599 
nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg,uint32_t mask)600 NRF_STATIC_INLINE void nrf_qdec_shorts_disable(NRF_QDEC_Type * p_reg, uint32_t mask)
601 {
602     p_reg->SHORTS &= ~mask;
603 }
604 
nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper)605 NRF_STATIC_INLINE uint32_t nrf_qdec_sampleper_to_value(nrf_qdec_sampleper_t sampleper)
606 {
607     return (1 << (7 + sampleper));
608 }
609 
nrf_qdec_sampleper_set(NRF_QDEC_Type * p_reg,nrf_qdec_sampleper_t sampleper)610 NRF_STATIC_INLINE void nrf_qdec_sampleper_set(NRF_QDEC_Type *      p_reg,
611                                               nrf_qdec_sampleper_t sampleper)
612 {
613     p_reg->SAMPLEPER = sampleper;
614 }
615 
nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg)616 NRF_STATIC_INLINE nrf_qdec_sampleper_t nrf_qdec_sampleper_get(NRF_QDEC_Type const * p_reg)
617 {
618     return (nrf_qdec_sampleper_t)(p_reg->SAMPLEPER);
619 }
620 
nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg)621 NRF_STATIC_INLINE int32_t nrf_qdec_sample_get(NRF_QDEC_Type const * p_reg)
622 {
623     return p_reg->SAMPLE;
624 }
625 
nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg)626 NRF_STATIC_INLINE int32_t nrf_qdec_acc_get(NRF_QDEC_Type const * p_reg)
627 {
628     return p_reg->ACC;
629 }
630 
nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg)631 NRF_STATIC_INLINE int32_t nrf_qdec_accread_get(NRF_QDEC_Type const * p_reg)
632 {
633     return p_reg->ACCREAD;
634 }
635 
nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg)636 NRF_STATIC_INLINE uint32_t nrf_qdec_accdbl_get(NRF_QDEC_Type const * p_reg)
637 {
638     return p_reg->ACCDBL;
639 }
640 
nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg)641 NRF_STATIC_INLINE uint32_t nrf_qdec_accdblread_get(NRF_QDEC_Type const * p_reg)
642 {
643     return p_reg->ACCDBLREAD;
644 }
645 
nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg,uint32_t time_us)646 NRF_STATIC_INLINE void nrf_qdec_ledpre_set(NRF_QDEC_Type * p_reg, uint32_t time_us)
647 {
648     p_reg->LEDPRE = time_us;
649 }
650 
nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg)651 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpre_get(NRF_QDEC_Type const * p_reg)
652 {
653     return p_reg->LEDPRE;
654 }
655 
nrf_qdec_reportper_set(NRF_QDEC_Type * p_reg,nrf_qdec_reportper_t reportper)656 NRF_STATIC_INLINE void nrf_qdec_reportper_set(NRF_QDEC_Type *      p_reg,
657                                               nrf_qdec_reportper_t reportper)
658 {
659     p_reg->REPORTPER = reportper;
660 }
661 
nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg)662 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_get(NRF_QDEC_Type const * p_reg)
663 {
664     return p_reg->REPORTPER;
665 }
666 
nrf_qdec_reportper_to_value(uint32_t reportper)667 NRF_STATIC_INLINE uint32_t nrf_qdec_reportper_to_value(uint32_t reportper)
668 {
669     return (reportper == NRF_QDEC_REPORTPER_10) ? 10 : reportper * 40;
670 }
671 
nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg,nrf_qdec_ledpol_t pol)672 NRF_STATIC_INLINE void nrf_qdec_ledpol_set(NRF_QDEC_Type * p_reg, nrf_qdec_ledpol_t pol)
673 {
674     p_reg->LEDPOL = pol;
675 }
676 
nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg)677 NRF_STATIC_INLINE uint32_t nrf_qdec_ledpol_get(NRF_QDEC_Type const * p_reg)
678 {
679     return p_reg->LEDPOL;
680 }
681 
682 #endif // NRF_DECLARE_ONLY
683 
684 /** @} */
685 
686 #ifdef __cplusplus
687 }
688 #endif
689 
690 #endif // NRF_QDEC_H__
691