stm32l1xx_ll_comp.c 17 KB

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