Statistics
| Branch: | Tag: | Revision:

amiro-os / kernel / patches / QEI-driver.patch @ 732a4657

History | View | Annotate | Download (28.212 KB)

1
diff --git a/os/hal/hal.mk b/os/hal/hal.mk
2
--- a/os/hal/hal.mk
3
+++ b/os/hal/hal.mk
4
@@ -54,6 +54,9 @@ endif
5
 ifneq ($(findstring HAL_USE_PWM TRUE,$(HALCONF)),)
6
 HALSRC += $(CHIBIOS)/os/hal/src/hal_pwm.c
7
 endif
8
+ifneq ($(findstring HAL_USE_QEI TRUE,$(HALCONF)),)
9
+HALSRC += $(CHIBIOS)/os/hal/src/hal_qei.c
10
+endif
11
 ifneq ($(findstring HAL_USE_RTC TRUE,$(HALCONF)),)
12
 HALSRC += $(CHIBIOS)/os/hal/src/hal_rtc.c
13
 endif
14
@@ -104,6 +107,7 @@ HALSRC = $(CHIBIOS)/os/hal/src/hal.c \
15
          $(CHIBIOS)/os/hal/src/hal_mmc_spi.c \
16
          $(CHIBIOS)/os/hal/src/hal_pal.c \
17
          $(CHIBIOS)/os/hal/src/hal_pwm.c \
18
+         $(CHIBIOS)/os/hal/src/hal_qei.c \
19
          $(CHIBIOS)/os/hal/src/hal_rtc.c \
20
          $(CHIBIOS)/os/hal/src/hal_sdc.c \
21
          $(CHIBIOS)/os/hal/src/hal_serial.c \
22
diff --git a/os/hal/include/hal.h b/os/hal/include/hal.h
23
--- a/os/hal/include/hal.h
24
+++ b/os/hal/include/hal.h
25
@@ -74,6 +74,10 @@
26
 #define HAL_USE_PWM                         FALSE
27
 #endif
28
 
29
+#if !defined(HAL_USE_QEI)
30
+#define HAL_USE_QEI                         FALSE
31
+#endif
32
+
33
 #if !defined(HAL_USE_RTC)
34
 #define HAL_USE_RTC                         FALSE
35
 #endif
36
@@ -142,6 +146,7 @@
37
 #include "hal_icu.h"
38
 #include "hal_mac.h"
39
 #include "hal_pwm.h"
40
+#include "hal_qei.h"
41
 #include "hal_rtc.h"
42
 #include "hal_serial.h"
43
 #include "hal_sdc.h"
