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