stm32f0xx_comp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_comp.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the comparators (COMP1 and COMP2) peripheral
  9. * applicable only on STM32F051 and STM32F072 devices:
  10. * + Comparators configuration
  11. * + Window mode control
  12. *
  13. * @verbatim
  14. *
  15. ===============================================================================
  16. ##### How to use this driver #####
  17. ===============================================================================
  18. [..]
  19. The device integrates two analog comparators COMP1 and COMP2:
  20. (+) The non inverting input is set to PA1 for COMP1 and to PA3
  21. for COMP2.
  22. (+) The inverting input can be selected among: DAC1_OUT, DAC2_OUT
  23. 1/4 VREFINT, 1/2 VERFINT, 3/4 VREFINT, VREFINT,
  24. I/O (PA0 for COMP1 and PA2 for COMP2)
  25. (+) The COMP output is internally is available using COMP_GetOutputLevel()
  26. and can be set on GPIO pins: PA0, PA6, PA11 for COMP1
  27. and PA2, PA7, PA12 for COMP2
  28. (+) The COMP output can be redirected to embedded timers (TIM1, TIM2
  29. and TIM3)
  30. (+) The two comparators COMP1 and COMP2 can be combined in window
  31. mode and only COMP1 non inverting (PA1) can be used as non-
  32. inverting input.
  33. (+) The two comparators COMP1 and COMP2 have interrupt capability
  34. with wake-up from Sleep and Stop modes (through the EXTI controller).
  35. COMP1 and COMP2 outputs are internally connected to EXTI Line 21
  36. and EXTI Line 22 respectively.
  37. ##### How to configure the comparator #####
  38. ===============================================================================
  39. [..]
  40. This driver provides functions to configure and program the Comparators
  41. of all STM32F0xx devices.
  42. [..] To use the comparator, perform the following steps:
  43. (#) Enable the SYSCFG APB clock to get write access to comparator
  44. register using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  45. (#) Configure the comparator input in analog mode using GPIO_Init()
  46. (#) Configure the comparator output in alternate function mode
  47. using GPIO_Init() and use GPIO_PinAFConfig() function to map the
  48. comparator output to the GPIO pin
  49. (#) Configure the comparator using COMP_Init() function:
  50. (++) Select the inverting input
  51. (++) Select the output polarity
  52. (++) Select the output redirection
  53. (++) Select the hysteresis level
  54. (++) Select the power mode
  55. (#) Enable the comparator using COMP_Cmd() function
  56. (#) If required enable the COMP interrupt by configuring and enabling
  57. EXTI line in Interrupt mode and selecting the desired sensitivity
  58. level using EXTI_Init() function. After that enable the comparator
  59. interrupt vector using NVIC_Init() function.
  60. @endverbatim
  61. *
  62. ******************************************************************************
  63. * @attention
  64. *
  65. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  66. *
  67. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  68. * You may not use this file except in compliance with the License.
  69. * You may obtain a copy of the License at:
  70. *
  71. * http://www.st.com/software_license_agreement_liberty_v2
  72. *
  73. * Unless required by applicable law or agreed to in writing, software
  74. * distributed under the License is distributed on an "AS IS" BASIS,
  75. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  76. * See the License for the specific language governing permissions and
  77. * limitations under the License.
  78. *
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32f0xx_comp.h"
  83. /** @addtogroup STM32F0xx_StdPeriph_Driver
  84. * @{
  85. */
  86. /** @defgroup COMP
  87. * @brief COMP driver modules
  88. * @{
  89. */
  90. /* Private typedef -----------------------------------------------------------*/
  91. /* Private define ------------------------------------------------------------*/
  92. /* CSR register Mask */
  93. #define COMP_CSR_CLEAR_MASK ((uint32_t)0x00003FFE)
  94. /* Private macro -------------------------------------------------------------*/
  95. /* Private variables ---------------------------------------------------------*/
  96. /* Private function prototypes -----------------------------------------------*/
  97. /* Private functions ---------------------------------------------------------*/
  98. /** @defgroup COMP_Private_Functions
  99. * @{
  100. */
  101. /** @defgroup COMP_Group1 Initialization and Configuration functions
  102. * @brief Initialization and Configuration functions
  103. *
  104. @verbatim
  105. ===============================================================================
  106. ##### Initialization and Configuration functions #####
  107. ===============================================================================
  108. @endverbatim
  109. * @{
  110. */
  111. /**
  112. * @brief Deinitializes COMP peripheral registers to their default reset values.
  113. * @note Deinitialization can't be performed if the COMP configuration is locked.
  114. * To unlock the configuration, perform a system reset.
  115. * @param None
  116. * @retval None
  117. */
  118. void COMP_DeInit(void)
  119. {
  120. COMP->CSR = ((uint32_t)0x00000000); /*!< Set COMP_CSR register to reset value */
  121. }
  122. /**
  123. * @brief Initializes the COMP peripheral according to the specified parameters
  124. * in COMP_InitStruct
  125. * @note If the selected comparator is locked, initialization can't be performed.
  126. * To unlock the configuration, perform a system reset.
  127. * @note By default, PA1 is selected as COMP1 non inverting input.
  128. * To use PA4 as COMP1 non inverting input call COMP_SwitchCmd() after COMP_Init()
  129. * @param COMP_Selection: the selected comparator.
  130. * This parameter can be one of the following values:
  131. * @arg COMP_Selection_COMP1: COMP1 selected
  132. * @arg COMP_Selection_COMP2: COMP2 selected
  133. * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains
  134. * the configuration information for the specified COMP peripheral.
  135. * @retval None
  136. */
  137. void COMP_Init(uint32_t COMP_Selection, COMP_InitTypeDef* COMP_InitStruct)
  138. {
  139. uint32_t tmpreg = 0;
  140. /* Check the parameters */
  141. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  142. assert_param(IS_COMP_INVERTING_INPUT(COMP_InitStruct->COMP_InvertingInput));
  143. assert_param(IS_COMP_OUTPUT(COMP_InitStruct->COMP_Output));
  144. assert_param(IS_COMP_OUTPUT_POL(COMP_InitStruct->COMP_OutputPol));
  145. assert_param(IS_COMP_HYSTERESIS(COMP_InitStruct->COMP_Hysteresis));
  146. assert_param(IS_COMP_MODE(COMP_InitStruct->COMP_Mode));
  147. /*!< Get the COMP_CSR register value */
  148. tmpreg = COMP->CSR;
  149. /*!< Clear the COMP1SW1, COMPx_IN_SEL, COMPx_OUT_TIM_SEL, COMPx_POL, COMPx_HYST and COMPx_PWR_MODE bits */
  150. tmpreg &= (uint32_t) ~(COMP_CSR_CLEAR_MASK<<COMP_Selection);
  151. /*!< Configure COMP: inverting input, output redirection, hysteresis value and power mode */
  152. /*!< Set COMPxINSEL bits according to COMP_InitStruct->COMP_InvertingInput value */
  153. /*!< Set COMPxOUTSEL bits according to COMP_InitStruct->COMP_Output value */
  154. /*!< Set COMPxPOL bit according to COMP_InitStruct->COMP_OutputPol value */
  155. /*!< Set COMPxHYST bits according to COMP_InitStruct->COMP_Hysteresis value */
  156. /*!< Set COMPxMODE bits according to COMP_InitStruct->COMP_Mode value */
  157. tmpreg |= (uint32_t)((COMP_InitStruct->COMP_InvertingInput | COMP_InitStruct->COMP_Output |
  158. COMP_InitStruct->COMP_OutputPol | COMP_InitStruct->COMP_Hysteresis |
  159. COMP_InitStruct->COMP_Mode)<<COMP_Selection);
  160. /*!< Write to COMP_CSR register */
  161. COMP->CSR = tmpreg;
  162. }
  163. /**
  164. * @brief Fills each COMP_InitStruct member with its default value.
  165. * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure which will
  166. * be initialized.
  167. * @retval None
  168. */
  169. void COMP_StructInit(COMP_InitTypeDef* COMP_InitStruct)
  170. {
  171. COMP_InitStruct->COMP_InvertingInput = COMP_InvertingInput_1_4VREFINT;
  172. COMP_InitStruct->COMP_Output = COMP_Output_None;
  173. COMP_InitStruct->COMP_OutputPol = COMP_OutputPol_NonInverted;
  174. COMP_InitStruct->COMP_Hysteresis = COMP_Hysteresis_No;
  175. COMP_InitStruct->COMP_Mode = COMP_Mode_UltraLowPower;
  176. }
  177. /**
  178. * @brief Enable or disable the COMP peripheral.
  179. * @note If the selected comparator is locked, enable/disable can't be performed.
  180. * To unlock the configuration, perform a system reset.
  181. * @param COMP_Selection: the selected comparator.
  182. * This parameter can be one of the following values:
  183. * @arg COMP_Selection_COMP1: COMP1 selected
  184. * @arg COMP_Selection_COMP2: COMP2 selected
  185. * @param NewState: new state of the COMP peripheral.
  186. * This parameter can be: ENABLE or DISABLE.
  187. * @note When enabled, the comparator compares the non inverting input with
  188. * the inverting input and the comparison result is available on comparator output.
  189. * @note When disabled, the comparator doesn't perform comparison and the
  190. * output level is low.
  191. * @retval None
  192. */
  193. void COMP_Cmd(uint32_t COMP_Selection, FunctionalState NewState)
  194. {
  195. /* Check the parameters */
  196. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  197. assert_param(IS_FUNCTIONAL_STATE(NewState));
  198. if (NewState != DISABLE)
  199. {
  200. /* Enable the selected COMP peripheral */
  201. COMP->CSR |= (uint32_t) (1<<COMP_Selection);
  202. }
  203. else
  204. {
  205. /* Disable the selected COMP peripheral */
  206. COMP->CSR &= (uint32_t)(~((uint32_t)1<<COMP_Selection));
  207. }
  208. }
  209. /**
  210. * @brief Close or Open the SW1 switch.
  211. * @note This switch is solely intended to redirect signals onto high
  212. * impedance input, such as COMP1 non-inverting input (highly resistive switch)
  213. * @param NewState: New state of the analog switch.
  214. * This parameter can be: ENABLE or DISABLE.
  215. * @note When enabled, the SW1 is closed; PA1 is connected to PA4
  216. * @note When disabled, the SW1 switch is open; PA1 is disconnected from PA4
  217. * @retval None
  218. */
  219. void COMP_SwitchCmd(FunctionalState NewState)
  220. {
  221. /* Check the parameter */
  222. assert_param(IS_FUNCTIONAL_STATE(NewState));
  223. if (NewState != DISABLE)
  224. {
  225. /* Close SW1 switch */
  226. COMP->CSR |= (uint32_t) (COMP_CSR_COMP1SW1);
  227. }
  228. else
  229. {
  230. /* Open SW1 switch */
  231. COMP->CSR &= (uint32_t)(~COMP_CSR_COMP1SW1);
  232. }
  233. }
  234. /**
  235. * @brief Return the output level (high or low) of the selected comparator.
  236. * @note The output level depends on the selected polarity.
  237. * @note If the polarity is not inverted:
  238. * - Comparator output is low when the non-inverting input is at a lower
  239. * voltage than the inverting input
  240. * - Comparator output is high when the non-inverting input is at a higher
  241. * voltage than the inverting input
  242. * @note If the polarity is inverted:
  243. * - Comparator output is high when the non-inverting input is at a lower
  244. * voltage than the inverting input
  245. * - Comparator output is low when the non-inverting input is at a higher
  246. * voltage than the inverting input
  247. * @param COMP_Selection: the selected comparator.
  248. * This parameter can be one of the following values:
  249. * @arg COMP_Selection_COMP1: COMP1 selected
  250. * @arg COMP_Selection_COMP2: COMP2 selected
  251. * @retval Returns the selected comparator output level: low or high.
  252. *
  253. */
  254. uint32_t COMP_GetOutputLevel(uint32_t COMP_Selection)
  255. {
  256. uint32_t compout = 0x0;
  257. /* Check the parameters */
  258. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  259. /* Check if selected comparator output is high */
  260. if ((COMP->CSR & (COMP_CSR_COMP1OUT<<COMP_Selection)) != 0)
  261. {
  262. compout = COMP_OutputLevel_High;
  263. }
  264. else
  265. {
  266. compout = COMP_OutputLevel_Low;
  267. }
  268. /* Return the comparator output level */
  269. return (uint32_t)(compout);
  270. }
  271. /**
  272. * @}
  273. */
  274. /** @defgroup COMP_Group2 Window mode control function
  275. * @brief Window mode control function
  276. *
  277. @verbatim
  278. ===============================================================================
  279. ##### Window mode control function #####
  280. ===============================================================================
  281. @endverbatim
  282. * @{
  283. */
  284. /**
  285. * @brief Enables or disables the window mode.
  286. * @note In window mode, COMP1 and COMP2 non inverting inputs are connected
  287. * together and only COMP1 non inverting input (PA1) can be used.
  288. * @param NewState: new state of the window mode.
  289. * This parameter can be :
  290. * @arg ENABLE: COMP1 and COMP2 non inverting inputs are connected together.
  291. * @arg DISABLE: OMP1 and COMP2 non inverting inputs are disconnected.
  292. * @retval None
  293. */
  294. void COMP_WindowCmd(FunctionalState NewState)
  295. {
  296. /* Check the parameters */
  297. assert_param(IS_FUNCTIONAL_STATE(NewState));
  298. if (NewState != DISABLE)
  299. {
  300. /* Enable the window mode */
  301. COMP->CSR |= (uint32_t) COMP_CSR_WNDWEN;
  302. }
  303. else
  304. {
  305. /* Disable the window mode */
  306. COMP->CSR &= (uint32_t)(~COMP_CSR_WNDWEN);
  307. }
  308. }
  309. /**
  310. * @}
  311. */
  312. /** @defgroup COMP_Group3 COMP configuration locking function
  313. * @brief COMP1 and COMP2 configuration locking function
  314. * COMP1 and COMP2 configuration can be locked each separately.
  315. * Unlocking is performed by system reset.
  316. *
  317. @verbatim
  318. ===============================================================================
  319. ##### Configuration Lock function #####
  320. ===============================================================================
  321. @endverbatim
  322. * @{
  323. */
  324. /**
  325. * @brief Lock the selected comparator (COMP1/COMP2) configuration.
  326. * @note Locking the configuration means that all control bits are read-only.
  327. * To unlock the comparator configuration, perform a system reset.
  328. * @param COMP_Selection: selects the comparator to be locked
  329. * This parameter can be a value of the following values:
  330. * @arg COMP_Selection_COMP1: COMP1 configuration is locked.
  331. * @arg COMP_Selection_COMP2: COMP2 configuration is locked.
  332. * @retval None
  333. */
  334. void COMP_LockConfig(uint32_t COMP_Selection)
  335. {
  336. /* Check the parameter */
  337. assert_param(IS_COMP_ALL_PERIPH(COMP_Selection));
  338. /* Set the lock bit corresponding to selected comparator */
  339. COMP->CSR |= (uint32_t) (COMP_CSR_COMP1LOCK<<COMP_Selection);
  340. }
  341. /**
  342. * @}
  343. */
  344. /**
  345. * @}
  346. */
  347. /**
  348. * @}
  349. */
  350. /**
  351. * @}
  352. */
  353. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/