nrfx_qdec.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright (c) 2015 - 2020, 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. .dbfen = NRFX_QDEC_CONFIG_DBFEN, \
  78. .sample_inten = NRFX_QDEC_CONFIG_SAMPLE_INTEN, \
  79. .interrupt_priority = NRFX_QDEC_CONFIG_IRQ_PRIORITY, \
  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; /**< Event 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; /**< Union to store event data. */
  101. } nrfx_qdec_event_t;
  102. /**
  103. * @brief QDEC event handler.
  104. *
  105. * @param[in] event QDEC event structure.
  106. */
  107. typedef void (*nrfx_qdec_event_handler_t)(nrfx_qdec_event_t event);
  108. /**
  109. * @brief Function for initializing QDEC.
  110. *
  111. * @param[in] p_config Pointer to the structure with the initial configuration.
  112. * @param[in] event_handler Event handler provided by the user.
  113. * Must not be NULL.
  114. *
  115. * @retval NRFX_SUCCESS Initialization was successful.
  116. * @retval NRFX_ERROR_INVALID_STATE The QDEC was already initialized.
  117. */
  118. nrfx_err_t nrfx_qdec_init(nrfx_qdec_config_t const * p_config,
  119. nrfx_qdec_event_handler_t event_handler);
  120. /**
  121. * @brief Function for uninitializing QDEC.
  122. *
  123. * @note Function asserts if module is uninitialized.
  124. */
  125. void nrfx_qdec_uninit(void);
  126. /**
  127. * @brief Function for enabling QDEC.
  128. *
  129. * @note Function asserts if module is uninitialized or enabled.
  130. */
  131. void nrfx_qdec_enable(void);
  132. /**
  133. * @brief Function for disabling QDEC.
  134. *
  135. * @note Function asserts if module is uninitialized or disabled.
  136. */
  137. void nrfx_qdec_disable(void);
  138. /**
  139. * @brief Function for reading accumulated transitions from the QDEC peripheral.
  140. *
  141. * @note Function asserts if module is not enabled.
  142. * @note Accumulators are cleared after reading.
  143. *
  144. * @param[out] p_acc Pointer to store the accumulated transitions.
  145. * @param[out] p_accdbl Pointer to store the accumulated double transitions.
  146. */
  147. void nrfx_qdec_accumulators_read(int16_t * p_acc, int16_t * p_accdbl);
  148. /**
  149. * @brief Function for returning the address of the specified QDEC task.
  150. *
  151. * @param task QDEC task.
  152. *
  153. * @return Task address.
  154. */
  155. __STATIC_INLINE uint32_t nrfx_qdec_task_address_get(nrf_qdec_task_t task)
  156. {
  157. return (uint32_t)nrf_qdec_task_address_get(task);
  158. }
  159. /**
  160. * @brief Function for returning the address of the specified QDEC event.
  161. *
  162. * @param event QDEC event.
  163. *
  164. * @return Event address.
  165. */
  166. __STATIC_INLINE uint32_t nrfx_qdec_event_address_get(nrf_qdec_event_t event)
  167. {
  168. return (uint32_t)nrf_qdec_event_address_get(event);
  169. }
  170. /** @} */
  171. void nrfx_qdec_irq_handler(void);
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #endif // NRFX_QDEC_H__