nrf_drv_comp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "nrf_drv_comp.h"
  13. #include "nrf_assert.h"
  14. #include "nrf_error.h"
  15. #include "nrf_soc.h"
  16. #include "nrf_drv_common.h"
  17. #include "app_util_platform.h"
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #define COMP_IRQ COMP_LPCOMP_IRQn
  22. #define COMP_IRQ_HANDLER COMP_LPCOMP_IRQHandler
  23. static comp_events_handler_t m_comp_events_handler = NULL;
  24. static nrf_drv_state_t m_state = NRF_DRV_STATE_UNINITIALIZED;
  25. static const nrf_drv_comp_config_t m_default_config = NRF_DRV_COMP_CONF_DEFAULT_CONFIG(NRF_COMP_INPUT_0);
  26. static void comp_execute_handler(nrf_comp_event_t event, uint32_t event_mask)
  27. {
  28. if ( nrf_comp_event_check(event) && nrf_comp_int_enable_check(event_mask) )
  29. {
  30. nrf_comp_event_clear(event);
  31. m_comp_events_handler(event);
  32. }
  33. }
  34. #if PERIPHERAL_RESOURCE_SHARING_ENABLED
  35. #define IRQ_HANDLER_NAME irq_handler_for_comp
  36. #define IRQ_HANDLER static void IRQ_HANDLER_NAME(void)
  37. IRQ_HANDLER;
  38. #else
  39. #define IRQ_HANDLER void COMP_IRQ_HANDLER(void)
  40. #endif // PERIPHERAL_RESOURCE_SHARING_ENABLED
  41. IRQ_HANDLER
  42. {
  43. comp_execute_handler(NRF_COMP_EVENT_READY, COMP_INTENSET_READY_Msk);
  44. comp_execute_handler(NRF_COMP_EVENT_DOWN, COMP_INTENSET_DOWN_Msk);
  45. comp_execute_handler(NRF_COMP_EVENT_UP, COMP_INTENSET_UP_Msk);
  46. comp_execute_handler(NRF_COMP_EVENT_CROSS, COMP_INTENSET_CROSS_Msk);
  47. }
  48. ret_code_t nrf_drv_comp_init(const nrf_drv_comp_config_t * p_config,
  49. comp_events_handler_t event_handler)
  50. {
  51. if (m_state != NRF_DRV_STATE_UNINITIALIZED)
  52. { // COMP driver is already initialized
  53. return NRF_ERROR_INVALID_STATE;
  54. }
  55. if (p_config == NULL)
  56. {
  57. p_config = &m_default_config;
  58. }
  59. #if PERIPHERAL_RESOURCE_SHARING_ENABLED
  60. if (nrf_drv_common_per_res_acquire(NRF_COMP, IRQ_HANDLER_NAME) != NRF_SUCCESS)
  61. {
  62. return NRF_ERROR_BUSY;
  63. }
  64. #endif
  65. nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
  66. nrf_comp_enable();
  67. // Clear events to be sure there are no leftovers.
  68. nrf_comp_event_clear(NRF_COMP_EVENT_READY);
  69. nrf_comp_event_clear(NRF_COMP_EVENT_DOWN);
  70. nrf_comp_event_clear(NRF_COMP_EVENT_UP);
  71. nrf_comp_event_clear(NRF_COMP_EVENT_CROSS);
  72. nrf_comp_ref_set(p_config->reference);
  73. //If external source is chosen, write to appropriate register.
  74. if (p_config->reference == COMP_REFSEL_REFSEL_ARef)
  75. {
  76. nrf_comp_ext_ref_set(p_config->ext_ref);
  77. }
  78. nrf_comp_th_set(p_config->threshold);
  79. nrf_comp_main_mode_set(p_config->main_mode);
  80. nrf_comp_speed_mode_set(p_config->speed_mode);
  81. nrf_comp_hysteresis_set(p_config->hyst);
  82. nrf_comp_isource_set(p_config->isource);
  83. nrf_comp_shorts_disable(NRF_DRV_COMP_SHORT_STOP_AFTER_CROSS_EVT | NRF_DRV_COMP_SHORT_STOP_AFTER_UP_EVT |
  84. NRF_DRV_COMP_SHORT_STOP_AFTER_DOWN_EVT);
  85. nrf_comp_int_disable(COMP_INTENCLR_CROSS_Msk | COMP_INTENCLR_UP_Msk |
  86. COMP_INTENCLR_DOWN_Msk | COMP_INTENCLR_READY_Msk);
  87. if (event_handler)
  88. {
  89. m_comp_events_handler = event_handler;
  90. }
  91. else
  92. {
  93. return NRF_ERROR_INVALID_PARAM;
  94. }
  95. nrf_comp_input_select(p_config->input);
  96. nrf_drv_common_irq_enable(COMP_LPCOMP_IRQn, p_config->interrupt_priority);
  97. m_state = NRF_DRV_STATE_INITIALIZED;
  98. return NRF_SUCCESS;
  99. }
  100. void nrf_drv_comp_uninit(void)
  101. {
  102. ASSERT(m_state != NRF_DRV_STATE_UNINITIALIZED);
  103. nrf_drv_common_irq_disable(COMP_LPCOMP_IRQn);
  104. nrf_comp_disable();
  105. m_state = NRF_DRV_STATE_UNINITIALIZED;
  106. m_comp_events_handler = NULL;
  107. }
  108. void nrf_drv_comp_pin_select(nrf_comp_input_t psel)
  109. {
  110. bool comp_enable_state = nrf_comp_enable_check();
  111. nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
  112. if(m_state == NRF_DRV_STATE_POWERED_ON)
  113. {
  114. m_state = NRF_DRV_STATE_INITIALIZED;
  115. }
  116. nrf_comp_disable();
  117. nrf_comp_input_select(psel);
  118. if(comp_enable_state == true)
  119. {
  120. nrf_comp_enable();
  121. }
  122. }
  123. void nrf_drv_comp_start(uint32_t comp_int_mask, uint32_t comp_shorts_mask)
  124. {
  125. ASSERT(m_state == NRF_DRV_STATE_INITIALIZED);
  126. nrf_comp_int_enable(comp_int_mask);
  127. nrf_comp_shorts_enable(comp_shorts_mask);
  128. nrf_comp_task_trigger(NRF_COMP_TASK_START);
  129. m_state = NRF_DRV_STATE_POWERED_ON;
  130. }
  131. void nrf_drv_comp_stop(void)
  132. {
  133. ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
  134. nrf_comp_shorts_disable(UINT32_MAX);
  135. nrf_comp_int_disable(UINT32_MAX);
  136. nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
  137. m_state = NRF_DRV_STATE_INITIALIZED;
  138. }
  139. uint32_t nrf_drv_comp_sample()
  140. {
  141. ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
  142. nrf_comp_task_trigger(NRF_COMP_TASK_SAMPLE);
  143. return nrf_comp_result_get();
  144. }