stm32f0xx_comp.c 15 KB

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