stm32l4xx_ll_comp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_comp.c
  4. * @author MCD Application Team
  5. * @brief COMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. #if defined(USE_FULL_LL_DRIVER)
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32l4xx_ll_comp.h"
  21. #ifdef USE_FULL_ASSERT
  22. #include "stm32_assert.h"
  23. #else
  24. #define assert_param(expr) ((void)0U)
  25. #endif /* USE_FULL_ASSERT */
  26. /** @addtogroup STM32L4xx_LL_Driver
  27. * @{
  28. */
  29. #if defined (COMP1) || defined (COMP2)
  30. /** @addtogroup COMP_LL COMP
  31. * @{
  32. */
  33. /* Private types -------------------------------------------------------------*/
  34. /* Private variables ---------------------------------------------------------*/
  35. /* Private constants ---------------------------------------------------------*/
  36. /* Private macros ------------------------------------------------------------*/
  37. /** @addtogroup COMP_LL_Private_Macros
  38. * @{
  39. */
  40. /* Check of parameters for configuration of COMP hierarchical scope: */
  41. /* COMP instance. */
  42. #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \
  43. (((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \
  44. || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
  45. || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \
  46. )
  47. /* Note: On this STM32 series, comparator input plus parameters are */
  48. /* the same on all COMP instances. */
  49. /* However, comparator instance kept as macro parameter for */
  50. /* compatibility with other STM32 series. */
  51. #if defined(COMP_CSR_INPSEL_1)
  52. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  53. (((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  54. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  55. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  56. )
  57. #else
  58. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  59. (((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  60. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  61. )
  62. #endif /* COMP_CSR_INPSEL_1 */
  63. /* Note: On this STM32 series, comparator input minus parameters are */
  64. /* the same on all COMP instances. */
  65. /* However, comparator instance kept as macro parameter for */
  66. /* compatibility with other STM32 series. */
  67. #if defined(COMP_CSR_INMESEL_1)
  68. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  69. (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  70. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  71. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  72. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  73. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  74. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  75. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  76. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  77. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \
  78. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4) \
  79. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5) \
  80. )
  81. #else
  82. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  83. (((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  84. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  85. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  86. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  87. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  88. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  89. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  90. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  91. )
  92. #endif /* COMP_CSR_INMESEL_1 */
  93. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
  94. (((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
  95. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
  96. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
  97. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
  98. )
  99. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  100. (((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  101. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  102. )
  103. #if defined(COMP2)
  104. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
  105. (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  106. || (((__COMP_INSTANCE__) == COMP1) \
  107. ? ( \
  108. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
  109. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
  110. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \
  111. ) \
  112. : \
  113. ( \
  114. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2) \
  115. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2) \
  116. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP2) \
  117. ) \
  118. ) \
  119. )
  120. #else
  121. #if defined(TIM3)
  122. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
  123. (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  124. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
  125. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
  126. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \
  127. )
  128. #else
  129. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
  130. (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  131. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
  132. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
  133. )
  134. #endif /* TIM3 */
  135. #endif /* COMP2 */
  136. /**
  137. * @}
  138. */
  139. /* Private function prototypes -----------------------------------------------*/
  140. /* Exported functions --------------------------------------------------------*/
  141. /** @addtogroup COMP_LL_Exported_Functions
  142. * @{
  143. */
  144. /** @addtogroup COMP_LL_EF_Init
  145. * @{
  146. */
  147. /**
  148. * @brief De-initialize registers of the selected COMP instance
  149. * to their default reset values.
  150. * @note If comparator is locked, de-initialization by software is
  151. * not possible.
  152. * The only way to unlock the comparator is a device hardware reset.
  153. * @param COMPx COMP instance
  154. * @retval An ErrorStatus enumeration value:
  155. * - SUCCESS: COMP registers are de-initialized
  156. * - ERROR: COMP registers are not de-initialized
  157. */
  158. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  159. {
  160. ErrorStatus status = SUCCESS;
  161. /* Check the parameters */
  162. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  163. /* Note: Hardware constraint (refer to description of this function): */
  164. /* COMP instance must not be locked. */
  165. if (LL_COMP_IsLocked(COMPx) == 0UL)
  166. {
  167. LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
  168. }
  169. else
  170. {
  171. /* Comparator instance is locked: de-initialization by software is */
  172. /* not possible. */
  173. /* The only way to unlock the comparator is a device hardware reset. */
  174. status = ERROR;
  175. }
  176. return status;
  177. }
  178. /**
  179. * @brief Initialize some features of COMP instance.
  180. * @note This function configures features of the selected COMP instance.
  181. * Some features are also available at scope COMP common instance
  182. * (common to several COMP instances).
  183. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  184. * @param COMPx COMP instance
  185. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  186. * @retval An ErrorStatus enumeration value:
  187. * - SUCCESS: COMP registers are initialized
  188. * - ERROR: COMP registers are not initialized
  189. */
  190. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, const LL_COMP_InitTypeDef *COMP_InitStruct)
  191. {
  192. ErrorStatus status = SUCCESS;
  193. /* Check the parameters */
  194. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  195. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  196. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  197. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  198. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  199. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  200. assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource));
  201. /* Note: Hardware constraint (refer to description of this function) */
  202. /* COMP instance must not be locked. */
  203. if (LL_COMP_IsLocked(COMPx) == 0UL)
  204. {
  205. /* Configuration of comparator instance : */
  206. /* - PowerMode */
  207. /* - InputPlus */
  208. /* - InputMinus */
  209. /* - InputHysteresis */
  210. /* - OutputPolarity */
  211. /* - OutputBlankingSource */
  212. #if defined(COMP_CSR_INMESEL_1)
  213. MODIFY_REG(COMPx->CSR,
  214. COMP_CSR_PWRMODE
  215. | COMP_CSR_INPSEL
  216. | COMP_CSR_SCALEN
  217. | COMP_CSR_BRGEN
  218. | COMP_CSR_INMESEL
  219. | COMP_CSR_INMSEL
  220. | COMP_CSR_HYST
  221. | COMP_CSR_POLARITY
  222. | COMP_CSR_BLANKING
  223. ,
  224. COMP_InitStruct->PowerMode
  225. | COMP_InitStruct->InputPlus
  226. | COMP_InitStruct->InputMinus
  227. | COMP_InitStruct->InputHysteresis
  228. | COMP_InitStruct->OutputPolarity
  229. | COMP_InitStruct->OutputBlankingSource
  230. );
  231. #else
  232. MODIFY_REG(COMPx->CSR,
  233. COMP_CSR_PWRMODE
  234. | COMP_CSR_INPSEL
  235. | COMP_CSR_SCALEN
  236. | COMP_CSR_BRGEN
  237. | COMP_CSR_INMSEL
  238. | COMP_CSR_HYST
  239. | COMP_CSR_POLARITY
  240. | COMP_CSR_BLANKING
  241. ,
  242. COMP_InitStruct->PowerMode
  243. | COMP_InitStruct->InputPlus
  244. | COMP_InitStruct->InputMinus
  245. | COMP_InitStruct->InputHysteresis
  246. | COMP_InitStruct->OutputPolarity
  247. | COMP_InitStruct->OutputBlankingSource
  248. );
  249. #endif /* COMP_CSR_INMESEL_1 */
  250. }
  251. else
  252. {
  253. /* Initialization error: COMP instance is locked. */
  254. status = ERROR;
  255. }
  256. return status;
  257. }
  258. /**
  259. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  260. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  261. * whose fields will be set to default values.
  262. * @retval None
  263. */
  264. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  265. {
  266. /* Set COMP_InitStruct fields to default values */
  267. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
  268. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  269. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  270. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
  271. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  272. COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
  273. }
  274. /**
  275. * @}
  276. */
  277. /**
  278. * @}
  279. */
  280. /**
  281. * @}
  282. */
  283. #endif /* COMP1 || COMP2 */
  284. /**
  285. * @}
  286. */
  287. #endif /* USE_FULL_LL_DRIVER */