cyhal_comp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /***************************************************************************/ /**
  2. * \file cyhal_comp.c
  3. *
  4. * \brief
  5. * Provides a high level interface for interacting with the Infineon analog
  6. * comparator. This interface abstracts out the chip specific details. If any chip
  7. * specific functionality is necessary, or performance is critical the low level
  8. * functions can be used directly.
  9. *
  10. ********************************************************************************
  11. * \copyright
  12. * Copyright 2018-2022 Cypress Semiconductor Corporation (an Infineon company) or
  13. * an affiliate of Cypress Semiconductor Corporation
  14. *
  15. * SPDX-License-Identifier: Apache-2.0
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License");
  18. * you may not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS,
  25. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *******************************************************************************/
  29. #include "cyhal_comp.h"
  30. #include "cyhal_comp_ctb.h"
  31. #include "cyhal_comp_lp.h"
  32. #include "cyhal_gpio.h"
  33. #include "cyhal_system.h"
  34. #include <string.h> // For memset
  35. /**
  36. * \addtogroup group_hal_impl_comp COMP (Analog Comparator)
  37. * \ingroup group_hal_impl
  38. * \{
  39. * On CAT1 & CAT2, the comparator driver can use either of two underlying hardware blocks:
  40. * - Opamp (configured as analog comparator)
  41. * - LPComp (Low Power Comparator)
  42. *
  43. * Generally, a set of pins can only connect to either an Opamp or an LPComp but not both. In the event
  44. * that both connections are possible, the LPComp will be preferred.
  45. *
  46. * \section group_hal_impl_comp_power Power Level Mapping
  47. * The following table shows how the HAL-defined power levels map to the hardware-specific power levels.
  48. * For the LPComp, some levels are named differently between CAT1 & CAT2. In this case, the differences
  49. * are in brackets, with the first item being for CAT1 and the second for CAT2.
  50. * | HAL Power Level | Opamp Power Level | LPComp Power Level |
  51. * | ------------------------------ | ------------------- | ---------------------------- |
  52. * | @ref CYHAL_POWER_LEVEL_HIGH | CY_CTB_POWER_HIGH | CY_LPCOMP_MODE_[NORMAL/FAST] |
  53. * | @ref CYHAL_POWER_LEVEL_MEDIUM | CY_CTB_POWER_MEDIUM | CY_LPCOMP_MODE_[LP/SLOW] |
  54. * | @ref CYHAL_POWER_LEVEL_LOW | CY_CTB_POWER_LOW | CY_LPCOMP_MODE_ULP |
  55. * | @ref CYHAL_POWER_LEVEL_DEFAULT | CY_CTB_POWER_MEDIUM | CY_LPCOMP_MODE_[LP/SLOW] |
  56. *
  57. * \} group_hal_impl_comp
  58. */
  59. /* This file is the top-level wrapper. Comp can be implemented on top of either LPComp or the CTB Opamps */
  60. #if (CYHAL_DRIVER_AVAILABLE_COMP)
  61. #if defined(__cplusplus)
  62. extern "C"
  63. {
  64. #endif
  65. cy_rslt_t cyhal_comp_init(cyhal_comp_t *obj, cyhal_gpio_t vin_p, cyhal_gpio_t vin_m, cyhal_gpio_t output, cyhal_comp_config_t *cfg)
  66. {
  67. cy_rslt_t result;
  68. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  69. result = _cyhal_comp_lp_init(obj, vin_p, vin_m, output, cfg);
  70. #endif
  71. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  72. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  73. if (CY_RSLT_SUCCESS != result)
  74. #endif
  75. {
  76. result = _cyhal_comp_ctb_init(obj, vin_p, vin_m, output, cfg);
  77. }
  78. #endif
  79. return result;
  80. }
  81. cy_rslt_t cyhal_comp_init_cfg(cyhal_comp_t *obj, const cyhal_comp_configurator_t *cfg)
  82. {
  83. cy_rslt_t result = CYHAL_COMP_RSLT_ERR_BAD_ARGUMENT;
  84. memset(obj, 0, sizeof(cyhal_comp_t));
  85. obj->owned_by_configurator = true;
  86. obj->resource = *cfg->resource;
  87. obj->pin_vin_p = NC;
  88. obj->pin_vin_m = NC;
  89. obj->pin_out = NC;
  90. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  91. if (CYHAL_RSC_LPCOMP == cfg->resource->type)
  92. {
  93. result = _cyhal_comp_lp_init_cfg(obj, cfg);
  94. }
  95. #endif
  96. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  97. if (CYHAL_RSC_OPAMP == cfg->resource->type)
  98. {
  99. result = _cyhal_comp_ctb_init_cfg(obj, cfg);
  100. }
  101. #endif
  102. return result;
  103. }
  104. void cyhal_comp_free(cyhal_comp_t *obj)
  105. {
  106. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  107. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  108. if(obj->resource.type == CYHAL_RSC_OPAMP)
  109. #endif
  110. {
  111. _cyhal_comp_ctb_free(obj);
  112. }
  113. #endif
  114. {
  115. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  116. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  117. if(obj->resource.type == CYHAL_RSC_LPCOMP)
  118. #endif
  119. {
  120. _cyhal_comp_lp_free(obj);
  121. }
  122. #endif
  123. }
  124. }
  125. cy_rslt_t cyhal_comp_set_power(cyhal_comp_t *obj, cyhal_power_level_t power)
  126. {
  127. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  128. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  129. if(obj->resource.type == CYHAL_RSC_OPAMP)
  130. #endif
  131. {
  132. return _cyhal_comp_ctb_set_power(obj, power);
  133. }
  134. #endif
  135. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  136. CY_ASSERT(obj->resource.type == CYHAL_RSC_LPCOMP);
  137. return _cyhal_comp_lp_set_power(obj, power);
  138. #endif
  139. }
  140. cy_rslt_t cyhal_comp_configure(cyhal_comp_t *obj, cyhal_comp_config_t *cfg)
  141. {
  142. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  143. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  144. if(obj->resource.type == CYHAL_RSC_OPAMP)
  145. #endif
  146. {
  147. return _cyhal_comp_ctb_configure(obj, cfg);
  148. }
  149. #endif
  150. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  151. CY_ASSERT(obj->resource.type == CYHAL_RSC_LPCOMP);
  152. return _cyhal_comp_lp_configure(obj, cfg);
  153. #endif
  154. }
  155. bool cyhal_comp_read(cyhal_comp_t *obj)
  156. {
  157. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  158. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  159. if(obj->resource.type == CYHAL_RSC_OPAMP)
  160. #endif
  161. {
  162. return _cyhal_comp_ctb_read(obj);
  163. }
  164. #endif
  165. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  166. CY_ASSERT(obj->resource.type == CYHAL_RSC_LPCOMP);
  167. return _cyhal_comp_lp_read(obj);
  168. #endif
  169. }
  170. void cyhal_comp_register_callback(cyhal_comp_t *obj, cyhal_comp_event_callback_t callback, void *callback_arg)
  171. {
  172. CY_UNUSED_PARAMETER(obj);
  173. CY_ASSERT(NULL != obj);
  174. uint32_t savedIntrStatus = cyhal_system_critical_section_enter();
  175. obj->callback_data.callback = (cy_israddress) callback;
  176. obj->callback_data.callback_arg = callback_arg;
  177. cyhal_system_critical_section_exit(savedIntrStatus);
  178. }
  179. void cyhal_comp_enable_event(cyhal_comp_t *obj, cyhal_comp_event_t event, uint8_t intr_priority, bool enable)
  180. {
  181. #if (_CYHAL_DRIVER_AVAILABLE_COMP_CTB)
  182. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  183. if(obj->resource.type == CYHAL_RSC_OPAMP)
  184. #endif
  185. {
  186. _cyhal_comp_ctb_enable_event(obj, event, intr_priority, enable);
  187. return;
  188. }
  189. #endif
  190. #if (_CYHAL_DRIVER_AVAILABLE_COMP_LP)
  191. CY_ASSERT(obj->resource.type == CYHAL_RSC_LPCOMP);
  192. _cyhal_comp_lp_enable_event(obj, event, intr_priority, enable);
  193. #endif
  194. }
  195. #if defined(__cplusplus)
  196. }
  197. #endif
  198. #endif /* CYHAL_DRIVER_AVAILABLE_COMP */