amiro-os / hal / platforms / STM32 / qei_lld.h @ 58fe0e0b
History | View | Annotate | Download (7.26 KB)
1 |
#ifndef _QEI_LLD_H_
|
---|---|
2 |
#define _QEI_LLD_H_
|
3 |
|
4 |
#include "stm32_tim.h" |
5 |
|
6 |
#if HAL_USE_QEI || defined(__DOXYGEN__)
|
7 |
|
8 |
/*===========================================================================*/
|
9 |
/* Driver constants. */
|
10 |
/*===========================================================================*/
|
11 |
|
12 |
/**
|
13 |
* @brief Number of input channels per QEI driver.
|
14 |
*/
|
15 |
#define QEI_CHANNELS 2 |
16 |
|
17 |
/*===========================================================================*/
|
18 |
/* Driver pre-compile time settings. */
|
19 |
/*===========================================================================*/
|
20 |
|
21 |
/**
|
22 |
* @name Configuration options
|
23 |
* @{
|
24 |
*/
|
25 |
/**
|
26 |
* @brief QEID1 driver enable switch.
|
27 |
* @details If set to @p TRUE the support for QEID1 is included.
|
28 |
* @note The default is @p TRUE.
|
29 |
*/
|
30 |
#if !defined(STM32_QEI_USE_TIM1) || defined(__DOXYGEN__)
|
31 |
#define STM32_QEI_USE_TIM1 TRUE
|
32 |
#endif
|
33 |
|
34 |
/**
|
35 |
* @brief QEID2 driver enable switch.
|
36 |
* @details If set to @p TRUE the support for QEID2 is included.
|
37 |
* @note The default is @p TRUE.
|
38 |
*/
|
39 |
#if !defined(STM32_QEI_USE_TIM2) || defined(__DOXYGEN__)
|
40 |
#define STM32_QEI_USE_TIM2 TRUE
|
41 |
#endif
|
42 |
|
43 |
/**
|
44 |
* @brief QEID3 driver enable switch.
|
45 |
* @details If set to @p TRUE the support for QEID3 is included.
|
46 |
* @note The default is @p TRUE.
|
47 |
*/
|
48 |
#if !defined(STM32_QEI_USE_TIM3) || defined(__DOXYGEN__)
|
49 |
#define STM32_QEI_USE_TIM3 TRUE
|
50 |
#endif
|
51 |
|
52 |
/**
|
53 |
* @brief QEID4 driver enable switch.
|
54 |
* @details If set to @p TRUE the support for QEID4 is included.
|
55 |
* @note The default is @p TRUE.
|
56 |
*/
|
57 |
#if !defined(STM32_QEI_USE_TIM4) || defined(__DOXYGEN__)
|
58 |
#define STM32_QEI_USE_TIM4 TRUE
|
59 |
#endif
|
60 |
|
61 |
/**
|
62 |
* @brief QEID5 driver enable switch.
|
63 |
* @details If set to @p TRUE the support for QEID5 is included.
|
64 |
* @note The default is @p TRUE.
|
65 |
*/
|
66 |
#if !defined(STM32_QEI_USE_TIM5) || defined(__DOXYGEN__)
|
67 |
#define STM32_QEI_USE_TIM5 TRUE
|
68 |
#endif
|
69 |
|
70 |
/**
|
71 |
* @brief QEID8 driver enable switch.
|
72 |
* @details If set to @p TRUE the support for QEID8 is included.
|
73 |
* @note The default is @p TRUE.
|
74 |
*/
|
75 |
#if !defined(STM32_QEI_USE_TIM8) || defined(__DOXYGEN__)
|
76 |
#define STM32_QEI_USE_TIM8 TRUE
|
77 |
#endif
|
78 |
/** @} */
|
79 |
|
80 |
/*===========================================================================*/
|
81 |
/* Derived constants and error checks. */
|
82 |
/*===========================================================================*/
|
83 |
|
84 |
#if STM32_QEI_USE_TIM1 && !STM32_HAS_TIM1
|
85 |
#error "TIM1 not present in the selected device" |
86 |
#endif
|
87 |
|
88 |
#if STM32_QEI_USE_TIM2 && !STM32_HAS_TIM2
|
89 |
#error "TIM2 not present in the selected device" |
90 |
#endif
|
91 |
|
92 |
#if STM32_QEI_USE_TIM3 && !STM32_HAS_TIM3
|
93 |
#error "TIM3 not present in the selected device" |
94 |
#endif
|
95 |
|
96 |
#if STM32_QEI_USE_TIM4 && !STM32_HAS_TIM4
|
97 |
#error "TIM4 not present in the selected device" |
98 |
#endif
|
99 |
|
100 |
#if STM32_QEI_USE_TIM5 && !STM32_HAS_TIM5
|
101 |
#error "TIM5 not present in the selected device" |
102 |
#endif
|
103 |
|
104 |
#if STM32_QEI_USE_TIM8 && !STM32_HAS_TIM8
|
105 |
#error "TIM8 not present in the selected device" |
106 |
#endif
|
107 |
|
108 |
#if !STM32_QEI_USE_TIM1 && !STM32_QEI_USE_TIM2 && \
|
109 |
!STM32_QEI_USE_TIM3 && !STM32_QEI_USE_TIM4 && \ |
110 |
!STM32_QEI_USE_TIM5 && !STM32_QEI_USE_TIM8 |
111 |
#error "QEI driver activated but no TIM peripheral assigned" |
112 |
#endif
|
113 |
|
114 |
/*===========================================================================*/
|
115 |
/* Driver data structures and types. */
|
116 |
/*===========================================================================*/
|
117 |
|
118 |
/**
|
119 |
* @brief QEI driver mode.
|
120 |
*/
|
121 |
typedef enum { |
122 |
QEI_COUNT_BOTH = 0,
|
123 |
QEI_COUNT_CH1 = 1,
|
124 |
QEI_COUNT_CH2 = 2,
|
125 |
} qeimode_t; |
126 |
|
127 |
/**
|
128 |
* @brief QEI input mode.
|
129 |
*/
|
130 |
typedef enum { |
131 |
QEI_INPUT_NONINVERTED = 0, /**< Input channel noninverted.*/ |
132 |
QEI_INPUT_INVERTED = 1, /**< Input channel inverted.*/ |
133 |
} qeiinputmode_t; |
134 |
|
135 |
/**
|
136 |
* @brief QEI count type.
|
137 |
*/
|
138 |
typedef uint32_t qeicnt_t;
|
139 |
|
140 |
/**
|
141 |
* @brief Driver channel configuration structure.
|
142 |
*/
|
143 |
typedef struct { |
144 |
/**
|
145 |
* @brief Channel input logic.
|
146 |
*/
|
147 |
qeiinputmode_t mode; |
148 |
/* End of the mandatory fields.*/
|
149 |
} QEIChannelConfig; |
150 |
|
151 |
/**
|
152 |
* @brief Driver configuration structure.
|
153 |
*/
|
154 |
typedef struct { |
155 |
/**
|
156 |
* @brief Driver mode.
|
157 |
*/
|
158 |
qeimode_t mode; |
159 |
/**
|
160 |
* @brief Channels configurations.
|
161 |
*/
|
162 |
QEIChannelConfig channels[QEI_CHANNELS]; |
163 |
/**
|
164 |
* @brief Range in pulses.
|
165 |
*/
|
166 |
qeicnt_t range; |
167 |
/* End of the mandatory fields.*/
|
168 |
} QEIConfig; |
169 |
|
170 |
/**
|
171 |
* @brief Structure representing an QEI driver.
|
172 |
*/
|
173 |
struct QEIDriver {
|
174 |
/**
|
175 |
* @brief Driver state.
|
176 |
*/
|
177 |
qeistate_t state; |
178 |
/**
|
179 |
* @brief Current configuration data.
|
180 |
*/
|
181 |
const QEIConfig *config;
|
182 |
#if defined(QEI_DRIVER_EXT_FIELDS)
|
183 |
QEI_DRIVER_EXT_FIELDS |
184 |
#endif
|
185 |
/* End of the mandatory fields.*/
|
186 |
/**
|
187 |
* @brief Pointer to the TIMx registers block.
|
188 |
*/
|
189 |
stm32_tim_t *tim; |
190 |
}; |
191 |
|
192 |
/*===========================================================================*/
|
193 |
/* Driver macros. */
|
194 |
/*===========================================================================*/
|
195 |
|
196 |
/**
|
197 |
* @brief Returns the direction of the last transition.
|
198 |
* @details The direction is defined as boolean and is
|
199 |
* calculated at each transition on any input.
|
200 |
*
|
201 |
* @param[in] qeip pointer to the @p QEIDriver object
|
202 |
* @return The request direction.
|
203 |
* @retval FALSE Position counted up.
|
204 |
* @retval TRUE Position counted down.
|
205 |
*
|
206 |
* @iclass
|
207 |
*/
|
208 |
#define qei_lld_get_direction(qeip) !!((qeip)->tim->CR1 & TIM_CR1_DIR)
|
209 |
|
210 |
/**
|
211 |
* @brief Returns the position of the encoder.
|
212 |
* @details The position is defined as number of pulses since last reset.
|
213 |
*
|
214 |
* @param[in] qeip pointer to the @p QEIDriver object
|
215 |
* @return The number of pulses.
|
216 |
*
|
217 |
* @iclass
|
218 |
*/
|
219 |
#define qei_lld_get_position(qeip) ((qeip)->tim->CNT)
|
220 |
|
221 |
/**
|
222 |
* @brief Returns the range of the encoder.
|
223 |
* @details The range is defined as number of maximum pulse count.
|
224 |
*
|
225 |
* @param[in] qeip pointer to the @p QEIDriver object
|
226 |
* @return The number of pulses.
|
227 |
*
|
228 |
* @iclass
|
229 |
*/
|
230 |
#define qei_lld_get_range(qeip) ((qeip)->tim->ARR + 1) |
231 |
|
232 |
/*===========================================================================*/
|
233 |
/* External declarations. */
|
234 |
/*===========================================================================*/
|
235 |
|
236 |
#if STM32_QEI_USE_TIM1 && !defined(__DOXYGEN__)
|
237 |
extern QEIDriver QEID1;
|
238 |
#endif
|
239 |
|
240 |
#if STM32_QEI_USE_TIM2 && !defined(__DOXYGEN__)
|
241 |
extern QEIDriver QEID2;
|
242 |
#endif
|
243 |
|
244 |
#if STM32_QEI_USE_TIM3 && !defined(__DOXYGEN__)
|
245 |
extern QEIDriver QEID3;
|
246 |
#endif
|
247 |
|
248 |
#if STM32_QEI_USE_TIM4 && !defined(__DOXYGEN__)
|
249 |
extern QEIDriver QEID4;
|
250 |
#endif
|
251 |
|
252 |
#if STM32_QEI_USE_TIM5 && !defined(__DOXYGEN__)
|
253 |
extern QEIDriver QEID5;
|
254 |
#endif
|
255 |
|
256 |
#if STM32_QEI_USE_TIM8 && !defined(__DOXYGEN__)
|
257 |
extern QEIDriver QEID8;
|
258 |
#endif
|
259 |
|
260 |
#ifdef __cplusplus
|
261 |
extern "C" { |
262 |
#endif
|
263 |
void qei_lld_init(void); |
264 |
void qei_lld_start(QEIDriver *qeip);
|
265 |
void qei_lld_stop(QEIDriver *qeip);
|
266 |
void qei_lld_enable(QEIDriver *qeip);
|
267 |
void qei_lld_disable(QEIDriver *qeip);
|
268 |
#ifdef __cplusplus
|
269 |
} |
270 |
#endif
|
271 |
|
272 |
#endif /* HAL_USE_QEI */ |
273 |
|
274 |
#endif /* _QEI_LLD_H_ */ |