/** * @brief Enables or disables the interrupt generation when an event is detected. * @param COMP_Selection : selects the comparator * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: Selection of Comparator 1 * @arg COMP_Selection_COMP2: Selection of Comparator 2 * @param NewState : new state of the COMPx interrupt. * This parameter can be: ENABLE or DISABLE. * @retval None */ void COMP_ITConfig(COMP_Selection_TypeDef COMP_Selection, FunctionalState NewState) { /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); assert_param(IS_FUNCTIONAL_STATE(NewState)); /* Check if Comparator 1 is selected */ if (COMP_Selection == COMP_Selection_COMP1) { if (NewState != DISABLE) { /* Enable the COMP1 Interrupt source */ COMP->CSR1 |= (uint8_t) COMP_CSR1_IE1; } else { /* Disable the COMP1 Interrupt source */ COMP->CSR1 &= (uint8_t)(~COMP_CSR1_IE1); } } else /* Comparator 2 is selected */ { if (NewState != DISABLE) { /* Enable the COMP2 Interrupt source */ COMP->CSR2 |= (uint8_t) COMP_CSR2_IE2; } else { /* Disable the COMP2 Interrupt source */ COMP->CSR2 &= (uint8_t)(~COMP_CSR2_IE2); } } }
/** * @brief Configures the COMP edge detection. * @param COMP_Selection: selects the comparator. * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: Selection of Comparator 1 * @arg COMP_Selection_COMP2: Selection of Comparator 2 * @param COMP_Edge: This parameter can be one of the following values: * @arg COMP_Edge_Falling: Falling edge selection * @arg COMP_Edge_Rising: Rising edge selection * @arg COMP_Edge_Rising_Falling: Rising and Falling edge selection * @retval None. */ void COMP_EdgeConfig(COMP_Selection_TypeDef COMP_Selection, COMP_Edge_TypeDef COMP_Edge) { /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); assert_param(IS_COMP_EDGE(COMP_Edge)); /* Check if comparator 1 is selected */ if (COMP_Selection == COMP_Selection_COMP1) { /* Reset the comparator 1 edge control bits */ COMP->CSR1 &= (uint8_t) (~COMP_CSR1_CMP1); /* Select the edge detection of comparator 1 output */ COMP->CSR1 |= (uint8_t) COMP_Edge; } /* The comparator 2 is selected */ else { /* Reset the comparator 2 edge control bits */ COMP->CSR2 &= (uint8_t) (~COMP_CSR2_CMP2); /* Select the edge detection of comparator 2 output */ COMP->CSR2 |= (uint8_t) COMP_Edge; } }
/** * @brief Initializes the COMP peripheral according to the specified parameters * in COMP_InitStruct * @note If the selected comparator is locked, initialization can't be performed. * To unlock the configuration, perform a system reset. * @note By default, PA1 is selected as COMP1 non inverting input. * To use PA4 as COMP1 non inverting input call COMP_SwitchCmd() after COMP_Init() * @param COMP_Selection: the selected comparator. * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: COMP1 selected * @arg COMP_Selection_COMP2: COMP2 selected * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains * the configuration information for the specified COMP peripheral. * @retval None */ void COMP_Init(uint32_t COMP_Selection, COMP_InitTypeDef* COMP_InitStruct) { uint32_t tmpreg = 0; /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); assert_param(IS_COMP_INVERTING_INPUT(COMP_InitStruct->COMP_InvertingInput)); assert_param(IS_COMP_OUTPUT(COMP_InitStruct->COMP_Output)); assert_param(IS_COMP_OUTPUT_POL(COMP_InitStruct->COMP_OutputPol)); assert_param(IS_COMP_HYSTERESIS(COMP_InitStruct->COMP_Hysteresis)); assert_param(IS_COMP_MODE(COMP_InitStruct->COMP_Mode)); /*!< Get the COMP_CSR register value */ tmpreg = COMP->CSR; /*!< Clear the COMP1SW1, COMPx_IN_SEL, COMPx_OUT_TIM_SEL, COMPx_POL, COMPx_HYST and COMPx_PWR_MODE bits */ tmpreg &= (uint32_t) ~(COMP_CSR_CLEAR_MASK<<COMP_Selection); /*!< Configure COMP: inverting input, output redirection, hysteresis value and power mode */ /*!< Set COMPxINSEL bits according to COMP_InitStruct->COMP_InvertingInput value */ /*!< Set COMPxOUTSEL bits according to COMP_InitStruct->COMP_Output value */ /*!< Set COMPxPOL bit according to COMP_InitStruct->COMP_OutputPol value */ /*!< Set COMPxHYST bits according to COMP_InitStruct->COMP_Hysteresis value */ /*!< Set COMPxMODE bits according to COMP_InitStruct->COMP_Mode value */ tmpreg |= (uint32_t)((COMP_InitStruct->COMP_InvertingInput | COMP_InitStruct->COMP_Output | COMP_InitStruct->COMP_OutputPol | COMP_InitStruct->COMP_Hysteresis | COMP_InitStruct->COMP_Mode)<<COMP_Selection); /*!< Write to COMP_CSR register */ COMP->CSR = tmpreg; }
/** * @brief Initializes the COMP peripheral according to the specified parameters * in COMP_InitStruct * @note If the selected comparator is locked, initialization can't be performed. * To unlock the configuration, perform a system reset. * @note By default, PA1 is selected as COMP1 non inverting input. * To use PA4 as COMP1 non inverting input call COMP_SwitchCmd() after COMP_Init() * @param COMP_Selection: the selected comparator. * This parameter can be COMP_Selection_COMPx where x can be 1 to 7 * to select the COMP peripheral. * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains * the configuration information for the specified COMP peripheral. * - COMP_InvertingInput specifies the inverting input of COMP * - COMP_NonInvertingInput specifies the non inverting input of COMP * - COMP_Output connect COMP output to selected timer * input (Input capture / Output Compare Reference Clear / Break Input) * - COMP_BlankingSrce specifies the blanking source of COMP * - COMP_OutputPol select output polarity * - COMP_Hysteresis configures COMP hysteresis value * - COMP_Mode configures COMP power mode * @note COMP_Hysteresis must be configured only for STM32F303xC. Otherwise, COMP_Hysteresis * must be kept at reset value(COMP_Hysteresis_No). * @note COMP_Mode field is only applicable for STM32F303xC devices. * @retval None */ void COMP_Init(uint32_t COMP_Selection, COMP_InitTypeDef* COMP_InitStruct) { uint32_t tmpreg = 0; /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); assert_param(IS_COMP_INVERTING_INPUT(COMP_InitStruct->COMP_InvertingInput)); assert_param(IS_COMP_NONINVERTING_INPUT(COMP_InitStruct->COMP_NonInvertingInput)); assert_param(IS_COMP_OUTPUT(COMP_InitStruct->COMP_Output)); assert_param(IS_COMP_BLANKING_SOURCE(COMP_InitStruct->COMP_BlankingSrce)); assert_param(IS_COMP_OUTPUT_POL(COMP_InitStruct->COMP_OutputPol)); assert_param(IS_COMP_HYSTERESIS(COMP_InitStruct->COMP_Hysteresis)); assert_param(IS_COMP_MODE(COMP_InitStruct->COMP_Mode)); /*!< Get the COMPx_CSR register value */ tmpreg = *(__IO uint32_t *) (COMP_BASE + COMP_Selection); /*!< Clear the COMP1SW1, COMPxINSEL, COMPxOUTSEL, COMPxPOL, COMPxHYST and COMPxMODE bits */ tmpreg &= (uint32_t) (COMP_CSR_CLEAR_MASK); /*!< Configure COMP: inverting input, output redirection, hysteresis value and power mode */ /*!< Set COMPxINSEL bits according to COMP_InitStruct->COMP_InvertingInput value */ /*!< Set COMPxNONINSEL bits according to COMP_InitStruct->COMP_NonInvertingInput value */ /*!< Set COMPxBLANKING bits according to COMP_InitStruct->COMP_BlankingSrce value */ /*!< Set COMPxOUTSEL bits according to COMP_InitStruct->COMP_Output value */ /*!< Set COMPxPOL bit according to COMP_InitStruct->COMP_OutputPol value */ /*!< Set COMPxHYST bits according to COMP_InitStruct->COMP_Hysteresis value */ /*!< Set COMPxMODE bits according to COMP_InitStruct->COMP_Mode value */ tmpreg |= (uint32_t)(COMP_InitStruct->COMP_InvertingInput | COMP_InitStruct->COMP_NonInvertingInput | COMP_InitStruct->COMP_Output | COMP_InitStruct->COMP_OutputPol | COMP_InitStruct->COMP_BlankingSrce | COMP_InitStruct->COMP_Hysteresis | COMP_InitStruct->COMP_Mode); /*!< Write to COMPx_CSR register */ *(__IO uint32_t *) (COMP_BASE + COMP_Selection) = tmpreg; }
/** * @brief Lock the selected comparator (COMP1/COMP2) configuration. * @note Locking the configuration means that all control bits are read-only. * To unlock the comparator configuration, perform a system reset. * @param COMP_Selection: selects the comparator to be locked * This parameter can be a value of the following values: * @arg COMP_Selection_COMP1: COMP1 configuration is locked. * @arg COMP_Selection_COMP2: COMP2 configuration is locked. * @retval None */ void COMP_LockConfig(uint32_t COMP_Selection) { /* Check the parameter */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); /* Set the lock bit corresponding to selected comparator */ COMP->CSR |= (uint32_t) (COMP_CSR_COMP1LOCK<<COMP_Selection); }
/** * @brief Lock the selected comparator (COMP1/COMP2) configuration. * @note Locking the configuration means that all control bits are read-only. * To unlock the comparator configuration, perform a system reset. * @param COMP_Selection: the selected comparator. * This parameter can be COMP_Selection_COMPx where x can be 1 to 7 * to select the COMP peripheral. * @retval None */ void COMP_LockConfig(uint32_t COMP_Selection) { /* Check the parameter */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); /* Set the lock bit corresponding to selected comparator */ *(__IO uint32_t *) (COMP_BASE + COMP_Selection) |= (uint32_t) (COMP_CSR_COMPxLOCK); }
/** * @brief Checks whether the comparator interrupt has occurred or not. * @param COMP_Selection : selects the comparator * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: Selection of Comparator 1 * @arg COMP_Selection_COMP2: Selection of Comparator 2 * @retval ITStatus : The state of the COMPx event flag (SET or RESET). */ ITStatus COMP_GetITStatus(COMP_Selection_TypeDef COMP_Selection) { ITStatus bitstatus = RESET; uint8_t itstatus = 0x00, itenable = 0x00; /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); if (COMP_Selection == COMP_Selection_COMP1) { /* Get the EF1 comparator event flag status */ itstatus = (uint8_t) (COMP->CSR1 & COMP_CSR1_EF1); /* Get the IE1 interrupt enable bit status */ itenable = (uint8_t) (COMP->CSR1 & COMP_CSR1_IE1); if ((itstatus != (uint8_t) RESET) && (itenable != (uint8_t) RESET)) { /* the EF1 and IE1 are set */ bitstatus = SET; } else { /* the EF1 or IE1 is reset */ bitstatus = RESET; } } else { /* Get the EF2 comparator event flag value */ itstatus = (uint8_t) (COMP->CSR2 & COMP_CSR2_EF2); /* Get the IE2 interrupt enable bit value */ itenable = (uint8_t) (COMP->CSR2 & COMP_CSR2_IE2); if ((itstatus != (uint8_t)RESET) && (itenable != (uint8_t)RESET)) { /* The EF2 and IE2 are set */ bitstatus = SET; } else { /* The EF2 or IE2 is reset */ bitstatus = RESET; } } /* Return the COMP interrupt status */ return (ITStatus) bitstatus; }
/** * @brief Enable or disable the COMP peripheral. * @note If the selected comparator is locked, enable/disable can't be performed. * To unlock the configuration, perform a system reset. * @param COMP_Selection: the selected comparator. * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: COMP1 selected * @arg COMP_Selection_COMP2: COMP2 selected * @param NewState: new state of the COMP peripheral. * This parameter can be: ENABLE or DISABLE. * @note When enabled, the comparator compares the non inverting input with * the inverting input and the comparison result is available on comparator output. * @note When disabled, the comparator doesn't perform comparison and the * output level is low. * @retval None */ void COMP_Cmd(uint32_t COMP_Selection, FunctionalState NewState) { /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); assert_param(IS_FUNCTIONAL_STATE(NewState)); if (NewState != DISABLE) { /* Enable the selected COMP peripheral */ COMP->CSR |= (uint32_t) (1 << COMP_Selection); } else { /* Disable the selected COMP peripheral */ COMP->CSR &= (uint32_t) (~((uint32_t) 1 << COMP_Selection)); } }
/** * @brief Clears the interrupt pending bits of the comparator. * @param COMP_Selection : selects the comparator * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: Selection of Comparator 1 * @arg COMP_Selection_COMP2: Selection of Comparator 2 * @retval None */ void COMP_ClearITPendingBit(COMP_Selection_TypeDef COMP_Selection) { /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); if (COMP_Selection == COMP_Selection_COMP1) { /* Clear the flag EF1 (rc_w0) clear this bit by writing 0. */ COMP->CSR1 &= (uint8_t) (~COMP_CSR1_EF1); } else { /* Clear the flag EF2 (rc_w0) clear this bit by writing 0. */ COMP->CSR2 &= (uint8_t) (~COMP_CSR2_EF2); } }
/** * @brief Return the output level (high or low) of the selected comparator. * @note The output level depends on the selected polarity. * @note If the polarity is not inverted: * - Comparator output is low when the non-inverting input is at a lower * voltage than the inverting input * - Comparator output is high when the non-inverting input is at a higher * voltage than the inverting input * @note If the polarity is inverted: * - Comparator output is high when the non-inverting input is at a lower * voltage than the inverting input * - Comparator output is low when the non-inverting input is at a higher * voltage than the inverting input * @param COMP_Selection: the selected comparator. * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: COMP1 selected * @arg COMP_Selection_COMP2: COMP2 selected * @retval Returns the selected comparator output level: low or high. * */ uint32_t COMP_GetOutputLevel(uint32_t COMP_Selection) { uint32_t compout = 0x0; /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); /* Check if selected comparator output is high */ if ((COMP->CSR & (COMP_CSR_COMP1OUT << COMP_Selection)) != 0) { compout = COMP_OutputLevel_High; } else { compout = COMP_OutputLevel_Low; } /* Return the comparator output level */ return (uint32_t) (compout); }
/** * @brief Enable or disable the COMP peripheral. * @note If the selected comparator is locked, enable/disable can't be performed. * To unlock the configuration, perform a system reset. * @param COMP_Selection: the selected comparator. * This parameter can be COMP_Selection_COMPx where x can be 1 to 7 * to select the COMP peripheral. * @param NewState: new state of the COMP peripheral. * This parameter can be: ENABLE or DISABLE. * When enabled, the comparator compares the non inverting input with * the inverting input and the comparison result is available * on comparator output. * When disabled, the comparator doesn't perform comparison and the * output level is low. * @retval None */ void COMP_Cmd(uint32_t COMP_Selection, FunctionalState NewState) { /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); assert_param(IS_FUNCTIONAL_STATE(NewState)); if (NewState != DISABLE) { /* Enable the selected COMPx peripheral */ *(__IO uint32_t *) (COMP_BASE + COMP_Selection) |= (uint32_t) (COMP_CSR_COMPxEN); } else { /* Disable the selected COMP peripheral */ *(__IO uint32_t *) (COMP_BASE + COMP_Selection) &= (uint32_t)(~COMP_CSR_COMPxEN); } }
/** * @brief Returns the output level of the comparator. * @note Comparators outputs aren't available on GPIO (outputs levels are * only internal). * @param COMP_Selection: selects the comparator. * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: Selection of Comparator 1 * @arg COMP_Selection_COMP2: Selection of Comparator 2 * @retval Returns the comparator output level * This value can be one of the following: * - COMP_OutputLevel_Low: Comparator output level is low * - COMP_OutputLevel_High: Comparator output level is high */ COMP_OutputLevel_TypeDef COMP_GetOutputLevel(COMP_Selection_TypeDef COMP_Selection) { uint8_t compout; /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); /* Check if Comparator 1 is selected */ if (COMP_Selection == COMP_Selection_COMP1) { /* Check if comparator 1 output level is high */ if ((COMP->CSR1 & COMP_CSR1_CMP1OUT) != (uint8_t) RESET) { /* Get Comparator 1 output level */ compout = (COMP_OutputLevel_TypeDef) COMP_OutputLevel_High; } /* comparator 1 output level is low */ else { /* Get Comparator 1 output level */ compout = (COMP_OutputLevel_TypeDef) COMP_OutputLevel_Low; } } /* Comparator 2 is selected */ else { /* Check if comparator 2 output level is high */ if ((COMP->CSR2 & COMP_CSR2_CMP2OUT) != (uint8_t) RESET) { /* Get Comparator output level */ compout = (COMP_OutputLevel_TypeDef) COMP_OutputLevel_High; } /* comparator 2 output level is low */ else { /* Get Comparator 2 output level */ compout = (COMP_OutputLevel_TypeDef) COMP_OutputLevel_Low; } } /* Return the comparator output level */ return (COMP_OutputLevel_TypeDef)(compout); }
/** * @brief Checks whether the comparator flag is set or not. * @param COMP_Selection : selects the comparator * This parameter can be one of the following values: * @arg COMP_Selection_COMP1: Selection of Comparator 1 * @arg COMP_Selection_COMP2: Selection of Comparator 2 * @retval The new state of COMPx event flag (SET or RESET). */ FlagStatus COMP_GetFlagStatus(COMP_Selection_TypeDef COMP_Selection) { FlagStatus bitstatus = RESET; /* Check the parameters */ assert_param(IS_COMP_ALL_PERIPH(COMP_Selection)); /* Check if COMP1 is selected */ if (COMP_Selection == COMP_Selection_COMP1) { if ((COMP->CSR1 & COMP_CSR1_EF1) != (uint8_t) RESET) { /* The comparator 1 event flag is set */ bitstatus = SET; } else { /* The comparator 1 event flag is reset */ bitstatus = RESET; } } else /* COMP2 is selected */ { if ((COMP->CSR2 & COMP_CSR2_EF2) != (uint8_t) RESET) { /* The comparator 2 event flag is set */ bitstatus = SET; } else { /* The comparator 2 event flag is reset */ bitstatus = RESET; } } /* return the comparator event flag status */ return (FlagStatus)(bitstatus); }