nrfx_comp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #ifndef NRFX_COMP_H__
  41. #define NRFX_COMP_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_comp.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_comp COMP driver
  49. * @{
  50. * @ingroup nrf_comp
  51. * @brief Comparator (COMP) peripheral driver.
  52. */
  53. /**
  54. * @brief Macro for converting the threshold voltage to an integer value
  55. * (needed by the COMP_TH register).
  56. *
  57. * @param[in] vol Voltage to be changed to COMP_TH register value. This value
  58. * must not be smaller than reference voltage divided by 64.
  59. * @param[in] ref Reference voltage.
  60. */
  61. #define NRFX_VOLTAGE_THRESHOLD_TO_INT(vol, ref) \
  62. (uint8_t)(((vol) > ((ref) / 64)) ? (NRFX_ROUNDED_DIV((vol) * 64,(ref)) - 1) : 0)
  63. /**
  64. * @brief COMP event handler function type.
  65. *
  66. * @param[in] event COMP event.
  67. */
  68. typedef void (* nrfx_comp_event_handler_t)(nrf_comp_event_t event);
  69. /** @brief COMP shortcut masks. */
  70. typedef enum
  71. {
  72. NRFX_COMP_SHORT_STOP_AFTER_CROSS_EVT = COMP_SHORTS_CROSS_STOP_Msk, /*!< Shortcut between the CROSS event and the STOP task. */
  73. NRFX_COMP_SHORT_STOP_AFTER_UP_EVT = COMP_SHORTS_UP_STOP_Msk, /*!< Shortcut between the UP event and the STOP task. */
  74. NRFX_COMP_SHORT_STOP_AFTER_DOWN_EVT = COMP_SHORTS_DOWN_STOP_Msk /*!< Shortcut between the DOWN event and the STOP task. */
  75. } nrfx_comp_short_mask_t;
  76. /** @brief COMP events masks. */
  77. typedef enum
  78. {
  79. NRFX_COMP_EVT_EN_CROSS_MASK = COMP_INTENSET_CROSS_Msk, /*!< CROSS event (generated after VIN+ == VIN-). */
  80. NRFX_COMP_EVT_EN_UP_MASK = COMP_INTENSET_UP_Msk, /*!< UP event (generated when VIN+ crosses VIN- while increasing). */
  81. NRFX_COMP_EVT_EN_DOWN_MASK = COMP_INTENSET_DOWN_Msk, /*!< DOWN event (generated when VIN+ crosses VIN- while decreasing). */
  82. NRFX_COMP_EVT_EN_READY_MASK = COMP_INTENSET_READY_Msk /*!< READY event (generated when the module is ready). */
  83. } nrfx_comp_evt_en_mask_t;
  84. /** @brief COMP configuration. */
  85. typedef struct
  86. {
  87. nrf_comp_ref_t reference; /**< Reference selection. */
  88. nrf_comp_ext_ref_t ext_ref; /**< External analog reference selection. */
  89. nrf_comp_main_mode_t main_mode; /**< Main operation mode. */
  90. nrf_comp_th_t threshold; /**< Structure holding THDOWN and THUP values needed by the COMP_TH register. */
  91. nrf_comp_sp_mode_t speed_mode; /**< Speed and power mode. */
  92. nrf_comp_hyst_t hyst; /**< Comparator hysteresis. */
  93. #if defined (COMP_ISOURCE_ISOURCE_Msk) || defined (__NRFX_DOXYGEN__)
  94. nrf_isource_t isource; /**< Current source selected on analog input. */
  95. #endif
  96. nrf_comp_input_t input; /**< Input to be monitored. */
  97. uint8_t interrupt_priority; /**< Interrupt priority. */
  98. } nrfx_comp_config_t;
  99. /** @brief COMP threshold default configuration. */
  100. #define NRFX_COMP_CONFIG_TH \
  101. { \
  102. .th_down = NRFX_VOLTAGE_THRESHOLD_TO_INT(0.5, 1.8), \
  103. .th_up = NRFX_VOLTAGE_THRESHOLD_TO_INT(1.5, 1.8) \
  104. }
  105. /** @brief COMP driver default configuration including the COMP HAL configuration. */
  106. #if defined (COMP_ISOURCE_ISOURCE_Msk) || defined (__NRFX_DOXYGEN__)
  107. #define NRFX_COMP_DEFAULT_CONFIG(_input) \
  108. { \
  109. .reference = (nrf_comp_ref_t)NRFX_COMP_CONFIG_REF, \
  110. .ext_ref = NRF_COMP_EXT_REF_0, \
  111. .main_mode = (nrf_comp_main_mode_t)NRFX_COMP_CONFIG_MAIN_MODE, \
  112. .threshold = NRFX_COMP_CONFIG_TH, \
  113. .speed_mode = (nrf_comp_sp_mode_t)NRFX_COMP_CONFIG_SPEED_MODE, \
  114. .hyst = (nrf_comp_hyst_t)NRFX_COMP_CONFIG_HYST, \
  115. .isource = (nrf_isource_t)NRFX_COMP_CONFIG_ISOURCE, \
  116. .input = (nrf_comp_input_t)_input, \
  117. .interrupt_priority = NRFX_COMP_CONFIG_IRQ_PRIORITY \
  118. }
  119. #else
  120. #define NRFX_COMP_DEFAULT_CONFIG(_input) \
  121. { \
  122. .reference = (nrf_comp_ref_t)NRFX_COMP_CONFIG_REF, \
  123. .ext_ref = NRF_COMP_EXT_REF_0, \
  124. .main_mode = (nrf_comp_main_mode_t)NRFX_COMP_CONFIG_MAIN_MODE, \
  125. .threshold = NRFX_COMP_CONFIG_TH, \
  126. .speed_mode = (nrf_comp_sp_mode_t)NRFX_COMP_CONFIG_SPEED_MODE, \
  127. .hyst = (nrf_comp_hyst_t)NRFX_COMP_CONFIG_HYST, \
  128. .input = (nrf_comp_input_t)_input, \
  129. .interrupt_priority = NRFX_COMP_CONFIG_IRQ_PRIORITY \
  130. }
  131. #endif
  132. /**
  133. * @brief Function for initializing the COMP driver.
  134. *
  135. * This function initializes the COMP driver, but does not enable the peripheral or any interrupts.
  136. * To start the driver, call the function @ref nrfx_comp_start() after initialization.
  137. *
  138. * @param[in] p_config Pointer to the structure with the initial configuration.
  139. * @param[in] event_handler Event handler provided by the user.
  140. * Must not be NULL.
  141. *
  142. * @retval NRFX_SUCCESS Initialization was successful.
  143. * @retval NRFX_ERROR_INVALID_STATE The driver has already been initialized.
  144. * @retval NRFX_ERROR_BUSY The LPCOMP peripheral is already in use.
  145. * This is possible only if @ref nrfx_prs module
  146. * is enabled.
  147. */
  148. nrfx_err_t nrfx_comp_init(nrfx_comp_config_t const * p_config,
  149. nrfx_comp_event_handler_t event_handler);
  150. /**
  151. * @brief Function for uninitializing the COMP driver.
  152. *
  153. * This function uninitializes the COMP driver. The COMP peripheral and
  154. * its interrupts are disabled, and local variables are cleaned. After this call, you must
  155. * initialize the driver again by calling nrfx_comp_init() if you want to use it.
  156. *
  157. * @sa nrfx_comp_stop
  158. */
  159. void nrfx_comp_uninit(void);
  160. /**
  161. * @brief Function for setting the analog input.
  162. *
  163. * @param[in] psel COMP analog pin selection.
  164. */
  165. void nrfx_comp_pin_select(nrf_comp_input_t psel);
  166. /**
  167. * @brief Function for starting the COMP peripheral and interrupts.
  168. *
  169. * Before calling this function, the driver must be initialized. This function
  170. * enables the COMP peripheral and its interrupts.
  171. *
  172. * @param[in] comp_evt_en_mask Mask of events to be enabled. This parameter is to be built as
  173. * an OR of elements from @ref nrfx_comp_evt_en_mask_t.
  174. * @param[in] comp_shorts_mask Mask of shortcuts to be enabled. This parameter is to be built as
  175. * an OR of elements from @ref nrfx_comp_short_mask_t.
  176. *
  177. * @sa nrfx_comp_init
  178. */
  179. void nrfx_comp_start(uint32_t comp_evt_en_mask, uint32_t comp_shorts_mask);
  180. /**
  181. * @brief Function for stopping the COMP peripheral.
  182. *
  183. * Before calling this function, the driver must be enabled. This function disables the COMP
  184. * peripheral and its interrupts.
  185. *
  186. * @sa nrfx_comp_uninit
  187. */
  188. void nrfx_comp_stop(void);
  189. /**
  190. * @brief Function for copying the current state of the comparator result to the RESULT register.
  191. *
  192. * @retval 0 The input voltage is below the threshold (VIN+ < VIN-).
  193. * @retval 1 The input voltage is above the threshold (VIN+ > VIN-).
  194. */
  195. uint32_t nrfx_comp_sample(void);
  196. /**
  197. * @brief Function for getting the address of a COMP task.
  198. *
  199. * @param[in] task COMP task.
  200. *
  201. * @return Address of the given COMP task.
  202. */
  203. __STATIC_INLINE uint32_t nrfx_comp_task_address_get(nrf_comp_task_t task)
  204. {
  205. return (uint32_t)nrf_comp_task_address_get(task);
  206. }
  207. /**
  208. * @brief Function for getting the address of a COMP event.
  209. *
  210. * @param[in] event COMP event.
  211. *
  212. * @return Address of the given COMP event.
  213. */
  214. __STATIC_INLINE uint32_t nrfx_comp_event_address_get(nrf_comp_event_t event)
  215. {
  216. return (uint32_t)nrf_comp_event_address_get(event);
  217. }
  218. /** @} */
  219. void nrfx_comp_irq_handler(void);
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223. #endif // NRFX_COMP_H__