nrfx_comp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * Copyright (c) 2015 - 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 <nrfx.h>
  41. #if NRFX_CHECK(NRFX_COMP_ENABLED)
  42. #include <nrfx_comp.h>
  43. #include "prs/nrfx_prs.h"
  44. #define NRFX_LOG_MODULE COMP
  45. #include <nrfx_log.h>
  46. #define EVT_TO_STR(event) \
  47. (event == NRF_COMP_EVENT_READY ? "NRF_COMP_EVENT_READY" : \
  48. (event == NRF_COMP_EVENT_DOWN ? "NRF_COMP_EVENT_DOWN" : \
  49. (event == NRF_COMP_EVENT_UP ? "NRF_COMP_EVENT_UP" : \
  50. (event == NRF_COMP_EVENT_CROSS ? "NRF_COMP_EVENT_CROSS" : \
  51. "UNKNOWN ERROR"))))
  52. static nrfx_comp_event_handler_t m_comp_event_handler = NULL;
  53. static nrfx_drv_state_t m_state = NRFX_DRV_STATE_UNINITIALIZED;
  54. static void comp_execute_handler(nrf_comp_event_t event, uint32_t event_mask)
  55. {
  56. if (nrf_comp_event_check(event) && nrf_comp_int_enable_check(event_mask))
  57. {
  58. nrf_comp_event_clear(event);
  59. NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(event));
  60. m_comp_event_handler(event);
  61. }
  62. }
  63. void nrfx_comp_irq_handler(void)
  64. {
  65. comp_execute_handler(NRF_COMP_EVENT_READY, COMP_INTENSET_READY_Msk);
  66. comp_execute_handler(NRF_COMP_EVENT_DOWN, COMP_INTENSET_DOWN_Msk);
  67. comp_execute_handler(NRF_COMP_EVENT_UP, COMP_INTENSET_UP_Msk);
  68. comp_execute_handler(NRF_COMP_EVENT_CROSS, COMP_INTENSET_CROSS_Msk);
  69. }
  70. nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,
  71. nrfx_comp_event_handler_t event_handler)
  72. {
  73. NRFX_ASSERT(p_config);
  74. NRFX_ASSERT(event_handler);
  75. nrfx_err_t err_code;
  76. if (m_state != NRFX_DRV_STATE_UNINITIALIZED)
  77. { // COMP driver is already initialized
  78. err_code = NRFX_ERROR_INVALID_STATE;
  79. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  80. __func__,
  81. NRFX_LOG_ERROR_STRING_GET(err_code));
  82. return err_code;
  83. }
  84. m_comp_event_handler = event_handler;
  85. #if NRFX_CHECK(NRFX_PRS_ENABLED)
  86. if (nrfx_prs_acquire(NRF_COMP, nrfx_comp_irq_handler) != NRFX_SUCCESS)
  87. {
  88. err_code = NRFX_ERROR_BUSY;
  89. NRFX_LOG_WARNING("Function: %s, error code: %s.",
  90. __func__,
  91. NRFX_LOG_ERROR_STRING_GET(err_code));
  92. return err_code;
  93. }
  94. #endif
  95. nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
  96. nrf_comp_enable();
  97. // Clear events to be sure there are no leftovers.
  98. nrf_comp_event_clear(NRF_COMP_EVENT_READY);
  99. nrf_comp_event_clear(NRF_COMP_EVENT_DOWN);
  100. nrf_comp_event_clear(NRF_COMP_EVENT_UP);
  101. nrf_comp_event_clear(NRF_COMP_EVENT_CROSS);
  102. nrf_comp_ref_set(p_config->reference);
  103. //If external source is chosen, write to appropriate register.
  104. if (p_config->reference == COMP_REFSEL_REFSEL_ARef)
  105. {
  106. nrf_comp_ext_ref_set(p_config->ext_ref);
  107. }
  108. nrf_comp_th_set(p_config->threshold);
  109. nrf_comp_main_mode_set(p_config->main_mode);
  110. nrf_comp_speed_mode_set(p_config->speed_mode);
  111. nrf_comp_hysteresis_set(p_config->hyst);
  112. #if defined (COMP_ISOURCE_ISOURCE_Msk)
  113. nrf_comp_isource_set(p_config->isource);
  114. #endif
  115. nrf_comp_shorts_disable(NRFX_COMP_SHORT_STOP_AFTER_CROSS_EVT |
  116. NRFX_COMP_SHORT_STOP_AFTER_UP_EVT |
  117. NRFX_COMP_SHORT_STOP_AFTER_DOWN_EVT);
  118. nrf_comp_int_disable(COMP_INTENCLR_CROSS_Msk |
  119. COMP_INTENCLR_UP_Msk |
  120. COMP_INTENCLR_DOWN_Msk |
  121. COMP_INTENCLR_READY_Msk);
  122. nrf_comp_input_select(p_config->input);
  123. NRFX_IRQ_PRIORITY_SET(nrfx_get_irq_number(NRF_COMP), p_config->interrupt_priority);
  124. NRFX_IRQ_ENABLE(nrfx_get_irq_number(NRF_COMP));
  125. m_state = NRFX_DRV_STATE_INITIALIZED;
  126. err_code = NRFX_SUCCESS;
  127. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
  128. return err_code;
  129. }
  130. void nrfx_comp_uninit(void)
  131. {
  132. NRFX_ASSERT(m_state != NRFX_DRV_STATE_UNINITIALIZED);
  133. NRFX_IRQ_DISABLE(nrfx_get_irq_number(NRF_COMP));
  134. nrf_comp_disable();
  135. #if NRFX_CHECK(NRFX_PRS_ENABLED)
  136. nrfx_prs_release(NRF_COMP);
  137. #endif
  138. m_state = NRFX_DRV_STATE_UNINITIALIZED;
  139. m_comp_event_handler = NULL;
  140. NRFX_LOG_INFO("Uninitialized.");
  141. }
  142. void nrfx_comp_pin_select(nrf_comp_input_t psel)
  143. {
  144. bool comp_enable_state = nrf_comp_enable_check();
  145. nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
  146. if (m_state == NRFX_DRV_STATE_POWERED_ON)
  147. {
  148. m_state = NRFX_DRV_STATE_INITIALIZED;
  149. }
  150. nrf_comp_disable();
  151. nrf_comp_input_select(psel);
  152. if (comp_enable_state == true)
  153. {
  154. nrf_comp_enable();
  155. }
  156. }
  157. void nrfx_comp_start(uint32_t comp_int_mask, uint32_t comp_shorts_mask)
  158. {
  159. NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
  160. nrf_comp_int_enable(comp_int_mask);
  161. nrf_comp_shorts_enable(comp_shorts_mask);
  162. nrf_comp_task_trigger(NRF_COMP_TASK_START);
  163. m_state = NRFX_DRV_STATE_POWERED_ON;
  164. NRFX_LOG_INFO("Enabled.");
  165. }
  166. void nrfx_comp_stop(void)
  167. {
  168. NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
  169. nrf_comp_shorts_disable(UINT32_MAX);
  170. nrf_comp_int_disable(UINT32_MAX);
  171. nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
  172. m_state = NRFX_DRV_STATE_INITIALIZED;
  173. NRFX_LOG_INFO("Disabled.");
  174. }
  175. uint32_t nrfx_comp_sample()
  176. {
  177. NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
  178. nrf_comp_task_trigger(NRF_COMP_TASK_SAMPLE);
  179. return nrf_comp_result_get();
  180. }
  181. #endif // NRFX_CHECK(NRFX_COMP_ENABLED)