stm32l4xx_ll_comp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 families. */
  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
  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 families. */
  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
  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. ? ( \
  107. (1UL) \
  108. ) \
  109. : \
  110. (((__COMP_INSTANCE__) == COMP1) \
  111. ? ( \
  112. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
  113. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
  114. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \
  115. ) \
  116. : \
  117. ( \
  118. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2) \
  119. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2) \
  120. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP2) \
  121. ) \
  122. ) \
  123. )
  124. #else
  125. #if defined(TIM3)
  126. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
  127. (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  128. ? ( \
  129. (1UL) \
  130. ) \
  131. : \
  132. ( \
  133. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
  134. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
  135. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \
  136. ) \
  137. )
  138. #else
  139. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
  140. (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  141. ? ( \
  142. (1UL) \
  143. ) \
  144. : \
  145. ( \
  146. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
  147. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
  148. ) \
  149. )
  150. #endif /* TIM3 */
  151. #endif /* COMP2 */
  152. /**
  153. * @}
  154. */
  155. /* Private function prototypes -----------------------------------------------*/
  156. /* Exported functions --------------------------------------------------------*/
  157. /** @addtogroup COMP_LL_Exported_Functions
  158. * @{
  159. */
  160. /** @addtogroup COMP_LL_EF_Init
  161. * @{
  162. */
  163. /**
  164. * @brief De-initialize registers of the selected COMP instance
  165. * to their default reset values.
  166. * @note If comparator is locked, de-initialization by software is
  167. * not possible.
  168. * The only way to unlock the comparator is a device hardware reset.
  169. * @param COMPx COMP instance
  170. * @retval An ErrorStatus enumeration value:
  171. * - SUCCESS: COMP registers are de-initialized
  172. * - ERROR: COMP registers are not de-initialized
  173. */
  174. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  175. {
  176. ErrorStatus status = SUCCESS;
  177. /* Check the parameters */
  178. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  179. /* Note: Hardware constraint (refer to description of this function): */
  180. /* COMP instance must not be locked. */
  181. if (LL_COMP_IsLocked(COMPx) == 0UL)
  182. {
  183. LL_COMP_WriteReg(COMPx, CSR, 0x00000000UL);
  184. }
  185. else
  186. {
  187. /* Comparator instance is locked: de-initialization by software is */
  188. /* not possible. */
  189. /* The only way to unlock the comparator is a device hardware reset. */
  190. status = ERROR;
  191. }
  192. return status;
  193. }
  194. /**
  195. * @brief Initialize some features of COMP instance.
  196. * @note This function configures features of the selected COMP instance.
  197. * Some features are also available at scope COMP common instance
  198. * (common to several COMP instances).
  199. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  200. * @param COMPx COMP instance
  201. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  202. * @retval An ErrorStatus enumeration value:
  203. * - SUCCESS: COMP registers are initialized
  204. * - ERROR: COMP registers are not initialized
  205. */
  206. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  207. {
  208. ErrorStatus status = SUCCESS;
  209. /* Check the parameters */
  210. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  211. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  212. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  213. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  214. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  215. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  216. assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource));
  217. /* Note: Hardware constraint (refer to description of this function) */
  218. /* COMP instance must not be locked. */
  219. if (LL_COMP_IsLocked(COMPx) == 0UL)
  220. {
  221. /* Configuration of comparator instance : */
  222. /* - PowerMode */
  223. /* - InputPlus */
  224. /* - InputMinus */
  225. /* - InputHysteresis */
  226. /* - OutputPolarity */
  227. /* - OutputBlankingSource */
  228. #if defined(COMP_CSR_INMESEL_1)
  229. MODIFY_REG(COMPx->CSR,
  230. COMP_CSR_PWRMODE
  231. | COMP_CSR_INPSEL
  232. | COMP_CSR_SCALEN
  233. | COMP_CSR_BRGEN
  234. | COMP_CSR_INMESEL
  235. | COMP_CSR_INMSEL
  236. | COMP_CSR_HYST
  237. | COMP_CSR_POLARITY
  238. | COMP_CSR_BLANKING
  239. ,
  240. COMP_InitStruct->PowerMode
  241. | COMP_InitStruct->InputPlus
  242. | COMP_InitStruct->InputMinus
  243. | COMP_InitStruct->InputHysteresis
  244. | COMP_InitStruct->OutputPolarity
  245. | COMP_InitStruct->OutputBlankingSource
  246. );
  247. #else
  248. MODIFY_REG(COMPx->CSR,
  249. COMP_CSR_PWRMODE
  250. | COMP_CSR_INPSEL
  251. | COMP_CSR_SCALEN
  252. | COMP_CSR_BRGEN
  253. | COMP_CSR_INMSEL
  254. | COMP_CSR_HYST
  255. | COMP_CSR_POLARITY
  256. | COMP_CSR_BLANKING
  257. ,
  258. COMP_InitStruct->PowerMode
  259. | COMP_InitStruct->InputPlus
  260. | COMP_InitStruct->InputMinus
  261. | COMP_InitStruct->InputHysteresis
  262. | COMP_InitStruct->OutputPolarity
  263. | COMP_InitStruct->OutputBlankingSource
  264. );
  265. #endif
  266. }
  267. else
  268. {
  269. /* Initialization error: COMP instance is locked. */
  270. status = ERROR;
  271. }
  272. return status;
  273. }
  274. /**
  275. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  276. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  277. * whose fields will be set to default values.
  278. * @retval None
  279. */
  280. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  281. {
  282. /* Set COMP_InitStruct fields to default values */
  283. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
  284. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  285. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  286. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
  287. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  288. COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
  289. }
  290. /**
  291. * @}
  292. */
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. #endif /* COMP1 || COMP2 */
  300. /**
  301. * @}
  302. */
  303. #endif /* USE_FULL_LL_DRIVER */