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.

stm32l1xx_ll_comp.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_ll_comp.c
  4. * @author MCD Application Team
  5. * @brief COMP LL module driver
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 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 "stm32l1xx_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 STM32L1xx_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. /* Note: On this STM32 serie, comparator input plus parameters are */
  48. /* the different depending on COMP instances. */
  49. #if defined(RI_ASCR1_CH_31)
  50. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  51. (((__COMP_INSTANCE__) == COMP1) \
  52. ? ( \
  53. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5) \
  54. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO6) \
  55. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO7) \
  56. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO8) \
  57. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO9) \
  58. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO10) \
  59. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO11) \
  60. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO12) \
  61. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO13) \
  62. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO14) \
  63. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO15) \
  64. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO16) \
  65. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO17) \
  66. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO18) \
  67. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO19) \
  68. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO20) \
  69. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO21) \
  70. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO22) \
  71. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO23) \
  72. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO24) \
  73. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO25) \
  74. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO26) \
  75. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO27) \
  76. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO28) \
  77. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO29) \
  78. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO30) \
  79. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO31) \
  80. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO32) \
  81. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO33) \
  82. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_NONE) \
  83. ) \
  84. : \
  85. ( \
  86. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  87. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  88. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  89. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO4) \
  90. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_NONE) \
  91. ) \
  92. )
  93. #else
  94. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  95. (((__COMP_INSTANCE__) == COMP1) \
  96. ? ( \
  97. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO5) \
  98. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO6) \
  99. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO7) \
  100. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO8) \
  101. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO9) \
  102. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO10) \
  103. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO11) \
  104. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO12) \
  105. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO13) \
  106. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO14) \
  107. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO15) \
  108. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO16) \
  109. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO17) \
  110. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO18) \
  111. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO19) \
  112. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO20) \
  113. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO21) \
  114. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO22) \
  115. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO23) \
  116. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO24) \
  117. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO25) \
  118. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO26) \
  119. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO27) \
  120. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO28) \
  121. ) \
  122. : \
  123. ( \
  124. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  125. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  126. ) \
  127. )
  128. #endif
  129. /* Note: On this STM32 serie, comparator input minus parameters are */
  130. /* the different depending on COMP instances. */
  131. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  132. (((__COMP_INSTANCE__) == COMP1) \
  133. ? ( \
  134. ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  135. ) \
  136. : \
  137. ( \
  138. ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  139. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  140. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  141. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  142. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  143. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  144. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  145. ) \
  146. )
  147. #define IS_LL_COMP_OUTPUT_SELECTION(__OUTPUT_SELECTION__) \
  148. ( ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_NONE) \
  149. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_IC4) \
  150. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_OCREFCLR) \
  151. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_IC4) \
  152. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_OCREFCLR) \
  153. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM4_IC4) \
  154. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM4_OCREFCLR) \
  155. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM10_IC1) \
  156. )
  157. /**
  158. * @}
  159. */
  160. /* Private function prototypes -----------------------------------------------*/
  161. /* Exported functions --------------------------------------------------------*/
  162. /** @addtogroup COMP_LL_Exported_Functions
  163. * @{
  164. */
  165. /** @addtogroup COMP_LL_EF_Init
  166. * @{
  167. */
  168. /**
  169. * @brief De-initialize registers of the selected COMP instance
  170. * to their default reset values.
  171. * @note If comparator is locked, de-initialization by software is
  172. * not possible.
  173. * The only way to unlock the comparator is a device hardware reset.
  174. * @param COMPx COMP instance
  175. * @retval An ErrorStatus enumeration value:
  176. * - SUCCESS: COMP registers are de-initialized
  177. * - ERROR: COMP registers are not de-initialized
  178. */
  179. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  180. {
  181. ErrorStatus status = SUCCESS;
  182. /* Check the parameters */
  183. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  184. /* Note: On this STM32 serie, only COMP instance COMP2 has */
  185. /* features settables: power mode, input minus selection */
  186. /* and output selection. */
  187. /* Note: On this STM32 serie, setting COMP instance COMP2 input minus */
  188. /* is enabling the comparator. */
  189. /* Reset COMP2 input minus also disable the comparator. */
  190. /* Note: In case of de-initialization of COMP instance COMP1: */
  191. /* Switch COMP_CSR_SW1 is not modified because can be used */
  192. /* to connect OPAMP3 to ADC. */
  193. /* Switches RI_ASCR1_VCOMP, RI_ASCR1_SCM are reset: let routing */
  194. /* interface under control of ADC. */
  195. if(COMPx == COMP1)
  196. {
  197. CLEAR_BIT(COMP->CSR,
  198. ( COMP_CSR_CMP1EN
  199. | COMP_CSR_10KPU
  200. | COMP_CSR_400KPU
  201. | COMP_CSR_10KPD
  202. | COMP_CSR_400KPD
  203. )
  204. );
  205. }
  206. else
  207. {
  208. CLEAR_BIT(COMP->CSR,
  209. ( COMP_CSR_SPEED
  210. | COMP_CSR_INSEL
  211. | COMP_CSR_OUTSEL
  212. )
  213. );
  214. }
  215. /* Set comparator input plus */
  216. LL_COMP_SetInputPlus(COMPx, LL_COMP_INPUT_PLUS_NONE);
  217. return status;
  218. }
  219. /**
  220. * @brief Initialize some features of COMP instance.
  221. * @note This function configures features of the selected COMP instance.
  222. * Some features are also available at scope COMP common instance
  223. * (common to several COMP instances).
  224. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  225. * @param COMPx COMP instance
  226. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  227. * @retval An ErrorStatus enumeration value:
  228. * - SUCCESS: COMP registers are initialized
  229. * - ERROR: COMP registers are not initialized
  230. */
  231. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  232. {
  233. ErrorStatus status = SUCCESS;
  234. /* Check the parameters */
  235. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  236. if(COMPx == COMP2)
  237. {
  238. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  239. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  240. assert_param(IS_LL_COMP_OUTPUT_SELECTION(COMP_InitStruct->OutputSelection));
  241. }
  242. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  243. /* Configuration of comparator instance : */
  244. /* - PowerMode */
  245. /* - InputPlus */
  246. /* - InputMinus */
  247. /* - OutputSelection */
  248. /* Note: On this STM32 serie, only COMP instance COMP2 has */
  249. /* features settables: power mode, input minus selection */
  250. /* and output selection. */
  251. /* Note: On this STM32 serie, setting COMP instance COMP2 input minus */
  252. /* is enabling the comparator. */
  253. if(COMPx == COMP2)
  254. {
  255. MODIFY_REG(COMP->CSR,
  256. COMP_CSR_SPEED
  257. | COMP_CSR_INSEL
  258. | COMP_CSR_OUTSEL
  259. ,
  260. COMP_InitStruct->PowerMode
  261. | COMP_InitStruct->InputMinus
  262. | COMP_InitStruct->OutputSelection
  263. );
  264. /* Set comparator input plus */
  265. LL_COMP_SetInputPlus(COMPx, COMP_InitStruct->InputPlus);
  266. }
  267. else /* COMPx == COMP1 */
  268. {
  269. /* If window mode is enabled, COMP1 input plus is not used and therefore
  270. not modified */
  271. if(READ_BIT(COMP12_COMMON->CSR, COMP_CSR_WNDWE) == COMP_CSR_WNDWE)
  272. {
  273. /* Set comparator input plus */
  274. LL_COMP_SetInputPlus(COMPx, COMP_InitStruct->InputPlus);
  275. }
  276. }
  277. return status;
  278. }
  279. /**
  280. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  281. * @param COMP_InitStruct: pointer to a @ref LL_COMP_InitTypeDef structure
  282. * whose fields will be set to default values.
  283. * @retval None
  284. */
  285. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  286. {
  287. /* Set COMP_InitStruct fields to default values */
  288. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
  289. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  290. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  291. COMP_InitStruct->OutputSelection = LL_COMP_OUTPUT_NONE;
  292. }
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. #endif /* COMP1 || COMP2 */
  303. /**
  304. * @}
  305. */
  306. #endif /* USE_FULL_LL_DRIVER */
  307. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/