nrfx_qdec.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRFX_QDEC_H__
  41. #define NRFX_QDEC_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_qdec.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_qdec QDEC driver
  49. * @{
  50. * @ingroup nrf_qdec
  51. * @brief Quadrature Decoder (QDEC) peripheral driver.
  52. */
  53. /**@brief QDEC configuration structure.*/
  54. typedef struct
  55. {
  56. nrf_qdec_reportper_t reportper; /**< Report period in samples. */
  57. nrf_qdec_sampleper_t sampleper; /**< Sampling period in microseconds. */
  58. uint32_t psela; /**< Pin number for A input. */
  59. uint32_t pselb; /**< Pin number for B input. */
  60. uint32_t pselled; /**< Pin number for LED output. */
  61. uint32_t ledpre; /**< Time (in microseconds) how long LED is switched on before sampling. */
  62. nrf_qdec_ledpol_t ledpol; /**< Active LED polarity. */
  63. bool dbfen; /**< State of debouncing filter. */
  64. bool sample_inten; /**< Enabling sample ready interrupt. */
  65. uint8_t interrupt_priority; /**< QDEC interrupt priority. */
  66. } nrfx_qdec_config_t;
  67. /**@brief QDEC default configuration. */
  68. #define NRFX_QDEC_DEFAULT_CONFIG \
  69. { \
  70. .reportper = (nrf_qdec_reportper_t)NRFX_QDEC_CONFIG_REPORTPER, \
  71. .sampleper = (nrf_qdec_sampleper_t)NRFX_QDEC_CONFIG_SAMPLEPER, \
  72. .psela = NRFX_QDEC_CONFIG_PIO_A, \
  73. .pselb = NRFX_QDEC_CONFIG_PIO_B, \
  74. .pselled = NRFX_QDEC_CONFIG_PIO_LED, \
  75. .ledpre = NRFX_QDEC_CONFIG_LEDPRE, \
  76. .ledpol = (nrf_qdec_ledpol_t)NRFX_QDEC_CONFIG_LEDPOL, \
  77. .interrupt_priority = NRFX_QDEC_CONFIG_IRQ_PRIORITY, \
  78. .dbfen = NRFX_QDEC_CONFIG_DBFEN, \
  79. .sample_inten = NRFX_QDEC_CONFIG_SAMPLE_INTEN \
  80. }
  81. /**@brief QDEC sample event data.*/
  82. typedef struct
  83. {
  84. int8_t value; /**< Sample value. */
  85. } nrfx_qdec_sample_data_evt_t;
  86. /**@brief QDEC report event data.*/
  87. typedef struct
  88. {
  89. int16_t acc; /**< Accumulated transitions. */
  90. uint16_t accdbl; /**< Accumulated double transitions. */
  91. } nrfx_qdec_report_data_evt_t;
  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. /**@brief QDEC event handler.
  103. * @param[in] event QDEC event structure.
  104. */
  105. typedef void (*nrfx_qdec_event_handler_t)(nrfx_qdec_event_t event);
  106. /**@brief Function for initializing QDEC.
  107. *
  108. * @param[in] p_config Pointer to the structure with initial configuration.
  109. * @param[in] event_handler Event handler provided by the user.
  110. * Must not be NULL.
  111. *
  112. * @retval NRFX_SUCCESS If initialization was successful.
  113. * @retval NRFX_ERROR_INVALID_STATE If QDEC was already initialized.
  114. */
  115. nrfx_err_t nrfx_qdec_init(nrfx_qdec_config_t const * p_config,
  116. nrfx_qdec_event_handler_t event_handler);
  117. /**@brief Function for uninitializing QDEC.
  118. * @note Function asserts if module is uninitialized.
  119. */
  120. void nrfx_qdec_uninit(void);
  121. /**@brief Function for enabling QDEC.
  122. * @note Function asserts if module is uninitialized or enabled.
  123. */
  124. void nrfx_qdec_enable(void);
  125. /**@brief Function for disabling QDEC.
  126. * @note Function asserts if module is uninitialized or disabled.
  127. */
  128. void nrfx_qdec_disable(void);
  129. /**@brief Function for reading accumulated transitions QDEC.
  130. * @note Function asserts if module is not enabled.
  131. * @note Accumulators are cleared after reading.
  132. *
  133. * @param[out] p_acc Pointer to store accumulated transitions.
  134. * @param[out] p_accdbl Pointer to store accumulated double transitions.
  135. */
  136. void nrfx_qdec_accumulators_read(int16_t * p_acc, int16_t * p_accdbl);
  137. /**
  138. * @brief Function for returning the address of a specific QDEC task.
  139. *
  140. * @param task QDEC task.
  141. *
  142. * @return Task address.
  143. */
  144. __STATIC_INLINE uint32_t nrfx_qdec_task_address_get(nrf_qdec_task_t task)
  145. {
  146. return (uint32_t)nrf_qdec_task_address_get(task);
  147. }
  148. /**
  149. * @brief Function for returning the address of a specific QDEC event.
  150. *
  151. * @param event QDEC event.
  152. *
  153. * @return Event address.
  154. */
  155. __STATIC_INLINE uint32_t nrfx_qdec_event_address_get(nrf_qdec_event_t event)
  156. {
  157. return (uint32_t)nrf_qdec_event_address_get(event);
  158. }
  159. void nrfx_qdec_irq_handler(void);
  160. /** @} */
  161. #ifdef __cplusplus
  162. }
  163. #endif
  164. #endif // NRFX_QDEC_H__