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.
 
 
 

332 lines
14 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_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</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32f0xx_ll_comp.h"
  38. #ifdef USE_FULL_ASSERT
  39. #include "stm32_assert.h"
  40. #else
  41. #define assert_param(expr) ((void)0U)
  42. #endif
  43. /** @addtogroup STM32F0xx_LL_Driver
  44. * @{
  45. */
  46. #if defined (COMP1) || defined (COMP2)
  47. /** @addtogroup COMP_LL COMP
  48. * @{
  49. */
  50. /* Private types -------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /* Private constants ---------------------------------------------------------*/
  53. /* Private macros ------------------------------------------------------------*/
  54. /** @addtogroup COMP_LL_Private_Macros
  55. * @{
  56. */
  57. /* Check of parameters for configuration of COMP hierarchical scope: */
  58. /* COMP instance. */
  59. #define IS_LL_COMP_POWER_MODE(__POWER_MODE__) \
  60. ( ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED) \
  61. || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED) \
  62. || ((__POWER_MODE__) == LL_COMP_POWERMODE_LOWPOWER) \
  63. || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER) \
  64. )
  65. /* Note: On this STM32 serie, comparator input plus parameters are */
  66. /* the different depending on COMP instances. */
  67. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  68. (((__COMP_INSTANCE__) == COMP1) \
  69. ? ( \
  70. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  71. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_DAC1_CH1) \
  72. ) \
  73. : \
  74. ( \
  75. ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  76. ) \
  77. )
  78. /* Note: On this STM32 serie, comparator input minus parameters are */
  79. /* the same on all COMP instances. */
  80. /* However, comparator instance kept as macro parameter for */
  81. /* compatibility with other STM32 families. */
  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. )
  91. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
  92. ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
  93. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
  94. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
  95. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
  96. )
  97. #define IS_LL_COMP_OUTPUT_SELECTION(__OUTPUT_SELECTION__) \
  98. ( ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_NONE) \
  99. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_BKIN) \
  100. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_IC1) \
  101. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_OCCLR) \
  102. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_IC4) \
  103. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_OCCLR) \
  104. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_IC1) \
  105. || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_OCCLR) \
  106. )
  107. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  108. ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  109. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  110. )
  111. /**
  112. * @}
  113. */
  114. /* Private function prototypes -----------------------------------------------*/
  115. /* Exported functions --------------------------------------------------------*/
  116. /** @addtogroup COMP_LL_Exported_Functions
  117. * @{
  118. */
  119. /** @addtogroup COMP_LL_EF_Init
  120. * @{
  121. */
  122. /**
  123. * @brief De-initialize registers of the selected COMP instance
  124. * to their default reset values.
  125. * @note If comparator is locked, de-initialization by software is
  126. * not possible.
  127. * The only way to unlock the comparator is a device hardware reset.
  128. * @param COMPx COMP instance
  129. * @retval An ErrorStatus enumeration value:
  130. * - SUCCESS: COMP registers are de-initialized
  131. * - ERROR: COMP registers are not de-initialized
  132. */
  133. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  134. {
  135. ErrorStatus status = SUCCESS;
  136. /* Check the parameters */
  137. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  138. /* Note: Hardware constraint (refer to description of this function): */
  139. /* COMP instance must not be locked. */
  140. if(LL_COMP_IsLocked(COMPx) == 0U)
  141. {
  142. /* Note: Connection switch is applicable only to COMP instance COMP1, */
  143. /* therefore is COMP2 is selected the equivalent bit is */
  144. /* kept unmodified. */
  145. if(COMPx == COMP1)
  146. {
  147. CLEAR_BIT(COMP->CSR,
  148. ( COMP_CSR_COMP1MODE
  149. | COMP_CSR_COMP1INSEL
  150. | COMP_CSR_COMP1SW1
  151. | COMP_CSR_COMP1OUTSEL
  152. | COMP_CSR_COMP1HYST
  153. | COMP_CSR_COMP1POL
  154. | COMP_CSR_COMP1EN
  155. ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  156. );
  157. }
  158. else
  159. {
  160. CLEAR_BIT(COMP->CSR,
  161. ( COMP_CSR_COMP1MODE
  162. | COMP_CSR_COMP1INSEL
  163. | COMP_CSR_COMP1OUTSEL
  164. | COMP_CSR_COMP1HYST
  165. | COMP_CSR_COMP1POL
  166. | COMP_CSR_COMP1EN
  167. ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  168. );
  169. }
  170. }
  171. else
  172. {
  173. /* Comparator instance is locked: de-initialization by software is */
  174. /* not possible. */
  175. /* The only way to unlock the comparator is a device hardware reset. */
  176. status = ERROR;
  177. }
  178. return status;
  179. }
  180. /**
  181. * @brief Initialize some features of COMP instance.
  182. * @note This function configures features of the selected COMP instance.
  183. * Some features are also available at scope COMP common instance
  184. * (common to several COMP instances).
  185. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  186. * @param COMPx COMP instance
  187. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  188. * @retval An ErrorStatus enumeration value:
  189. * - SUCCESS: COMP registers are initialized
  190. * - ERROR: COMP registers are not initialized
  191. */
  192. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  193. {
  194. ErrorStatus status = SUCCESS;
  195. /* Check the parameters */
  196. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  197. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  198. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  199. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  200. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  201. assert_param(IS_LL_COMP_OUTPUT_SELECTION(COMP_InitStruct->OutputSelection));
  202. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  203. /* Note: Hardware constraint (refer to description of this function) */
  204. /* COMP instance must not be locked. */
  205. if(LL_COMP_IsLocked(COMPx) == 0U)
  206. {
  207. /* Configuration of comparator instance : */
  208. /* - PowerMode */
  209. /* - InputPlus */
  210. /* - InputMinus */
  211. /* - InputHysteresis */
  212. /* - OutputSelection */
  213. /* - OutputPolarity */
  214. /* Note: Connection switch is applicable only to COMP instance COMP1, */
  215. /* therefore is COMP2 is selected the equivalent bit is */
  216. /* kept unmodified. */
  217. if(COMPx == COMP1)
  218. {
  219. MODIFY_REG(COMP->CSR,
  220. ( COMP_CSR_COMP1MODE
  221. | COMP_CSR_COMP1INSEL
  222. | COMP_CSR_COMP1SW1
  223. | COMP_CSR_COMP1OUTSEL
  224. | COMP_CSR_COMP1HYST
  225. | COMP_CSR_COMP1POL
  226. ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  227. ,
  228. ( COMP_InitStruct->PowerMode
  229. | COMP_InitStruct->InputPlus
  230. | COMP_InitStruct->InputMinus
  231. | COMP_InitStruct->InputHysteresis
  232. | COMP_InitStruct->OutputSelection
  233. | COMP_InitStruct->OutputPolarity
  234. ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  235. );
  236. }
  237. else
  238. {
  239. MODIFY_REG(COMP->CSR,
  240. ( COMP_CSR_COMP1MODE
  241. | COMP_CSR_COMP1INSEL
  242. | COMP_CSR_COMP1OUTSEL
  243. | COMP_CSR_COMP1HYST
  244. | COMP_CSR_COMP1POL
  245. ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  246. ,
  247. ( COMP_InitStruct->PowerMode
  248. | COMP_InitStruct->InputPlus
  249. | COMP_InitStruct->InputMinus
  250. | COMP_InitStruct->InputHysteresis
  251. | COMP_InitStruct->OutputSelection
  252. | COMP_InitStruct->OutputPolarity
  253. ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  254. );
  255. }
  256. }
  257. else
  258. {
  259. /* Initialization error: COMP instance is locked. */
  260. status = ERROR;
  261. }
  262. return status;
  263. }
  264. /**
  265. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  266. * @param COMP_InitStruct pointer to a @ref LL_COMP_InitTypeDef structure
  267. * whose fields will be set to default values.
  268. * @retval None
  269. */
  270. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  271. {
  272. /* Set COMP_InitStruct fields to default values */
  273. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
  274. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  275. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  276. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
  277. COMP_InitStruct->OutputSelection = LL_COMP_OUTPUT_NONE;
  278. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  279. }
  280. /**
  281. * @}
  282. */
  283. /**
  284. * @}
  285. */
  286. /**
  287. * @}
  288. */
  289. #endif /* COMP1 || COMP2 */
  290. /**
  291. * @}
  292. */
  293. #endif /* USE_FULL_LL_DRIVER */
  294. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/