1 /*
2  * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRFX_QDEC_H__
33 #define NRFX_QDEC_H__
34 
35 #include <nrfx.h>
36 #include <hal/nrf_qdec.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * @defgroup nrfx_qdec QDEC driver
44  * @{
45  * @ingroup nrf_qdec
46  * @brief   Quadrature Decoder (QDEC) peripheral driver.
47  */
48 
49 /** @brief QDEC configuration structure. */
50 typedef struct
51 {
52     nrf_qdec_reportper_t reportper;          /**< Report period in samples. */
53     nrf_qdec_sampleper_t sampleper;          /**< Sampling period in microseconds. */
54     uint32_t             psela;              /**< Pin number for A input. */
55     uint32_t             pselb;              /**< Pin number for B input. */
56     uint32_t             pselled;            /**< Pin number for LED output. */
57     uint32_t             ledpre;             /**< Time (in microseconds) how long LED is switched on before sampling. */
58     nrf_qdec_ledpol_t    ledpol;             /**< Active LED polarity. */
59     bool                 dbfen;              /**< State of debouncing filter. */
60     bool                 sample_inten;       /**< Enabling sample ready interrupt. */
61     uint8_t              interrupt_priority; /**< QDEC interrupt priority. */
62 } nrfx_qdec_config_t;
63 
64 /**
65  * @brief QDEC driver default configuration.
66  *
67  * This configuration sets up QDEC with the following options:
68  * - report period: 10 samples
69  * - sampling period: 16384 us
70  * - LED enabled for 500 us before sampling
71  * - LED polarity: active high
72  * - debouncing filter disabled
73  * - sample ready interrupt disabled
74  *
75  * @param[in] _pin_a   Pin for A encoder channel input.
76  * @param[in] _pin_b   Pin for B encoder channel input.
77  * @param[in] _pin_led Pin for LED output.
78  */
79 #define NRFX_QDEC_DEFAULT_CONFIG(_pin_a, _pin_b, _pin_led)           \
80     {                                                                \
81         .reportper          = NRF_QDEC_REPORTPER_10,                 \
82         .sampleper          = NRF_QDEC_SAMPLEPER_16384us,            \
83         .psela              = _pin_a,                                \
84         .pselb              = _pin_b,                                \
85         .pselled            = _pin_led,                              \
86         .ledpre             = 500,                                   \
87         .ledpol             = NRF_QDEC_LEPOL_ACTIVE_HIGH,            \
88         .dbfen              = NRF_QDEC_DBFEN_DISABLE,                \
89         .sample_inten       = false,                                 \
90         .interrupt_priority = NRFX_QDEC_DEFAULT_CONFIG_IRQ_PRIORITY  \
91     }
92 
93 /** @brief QDEC sample event data. */
94 typedef struct
95 {
96     int8_t value; /**< Sample value. */
97 } nrfx_qdec_sample_data_evt_t;
98 
99 /** @brief QDEC report event data. */
100 typedef struct
101 {
102     int16_t acc;     /**< Accumulated transitions. */
103     uint16_t accdbl; /**< Accumulated double transitions. */
104 } nrfx_qdec_report_data_evt_t;
105 
106 /** @brief QDEC event handler structure. */
107 typedef struct
108 {
109     nrf_qdec_event_t  type; /**< Event type. */
110     union
111     {
112         nrfx_qdec_sample_data_evt_t sample; /**< Sample event data. */
113         nrfx_qdec_report_data_evt_t report; /**< Report event data. */
114     } data;                                 /**< Union to store event data. */
115 } nrfx_qdec_event_t;
116 
117 /**
118  * @brief QDEC event handler.
119  *
120  * @param[in] event QDEC event structure.
121  */
122 typedef void (*nrfx_qdec_event_handler_t)(nrfx_qdec_event_t event);
123 
124 /**
125  * @brief Function for initializing QDEC.
126  *
127  * @param[in] p_config      Pointer to the structure with the initial configuration.
128  * @param[in] event_handler Event handler provided by the user.
129  *                          Must not be NULL.
130  *
131  * @retval NRFX_SUCCESS             Initialization was successful.
132  * @retval NRFX_ERROR_INVALID_STATE The QDEC was already initialized.
133  */
134 nrfx_err_t nrfx_qdec_init(nrfx_qdec_config_t const * p_config,
135                           nrfx_qdec_event_handler_t  event_handler);
136 
137 /**
138  * @brief Function for uninitializing QDEC.
139  *
140  * @note Function asserts if module is uninitialized.
141  */
142 void nrfx_qdec_uninit(void);
143 
144 /**
145  * @brief Function for enabling QDEC.
146  *
147  * @note Function asserts if module is uninitialized or enabled.
148  */
149 void nrfx_qdec_enable(void);
150 
151 /**
152  * @brief Function for disabling QDEC.
153  *
154  * @note Function asserts if module is uninitialized or disabled.
155  */
156 void nrfx_qdec_disable(void);
157 
158 /**
159  * @brief Function for reading accumulated transitions from the QDEC peripheral.
160  *
161  * @note Function asserts if module is not enabled.
162  * @note Accumulators are cleared after reading.
163  *
164  * @param[out] p_acc    Pointer to store the accumulated transitions.
165  * @param[out] p_accdbl Pointer to store the accumulated double transitions.
166  */
167 void nrfx_qdec_accumulators_read(int16_t * p_acc, int16_t * p_accdbl);
168 
169 /**
170  * @brief Function for returning the address of the specified QDEC task.
171  *
172  * @param task QDEC task.
173  *
174  * @return Task address.
175  */
176 NRFX_STATIC_INLINE uint32_t nrfx_qdec_task_address_get(nrf_qdec_task_t task);
177 
178 /**
179  * @brief Function for returning the address of the specified QDEC event.
180  *
181  * @param event QDEC event.
182  *
183  * @return Event address.
184  */
185 NRFX_STATIC_INLINE uint32_t nrfx_qdec_event_address_get(nrf_qdec_event_t event);
186 
187 #ifndef NRFX_DECLARE_ONLY
nrfx_qdec_task_address_get(nrf_qdec_task_t task)188 NRFX_STATIC_INLINE uint32_t nrfx_qdec_task_address_get(nrf_qdec_task_t task)
189 {
190     return nrf_qdec_task_address_get(NRF_QDEC, task);
191 }
192 
nrfx_qdec_event_address_get(nrf_qdec_event_t event)193 NRFX_STATIC_INLINE uint32_t nrfx_qdec_event_address_get(nrf_qdec_event_t event)
194 {
195     return nrf_qdec_event_address_get(NRF_QDEC, event);
196 }
197 #endif // NRFX_DECLARE_ONLY
198 
199 /** @} */
200 
201 
202 void nrfx_qdec_irq_handler(void);
203 
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif // NRFX_QDEC_H__
210