nrfx_comp.h 9.9 KB

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