aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/NRF5/LLD/hal_qei_lld.h
blob: 85c96a5030109aaddb860d6060630d6b5c806c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
    ChibiOS - Copyright (C) 2016..2016 Stéphane D'Alu

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    NRF51/hal_qei_lld.h
 * @brief   NRF51 QEI subsystem low level driver header.
 *
 * @note    Not tested with LED pin
 *
 * @note    Pins are configured as input with no pull.
 *
 * @addtogroup QEI
 * @{
 */

#ifndef HAL_QEI_LLD_H
#define HAL_QEI_LLD_H

#if (HAL_USE_QEI == TRUE) || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver constants.                                                         */
/*===========================================================================*/

/**
 * @brief For LED active on LOW
 */
#define QEI_LED_POLARITY_LOW   0

/**
 * @brief For LED active on HIGH
 */
#define QEI_LED_POLARITY_HIGH  1

/**
 * @brief Mininum usable value for defining counter underflow
 */
#define QEI_COUNT_MIN (-2147483648)

/**
 * @brief Maximum usable value for defining counter overflow
 */
#define QEI_COUNT_MAX ( 2147483647)



/*===========================================================================*/
/* Driver pre-compile time settings.                                         */
/*===========================================================================*/

/**
 * @name    Configuration options
 * @{
 */
/**
 * @brief   LED control enable switch.
 * @details If set to @p TRUE the support for LED control
 *          is included.
 * @note    The default is @p FALSE.
 */
#if !defined(NRF5_QEI_USE_LED) || defined(__DOXYGEN__)
#define NRF5_QEI_USE_LED                   FALSE
#endif

/**
 * @brief   Accumulator overflow notification enable switch.
 * @details If set to @p TRUE the support for accumulator overflow
 *          is included.
 * @note    The default is @p FALSE.
 */
#if !defined(NRF5_QEI_USE_ACC_OVERFLOWED_CB) || defined(__DOXYGEN__)
#define NRF5_QEI_USE_ACC_OVERFLOWED_CB       FALSE
#endif

/**
 * @brief   QEID1 driver enable switch.
 * @details If set to @p TRUE the support for QEID1 is included.
 * @note    The default is @p FALSE.
 */
#if !defined(NRF5_QEI_USE_QDEC0) || defined(__DOXYGEN__)
#define NRF5_QEI_USE_QDEC0                 FALSE
#endif

/**
 * @brief   QEID interrupt priority level setting for QDEC0.
 */
#if !defined(NRF5_QEI_QDEC0_IRQ_PRIORITY) || defined(__DOXYGEN__)
#define NRF5_QEI_QDEC0_IRQ_PRIORITY              2
#endif
/** @} */

/*===========================================================================*/
/* Derived constants and error checks.                                       */
/*===========================================================================*/

#if NRF5_QEI_USE_QDEC0 == FALSE
#error "Requesting QEI driver, but no QDEC peripheric attached"
#endif

#if NRF5_QEI_USE_QDEC0 &&					\
    !OSAL_IRQ_IS_VALID_PRIORITY(NRF5_QEI_QDEC0_IRQ_PRIORITY)
#error "Invalid IRQ priority assigned to QDEC0"
#endif


/*===========================================================================*/
/* Driver data structures and types.                                         */
/*===========================================================================*/

/**
 * @brief   QEI count mode.
 */
typedef enum {
  QEI_MODE_QUADRATURE = 0,          /**< Quadrature encoder mode.           */
} qeimode_t;

/**
 * @brief   QEI resolution.
 */
typedef enum {
  QEI_RESOLUTION_128us   = 0x00UL,  /**<   128us sample period. */
  QEI_RESOLUTION_256us   = 0x01UL,  /**<   256us sample period. */
  QEI_RESOLUTION_512us   = 0x02UL,  /**<   512us sample period. */
  QEI_RESOLUTION_1024us  = 0x03UL,  /**<  1024us sample period. */
  QEI_RESOLUTION_2048us  = 0x04UL,  /**<  2048us sample period. */
  QEI_RESOLUTION_4096us  = 0x05UL,  /**<  4096us sample period. */
  QEI_RESOLUTION_8192us  = 0x06UL,  /**<  8192us sample period. */
  QEI_RESOLUTION_16384us = 0x07UL,  /**< 16384us sample period. */
} qeiresolution_t;

/**
 * @brief   Clusters of samples.
 */
typedef enum {
  QEI_REPORT_10          = 0x00UL,  /**<  10 samples per report. */
  QEI_REPORT_40          = 0x01UL,  /**<  40 samples per report. */
  QEI_REPORT_80          = 0x02UL,  /**<  80 samples per report. */
  QEI_REPORT_120         = 0x03UL,  /**< 120 samples per report. */
  QEI_REPORT_160         = 0x04UL,  /**< 160 samples per report. */
  QEI_REPORT_200         = 0x05UL,  /**< 200 samples per report. */
  QEI_REPORT_240         = 0x06UL,  /**< 240 samples per report. */
  QEI_REPORT_280         = 0x07UL,  /**< 280 samples per report. */
} qeireport_t;

/**
 * @brief   QEI direction inversion.
 */
typedef enum {
  QEI_DIRINV_FALSE = 0,             /**< Do not invert counter direction.   */
  QEI_DIRINV_TRUE = 1,              /**< Invert counter direction.          */
} qeidirinv_t;

/**
 * @brief   QEI counter type.
 */
typedef int16_t qeicnt_t;

/**
 * @brief   QEI delta type.
 */
typedef int16_t qeidelta_t;

/**
 * @brief   Driver configuration structure.
 * @note    It could be empty on some architectures.
 */
typedef struct {
  /**
   * @brief   Count mode.
   */
  qeimode_t                 mode;
  /**
   * @brief   Resolution.
   */
  qeiresolution_t           resolution;
  /**
   * @brief   Direction inversion.
   */
  qeidirinv_t               dirinv;
  /**
   * @brief   Handling of counter overflow/underflow
   *
   * @details When overflow occurs, the counter value is updated
   *          according to:
   *            - QEI_OVERFLOW_DISCARD:
   *                discard the update value, counter doesn't change
   *            - QEI_OVERFLOW_MINMAX
   *                counter will be updated to reach min or max
   *            - QEI_OVERFLOW_WRAP:
   *                counter value will wrap around
   */
  qeioverflow_t             overflow;
  /**
   * @brief   Min count value.
   * 
   * @note    If min == max, then QEI_COUNT_MIN is used.
   */
  qeicnt_t                  min;
  /**
   * @brief   Max count value.
   * 
   * @note    If min == max, then QEI_COUNT_MAX is used.
   */
  qeicnt_t                  max;
  /**
    * @brief  Notify of value change
    *
    * @note   Called from ISR context.
    */
  qeicallback_t             notify_cb;
  /**
   * @brief   Notify of overflow
   *
   * @note    Overflow notification is performed after 
   *          value changed notification.
   * @note    Called from ISR context.
   */
  void (*overflow_cb)(QEIDriver *qeip, qeidelta_t delta);
  /* End of the mandatory fields.*/
  /**
   * @brief   Line for reading Phase A
   */
  ioline_t                  phase_a;
  /**
   * @brief   Line for reading Phase B
   */
  ioline_t                  phase_b;
#if (NRF5_QEI_USE_LED == TRUE) || defined(__DOXYGEN__)
  /**
   * @brief   Line used to control LED
   *
   * @note    If LED is not controlled by MCU, you need to use the 
   *          PAL_NOLINE value.
   */
  ioline_t                  led;
  /**
   * @brief   Period in µs the LED is switched on prior to sampling.
   *
   * @details LED warming is expressed in micro-seconds and value
   *          is [0..511]
   *
   * @note    31µs is the recommanded default.
   *
   * @note    If debouncing is activated, LED is always on for the
   *          whole sampling period (aka: resolution)
   */
  uint16_t                  led_warming;
  /**
   * @brief   LED polarity to used (when LED is controlled by MCU) 
   */
  uint8_t                   led_polarity;
#endif
   /**
    * @brief  Activate debouncing filter
    *
    * @note   If LED is controlled by MCU, the led_warming is ignored and,
    *         LED is always on for the whole sampling period (aka: resolution)
    */
  bool                      debouncing;
   /**
    * @brief  Number of samples per report
    *
    * @details Default to QEI_REPORT_10
    */
  qeireport_t 		    report;
#if NRF5_QEI_USE_ACC_OVERFLOWED_CB == TRUE
   /**
    * @brief  Notify of internal accumulator overflowed
    *         (ie: MCU discarding samples)
    * 
    * @note   Called from ISR context.
    */
  qeicallback_t             overflowed_cb;
#endif
} QEIConfig;

/**
 * @brief   Structure representing an QEI driver.
 */
struct QEIDriver {
  /**
   * @brief Driver state.
   */
  qeistate_t                state;
  /**
   * @brief Last count value.
   */
  qeicnt_t                  last;
  /**
   * @brief Current configuration data.
   */
  const QEIConfig           *config;
#if defined(QEI_DRIVER_EXT_FIELDS)
  QEI_DRIVER_EXT_FIELDS
#endif
  /* End of the mandatory fields.*/
  /**
   * @brief Counter
   */
  qeicnt_t                  count;
#if NRF5_QEI_USE_ACC_OVERFLOWED_CB == TRUE
  /**
   * @brief Number of time the MCU discarded updates due to
   *        accumulator overflow
   */
  uint32_t                  overflowed;
#endif
  /**
   * @brief Pointer to the QDECx registers block.
   */
  NRF_QDEC_Type             *qdec;
};

/*===========================================================================*/
/* Driver macros.                                                            */
/*===========================================================================*/

/**
 * @brief   Returns the counter value.
 *
 * @param[in] qeip      pointer to the @p QEIDriver object
 * @return              The current counter value.
 *
 * @notapi
 */
#define qei_lld_get_count(qeip) ((qeip)->count)


/**
 * @brief   Set the counter value.
 *
 * @param[in] qeip      pointer to the @p QEIDriver object
 * @param[in] value     counter value
 *
 * @notapi
 */
#define qei_lld_set_count(qeip, value)		\
    if ((qeip)->count != ((qeicnt_t)value)) {	\
      (qeip)->count = value;			\
      if ((qeip)->config->notify_cb)		\
        (qeip)->config->notify_cb(qeip);	\
    } while(0)


/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/

#if NRF5_QEI_USE_QDEC0 && !defined(__DOXYGEN__)
extern QEIDriver QEID1;
#endif

#ifdef __cplusplus
extern "C" {
#endif
  void qei_lld_init(void);
  void qei_lld_start(QEIDriver *qeip);
  void qei_lld_stop(QEIDriver *qeip);
  void qei_lld_enable(QEIDriver *qeip);
  void qei_lld_disable(QEIDriver *qeip);
  qeidelta_t qei_lld_adjust_count(QEIDriver *qeip, qeidelta_t delta);
#ifdef __cplusplus
}
#endif

/*===========================================================================*/
/* To be moved in hal_qei                                                    */
/*===========================================================================*/

void qeiSetCount(QEIDriver *qeip, qeicnt_t value);
qeidelta_t qeiAdjust(QEIDriver *qeip, qeidelta_t delta);

#endif /* HAL_USE_QEI */

#endif /* HAL_QEI_LLD_H */

/** @} */