stm32l4xx_ll_comp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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</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 "stm32l4xx_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 STM32L4xx_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_ULTRALOWPOWER) \
  63. )
  64. /* Note: On this STM32 serie, comparator input plus parameters are */
  65. /* the same on all COMP instances. */
  66. /* However, comparator instance kept as macro parameter for */
  67. /* compatibility with other STM32 families. */
  68. #if defined(COMP_CSR_INPSEL_1)
  69. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  70. ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  71. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  72. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO3) \
  73. )
  74. #else
  75. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__) \
  76. ( ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1) \
  77. || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO2) \
  78. )
  79. #endif
  80. /* Note: On this STM32 serie, comparator input minus parameters are */
  81. /* the same on all COMP instances. */
  82. /* However, comparator instance kept as macro parameter for */
  83. /* compatibility with other STM32 families. */
  84. #if defined(COMP_CSR_INMESEL_1)
  85. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  86. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  87. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  88. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  89. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  90. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  91. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  92. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  93. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  94. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO3) \
  95. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO4) \
  96. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO5) \
  97. )
  98. #else
  99. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__) \
  100. ( ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT) \
  101. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT) \
  102. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT) \
  103. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT) \
  104. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1) \
  105. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2) \
  106. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1) \
  107. || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO2) \
  108. )
  109. #endif
  110. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__) \
  111. ( ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE) \
  112. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW) \
  113. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM) \
  114. || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH) \
  115. )
  116. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__) \
  117. ( ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED) \
  118. || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED) \
  119. )
  120. #define IS_LL_COMP_OUTPUT_BLANKING_SOURCE(__COMP_INSTANCE__, __OUTPUT_BLANKING_SOURCE__) \
  121. (((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_NONE) \
  122. ? ( \
  123. (1U) \
  124. ) \
  125. : \
  126. (((__COMP_INSTANCE__) == COMP1) \
  127. ? ( \
  128. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM1_OC5_COMP1) \
  129. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM2_OC3_COMP1) \
  130. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC3_COMP1) \
  131. ) \
  132. : \
  133. ( \
  134. ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM3_OC4_COMP2) \
  135. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM8_OC5_COMP2) \
  136. || ((__OUTPUT_BLANKING_SOURCE__) == LL_COMP_BLANKINGSRC_TIM15_OC1_COMP2) \
  137. ) \
  138. ) \
  139. )
  140. /**
  141. * @}
  142. */
  143. /* Private function prototypes -----------------------------------------------*/
  144. /* Exported functions --------------------------------------------------------*/
  145. /** @addtogroup COMP_LL_Exported_Functions
  146. * @{
  147. */
  148. /** @addtogroup COMP_LL_EF_Init
  149. * @{
  150. */
  151. /**
  152. * @brief De-initialize registers of the selected COMP instance
  153. * to their default reset values.
  154. * @note If comparator is locked, de-initialization by software is
  155. * not possible.
  156. * The only way to unlock the comparator is a device hardware reset.
  157. * @param COMPx COMP instance
  158. * @retval An ErrorStatus enumeration value:
  159. * - SUCCESS: COMP registers are de-initialized
  160. * - ERROR: COMP registers are not de-initialized
  161. */
  162. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  163. {
  164. ErrorStatus status = SUCCESS;
  165. /* Check the parameters */
  166. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  167. /* Note: Hardware constraint (refer to description of this function): */
  168. /* COMP instance must not be locked. */
  169. if(LL_COMP_IsLocked(COMPx) == 0U)
  170. {
  171. LL_COMP_WriteReg(COMPx, CSR, 0x00000000U);
  172. }
  173. else
  174. {
  175. /* Comparator instance is locked: de-initialization by software is */
  176. /* not possible. */
  177. /* The only way to unlock the comparator is a device hardware reset. */
  178. status = ERROR;
  179. }
  180. return status;
  181. }
  182. /**
  183. * @brief Initialize some features of COMP instance.
  184. * @note This function configures features of the selected COMP instance.
  185. * Some features are also available at scope COMP common instance
  186. * (common to several COMP instances).
  187. * Refer to functions having argument "COMPxy_COMMON" as parameter.
  188. * @param COMPx COMP instance
  189. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  190. * @retval An ErrorStatus enumeration value:
  191. * - SUCCESS: COMP registers are initialized
  192. * - ERROR: COMP registers are not initialized
  193. */
  194. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  195. {
  196. ErrorStatus status = SUCCESS;
  197. /* Check the parameters */
  198. assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  199. assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  200. assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  201. assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  202. assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  203. assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  204. assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMPx, COMP_InitStruct->OutputBlankingSource));
  205. /* Note: Hardware constraint (refer to description of this function) */
  206. /* COMP instance must not be locked. */
  207. if(LL_COMP_IsLocked(COMPx) == 0U)
  208. {
  209. /* Configuration of comparator instance : */
  210. /* - PowerMode */
  211. /* - InputPlus */
  212. /* - InputMinus */
  213. /* - InputHysteresis */
  214. /* - OutputPolarity */
  215. /* - OutputBlankingSource */
  216. #if defined(COMP_CSR_INMESEL_1)
  217. MODIFY_REG(COMPx->CSR,
  218. COMP_CSR_PWRMODE
  219. | COMP_CSR_INPSEL
  220. | COMP_CSR_SCALEN
  221. | COMP_CSR_BRGEN
  222. | COMP_CSR_INMESEL
  223. | COMP_CSR_INMSEL
  224. | COMP_CSR_HYST
  225. | COMP_CSR_POLARITY
  226. | COMP_CSR_BLANKING
  227. ,
  228. COMP_InitStruct->PowerMode
  229. | COMP_InitStruct->InputPlus
  230. | COMP_InitStruct->InputMinus
  231. | COMP_InitStruct->InputHysteresis
  232. | COMP_InitStruct->OutputPolarity
  233. | COMP_InitStruct->OutputBlankingSource
  234. );
  235. #else
  236. MODIFY_REG(COMPx->CSR,
  237. COMP_CSR_PWRMODE
  238. | COMP_CSR_INPSEL
  239. | COMP_CSR_SCALEN
  240. | COMP_CSR_BRGEN
  241. | COMP_CSR_INMSEL
  242. | COMP_CSR_HYST
  243. | COMP_CSR_POLARITY
  244. | COMP_CSR_BLANKING
  245. ,
  246. COMP_InitStruct->PowerMode
  247. | COMP_InitStruct->InputPlus
  248. | COMP_InitStruct->InputMinus
  249. | COMP_InitStruct->InputHysteresis
  250. | COMP_InitStruct->OutputPolarity
  251. | COMP_InitStruct->OutputBlankingSource
  252. );
  253. #endif
  254. }
  255. else
  256. {
  257. /* Initialization error: COMP instance is locked. */
  258. status = ERROR;
  259. }
  260. return status;
  261. }
  262. /**
  263. * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  264. * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  265. * whose fields will be set to default values.
  266. * @retval None
  267. */
  268. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  269. {
  270. /* Set COMP_InitStruct fields to default values */
  271. COMP_InitStruct->PowerMode = LL_COMP_POWERMODE_ULTRALOWPOWER;
  272. COMP_InitStruct->InputPlus = LL_COMP_INPUT_PLUS_IO1;
  273. COMP_InitStruct->InputMinus = LL_COMP_INPUT_MINUS_VREFINT;
  274. COMP_InitStruct->InputHysteresis = LL_COMP_HYSTERESIS_NONE;
  275. COMP_InitStruct->OutputPolarity = LL_COMP_OUTPUTPOL_NONINVERTED;
  276. COMP_InitStruct->OutputBlankingSource = LL_COMP_BLANKINGSRC_NONE;
  277. }
  278. /**
  279. * @}
  280. */
  281. /**
  282. * @}
  283. */
  284. /**
  285. * @}
  286. */
  287. #endif /* COMP1 || COMP2 */
  288. /**
  289. * @}
  290. */
  291. #endif /* USE_FULL_LL_DRIVER */
  292. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/