You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

308 lines
12 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_ll_comp.c
  4. * @author MCD Application Team
  5. * @brief COMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright(c) 2016 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32l0xx_ll_comp.h"
  22. #ifdef USE_FULL_ASSERT
  23. #include "stm32_assert.h"
  24. #else
  25. #define assert_param(expr) ((void)0U)
  26. #endif
  27. /** @addtogroup STM32L0xx_LL_Driver
  28. * @{
  29. */
  30. #if defined (COMP1) || defined (COMP2)
  31. /** @addtogroup COMP_LL COMP
  32. * @{
  33. */
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /** @addtogroup COMP_LL_Private_Macros
  39. * @{
  40. */
  41. /* Check of parameters for configuration of COMP hierarchical scope: */
  42. /* COMP instance. */
  43. #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \
  44. ( ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
  45. || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \
  46. )
  47. #if defined (STM32L011xx) || defined (STM32L021xx)
  48. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  49. (((__COMP_INSTANCE__) == COMP1) \
  50. ? ( \
  51. (__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1 \
  52. ) \
  53. : \
  54. ( \
  55. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  56. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  57. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  58. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4) \
  59. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5) \
  60. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO6) \
  61. ) \
  62. )
  63. #else
  64. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  65. (((__COMP_INSTANCE__) == COMP1) \
  66. ? ( \
  67. (__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1 \
  68. ) \
  69. : \
  70. ( \
  71. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  72. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  73. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  74. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4) \
  75. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5) \
  76. ) \
  77. )
  78. #endif
  79. /* Note: On this STM32 serie, comparator input minus parameters are */
  80. /* the different depending on COMP instances. */
  81. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  82. (((__COMP_INSTANCE__) == COMP1) \
  83. ? ( \
  84. ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  85. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  86. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  87. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  88. ) \
  89. : \
  90. ( \
  91. ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  92. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  93. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  94. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  95. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  96. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  97. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  98. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  99. ) \
  100. )
  101. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  102. ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  103. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  104. )
  105. /**
  106. * @}
  107. */
  108. /* Private function prototypes -----------------------------------------------*/
  109. /* Exported functions --------------------------------------------------------*/
  110. /** @addtogroup COMP_LL_Exported_Functions
  111. * @{
  112. */
  113. /** @addtogroup COMP_LL_EF_Init
  114. * @{
  115. */
  116. /**
  117. * @brief De-initialize registers of the selected COMP instance
  118. * to their default reset values.
  119. * @note If comparator is locked, de-initialization by software is
  120. * not possible.
  121. * The only way to unlock the comparator is a device hardware reset.
  122. * @param COMPx COMP instance
  123. * @retval An ErrorStatus enumeration value:
  124. * - SUCCESS: COMP registers are de-initialized
  125. * - ERROR: COMP registers are not de-initialized
  126. */
  127. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  128. {
  129. ErrorStatus status = SUCCESS;
  130. /* Check the parameters */
  131. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  132. /* Note: Hardware constraint (refer to description of this function): */
  133. /* COMP instance must not be locked. */
  134. if(LL_COMP_IsLocked(COMPx) == 0U)
  135. {
  136. if(COMPx == COMP1)
  137. {
  138. CLEAR_BIT(COMPx->CSR,
  139. ( COMP_CSR_COMP1EN
  140. | COMP_CSR_COMP1INNSEL
  141. | COMP_CSR_COMP1WM
  142. | COMP_CSR_COMP1LPTIM1IN1
  143. | COMP_CSR_COMP1POLARITY
  144. | COMP_CSR_COMP1LOCK
  145. )
  146. );
  147. }
  148. else
  149. {
  150. CLEAR_BIT(COMPx->CSR,
  151. ( COMP_CSR_COMP2EN
  152. | COMP_CSR_COMP2SPEED
  153. | COMP_CSR_COMP2INNSEL
  154. | COMP_CSR_COMP2INPSEL
  155. | COMP_CSR_COMP2LPTIM1IN2
  156. | COMP_CSR_COMP2LPTIM1IN1
  157. | COMP_CSR_COMP2POLARITY
  158. | COMP_CSR_COMP2LOCK
  159. )
  160. );
  161. }
  162. }
  163. else
  164. {
  165. /* Comparator instance is locked: de-initialization by software is */
  166. /* not possible. */
  167. /* The only way to unlock the comparator is a device hardware reset. */
  168. status = ERROR;
  169. }
  170. return status;
  171. }
  172. /**
  173. * @brief Initialize some features of COMP instance.
  174. * @note This function configures features of the selected COMP instance.
  175. * Some features are also available at scope COMP common instance
  176. * (common to several COMP instances).
  177. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  178. * @param COMPx COMP instance
  179. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  180. * @retval An ErrorStatus enumeration value:
  181. * - SUCCESS: COMP registers are initialized
  182. * - ERROR: COMP registers are not initialized
  183. */
  184. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  185. {
  186. ErrorStatus status = SUCCESS;
  187. /* Check the parameters */
  188. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  189. if(COMPx == COMP2)
  190. {
  191. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  192. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  193. }
  194. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  195. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  196. /* Note: Hardware constraint (refer to description of this function) */
  197. /* COMP instance must not be locked. */
  198. if(LL_COMP_IsLocked(COMPx) == 0U)
  199. {
  200. /* Configuration of comparator instance : */
  201. /* - PowerMode */
  202. /* - InputPlus */
  203. /* - InputMinus */
  204. /* - OutputPolarity */
  205. /* Note: Connection switch is applicable only to COMP instance COMP1, */
  206. /* therefore is COMP2 is selected the equivalent bit is */
  207. /* kept unmodified. */
  208. if(COMPx == COMP1)
  209. {
  210. MODIFY_REG(COMPx->CSR,
  211. ( COMP_CSR_COMP1INNSEL
  212. | COMP_CSR_COMP1POLARITY
  213. )
  214. ,
  215. ( COMP_InitStruct->InputMinus
  216. | COMP_InitStruct->OutputPolarity
  217. )
  218. );
  219. }
  220. else
  221. {
  222. MODIFY_REG(COMPx->CSR,
  223. ( COMP_CSR_COMP2SPEED
  224. | COMP_CSR_COMP2INPSEL
  225. | COMP_CSR_COMP2INNSEL
  226. | COMP_CSR_COMP2POLARITY
  227. )
  228. ,
  229. ( COMP_InitStruct->PowerMode
  230. | COMP_InitStruct->InputPlus
  231. | COMP_InitStruct->InputMinus
  232. | COMP_InitStruct->OutputPolarity
  233. )
  234. );
  235. }
  236. }
  237. else
  238. {
  239. /* Initialization error: COMP instance is locked. */
  240. status = ERROR;
  241. }
  242. return status;
  243. }
  244. /**
  245. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  246. * @param COMP_InitStruct: pointer to a @ref LL_COMP_InitTypeDef structure
  247. * whose fields will be set to default values.
  248. * @retval None
  249. */
  250. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  251. {
  252. /* Set COMP_InitStruct fields to default values */
  253. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_MEDIUMSPEED;
  254. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  255. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  256. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  257. }
  258. /**
  259. * @}
  260. */
  261. /**
  262. * @}
  263. */
  264. /**
  265. * @}
  266. */
  267. #endif /* COMP1 || COMP2 */
  268. /**
  269. * @}
  270. */
  271. #endif /* USE_FULL_LL_DRIVER */
  272. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/