stm32l4xx_ll_comp.c 15 KB

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