44
diff --git a/os/hal/include/hal_qei.h b/os/hal/include/hal_qei.h
45
new file mode 100644
46
--- /dev/null
47
+++ b/os/hal/include/hal_qei.h
48
@@ -0,0 +1,148 @@
49
+/*
50
+AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
51
+Copyright (C) 2016..2019  Thomas Schöpping et al.
52
+
53
+This program is free software: you can redistribute it and/or modify
54
+it under the terms of the GNU General Public License as published by
55
+the Free Software Foundation, either version 3 of the License, or
56
+(at your option) any later version.
57
+
58
+This program is distributed in the hope that it will be useful,
59
+but WITHOUT ANY WARRANTY; without even the implied warranty of
60
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
61
+GNU General Public License for more details.
62
+
63
+You should have received a copy of the GNU General Public License
64
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
65
+*/
66
+
67
+/**
68
+ * @file    hal_qei.h
69
+ * @brief   QEI Driver macros and structures.
70
+ *
71
+ * @addtogroup QEI
72
+ * @{
73
+ */
74
+
75
+#ifndef HAL_QEI_H
76
+#define HAL_QEI_H
77
+
78
+#if (HAL_USE_QEI == TRUE) || defined(__DOXYGEN__)
79
+
80
+/*===========================================================================*/
81
+/* Driver constants.                                                         */
82
+/*===========================================================================*/
83
+
84
+/*===========================================================================*/
85
+/* Driver pre-compile time settings.                                         */
86
+/*===========================================================================*/
87
+
88
+/*===========================================================================*/
89
+/* Derived constants and error checks.                                       */
90
+/*===========================================================================*/
91
+
92
+/*===========================================================================*/
93
+/* Driver data structures and types.                                         */
94
+/*===========================================================================*/
95
+
96
+/**
97
+ * @brief   Driver state machine possible states.
98
+ */
99
+typedef enum {
100
+  QEI_UNINIT = 0,                   /**< Not initialized.                   */
101
+  QEI_STOP = 1,                     /**< Stopped.                           */
102
+  QEI_READY = 2,                    /**< Ready.                             */
103
+  QEI_ACTIVE = 4,                   /**< Active.                            */
104
+} qeistate_t;
105
+
106
+/**
107
+ * @brief   Type of a structure representing an QEI driver.
108
+ */
109
+typedef struct QEIDriver QEIDriver;
110
+
111
+#include "hal_qei_lld.h"
112
+
113
+/*===========================================================================*/
114
+/* Driver macros.                                                            */
115
+/*===========================================================================*/
116
+
117
+/**
118
+ * @name    Macro Functions
119
+ * @{
120
+ */
121
+/**
122
+ * @brief   Enables the quadrature encoder.
123
+ *
124
+ * @param[in] qeip      pointer to the @p QEIDriver object
125
+ *
126
+ * @iclass
127
+ */
128
+#define qeiEnableI(qeip) qei_lld_enable(qeip)
129
+
130
+/**
131
+ * @brief   Disables the quadrature encoder.
132
+ *
133
+ * @param[in] qeip      pointer to the @p QEIDriver object
134
+ *
135
+ * @iclass
136
+ */
137
+#define qeiDisableI(qeip) qei_lld_disable(qeip)
138
+
139
+/**
140
+ * @brief   Returns the direction of the last transition.
141
+ * @details The direction is defined as boolean and is
142
+ *          calculated at each transition on any input.
143
+ *
144
+ * @param[in] qeip      pointer to the @p QEIDriver object
145
+ * @return              The request direction.
146
+ * @retval FALSE        Position counted up.
147
+ * @retval TRUE         Position counted down.
148
+ * @iclass
149
+ */
150
+#define qeiGetDirectionI(qeip) qei_lld_get_direction(qeip)
151
+
152
+/**
153
+ * @brief   Returns the position of the encoder.
154
+ * @details The position is defined as number of pulses since last reset.
155
+ *
156
+ * @param[in] qeip      pointer to the @p QEIDriver object
157
+ * @return              The number of pulses.
158
+ *
159
+ * @iclass
160
+ */
161
+#define qeiGetPositionI(qeip) qei_lld_get_position(qeip)
162
+
163
+/**
164
+ * @brief   Returns the range of the encoder.
165
+ * @details The range is defined as number of maximum pulse count.
166
+ *
167
+ * @param[in] qeip      pointer to the @p QEIDriver object
168
+ * @return              The number of pulses.
169
+ *
170
+ * @iclass
171
+ */
172
+#define qeiGetRangeI(qeip) qei_lld_get_range(qeip)
173
+/** @} */
174
+
175
+/*===========================================================================*/
176
+/* External declarations.                                                    */
177
+/*===========================================================================*/
178
+
179
+#ifdef __cplusplus
180
+extern "C" {
181
+#endif
182
+  void qeiInit(void);
183
+  void qeiObjectInit(QEIDriver *qeip);
184
+  void qeiStart(QEIDriver *qeip, const QEIConfig *config);
185
+  void qeiStop(QEIDriver *qeip);
186
+  void qeiEnable(QEIDriver *qeip);
187
+  void qeiDisable(QEIDriver *qeip);
188
+#ifdef __cplusplus
189
+}
190
+#endif
191
+
192
+#endif /* HAL_USE_QEI == TRUE */
193
+
194
+#endif /* HAL_QEI_H */
195
+
196
+/** @} */
197
diff --git a/os/hal/ports/STM32/LLD/TIMv1/driver.mk b/os/hal/ports/STM32/LLD/TIMv1/driver.mk
198
--- a/os/hal/ports/STM32/LLD/TIMv1/driver.mk
199
+++ b/os/hal/ports/STM32/LLD/TIMv1/driver.mk
200
@@ -10,10 +10,14 @@ endif
201
 ifneq ($(findstring HAL_USE_PWM TRUE,$(HALCONF)),)
