nrf_drv_qdec.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_DRV_QDEC_H__
  13. #define NRF_DRV_QDEC_H__
  14. #include "nrf_qdec.h"
  15. #include "nrf_drv_config.h"
  16. #include "sdk_errors.h"
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. /**
  20. * @addtogroup nrf_qdec QDEC HAL and driver
  21. * @ingroup nrf_drivers
  22. * @brief Quadrature decoder (QDEC) APIs.
  23. * @details The QDEC HAL provides basic APIs for accessing the registers of the QDEC.
  24. * The QDEC driver provides APIs on a higher level.
  25. *
  26. * @defgroup nrf_drivers_qdec QDEC driver
  27. * @{
  28. * @ingroup nrf_qdec
  29. * @brief Quadrature decoder (QDEC) driver.
  30. */
  31. /**@brief QDEC configuration structure.*/
  32. typedef struct
  33. {
  34. nrf_qdec_reportper_t reportper; /**< Report period in samples. */
  35. nrf_qdec_sampleper_t sampleper; /**< Sampling period in microseconds. */
  36. uint32_t psela; /**< Pin number for A input. */
  37. uint32_t pselb; /**< Pin number for B input. */
  38. uint32_t pselled; /**< Pin number for LED output. */
  39. uint32_t ledpre; /**< Time (in microseconds) how long LED is switched on before sampling. */
  40. nrf_qdec_ledpol_t ledpol; /**< Active LED polarity. */
  41. bool dbfen; /**< State of debouncing filter. */
  42. bool sample_inten; /**< Enabling sample ready interrupt. */
  43. uint8_t interrupt_priority; /**< QDEC interrupt priority. */
  44. } nrf_drv_qdec_config_t;
  45. /**@brief QDEC default configuration. */
  46. #define NRF_DRV_QDEC_DEFAULT_CONFIG \
  47. { \
  48. .reportper = QDEC_CONFIG_REPORTPER, \
  49. .sampleper = QDEC_CONFIG_SAMPLEPER, \
  50. .psela = QDEC_CONFIG_PIO_A, \
  51. .pselb = QDEC_CONFIG_PIO_B, \
  52. .pselled = QDEC_CONFIG_PIO_LED, \
  53. .ledpre = QDEC_CONFIG_LEDPRE, \
  54. .ledpol = QDEC_CONFIG_LEDPOL, \
  55. .interrupt_priority = QDEC_CONFIG_IRQ_PRIORITY, \
  56. .dbfen = QDEC_CONFIG_DBFEN, \
  57. .sample_inten = QDEC_CONFIG_SAMPLE_INTEN \
  58. }
  59. /**@brief QDEC sample event data.*/
  60. typedef struct
  61. {
  62. int8_t value; /**< Sample value. */
  63. } nrf_drv_qdec_sample_data_evt_t;
  64. /**@brief QDEC report event data.*/
  65. typedef struct
  66. {
  67. int16_t acc; /**< Accumulated transitions. */
  68. uint16_t accdbl; /**< Accumulated double transitions. */
  69. } nrf_drv_qdec_report_data_evt_t;
  70. /**@brief QDEC event handler structure. */
  71. typedef struct
  72. {
  73. nrf_qdec_event_t type;
  74. union
  75. {
  76. nrf_drv_qdec_sample_data_evt_t sample; /**< Sample event data. */
  77. nrf_drv_qdec_report_data_evt_t report; /**< Report event data. */
  78. } data;
  79. } nrf_drv_qdec_event_t;
  80. /**@brief QDEC event handler.
  81. * @param[in] event QDEC event structure.
  82. */
  83. typedef void (*qdec_event_handler_t)(nrf_drv_qdec_event_t event);
  84. /**@brief Function for initializing QDEC.
  85. *
  86. * @param[in] p_config Pointer to configuration parameters.
  87. * @param[in] event_handler Event handler function.
  88. *
  89. * @retval NRF_SUCCESS If initialization was successful.
  90. * @retval NRF_ERROR_INVALID_PARAM If invalid parameters were supplied.
  91. * @retval NRF_ERROR_INVALID_STATE If QDEC was already initialized.
  92. */
  93. ret_code_t nrf_drv_qdec_init(nrf_drv_qdec_config_t const * p_config,
  94. qdec_event_handler_t event_handler);
  95. /**@brief Function for uninitializing QDEC.
  96. * @note Function asserts if module is uninitialized.
  97. */
  98. void nrf_drv_qdec_uninit(void);
  99. /**@brief Function for enabling QDEC.
  100. * @note Function asserts if module is uninitialized or enabled.
  101. */
  102. void nrf_drv_qdec_enable(void);
  103. /**@brief Function for disabling QDEC.
  104. * @note Function asserts if module is uninitialized or disabled.
  105. */
  106. void nrf_drv_qdec_disable(void);
  107. /**@brief Function for reading accumulated transitions QDEC.
  108. * @note Function asserts if module is not enabled.
  109. * @note Accumulators are cleared after reading.
  110. *
  111. * @param[out] p_acc Pointer to store accumulated transitions.
  112. * @param[out] p_accdbl Pointer to store accumulated double transitions.
  113. */
  114. void nrf_drv_qdec_accumulators_read(int16_t * p_acc, int16_t * p_accdbl);
  115. /**
  116. * @brief Function for returning the address of a specific timer task.
  117. *
  118. * @param[in] task QDEC task.
  119. * @param[out] p_task Task address.
  120. */
  121. void nrf_drv_qdec_task_address_get(nrf_qdec_task_t task, uint32_t * p_task);
  122. /**
  123. * @brief Function for returning the address of a specific timer event.
  124. *
  125. * @param[in] event QDEC event.
  126. * @param[out] p_event Event address.
  127. */
  128. void nrf_drv_qdec_event_address_get(nrf_qdec_event_t event, uint32_t * p_event);
  129. /**
  130. *@}
  131. **/
  132. #endif /* NRF_DRV_QDEC_H__ */