1 /*
2 * Copyright (c) 2015 - 2018, 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 /**@brief QDEC default configuration. */
65 #define NRFX_QDEC_DEFAULT_CONFIG \
66 { \
67 .reportper = (nrf_qdec_reportper_t)NRFX_QDEC_CONFIG_REPORTPER, \
68 .sampleper = (nrf_qdec_sampleper_t)NRFX_QDEC_CONFIG_SAMPLEPER, \
69 .psela = NRFX_QDEC_CONFIG_PIO_A, \
70 .pselb = NRFX_QDEC_CONFIG_PIO_B, \
71 .pselled = NRFX_QDEC_CONFIG_PIO_LED, \
72 .ledpre = NRFX_QDEC_CONFIG_LEDPRE, \
73 .ledpol = (nrf_qdec_ledpol_t)NRFX_QDEC_CONFIG_LEDPOL, \
74 .interrupt_priority = NRFX_QDEC_CONFIG_IRQ_PRIORITY, \
75 .dbfen = NRFX_QDEC_CONFIG_DBFEN, \
76 .sample_inten = NRFX_QDEC_CONFIG_SAMPLE_INTEN \
77 }
78
79 /**@brief QDEC sample event data.*/
80 typedef struct
81 {
82 int8_t value; /**< Sample value. */
83 } nrfx_qdec_sample_data_evt_t;
84
85 /**@brief QDEC report event data.*/
86 typedef struct
87 {
88 int16_t acc; /**< Accumulated transitions. */
89 uint16_t accdbl; /**< Accumulated double transitions. */
90 } nrfx_qdec_report_data_evt_t;
91
92 /**@brief QDEC event handler structure. */
93 typedef struct
94 {
95 nrf_qdec_event_t type;
96 union
97 {
98 nrfx_qdec_sample_data_evt_t sample; /**< Sample event data. */
99 nrfx_qdec_report_data_evt_t report; /**< Report event data. */
100 } data;
101 } nrfx_qdec_event_t;
102
103 /**@brief QDEC event handler.
104 * @param[in] event QDEC event structure.
105 */
106 typedef void (*nrfx_qdec_event_handler_t)(nrfx_qdec_event_t event);
107
108 /**@brief Function for initializing QDEC.
109 *
110 * @param[in] p_config Pointer to the structure with initial configuration.
111 * @param[in] event_handler Event handler provided by the user.
112 * Must not be NULL.
113 *
114 * @retval NRFX_SUCCESS If initialization was successful.
115 * @retval NRFX_ERROR_INVALID_STATE If QDEC was already initialized.
116 */
117 nrfx_err_t nrfx_qdec_init(nrfx_qdec_config_t const * p_config,
118 nrfx_qdec_event_handler_t event_handler);
119
120 /**@brief Function for uninitializing QDEC.
121 * @note Function asserts if module is uninitialized.
122 */
123 void nrfx_qdec_uninit(void);
124
125 /**@brief Function for enabling QDEC.
126 * @note Function asserts if module is uninitialized or enabled.
127 */
128 void nrfx_qdec_enable(void);
129
130 /**@brief Function for disabling QDEC.
131 * @note Function asserts if module is uninitialized or disabled.
132 */
133 void nrfx_qdec_disable(void);
134
135 /**@brief Function for reading accumulated transitions QDEC.
136 * @note Function asserts if module is not enabled.
137 * @note Accumulators are cleared after reading.
138 *
139 * @param[out] p_acc Pointer to store accumulated transitions.
140 * @param[out] p_accdbl Pointer to store accumulated double transitions.
141 */
142 void nrfx_qdec_accumulators_read(int16_t * p_acc, int16_t * p_accdbl);
143
144 /**
145 * @brief Function for returning the address of a specific QDEC task.
146 *
147 * @param task QDEC task.
148 *
149 * @return Task address.
150 */
nrfx_qdec_task_address_get(nrf_qdec_task_t task)151 __STATIC_INLINE uint32_t nrfx_qdec_task_address_get(nrf_qdec_task_t task)
152 {
153 return (uint32_t)nrf_qdec_task_address_get(task);
154 }
155
156 /**
157 * @brief Function for returning the address of a specific QDEC event.
158 *
159 * @param event QDEC event.
160 *
161 * @return Event address.
162 */
nrfx_qdec_event_address_get(nrf_qdec_event_t event)163 __STATIC_INLINE uint32_t nrfx_qdec_event_address_get(nrf_qdec_event_t event)
164 {
165 return (uint32_t)nrf_qdec_event_address_get(event);
166 }
167
168
169 void nrfx_qdec_irq_handler(void);
170
171 /** @} */
172
173 #ifdef __cplusplus
174 }
175 #endif
176
177 #endif // NRFX_QDEC_H__
178