nrf_drv_lpcomp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_lpcomp.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 LPCOMP_IRQ LPCOMP_IRQn
  22. #define LPCOMP_IRQ_HANDLER LPCOMP_IRQHandler
  23. static lpcomp_events_handler_t m_lpcomp_events_handler = NULL;
  24. static nrf_drv_state_t m_state = NRF_DRV_STATE_UNINITIALIZED;
  25. static const nrf_drv_lpcomp_config_t m_default_config = NRF_DRV_LPCONF_DEFAULT_CONFIG;
  26. #if PERIPHERAL_RESOURCE_SHARING_ENABLED
  27. #define IRQ_HANDLER_NAME irq_handler_for_lpcomp
  28. #define IRQ_HANDLER static void IRQ_HANDLER_NAME(void)
  29. IRQ_HANDLER;
  30. #else
  31. #define IRQ_HANDLER void LPCOMP_IRQ_HANDLER(void)
  32. #endif // PERIPHERAL_RESOURCE_SHARING_ENABLED
  33. static void lpcomp_execute_handler(nrf_lpcomp_event_t event, uint32_t event_mask)
  34. {
  35. if ( nrf_lpcomp_event_check(event) && nrf_lpcomp_int_enable_check(event_mask) )
  36. {
  37. nrf_lpcomp_event_clear(event);
  38. m_lpcomp_events_handler(event);
  39. }
  40. }
  41. IRQ_HANDLER
  42. {
  43. lpcomp_execute_handler(NRF_LPCOMP_EVENT_READY, LPCOMP_INTENSET_READY_Msk);
  44. lpcomp_execute_handler(NRF_LPCOMP_EVENT_DOWN, LPCOMP_INTENSET_DOWN_Msk);
  45. lpcomp_execute_handler(NRF_LPCOMP_EVENT_UP, LPCOMP_INTENSET_UP_Msk);
  46. lpcomp_execute_handler(NRF_LPCOMP_EVENT_CROSS, LPCOMP_INTENSET_CROSS_Msk);
  47. }
  48. ret_code_t nrf_drv_lpcomp_init(const nrf_drv_lpcomp_config_t * p_config,
  49. lpcomp_events_handler_t events_handler)
  50. {
  51. if (m_state != NRF_DRV_STATE_UNINITIALIZED)
  52. { // LPCOMP 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_LPCOMP, IRQ_HANDLER_NAME) != NRF_SUCCESS)
  61. {
  62. return NRF_ERROR_BUSY;
  63. }
  64. #endif
  65. nrf_lpcomp_configure(&(p_config->hal) );
  66. if (events_handler)
  67. {
  68. m_lpcomp_events_handler = events_handler;
  69. }
  70. else
  71. {
  72. return NRF_ERROR_INVALID_PARAM;
  73. }
  74. nrf_lpcomp_input_select(p_config->input);
  75. switch (p_config->hal.detection)
  76. {
  77. case NRF_LPCOMP_DETECT_UP:
  78. nrf_lpcomp_int_enable(LPCOMP_INTENSET_UP_Msk);
  79. break;
  80. case NRF_LPCOMP_DETECT_DOWN:
  81. nrf_lpcomp_int_enable(LPCOMP_INTENSET_DOWN_Msk);
  82. break;
  83. case NRF_LPCOMP_DETECT_CROSS:
  84. nrf_lpcomp_int_enable(LPCOMP_INTENSET_CROSS_Msk);
  85. break;
  86. default:
  87. break;
  88. }
  89. nrf_lpcomp_shorts_enable(NRF_LPCOMP_SHORT_READY_SAMPLE_MASK);
  90. nrf_drv_common_irq_enable(LPCOMP_IRQn, p_config->interrupt_priority);
  91. m_state = NRF_DRV_STATE_INITIALIZED;
  92. return NRF_SUCCESS;
  93. }
  94. void nrf_drv_lpcomp_uninit(void)
  95. {
  96. ASSERT(m_state != NRF_DRV_STATE_UNINITIALIZED);
  97. nrf_drv_common_irq_disable(LPCOMP_IRQn);
  98. nrf_drv_lpcomp_disable();
  99. m_state = NRF_DRV_STATE_UNINITIALIZED;
  100. m_lpcomp_events_handler = NULL;
  101. }
  102. void nrf_drv_lpcomp_enable(void)
  103. {
  104. ASSERT(m_state == NRF_DRV_STATE_INITIALIZED);
  105. nrf_lpcomp_enable();
  106. nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_START);
  107. m_state = NRF_DRV_STATE_POWERED_ON;
  108. }
  109. void nrf_drv_lpcomp_disable(void)
  110. {
  111. ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
  112. nrf_lpcomp_disable();
  113. nrf_lpcomp_task_trigger(NRF_LPCOMP_TASK_STOP);
  114. m_state = NRF_DRV_STATE_POWERED_ON;
  115. }
  116. void nrf_drv_lpcomp_event_handler_register(lpcomp_events_handler_t lpcomp_events_handler)
  117. {
  118. m_lpcomp_events_handler = lpcomp_events_handler;
  119. }