qenc_sim.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2014 - 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. #include "nrf_gpio.h"
  41. #include "nrf_drv_gpiote.h"
  42. #include "nrf_error.h"
  43. #include "nrf_qdec.h"
  44. #include "qenc_sim.h"
  45. /**
  46. * @enum nrf_qdec_task_t
  47. * @brief enumeration of QDEC tasks.
  48. * coding is [A|B] i.e. B is LSB
  49. */
  50. #define _static
  51. typedef enum {
  52. state_0 = 0, /**< AB=00 */
  53. state_1 = 1, /**< AB=01 */
  54. state_2 = 2, /**< AB=10 */
  55. state_3 = 3 /**< AB=11 */
  56. } qenc_state_t;
  57. const qenc_state_t next_pos[4] = { state_1, state_3, state_0, state_2 }; // change of state to get phase increment
  58. const qenc_state_t next_neg[4] = { state_2, state_0, state_3, state_1 }; // change of state to get phase decrement
  59. const qenc_state_t next_dbl[4] = { state_3, state_2, state_1, state_0 }; // change of state to get double transition
  60. _static volatile qenc_state_t qenc_state = state_0;
  61. _static volatile int32_t qenc_count = 0;
  62. _static volatile uint32_t qenc_dbl_count = 0;
  63. _static volatile bool qenc_enable_flag = false;
  64. static void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
  65. {
  66. if ((qenc_count > 0) && qenc_enable_flag)
  67. {
  68. qenc_count--;
  69. qenc_state = next_pos[qenc_state];
  70. }
  71. else if ((qenc_count < 0 ) && qenc_enable_flag )
  72. {
  73. qenc_count++;
  74. qenc_state = next_neg[qenc_state];
  75. }
  76. else if ((qenc_dbl_count > 0 ) && qenc_enable_flag)
  77. {
  78. qenc_dbl_count--;
  79. qenc_state = next_dbl[qenc_state];
  80. }
  81. else
  82. {
  83. qenc_enable_flag = false;
  84. }
  85. if (qenc_state & 0x01)
  86. {
  87. nrf_drv_gpiote_out_set(QENC_CONFIG_PIO_B);
  88. }
  89. else
  90. {
  91. nrf_drv_gpiote_out_clear(QENC_CONFIG_PIO_B);
  92. }
  93. if (qenc_state & 0x02)
  94. {
  95. nrf_drv_gpiote_out_set(QENC_CONFIG_PIO_A);
  96. }
  97. else
  98. {
  99. nrf_drv_gpiote_out_clear(QENC_CONFIG_PIO_A);
  100. }
  101. }
  102. static void qenc_init_gpiote(nrf_qdec_ledpol_t led_pol)
  103. {
  104. nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
  105. nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
  106. config.pull = NRF_GPIO_PIN_PULLUP;
  107. if (!nrf_drv_gpiote_is_init())
  108. {
  109. (void)nrf_drv_gpiote_init();
  110. }
  111. // change state on inactive edge of led pulse
  112. if (led_pol == NRF_QDEC_LEPOL_ACTIVE_LOW)
  113. {
  114. config.sense = NRF_GPIOTE_POLARITY_HITOLO;
  115. }
  116. (void)nrf_drv_gpiote_in_init(QENC_CONFIG_PIO_LED,&config,gpiote_event_handler);
  117. nrf_drv_gpiote_in_event_enable(QENC_CONFIG_PIO_LED, true);
  118. //Configure output pins.
  119. (void)nrf_drv_gpiote_out_init(QENC_CONFIG_PIO_A, &out_config);
  120. (void)nrf_drv_gpiote_out_init(QENC_CONFIG_PIO_B, &out_config);
  121. }
  122. void qenc_init(nrf_qdec_ledpol_t led_pol)
  123. {
  124. qenc_init_gpiote(led_pol);
  125. }
  126. // this function is used mainly in tests
  127. void qenc_pulse_dblpulse_count_set(int32_t pulse_count, uint32_t dble_pulse_count)
  128. {
  129. qenc_count = pulse_count;
  130. qenc_dbl_count = dble_pulse_count;
  131. qenc_enable_flag = true;
  132. }
  133. // this function is used mainly in a example
  134. void qenc_pulse_count_set(int32_t pulse_count)
  135. {
  136. qenc_pulse_dblpulse_count_set(pulse_count, 0);
  137. }