main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_adc_example main.c
  42. * @{
  43. * @ingroup nrf_adc_example
  44. * @brief ADC Example Application main file.
  45. *
  46. * This file contains the source code for a sample application using ADC.
  47. *
  48. * @image html example_board_setup_a.jpg "Use board setup A for this example."
  49. */
  50. #include <stdbool.h>
  51. #include <stdint.h>
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include "nrf.h"
  55. #include "nrf_drv_saadc.h"
  56. #include "nrf_drv_ppi.h"
  57. #include "nrf_drv_timer.h"
  58. #include "boards.h"
  59. #include "app_error.h"
  60. #include "nrf_delay.h"
  61. #include "app_util_platform.h"
  62. #include "nrf_pwr_mgmt.h"
  63. #include "nrf_log.h"
  64. #include "nrf_log_ctrl.h"
  65. #include "nrf_log_default_backends.h"
  66. #define SAMPLES_IN_BUFFER 5
  67. volatile uint8_t state = 1;
  68. static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0);
  69. static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER];
  70. static nrf_ppi_channel_t m_ppi_channel;
  71. static uint32_t m_adc_evt_counter;
  72. void timer_handler(nrf_timer_event_t event_type, void * p_context)
  73. {
  74. }
  75. void saadc_sampling_event_init(void)
  76. {
  77. ret_code_t err_code;
  78. err_code = nrf_drv_ppi_init();
  79. APP_ERROR_CHECK(err_code);
  80. nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
  81. timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
  82. err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);
  83. APP_ERROR_CHECK(err_code);
  84. /* setup m_timer for compare event every 400ms */
  85. uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 400);
  86. nrf_drv_timer_extended_compare(&m_timer,
  87. NRF_TIMER_CC_CHANNEL0,
  88. ticks,
  89. NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
  90. false);
  91. nrf_drv_timer_enable(&m_timer);
  92. uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer,
  93. NRF_TIMER_CC_CHANNEL0);
  94. uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();
  95. /* setup ppi channel so that timer compare event is triggering sample task in SAADC */
  96. err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
  97. APP_ERROR_CHECK(err_code);
  98. err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
  99. timer_compare_event_addr,
  100. saadc_sample_task_addr);
  101. APP_ERROR_CHECK(err_code);
  102. }
  103. void saadc_sampling_event_enable(void)
  104. {
  105. ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);
  106. APP_ERROR_CHECK(err_code);
  107. }
  108. void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
  109. {
  110. if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
  111. {
  112. ret_code_t err_code;
  113. err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
  114. APP_ERROR_CHECK(err_code);
  115. int i;
  116. NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter);
  117. for (i = 0; i < SAMPLES_IN_BUFFER; i++)
  118. {
  119. NRF_LOG_INFO("%d", p_event->data.done.p_buffer[i]);
  120. }
  121. m_adc_evt_counter++;
  122. }
  123. }
  124. void saadc_init(void)
  125. {
  126. ret_code_t err_code;
  127. nrf_saadc_channel_config_t channel_config =
  128. NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
  129. err_code = nrf_drv_saadc_init(NULL, saadc_callback);
  130. APP_ERROR_CHECK(err_code);
  131. err_code = nrf_drv_saadc_channel_init(0, &channel_config);
  132. APP_ERROR_CHECK(err_code);
  133. err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
  134. APP_ERROR_CHECK(err_code);
  135. err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
  136. APP_ERROR_CHECK(err_code);
  137. }
  138. /**
  139. * @brief Function for main application entry.
  140. */
  141. int main(void)
  142. {
  143. uint32_t err_code = NRF_LOG_INIT(NULL);
  144. APP_ERROR_CHECK(err_code);
  145. NRF_LOG_DEFAULT_BACKENDS_INIT();
  146. ret_code_t ret_code = nrf_pwr_mgmt_init();
  147. APP_ERROR_CHECK(ret_code);
  148. saadc_init();
  149. saadc_sampling_event_init();
  150. saadc_sampling_event_enable();
  151. NRF_LOG_INFO("SAADC HAL simple example started.");
  152. while (1)
  153. {
  154. nrf_pwr_mgmt_run();
  155. NRF_LOG_FLUSH();
  156. }
  157. }
  158. /** @} */