202
 PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/TIMv1/hal_pwm_lld.c
203
 endif
204
+ifneq ($(findstring HAL_USE_QEI TRUE,$(HALCONF)),)
205
+PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/TIMv1/hal_qei_lld.c
206
+endif
207
 else
208
 PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/TIMv1/hal_gpt_lld.c
209
 PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/TIMv1/hal_icu_lld.c
210
 PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/TIMv1/hal_pwm_lld.c
211
+PLATFORMSRC += $(CHIBIOS)/os/hal/ports/STM32/LLD/TIMv1/hal_qei_lld.c
212
 endif
213
 
214
 PLATFORMINC += $(CHIBIOS)/os/hal/ports/STM32/LLD/TIMv1
215
diff --git a/os/hal/ports/STM32/LLD/TIMv1/hal_qei_lld.c b/os/hal/ports/STM32/LLD/TIMv1/hal_qei_lld.c
216
new file mode 100644
217
--- /dev/null
218
+++ b/os/hal/ports/STM32/LLD/TIMv1/hal_qei_lld.c
219
@@ -0,0 +1,304 @@
220
+/*
221
+AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
222
+Copyright (C) 2016..2019  Thomas Schöpping et al.
223
+
224
+This program is free software: you can redistribute it and/or modify
225
+it under the terms of the GNU General Public License as published by
226
+the Free Software Foundation, either version 3 of the License, or
227
+(at your option) any later version.
228
+
229
+This program is distributed in the hope that it will be useful,
230
+but WITHOUT ANY WARRANTY; without even the implied warranty of
231
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
232
+GNU General Public License for more details.
233
+
234
+You should have received a copy of the GNU General Public License
235
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
236
+*/
237
+
238
+/**
239
+ * @file    STM32/hal_qei_lld.c
240
+ * @brief   STM32 QEI subsystem low level driver.
241
+ *
242
+ * @addtogroup QEI
243
+ * @{
244
+ */
245
+
246
+#include "hal.h"
247
+
248
+#if (HAL_USE_QEI == TRUE) || defined(__DOXYGEN__)
249
+
250
+/*===========================================================================*/
251
+/* Driver local definitions.                                                 */
252
+/*===========================================================================*/
253
+
254
+/*===========================================================================*/
255
+/* Driver exported variables.                                                */
256
+/*===========================================================================*/
257
+
258
+/**
259
+ * @brief   QEID1 driver identifier.
260
+ * @note    The driver QEID1 allocates the complex timer TIM1 when enabled.
261
+ */
262
+#if STM32_QEI_USE_TIM1 || defined(__DOXYGEN__)
263
+QEIDriver QEID1;
264
+#endif
265
+
266
+/**
267
+ * @brief   QEID2 driver identifier.
268
+ * @note    The driver QEID1 allocates the timer TIM2 when enabled.
269
+ */
270
+#if STM32_QEI_USE_TIM2 || defined(__DOXYGEN__)
271
+QEIDriver QEID2;
272
+#endif
273
+
274
+/**
275
+ * @brief   QEID3 driver identifier.
276
+ * @note    The driver QEID1 allocates the timer TIM3 when enabled.
277
+ */
278
+#if STM32_QEI_USE_TIM3 || defined(__DOXYGEN__)
279
+QEIDriver QEID3;
280
+#endif
281
+
282
+/**
283
+ * @brief   QEID4 driver identifier.
284
+ * @note    The driver QEID4 allocates the timer TIM4 when enabled.
285
+ */
286
+#if STM32_QEI_USE_TIM4 || defined(__DOXYGEN__)
287
+QEIDriver QEID4;
288
+#endif
289
+
290
+/**
291
+ * @brief   QEID5 driver identifier.
292
+ * @note    The driver QEID5 allocates the timer TIM5 when enabled.
293
+ */
294
+#if STM32_QEI_USE_TIM5 || defined(__DOXYGEN__)
295
+QEIDriver QEID5;
296
+#endif
297
+
298