main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_lpcomp_example main.c
  42. * @{
  43. * @ingroup nrf_lpcomp_example
  44. * @brief LPCOMP example application main file.
  45. *
  46. * This is an example low power comparator application.
  47. * The example requires that LPCOMP A,B inputs are connected with QENC A,B outputs and
  48. * LPCOMP LED output is connected with LPCOMP LED input.
  49. *
  50. * Example uses software quadrature encoder simulator QENC.
  51. * Quadrature encoder simulator uses one channel of GPIOTE module.
  52. * The state of the encoder changes on the inactive edge of the sampling clock generated by LED output.
  53. *
  54. * In a infinite loop QENC produces variable number of positive and negative pulses
  55. * synchronously with bursts of clock impulses generated by LPCOMP at LED output.
  56. * The pulses are counted by LPCOMP operating in a REPORT mode.
  57. * Pulses counted by LPCOMP are compared with pulses generated by QENC.
  58. * The tests stops if there is a difference between number of pulses counted and generated.
  59. *
  60. */
  61. #include <stdbool.h>
  62. #include <stdint.h>
  63. #include <stdio.h>
  64. #include "nrf.h"
  65. #include "nrf_delay.h"
  66. #include "nrf_drv_lpcomp.h"
  67. #include "nrf_error.h"
  68. #include "app_error.h"
  69. #include "boards.h"
  70. #include "nrf_log.h"
  71. #include "nrf_log_ctrl.h"
  72. #include "nrf_log_default_backends.h"
  73. #define WAVE_ON_PIN_NUMBER 2
  74. static volatile uint32_t voltage_falls_detected = 0;
  75. static volatile uint32_t voltage_falls_total = 0;
  76. /**
  77. * @brief LPCOMP event handler is called when LPCOMP detects voltage drop.
  78. *
  79. * This function is called from interrupt context so it is very important
  80. * to return quickly. Don't put busy loops or any other CPU intensive actions here.
  81. * It is also not allowed to call soft device functions from it (if LPCOMP IRQ
  82. * priority is set to APP_IRQ_PRIORITY_HIGH).
  83. */
  84. static void lpcomp_event_handler(nrf_lpcomp_event_t event)
  85. {
  86. if (event == NRF_LPCOMP_EVENT_DOWN)
  87. {
  88. bsp_board_led_invert(BSP_BOARD_LED_0); // just change state of first LED
  89. voltage_falls_detected++;
  90. voltage_falls_total++;
  91. }
  92. }
  93. /**
  94. * @brief Print out detection statistics.
  95. */
  96. static void print_statistics(void)
  97. {
  98. while (voltage_falls_detected)
  99. {
  100. voltage_falls_detected--;
  101. NRF_LOG_INFO("#%d fall detected", (int)voltage_falls_total);
  102. }
  103. }
  104. /**
  105. * @brief Initialize LPCOMP driver.
  106. */
  107. static void lpcomp_init(void)
  108. {
  109. uint32_t err_code;
  110. nrf_drv_lpcomp_config_t config = NRF_DRV_LPCOMP_DEFAULT_CONFIG;
  111. config.input = NRF_LPCOMP_INPUT_2;
  112. // initialize LPCOMP driver, from this point LPCOMP will be active and provided
  113. // event handler will be executed when defined action is detected
  114. err_code = nrf_drv_lpcomp_init(&config, lpcomp_event_handler);
  115. APP_ERROR_CHECK(err_code);
  116. nrf_drv_lpcomp_enable();
  117. }
  118. int main(void)
  119. {
  120. bsp_board_init(BSP_INIT_LEDS);
  121. nrf_gpio_cfg_output(WAVE_ON_PIN_NUMBER); // on this pin 2Hz wave will be generated
  122. #ifdef BSP_BUTTON_0
  123. // configure pull-up on first button
  124. nrf_gpio_cfg_input(BSP_BUTTON_0, NRF_GPIO_PIN_PULLUP);
  125. #endif
  126. uint32_t err_code = NRF_LOG_INIT(NULL);
  127. APP_ERROR_CHECK(err_code);
  128. NRF_LOG_DEFAULT_BACKENDS_INIT();
  129. lpcomp_init();
  130. NRF_LOG_INFO("LPCOMP driver usage example started.");
  131. while (true)
  132. {
  133. print_statistics();
  134. bsp_board_led_on(BSP_BOARD_LED_1);
  135. NRF_GPIO->OUTCLR = (1 << WAVE_ON_PIN_NUMBER);
  136. nrf_delay_ms(100); // generate 100 ms pulse on selected pin
  137. print_statistics();
  138. bsp_board_led_off(BSP_BOARD_LED_1);
  139. NRF_GPIO->OUTSET = (1 << WAVE_ON_PIN_NUMBER);
  140. nrf_delay_ms(400);
  141. NRF_LOG_FLUSH();
  142. }
  143. }
  144. /** @} */