nrf_drv_comp.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #ifndef NRF_DRV_COMP_H__
  13. #define NRF_DRV_COMP_H__
  14. #include "nrf_comp.h"
  15. #include "sdk_errors.h"
  16. #include "nrf_drv_common.h"
  17. #include "nrf_drv_config.h"
  18. #include "app_util_platform.h"
  19. /**
  20. * @addtogroup nrf_comp COMP HAL and driver
  21. * @ingroup nrf_drivers
  22. * @brief @tagAPI52 Comparator (COMP) APIs.
  23. * @details The COMP HAL provides basic APIs for accessing the registers of Comparator.
  24. * The COMP driver provides APIs on a higher level.
  25. *
  26. * @defgroup nrf_drivers_comp COMP driver
  27. * @{
  28. * @ingroup nrf_comp
  29. * @brief @tagAPI52 Comparator (COMP) driver.
  30. */
  31. /**
  32. * @brief Macro to convert the threshold voltage to an integer value (needed by the COMP_TH register).
  33. *
  34. * @param[in] vol Voltage to be changed to COMP_TH register value. This value must not be smaller than
  35. * reference voltage divided by 64.
  36. * @param[in] ref Reference voltage.
  37. */
  38. #define VOLTAGE_THRESHOLD_TO_INT(vol, ref) (uint8_t)(((vol) > ((ref)/64)) ? (ROUNDED_DIV((vol)*64,(ref))-1) : 0)
  39. /**@brief COMP event handler function type.
  40. * @param[in] event COMP event.
  41. */
  42. typedef void (* comp_events_handler_t)(nrf_comp_event_t event);
  43. /**
  44. * @enum nrf_drv_comp_short_mask_t
  45. * @brief COMP shortcut masks.
  46. */
  47. typedef enum
  48. {
  49. NRF_DRV_COMP_SHORT_STOP_AFTER_CROSS_EVT = COMP_SHORTS_CROSS_STOP_Msk, /*!< Shortcut between the CROSS event and the STOP task. */
  50. NRF_DRV_COMP_SHORT_STOP_AFTER_UP_EVT = COMP_SHORTS_UP_STOP_Msk, /*!< Shortcut between the UP event and the STOP task. */
  51. NRF_DRV_COMP_SHORT_STOP_AFTER_DOWN_EVT = COMP_SHORTS_DOWN_STOP_Msk /*!< Shortcut between the DOWN event and the STOP task. */
  52. }nrf_drv_comp_short_mask_t;
  53. /**
  54. * @enum nrf_drv_comp_evt_en_mask_t
  55. * @brief COMP events masks.
  56. */
  57. typedef enum
  58. {
  59. NRF_DRV_COMP_EVT_EN_CROSS_MASK = COMP_INTENSET_CROSS_Msk, /*!< CROSS event (generated after VIN+ == VIN-). */
  60. NRF_DRV_COMP_EVT_EN_UP_MASK = COMP_INTENSET_UP_Msk, /*!< UP event (generated when VIN+ crosses VIN- while increasing). */
  61. NRF_DRV_COMP_EVT_EN_DOWN_MASK = COMP_INTENSET_DOWN_Msk, /*!< DOWN event (generated when VIN+ crosses VIN- while decreasing). */
  62. NRF_DRV_COMP_EVT_EN_READY_MASK = COMP_INTENSET_READY_Msk /*!< READY event (generated when the module is ready). */
  63. }nrf_drv_comp_evt_en_mask_t;
  64. /**@brief COMP configuration.
  65. */
  66. typedef struct
  67. {
  68. nrf_comp_ref_t reference; /**< Reference selection. */
  69. nrf_comp_ext_ref_t ext_ref; /**< External analog reference selection. */
  70. nrf_comp_main_mode_t main_mode; /**< Main operation mode. */
  71. nrf_comp_th_t threshold; /**< Structure holding THDOWN and THUP values needed by the COMP_TH register. */
  72. nrf_comp_sp_mode_t speed_mode; /**< Speed and power mode. */
  73. nrf_comp_hyst_t hyst; /**< Comparator hysteresis.*/
  74. nrf_isource_t isource; /**< Current source selected on analog input. */
  75. nrf_comp_input_t input; /**< Input to be monitored. */
  76. uint8_t interrupt_priority; /**< Interrupt priority. */
  77. } nrf_drv_comp_config_t;
  78. /** @brief COMP threshold default configuration. */
  79. #define COMP_CONFIG_TH \
  80. { \
  81. .th_down = VOLTAGE_THRESHOLD_TO_INT(0.5, 1.8), \
  82. .th_up = VOLTAGE_THRESHOLD_TO_INT(1.5, 1.8) \
  83. }
  84. /** @brief COMP driver default configuration including the COMP HAL configuration. */
  85. #define NRF_DRV_COMP_CONF_DEFAULT_CONFIG(INPUT) \
  86. { \
  87. .reference = COMP_CONFIG_REF, \
  88. .main_mode = COMP_CONFIG_MAIN_MODE, \
  89. .threshold = COMP_CONFIG_TH, \
  90. .speed_mode = COMP_CONFIG_SPEED_MODE, \
  91. .hyst = COMP_CONFIG_HYST, \
  92. .isource = COMP_CONFIG_ISOURCE, \
  93. .input = INPUT, \
  94. .interrupt_priority = COMP_CONFIG_IRQ_PRIORITY \
  95. }
  96. /**
  97. * @brief Function for initializing the COMP driver.
  98. *
  99. * This function initializes the COMP driver, but does not enable the peripheral or any interrupts.
  100. * To start the driver, call the function @ref nrf_drv_comp_start() after initialization.
  101. *
  102. * If no configuration structure is provided, the driver is initialized with the default settings.
  103. *
  104. * @param[in] p_config Initial configuration. If NULL, the default configuration is used.
  105. * @param[in] event_handler Handler function.
  106. *
  107. * @retval NRF_ERROR_INVALID_PARAM If the configuration is invalid.
  108. * @retval NRF_ERROR_INVALID_STATE If the driver has already been initialized.
  109. * @retval NRF_ERROR_BUSY If the LPCOMP driver is initialized.
  110. */
  111. ret_code_t nrf_drv_comp_init(const nrf_drv_comp_config_t * p_config,
  112. comp_events_handler_t event_handler);
  113. /**
  114. * @brief Function for uninitializing the COMP driver.
  115. *
  116. * This function uninitializes the COMP driver. The COMP peripheral and
  117. * its interrupts are disabled, and local variables are cleaned. After this call, you must
  118. * initialize the driver again by calling nrf_drv_comp_init() if you want to use it.
  119. *
  120. * @sa nrf_drv_comp_stop()
  121. */
  122. void nrf_drv_comp_uninit(void);
  123. /**
  124. * @brief Function for setting the analog input.
  125. *
  126. * @param[in] psel COMP analog pin selection.
  127. */
  128. void nrf_drv_comp_pin_select(nrf_comp_input_t psel);
  129. /**
  130. * @brief Function for starting the COMP peripheral and interrupts.
  131. *
  132. * Before calling this function, the driver must be initialized. This function
  133. * enables the COMP peripheral and its interrupts.
  134. *
  135. * @param[in] comp_evt_en_mask Mask of events to be enabled. This parameter should be built as
  136. * 'or' of elements from @ref nrf_drv_comp_evt_en_mask_t.
  137. * @param[in] comp_shorts_mask Mask of shorts to be enabled. This parameter should be built as
  138. * 'or' of elements from @ref nrf_drv_comp_short_mask_t.
  139. *
  140. * @sa nrf_drv_comp_init()
  141. *
  142. */
  143. void nrf_drv_comp_start(uint32_t comp_evt_en_mask, uint32_t comp_shorts_mask);
  144. /**@brief Function for stopping the COMP peripheral.
  145. *
  146. * Before calling this function, the driver must be enabled. This function disables the COMP
  147. * peripheral and its interrupts.
  148. *
  149. * @sa nrf_drv_comp_uninit()
  150. *
  151. */
  152. void nrf_drv_comp_stop(void);
  153. /**
  154. * @brief Function for copying the current state of the comparator result to the RESULT register.
  155. *
  156. * @retval 0 If the input voltage is below the threshold (VIN+ < VIN-).
  157. * @retval 1 If the input voltage is above the threshold (VIN+ > VIN-).
  158. */
  159. uint32_t nrf_drv_comp_sample(void);
  160. /**
  161. * @brief Function for getting the task address.
  162. *
  163. * Before calling this function, the driver must be enabled.
  164. *
  165. * @param[in] comp_task COMP task.
  166. *
  167. * @return Address of the given COMP task.
  168. */
  169. __STATIC_INLINE uint32_t nrf_drv_comp_task_address_get(nrf_comp_task_t comp_task)
  170. {
  171. return (uint32_t)nrf_comp_task_address_get(comp_task);
  172. }
  173. /**
  174. * @brief Function for getting the event address.
  175. *
  176. * @param[in] comp_event COMP event.
  177. *
  178. * @return Address of the given COMP event.
  179. */
  180. __STATIC_INLINE uint32_t nrf_drv_comp_event_address_get(nrf_comp_event_t comp_event)
  181. {
  182. return (uint32_t)nrf_comp_event_address_get(comp_event);
  183. }
  184. /**
  185. * @brief Function for converting a GPIO pin number to an analog COMP channel.
  186. *
  187. * @param[in] pin GPIO pin number.
  188. *
  189. * @return COMP channel. The function returns UINT8_MAX
  190. * if the specified pin is not an analog input.
  191. */
  192. __STATIC_INLINE nrf_comp_input_t nrf_drv_comp_gpio_to_ain(uint8_t pin);
  193. /**
  194. * @brief Function for converting a COMP channel to a GPIO pin number.
  195. *
  196. * @param[in] ain COMP channel.
  197. *
  198. * @return GPIO pin number. The function returns UINT8_MAX
  199. * if the specified channel is not a GPIO pin.
  200. */
  201. __STATIC_INLINE uint8_t nrf_drv_comp_ain_to_gpio(nrf_comp_input_t ain);
  202. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  203. __STATIC_INLINE nrf_comp_input_t nrf_drv_comp_gpio_to_ain(uint8_t pin)
  204. {
  205. // AIN0 - AIN3
  206. if ((pin >= 2) && (pin <= 5))
  207. {
  208. return (nrf_comp_input_t)(pin-2);
  209. }
  210. // AIN4 - AIN7
  211. else if ((pin >= 28) && (pin <= 31))
  212. {
  213. return (nrf_comp_input_t)(pin - 24);
  214. }
  215. else
  216. {
  217. return (nrf_comp_input_t)UINT8_MAX;
  218. }
  219. }
  220. __STATIC_INLINE uint8_t nrf_drv_comp_ain_to_gpio(nrf_comp_input_t ain)
  221. {
  222. // AIN0 - AIN3
  223. if (ain <= 3)
  224. {
  225. return (uint8_t)(ain + 2);
  226. }
  227. // AIN4 - AIN7
  228. else if ((ain >= 4) && (ain <= 7))
  229. {
  230. return (uint8_t)(ain + 24);
  231. }
  232. else
  233. {
  234. return UINT8_MAX;
  235. }
  236. }
  237. #endif //SUPPRESS_INLINE_IMPLEMENTATION
  238. /**
  239. *@}
  240. **/
  241. #endif /* NRF_DRV_COMP_H__ */