nrf_drv_qdec.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include <stdint.h>
  13. #include <stddef.h>
  14. #include "nrf.h"
  15. #include "nrf_gpio.h"
  16. #include "nrf_error.h"
  17. #include "nrf_assert.h"
  18. #include "nrf_drv_common.h"
  19. #include "nrf_drv_qdec.h"
  20. #include "app_util_platform.h"
  21. #include "nrf_assert.h"
  22. static qdec_event_handler_t m_qdec_event_handler = NULL;
  23. static const nrf_drv_qdec_config_t m_default_config = NRF_DRV_QDEC_DEFAULT_CONFIG;
  24. static nrf_drv_state_t m_state = NRF_DRV_STATE_UNINITIALIZED;
  25. void QDEC_IRQHandler(void)
  26. {
  27. nrf_drv_qdec_event_t event;
  28. if ( nrf_qdec_event_check(NRF_QDEC_EVENT_SAMPLERDY) &&
  29. nrf_qdec_int_enable_check(NRF_QDEC_INT_SAMPLERDY_MASK) )
  30. {
  31. nrf_qdec_event_clear(NRF_QDEC_EVENT_SAMPLERDY);
  32. event.type = NRF_QDEC_EVENT_SAMPLERDY;
  33. event.data.sample.value = (int8_t)nrf_qdec_sample_get();
  34. m_qdec_event_handler(event);
  35. }
  36. if ( nrf_qdec_event_check(NRF_QDEC_EVENT_REPORTRDY) &&
  37. nrf_qdec_int_enable_check(NRF_QDEC_INT_REPORTRDY_MASK) )
  38. {
  39. nrf_qdec_event_clear(NRF_QDEC_EVENT_REPORTRDY);
  40. event.type = NRF_QDEC_EVENT_REPORTRDY;
  41. event.data.report.acc = (int16_t)nrf_qdec_accread_get();
  42. event.data.report.accdbl = (uint16_t)nrf_qdec_accdblread_get();
  43. m_qdec_event_handler(event);
  44. }
  45. if ( nrf_qdec_event_check(NRF_QDEC_EVENT_ACCOF) &&
  46. nrf_qdec_int_enable_check(NRF_QDEC_INT_ACCOF_MASK) )
  47. {
  48. nrf_qdec_event_clear(NRF_QDEC_EVENT_ACCOF);
  49. event.type = NRF_QDEC_EVENT_ACCOF;
  50. m_qdec_event_handler(event);
  51. }
  52. }
  53. ret_code_t nrf_drv_qdec_init(const nrf_drv_qdec_config_t * p_config,
  54. qdec_event_handler_t event_handler)
  55. {
  56. if (m_state != NRF_DRV_STATE_UNINITIALIZED)
  57. {
  58. return NRF_ERROR_INVALID_STATE; // qdec_event_handler has been already registered
  59. }
  60. if (p_config == NULL)
  61. {
  62. p_config = &m_default_config;
  63. }
  64. if (event_handler)
  65. {
  66. m_qdec_event_handler = event_handler;
  67. }
  68. else
  69. {
  70. return NRF_ERROR_INVALID_PARAM;
  71. }
  72. nrf_qdec_sampleper_set(p_config->sampleper);
  73. nrf_gpio_cfg_input(p_config->pselled,NRF_GPIO_PIN_NOPULL);
  74. nrf_gpio_cfg_input(p_config->psela, NRF_GPIO_PIN_NOPULL);
  75. nrf_gpio_cfg_input(p_config->pselb, NRF_GPIO_PIN_NOPULL);
  76. nrf_qdec_pio_assign( p_config->psela, p_config->pselb, p_config->pselled);
  77. nrf_qdec_ledpre_set(p_config->ledpre);
  78. nrf_qdec_ledpol_set(p_config->ledpol);
  79. nrf_qdec_shorts_enable(NRF_QDEC_SHORT_REPORTRDY_READCLRACC_MASK);
  80. if (p_config->dbfen)
  81. {
  82. nrf_qdec_dbfen_enable();
  83. }
  84. else
  85. {
  86. nrf_qdec_dbfen_disable();
  87. }
  88. uint32_t int_mask = NRF_QDEC_INT_ACCOF_MASK;
  89. if (p_config->reportper != NRF_QDEC_REPORTPER_DISABLED)
  90. {
  91. nrf_qdec_reportper_set(p_config->reportper);
  92. int_mask |= NRF_QDEC_INT_REPORTRDY_MASK;
  93. }
  94. if (p_config->sample_inten)
  95. {
  96. int_mask |= NRF_QDEC_INT_SAMPLERDY_MASK;
  97. }
  98. nrf_qdec_int_enable(int_mask);
  99. nrf_drv_common_irq_enable(QDEC_IRQn, p_config->interrupt_priority);
  100. m_state = NRF_DRV_STATE_INITIALIZED;
  101. return NRF_SUCCESS;
  102. }
  103. void nrf_drv_qdec_uninit(void)
  104. {
  105. ASSERT(m_state != NRF_DRV_STATE_UNINITIALIZED);
  106. nrf_drv_qdec_disable();
  107. nrf_drv_common_irq_disable(QDEC_IRQn);
  108. m_state = NRF_DRV_STATE_UNINITIALIZED;
  109. }
  110. void nrf_drv_qdec_enable(void)
  111. {
  112. ASSERT(m_state == NRF_DRV_STATE_INITIALIZED);
  113. nrf_qdec_enable();
  114. nrf_qdec_task_trigger(NRF_QDEC_TASK_START);
  115. m_state = NRF_DRV_STATE_POWERED_ON;
  116. }
  117. void nrf_drv_qdec_disable(void)
  118. {
  119. ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
  120. nrf_qdec_disable();
  121. nrf_qdec_task_trigger(NRF_QDEC_TASK_STOP);
  122. m_state = NRF_DRV_STATE_INITIALIZED;
  123. }
  124. void nrf_drv_qdec_accumulators_read(int16_t * p_acc, int16_t * p_accdbl)
  125. {
  126. ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
  127. nrf_qdec_task_trigger(NRF_QDEC_TASK_READCLRACC);
  128. *p_acc = (int16_t)nrf_qdec_accread_get();
  129. *p_accdbl = (int16_t)nrf_qdec_accdblread_get();
  130. }
  131. void nrf_drv_qdec_task_address_get(nrf_qdec_task_t task, uint32_t * p_task)
  132. {
  133. *p_task = (uint32_t)nrf_qdec_task_address_get(task);
  134. }
  135. void nrf_drv_qdec_event_address_get(nrf_qdec_event_t event, uint32_t * p_event)
  136. {
  137. *p_event = (uint32_t)nrf_qdec_event_address_get(event);
  138. }