/** * @brief Initializes the COMP2 peripheral according to the specified parameters * in the COMP_InitStruct: * - COMP_InvertingInput specify the inverting input of COMP2 * - COMP_OutputSelect connect the output of COMP2 to selected timer * input (Input capture / Output Compare Reference Clear) * - COMP_Speed configures COMP2 speed for optimum speed/consumption ratio * @note This function configures only COMP2. * @note COMP2 comparator is enabled as soon as the INSEL[2:0] bits are * different from "000". * @param COMP_InitStruct: pointer to an COMP_InitTypeDef structure that contains * the configuration information for the specified COMP peripheral. * @retval None */ void COMP_Init(COMP_InitTypeDef* COMP_InitStruct) { uint32_t tmpreg = 0; /* Check the parameters */ assert_param(IS_COMP_INVERTING_INPUT(COMP_InitStruct->COMP_InvertingInput)); assert_param(IS_COMP_OUTPUT(COMP_InitStruct->COMP_OutputSelect)); assert_param(IS_COMP_SPEED(COMP_InitStruct->COMP_Speed)); /*!< Get the COMP CSR value */ tmpreg = COMP->CSR; /*!< Clear the INSEL[2:0], OUTSEL[1:0] and SPEED bits */ tmpreg &= (uint32_t) (~(uint32_t) (COMP_CSR_OUTSEL | COMP_CSR_INSEL | COMP_CSR_SPEED)); /*!< Configure COMP: speed, inversion input selection and output redirection */ /*!< Set SPEED bit according to COMP_InitStruct->COMP_Speed value */ /*!< Set INSEL bits according to COMP_InitStruct->COMP_InvertingInput value */ /*!< Set OUTSEL bits according to COMP_InitStruct->COMP_OutputSelect value */ tmpreg |= (uint32_t)((COMP_InitStruct->COMP_Speed | COMP_InitStruct->COMP_InvertingInput | COMP_InitStruct->COMP_OutputSelect)); /*!< The COMP2 comparator is enabled as soon as the INSEL[2:0] bits value are different from "000" */ /*!< 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 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 Initialize the COMP peripheral according to the specified * parameters in the COMP_InitTypeDef and initialize the associated handle. * @note If the selected comparator is locked, initialization cannot be performed. * To unlock the configuration, perform a system reset. * @param hcomp COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp) { HAL_StatusTypeDef status = HAL_OK; /* Check the COMP handle allocation and lock status */ if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) { status = HAL_ERROR; } else { /* Check the parameters */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput)); assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput)); assert_param(IS_COMP_NONINVERTINGINPUT_INSTANCE(hcomp->Instance, hcomp->Init.NonInvertingInput)); assert_param(IS_COMP_OUTPUT(hcomp->Init.Output)); assert_param(IS_COMP_OUTPUT_INSTANCE(hcomp->Instance, hcomp->Init.Output)); assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol)); assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis)); assert_param(IS_COMP_MODE(hcomp->Init.Mode)); assert_param(IS_COMP_BLANKINGSRCE(hcomp->Init.BlankingSrce)); assert_param(IS_COMP_BLANKINGSRCE_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce)); assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode)); if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE) { assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance)); assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode)); } /* Init SYSCFG and the low level hardware to access comparators */ __HAL_RCC_SYSCFG_CLK_ENABLE(); /* Init the low level hardware : SYSCFG to access comparators */ HAL_COMP_MspInit(hcomp); if(hcomp->State == HAL_COMP_STATE_RESET) { /* Allocate lock resource and initialize it */ hcomp->Lock = HAL_UNLOCKED; } /* Set COMP parameters */ /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */ /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */ /* Set COMPxBLANKING bits according to hcomp->Init.BlankingSrce value */ /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */ /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */ /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */ /* Set COMPxMODE bits according to hcomp->Init.Mode value */ COMP_INIT(hcomp); /* Initialize the COMP state*/ hcomp->State = HAL_COMP_STATE_READY; } return status; }
/** * @brief Checks whether the specified COMP output is set or not. * @param COMP_Output Specifies the output to check. * This parameter can be any combination of the following values: * @arg COMP_Output_COMP1: COMP1 output * @arg COMP_Output_COMP2: COMP2 output * @retval The new state of Output. */ FlagStatus COMP_GetOutputStatus(COMP_Output_TypeDef COMP_Output) { FlagStatus status = RESET; /* Check the parameters */ assert_param(IS_COMP_OUTPUT(COMP_Output)); if ((COMP->CSR & (uint8_t)COMP_Output) != 0) { status = SET; } else { status = RESET; } return status; }
/** * @brief Initializes the comparator inverting input, output and speed. * @note This function configures only COMP2. * @param COMP_InvertingInput : selects the comparator inverting input. * This parameter can be one of the following values: * @arg COMP_InvertingInput_IO: Input/Output on comparator inverting input enable * @arg COMP_InvertingInput_VREFINT: VREFINT on comparator inverting input enable * @arg COMP_InvertingInput_3_4VREFINT: 3/4 VREFINT on comparator inverting input enable * @arg COMP_InvertingInput_1_2VREFINT: 1/2 VREFINT on comparator inverting input enable * @arg COMP_InvertingInput_1_4VREFINT: 1/4 VREFINT on comparator inverting input enable * @arg COMP_InvertingInput_DAC1: DAC1 output on comparator inverting input enable * @arg COMP_InvertingInput_DAC2: DAC2 output on comparator inverting input enable * @param COMP_OutputSelect : selects the comparator output * This parameter can be one of the following values: * @arg COMP_OutputSelect_TIM2IC2: COMP2 output connected to TIM2 Input Capture 2 * @arg COMP_OutputSelect_TIM3IC2: COMP2 output connected to TIM3 Input Capture 2 * @arg COMP_OutputSelect_TIM1BRK: COMP2 output connected to TIM1 Break Input * @arg COMP_OutputSelect_TIM1OCREFCLR: COMP2 output connected to TIM1 OCREF Clear * @param COMP_Speed selects the comparator speed * This parameter can be one of the following values: * @arg COMP_Speed_Slow: Comparator speed: slow * @arg COMP_Speed_Fast: Comparator speed: fast * @retval None. */ void COMP_Init(COMP_InvertingInput_Typedef COMP_InvertingInput, COMP_OutputSelect_Typedef COMP_OutputSelect, COMP_Speed_TypeDef COMP_Speed) { /* Check the parameters */ assert_param(IS_COMP_INVERTING_INPUT(COMP_InvertingInput)); assert_param(IS_COMP_OUTPUT(COMP_OutputSelect)); assert_param(IS_COMP_SPEED(COMP_Speed)); /* Reset the INSEL[2:0] bits in CSR3 register */ COMP->CSR3 &= (uint8_t) (~COMP_CSR3_INSEL); /* Select the comparator inverting input */ COMP->CSR3 |= (uint8_t) COMP_InvertingInput; /* Reset the OUTSEL[1:0] bits in CSR3 register */ COMP->CSR3 &= (uint8_t) (~COMP_CSR3_OUTSEL); /* Redirect the comparator output */ COMP->CSR3 |= (uint8_t) COMP_OutputSelect; /* Reset the comparator speed bit */ COMP->CSR2 &= (uint8_t) (~COMP_CSR2_SPEED); /* Select the comparator speed */ COMP->CSR2 |= (uint8_t) COMP_Speed; }
/** * @brief Initializes the COMP according to the specified * parameters in the COMP_InitTypeDef and create the associated handle. * @note If the selected comparator is locked, initialization can't be performed. * To unlock the configuration, perform a system reset. * @param hcomp: COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp) { HAL_StatusTypeDef status = HAL_OK; /* Check the COMP handle allocation and lock status */ if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) { status = HAL_ERROR; } else { /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); if (hcomp->Instance == COMP1) { assert_param(IS_COMP_NONINVERTINGINPUTPULL(hcomp->Init.NonInvertingInputPull)); } else /* if (hcomp->Instance == COMP2) */ { assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput)); assert_param(IS_COMP_OUTPUT(hcomp->Init.Output)); assert_param(IS_COMP_MODE(hcomp->Init.Mode)); assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode)); } /* In window mode, non-inverting inputs of the 2 comparators are */ /* connected together and are using inputs of COMP2 only. If COMP1 is */ /* selected, this parameter is discarded. */ if ((hcomp->Init.WindowMode == COMP_WINDOWMODE_DISABLE) || (hcomp->Instance == COMP2) ) { assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput)); } /* Enable SYSCFG clock and the low level hardware to access comparators */ if(hcomp->State == HAL_COMP_STATE_RESET) { /* Allocate lock resource and initialize it */ hcomp->Lock = HAL_UNLOCKED; /* Enable SYSCFG clock to control the routing Interface (RI) */ __HAL_RCC_SYSCFG_CLK_ENABLE(); /* Init the low level hardware */ HAL_COMP_MspInit(hcomp); } /* Configuration of comparator: */ /* - Output selection */ /* - Inverting input selection */ /* - Window mode */ /* - Mode fast/slow speed */ /* - Inverting input pull-up/down resistors */ /* Configuration depending on comparator instance */ if (hcomp->Instance == COMP1) { MODIFY_REG(COMP->CSR, COMP_CSR_400KPD | COMP_CSR_10KPD | COMP_CSR_400KPU | COMP_CSR_10KPU, hcomp->Init.NonInvertingInputPull ); } else /* if (hcomp->Instance == COMP2) */ { /* Note: If comparator 2 is not enabled, inverting input (parameter */ /* "hcomp->Init.InvertingInput") is configured into function */ /* "HAL_COMP_Start()" since inverting input selection also */ /* enables the comparator 2. */ /* If comparator 2 is already enabled, inverting input is */ /* reconfigured on the fly. */ if (__COMP_IS_ENABLED(hcomp) == RESET) { MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL | COMP_CSR_WNDWE | COMP_CSR_SPEED , hcomp->Init.Output | hcomp->Init.WindowMode | hcomp->Init.Mode ); } else { MODIFY_REG(COMP->CSR, COMP_CSR_OUTSEL | COMP_CSR_INSEL | COMP_CSR_WNDWE | COMP_CSR_SPEED , hcomp->Init.Output | hcomp->Init.InvertingInput | hcomp->Init.WindowMode | hcomp->Init.Mode ); } } /* Configure Routing Interface (RI) switches for comparator non-inverting */ /* input. */ /* Except in 2 cases: */ /* - if non-inverting input has no selection: it can be the case for */ /* COMP1 in window mode. */ /* - particular case for PC3: if switch COMP1_SW1 is closed */ /* (by macro "__HAL_OPAMP_OPAMP3OUT_CONNECT_ADC_COMP1()" or */ /* "__HAL_RI_SWITCH_COMP1_SW1_CLOSE()"), connection between pin PC3 */ /* (or OPAMP3, if available) and COMP1 is done directly, without going */ /* through ADC switch matrix. */ if (__COMP_ROUTING_INTERFACE_TOBECONFIGURED(hcomp)) { if (hcomp->Instance == COMP1) { /* Enable the switch control mode */ __HAL_RI_SWITCHCONTROLMODE_ENABLE(); /* Close the analog switch of ADC switch matrix to COMP1 (ADC */ /* channel 26: Vcomp) */ __HAL_RI_IOSWITCH_CLOSE(RI_IOSWITCH_VCOMP); } /* Close the I/O analog switch corresponding to comparator */ /* non-inverting input selected. */ __HAL_RI_IOSWITCH_CLOSE(hcomp->Init.NonInvertingInput); } /* Initialize the COMP state*/ if(hcomp->State == HAL_COMP_STATE_RESET) { hcomp->State = HAL_COMP_STATE_READY; } } return status; }
/** * @brief Initializes the COMP according to the specified * parameters in the COMP_InitTypeDef and create the associated handle. * @note If the selected comparator is locked, initialization can't be performed. * To unlock the configuration, perform a system reset. * @param hcomp: COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp) { HAL_StatusTypeDef status = HAL_OK; uint32_t regshift = COMP_CSR_COMP1_SHIFT; /* Check the COMP handle allocation and lock status */ if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) { status = HAL_ERROR; } else { /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); assert_param(IS_COMP_INVERTINGINPUT(hcomp->Init.InvertingInput)); assert_param(IS_COMP_NONINVERTINGINPUT(hcomp->Init.NonInvertingInput)); assert_param(IS_COMP_OUTPUT(hcomp->Init.Output)); assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol)); assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis)); assert_param(IS_COMP_MODE(hcomp->Init.Mode)); if(hcomp->Init.NonInvertingInput == COMP_NONINVERTINGINPUT_DAC1SWITCHCLOSED) { assert_param(IS_COMP_DAC1SWITCH_INSTANCE(hcomp->Instance)); } if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLED) { assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance)); } if(hcomp->State == HAL_COMP_STATE_RESET) { /* Init SYSCFG and the low level hardware to access comparators */ __SYSCFG_CLK_ENABLE(); HAL_COMP_MspInit(hcomp); } /* Set COMP parameters */ /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */ /* Set COMPxOUTSEL bits according to hcomp->Init.Output value */ /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */ /* Set COMPxHYST bits according to hcomp->Init.Hysteresis value */ /* Set COMPxMODE bits according to hcomp->Init.Mode value */ if(hcomp->Instance == COMP2) { regshift = COMP_CSR_COMP2_SHIFT; } MODIFY_REG(COMP->CSR, (uint32_t)(COMP_CSR_COMPxINSEL | COMP_CSR_COMPxNONINSEL_MASK | \ COMP_CSR_COMPxOUTSEL | COMP_CSR_COMPxPOL | \ COMP_CSR_COMPxHYST | COMP_CSR_COMPxMODE) << regshift, (hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ hcomp->Init.Output | \ hcomp->Init.OutputPol | \ hcomp->Init.Hysteresis | \ hcomp->Init.Mode) << regshift); if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLED) { COMP->CSR |= COMP_CSR_WNDWEN; } /* Initialize the COMP state*/ if(hcomp->State == HAL_COMP_STATE_RESET) { hcomp->State = HAL_COMP_STATE_READY; } } return status; }