nrfx_lpcomp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Copyright (c) 2015 - 2018, 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_LPCOMP_ENABLED)
  42. #include <nrfx_lpcomp.h>
  43. #include "prs/nrfx_prs.h"
  44. #define NRFX_LOG_MODULE LPCOMP
  45. #include <nrfx_log.h>
  46. #define EVT_TO_STR(event) \
  47. (event == NRF_LPCOMP_EVENT_READY ? "NRF_LPCOMP_EVENT_READY" : \
  48. (event == NRF_LPCOMP_EVENT_DOWN ? "NRF_LPCOMP_EVENT_DOWN" : \
  49. (event == NRF_LPCOMP_EVENT_UP ? "NRF_LPCOMP_EVENT_UP" : \
  50. (event == NRF_LPCOMP_EVENT_CROSS ? "NRF_LPCOMP_EVENT_CROSS" : \
  51. "UNKNOWN EVENT"))))
  52. static nrfx_lpcomp_event_handler_t m_lpcomp_event_handler = NULL;
  53. static nrfx_drv_state_t m_state = NRFX_DRV_STATE_UNINITIALIZED;
  54. static void lpcomp_execute_handler(nrf_lpcomp_event_t event, uint32_t event_mask)
  55. {
  56. if (nrf_lpcomp_event_check(event) && nrf_lpcomp_int_enable_check(event_mask))
  57. {
  58. nrf_lpcomp_event_clear(event);
  59. NRFX_LOG_DEBUG("Event: %s.", EVT_TO_STR(event));
  60. m_lpcomp_event_handler(event);
  61. }
  62. }
  63. void nrfx_lpcomp_irq_handler(void)
  64. {
  65. lpcomp_execute_handler(NRF_LPCOMP_EVENT_READY, LPCOMP_INTENSET_READY_Msk);
  66. lpcomp_execute_handler(NRF_LPCOMP_EVENT_DOWN, LPCOMP_INTENSET_DOWN_Msk);
  67. lpcomp_execute_handler(NRF_LPCOMP_EVENT_UP, LPCOMP_INTENSET_UP_Msk);
  68. lpcomp_execute_handler(NRF_LPCOMP_EVENT_CROSS, LPCOMP_INTENSET_CROSS_Msk);
  69. }
  70. nrfx_err_t nrfx_lpcomp_init(nrfx_lpcomp_config_t const * p_config,
  71. nrfx_lpcomp_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. { // LPCOMP 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_lpcomp_event_handler = event_handler;
  85. #if NRFX_CHECK(NRFX_PRS_ENABLED)
  86. if (nrfx_prs_acquire(NRF_LPCOMP, nrfx_lpcomp_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_lpcomp_configure(&(p_config->hal));
  96. nrf_lpcomp_input_select(p_config->input);
  97. switch (p_config->hal.detection)
  98. {
  99. case NRF_LPCOMP_DETECT_UP:
  100. nrf_lpcomp_int_enable(LPCOMP_INTENSET_UP_Msk);
  101. break;
  102. case NRF_LPCOMP_DETECT_DOWN:
  103. nrf_lpcomp_int_enable(LPCOMP_INTENSET_DOWN_Msk);
  104. break;
  105. case NRF_LPCOMP_DETECT_CROSS:
  106. nrf_lpcomp_int_enable(LPCOMP_INTENSET_CROSS_Msk);
  107. break;
  108. default:
  109. break;
  110. }
  111. nrf_lpcomp_shorts_enable(NRF_LPCOMP_SHORT_READY_SAMPLE_MASK);
  112. NRFX_IRQ_PRIORITY_SET(LPCOMP_IRQn, p_config->interrupt_priority);
  113. NRFX_IRQ_ENABLE(LPCOMP_IRQn);
  114. m_state = NRFX_DRV_STATE_INITIALIZED;
  115. err_code = NRFX_SUCCESS;
  116. NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
  117. return err_code;
  118. }
  119. void nrfx_lpcomp_uninit(void)
  120. {
  121. NRFX_ASSERT(m_state != NRFX_DRV_STATE_UNINITIALIZED);
  122. NRFX_IRQ_DISABLE(LPCOMP_IRQn);
  123. nrfx_lpcomp_disable();
  124. #if NRFX_CHECK(NRFX_PRS_ENABLED)
  125. nrfx_prs_release(NRF_LPCOMP);
  126. #endif
  127. m_state = NRFX_DRV_STATE_UNINITIALIZED;
  128. m_lpcomp_event_handler = NULL;
  129. NRFX_LOG_INFO("Uninitialized.");
  130. }
  131. void nrfx_lpcomp_enable(void)
  132. {
  133. NRFX_ASSERT(m_state == NRFX_DRV_STATE_INITIALIZED);
  134. nrf_lpcomp_enable();
  135. nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_START);
  136. m_state = NRFX_DRV_STATE_POWERED_ON;
  137. NRFX_LOG_INFO("Enabled.");
  138. }
  139. void nrfx_lpcomp_disable(void)
  140. {
  141. NRFX_ASSERT(m_state == NRFX_DRV_STATE_POWERED_ON);
  142. nrf_lpcomp_disable();
  143. nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_STOP);
  144. m_state = NRFX_DRV_STATE_INITIALIZED;
  145. NRFX_LOG_INFO("Disabled.");
  146. }
  147. #endif // NRFX_CHECK(NRFX_LPCOMP_ENABLED)