1 /**
2  * \file
3  *
4  * \brief SAM QUAD DECODER Driver for SAMB11
5  *
6  * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #ifndef QUAD_DECODER_H_INCLUDED
47 #define QUAD_DECODER_H_INCLUDED
48 
49 /**
50  * \defgroup asfdoc_samb_quad_decoder_group SAM Quadrature Decoder
51  *
52  * This driver for Atmel&reg; | SMART SAM devices provides an interface for the
53  * configuration and management of the device's Quadrature Decoder functionality.
54  *
55  * The following peripherals are used by this module:
56  *  - Quadrature Decoder
57  *
58  * The following devices can use this module:
59  *  - Atmel | SMART SAM B11
60  *
61  * The outline of this documentation is as follows:
62  *  - \ref asfdoc_samb_quad_decoder_prerequisites
63  *  - \ref asfdoc_samb_quad_decoder_module_overview
64  *  - \ref asfdoc_samb_quad_decoder_special_considerations
65  *  - \ref asfdoc_samb_quad_decoder_extra_info
66  *  - \ref asfdoc_samb_quad_decoder_examples
67  *  - \ref asfdoc_samb_quad_decoder_api_overview
68  *
69  *
70  * \section asfdoc_samb_quad_decoder_prerequisites Prerequisites
71  *
72  * There are no prerequisites for this module.
73  *
74  *
75  * \section asfdoc_samb_quad_decoder_module_overview Module Overview
76  * A Quadrature Decoder is used to decode the output of a Quadrature Encoder,
77  * it can be used for precision measurement of speed, acceleration, and position
78  * of a motor's rotor and with rotary knobs to determine user input. The SAMB11
79  * contains 3 Quadrature Decoder. Each one may be programmed independently.
80  * The Quadrature Decoder require initialization prior to being enabled.
81  * Quadrature Decoder is a flexible fully configurable module which has the following features:
82  *
83  *   - 3 independent configurable Quadrature Decoder blocks.
84  *
85  *   - 16-bits resolution counters.
86  *
87  *   - 16-bits resolution Lower and Upper threshold control values.
88  *
89  *   - Input clock selection (only for MP).
90  *
91  * \subsection asfdoc_samb_quad_decoder_module_overview_thresholds Setting Thresholds
92  * The Quadrature Decoder have lower and upper thresholds. These are used for interrupt
93  * generation. The interrupt is generated whenever the counter is greater than or equal
94  * to the higher threshold and less than or equal to the lower threshold. Setting each
95  * module Higher and Lower threshold values as described below:
96  *
97  * \anchor asfdoc_sam0_quad_decoder_thresholds
98  * <table>
99  *  <caption>Quad-Decoder Register Thresholds</caption>
100  *  <tr>
101  *    <th>Filed name</th>
102  *    <th>Bits range</th>
103  *    <th>Description</th>
104  *  </tr>
105  *  <tr>
106  *    <td>Upper</td>
107  *    <td>[15 : 0]</td>
108  *    <td>Upper threshold value</td>
109  *  </tr>
110  *  <tr>
111  *    <td>Lower</td>
112  *    <td>[31 : 16]</td>
113  *    <td>Lower threshold value</td>
114  *  </tr>
115  * </table>
116  *
117  * \subsection asfdoc_samb_quad_decoder_module_overview_clock Setting Clock
118  * The Quadrature Decoder have 4 different clock options to support different
119  * application requirements. The possible options are 26 MHz, 13 MHz, 6.5 MHz
120  * and 3.25 MHz This clock is used to sample the incoming Quad Decoder inputs.
121  *
122  * \section asfdoc_samb_quad_decoder_special_considerations Special Considerations
123  *
124  * There are no special considerations for this module.
125  *
126  * \section asfdoc_samb_quad_decoder_extra_info Extra Information
127  *
128  * For extra information, see \ref asfdoc_samb_quad_decoder_extra. This includes:
129  *  - \ref asfdoc_samb_quad_decoder_extra_acronyms
130  *  - \ref asfdoc_samb_quad_decoder_extra_dependencies
131  *  - \ref asfdoc_samb_quad_decoder_extra_errata
132  *  - \ref asfdoc_samb_quad_decoder_extra_history
133  *
134  *
135  * \section asfdoc_samb_quad_decoder_examples Examples
136  *
137  * For a list of examples related to this driver, see
138  * \ref asfdoc_samb_quad_decoder_exqsg.
139  *
140  *
141  * \section asfdoc_samb_quad_decoder_api_overview API Overview
142  * @{
143  */
144 
145 #include <compiler.h>
146 #include "gpio.h"
147 
148 #ifdef __cplusplus
149 extern "C" {
150 #endif
151 
152 /** Type definition for a DUALTIMER module callback function. */
153 typedef void (*quad_decoder_callback_t)(void);
154 
155 /**
156  * \brief QUAD DECODER index number
157  *
158  * Values is the axis(x,y,z) of QUAD DECODER instance.
159  */
160 enum quad_decoder_axis {
161 	/** AXIS X */
162 	QDEC_AXIS_X = 0,
163 	/** AXIS Y */
164 	QDEC_AXIS_Y,
165 	/** AXIS Z */
166 	QDEC_AXIS_Z,
167 };
168 
169 /**
170  * \brief QUAD DECODER module clock input
171  *
172  * QUAD DECODER module clock.
173  *
174  */
175 enum quad_decoder_clock_input {
176 	/** Source from clock input 0: 26MHz */
177 	QDEC_CLK_INPUT_0 = 0,
178 	/** Source from clock input 1: 13MHz */
179 	QDEC_CLK_INPUT_1,
180 	/** Source from clock input 2: 6.5MHz */
181 	QDEC_CLK_INPUT_2,
182 	/** Source from clock input 3: 3MHz */
183 	QDEC_CLK_INPUT_3,
184 };
185 
186 /**
187  * \brief Quad Decoder private configuration structure.
188  *
189  * Private configuration struct for Quad Decoder instance.
190  */
191 struct quad_decoder_private_config {
192 	/** Quad Decoder Source Clock Frequency Select */
193 	enum quad_decoder_clock_input clock_sel;
194 	/** Upper Threshold of counter for Quad Decoder */
195 	int16_t threshold_upper;
196 	/** Lower Threshold of counter for Quad Decoder */
197 	int16_t threshold_lower;
198 	/** Dec_in_a pin number */
199 	/** Dec_in_b pin number */
200 	uint32_t pin_number_pad[2];
201 	/** Dec_in_a pinmux selection */
202 	/** Dec_in_b pinmux selection */
203 	uint32_t pinmux_sel_pad[2];
204 };
205 
206 /**
207  * \brief QUAD DECODER configuration structure.
208  *
209  * Configuration struct for a QUAD DECODER instance.
210  * This structure should be initialized by the
211  * \ref quad_decoder_get_config_defaults function
212  * before being modified by the user application.
213  */
214 struct quad_decoder_config {
215 	uint8_t qdec_enalbe;
216 	struct quad_decoder_private_config qdec0;
217 	struct quad_decoder_private_config qdec1;
218 	struct quad_decoder_private_config qdec2;
219 };
220 
221 /**
222  * \name Configuration and Initialization
223  * @{
224  */
225 void quad_decoder_get_config_defaults(
226 		struct quad_decoder_config *config);
227 void quad_decoder_init(const struct quad_decoder_config *config);
228 /** @} */
229 
230 /**
231  * \name Enable and disable module
232  * @{
233  */
234 void quad_decoder_enable(enum quad_decoder_axis qdec);
235 void quad_decoder_disable(enum quad_decoder_axis qdec);
236 /** @} */
237 
238 /**
239  * \name Get value
240  * @{
241  */
242 int16_t quad_decoder_get_counter(enum quad_decoder_axis qdec);
243 /** @} */
244 
245 /**
246  * \name Callback register/unregister
247  * @{
248  */
249 void quad_decoder_register_callback(enum quad_decoder_axis qdec,
250 		quad_decoder_callback_t fun);
251 void quad_decoder_unregister_callback(enum quad_decoder_axis qdec);
252 /** @} */
253 
254 /** @}*/
255 #ifdef __cplusplus
256 }
257 #endif
258 
259 /**
260  * \page asfdoc_samb_quad_decoder_extra Extra Information for Quadrature Decoder Driver
261  *
262  * \section asfdoc_samb_quad_decoder_extra_acronyms Acronyms
263  * Below is a table listing the acronyms used in this module, along with their
264  * intended meanings.
265  * <table>
266  *	<tr>
267  *		<th>Acronym</th>
268  *		<th>Description</th>
269  *	</tr>
270  *	<tr>
271  *		<td>Quadrature Decoder</td>
272  *		<td>Quadrature Decoder</td>
273  *	</tr>
274  * </table>
275  *
276  * \section asfdoc_samb_quad_decoder_extra_dependencies Dependencies
277  * There are no dependencies related to this driver.
278  *
279  *
280  * \section asfdoc_samb_quad_decoder_extra_errata Errata
281  * There are no errata related to this driver.
282  *
283  *
284  * \section asfdoc_samb_quad_decoder_extra_history Module History
285  * An overview of the module history is presented in the table below, with
286  * details on the enhancements and fixes made to the module since its first
287  * release. The current version of this corresponds to the newest version in
288  * the table.
289  *
290  * <table>
291  *  <tr>
292  *      <th>Changelog</th>
293  *  </tr>
294  *  <tr>
295  *      <td>Initial Release</td>
296  *  </tr>
297  * </table>
298  */
299 
300 /**
301  * \page asfdoc_samb_quad_decoder_exqsg Examples for Quadrature Decoder Driver
302  *
303  * This is a list of the available Quick Start guides (QSGs) and example
304  * applications for \ref asfdoc_samb_quad_decoder_group. QSGs are simple examples with
305  * step-by-step instructions to configure and use this driver in a selection of
306  * use cases. Note that QSGs can be compiled as a standalone application or be
307  * added to the user application.
308  *
309  *  - \subpage asfdoc_samb_quad_decoder_basic_use_case
310  *
311  * \page asfdoc_samb_quad_decoder_document_revision_history Document Revision History
312  *
313  * <table>
314  *  <tr>
315  *    <th>Doc. Rev.</td>
316  *    <th>Date</td>
317  *    <th>Comments</td>
318  *  </tr>
319  *  <tr>
320  *    <td>A</td>
321  *    <td>09/2015</td>
322  *    <td>Initial release</td>
323  *  </tr>
324  * </table>
325  */
326 
327 #endif