main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /** @file
  41. * @defgroup nrf_qdec_example main.c
  42. * @{
  43. * @ingroup nrf_qdec_example
  44. * @brief QDEC example application main file.
  45. *
  46. * This is an example quadrature decoder application.
  47. * The example requires that the QDEC A,B inputs are connected with the QENC A,B outputs and
  48. * the QDEC LED output is connected with the QDEC LED input.
  49. *
  50. * The example uses the software quadrature encoder simulator QENC.
  51. * The quadrature encoder simulator uses one channel of the GPIOTE module.
  52. * The state of the encoder changes on the inactive edge of the sampling clock generated by the LED output.
  53. *
  54. * In an infinite loop, QENC produces a variable number of positive and negative pulses
  55. * synchronously with bursts of clock impulses generated by QDEC at the LED output.
  56. * The pulses are counted by QDEC operating in a REPORT mode.
  57. * The pulses counted by QDEC are compared with the pulses generated by QENC.
  58. * The test stops if there is a difference between the number of pulses counted and generated.
  59. *
  60. */
  61. #include <stdbool.h>
  62. #include <stdint.h>
  63. #include <string.h>
  64. #include <stdio.h>
  65. #include "nrf.h"
  66. #include "bsp.h"
  67. #include "nrf_delay.h"
  68. #include "nrf_drv_qdec.h"
  69. #include "nrf_error.h"
  70. #include "app_error.h"
  71. #include "qenc_sim.h"
  72. #include "nordic_common.h"
  73. #include "nrf_log.h"
  74. #include "nrf_log_ctrl.h"
  75. #include "nrf_log_default_backends.h"
  76. static volatile bool m_report_ready_flag = false;
  77. static volatile bool m_first_report_flag = true;
  78. static volatile uint32_t m_accdblread;
  79. static volatile int32_t m_accread;
  80. #if (QDEC_CONFIG_LEDPRE >= 128)
  81. #warning "This example assumes that the QDEC LED changes state. Make sure that 'Sample Period' in QDEC config is less than 'LED pre-time'."
  82. #endif
  83. static void qdec_event_handler(nrf_drv_qdec_event_t event)
  84. {
  85. if (event.type == NRF_QDEC_EVENT_REPORTRDY)
  86. {
  87. m_accdblread = event.data.report.accdbl;
  88. m_accread = event.data.report.acc;
  89. m_report_ready_flag = true;
  90. nrf_drv_qdec_disable();
  91. }
  92. }
  93. void check_report(int32_t expected)
  94. {
  95. // only first run is specific...
  96. if ((expected > 0) && m_first_report_flag)
  97. {
  98. expected --;
  99. }
  100. else if ((expected < 0) && m_first_report_flag)
  101. {
  102. expected ++;
  103. }
  104. // Error checking and printing
  105. if ( m_accdblread != 0 )
  106. {
  107. NRF_LOG_ERROR("m_accdblread was expected to have value 0 but is %u", (unsigned int)m_accdblread);
  108. APP_ERROR_HANDLER(0);
  109. }
  110. if ( m_accread != expected )
  111. {
  112. NRF_LOG_ERROR("m_accread should be %d but is %d", (int)expected, (int)m_accread);
  113. APP_ERROR_HANDLER(0);
  114. }
  115. m_first_report_flag = false; // clear silently after first run
  116. }
  117. int main(void)
  118. {
  119. uint32_t err_code;
  120. uint32_t number_of_pulses;
  121. uint32_t min_number_of_pulses = 2;
  122. uint32_t max_number_of_pulses = 0;
  123. int32_t pulses;
  124. int32_t sign = 1;
  125. err_code = NRF_LOG_INIT(NULL);
  126. APP_ERROR_CHECK(err_code);
  127. NRF_LOG_DEFAULT_BACKENDS_INIT();
  128. // Initialize hardware
  129. err_code = nrf_drv_qdec_init(NULL, qdec_event_handler);
  130. APP_ERROR_CHECK(err_code);
  131. max_number_of_pulses = nrf_qdec_reportper_to_value(QDEC_CONFIG_REPORTPER);
  132. // Initialize quadrature encoder simulator
  133. qenc_init((nrf_qdec_ledpol_t)nrf_qdec_ledpol_get());
  134. NRF_LOG_INFO("QDEC testing started.");
  135. while (true)
  136. {
  137. // change a number and sign of pulses produced by simulator in a loop
  138. for (number_of_pulses=min_number_of_pulses; number_of_pulses<= max_number_of_pulses; number_of_pulses++ )
  139. {
  140. pulses = sign * number_of_pulses; // pulses have sign
  141. qenc_pulse_count_set(pulses); // set pulses to be produced by encoder
  142. nrf_drv_qdec_enable(); // start burst sampling clock, clock will be stopped by REPORTRDY event
  143. while (! m_report_ready_flag) // wait for a report
  144. {
  145. __WFE();
  146. }
  147. NRF_LOG_RAW_INFO("*");
  148. m_report_ready_flag = false;
  149. check_report(pulses); // check if pulse count is as expected, assert otherwise
  150. }
  151. min_number_of_pulses = 1; // only first run is specific, for 1 there would be no call back...
  152. sign = -sign; // change sign of pulses in a loop
  153. NRF_LOG_FLUSH();
  154. }
  155. }
  156. /** @} */