/** * @brief Return the COMP state * @param hcomp : COMP handle * @retval HAL state */ uint32_t HAL_COMP_GetState(COMP_HandleTypeDef *hcomp) { /* Check the COMP handle allocation */ if(hcomp == NULL) { return HAL_COMP_STATE_RESET; } /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); return hcomp->State; }
/** * @brief De-initialize registers of the selected COMP instance * to their default reset values. * @note If comparator is locked, de-initialization by software is * not possible. * The only way to unlock the comparator is a device hardware reset. * @param COMPx COMP instance * @retval An ErrorStatus enumeration value: * - SUCCESS: COMP registers are de-initialized * - ERROR: COMP registers are not de-initialized */ ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx) { ErrorStatus status = SUCCESS; /* Check the parameters */ assert_param(IS_COMP_ALL_INSTANCE(COMPx)); /* Note: Hardware constraint (refer to description of this function): */ /* COMP instance must not be locked. */ if(LL_COMP_IsLocked(COMPx) == 0U) { /* Note: Connection switch is applicable only to COMP instance COMP1, */ /* therefore is COMP2 is selected the equivalent bit is */ /* kept unmodified. */ if(COMPx == COMP1) { CLEAR_BIT(COMP->CSR, ( COMP_CSR_COMP1MODE | COMP_CSR_COMP1INSEL | COMP_CSR_COMP1SW1 | COMP_CSR_COMP1OUTSEL | COMP_CSR_COMP1HYST | COMP_CSR_COMP1POL | COMP_CSR_COMP1EN ) << __COMP_BITOFFSET_INSTANCE(COMPx) ); } else { CLEAR_BIT(COMP->CSR, ( COMP_CSR_COMP1MODE | COMP_CSR_COMP1INSEL | COMP_CSR_COMP1OUTSEL | COMP_CSR_COMP1HYST | COMP_CSR_COMP1POL | COMP_CSR_COMP1EN ) << __COMP_BITOFFSET_INSTANCE(COMPx) ); } } else { /* Comparator instance is locked: de-initialization by software is */ /* not possible. */ /* The only way to unlock the comparator is a device hardware reset. */ status = ERROR; } return status; }
/** * @brief Initialize some features of COMP instance. * @note This function configures features of the selected COMP instance. * Some features are also available at scope COMP common instance * (common to several COMP instances). * Refer to functions having argument "COMPxy_COMMON" as parameter. * @param COMPx COMP instance * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure * @retval An ErrorStatus enumeration value: * - SUCCESS: COMP registers are initialized * - ERROR: COMP registers are not initialized */ ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct) { ErrorStatus status = SUCCESS; /* Check the parameters */ assert_param(IS_COMP_ALL_INSTANCE(COMPx)); assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode)); assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus)); assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus)); assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis)); assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity)); assert_param(IS_LL_COMP_OUTPUT_BLANKING_SOURCE(COMP_InitStruct->OutputBlankingSource)); /* Note: Hardware constraint (refer to description of this function) */ /* COMP instance must not be locked. */ if(LL_COMP_IsLocked(COMPx) == 0) { /* Configuration of comparator instance : */ /* - PowerMode */ /* - InputPlus */ /* - InputMinus */ /* - InputHysteresis */ /* - OutputPolarity */ /* - OutputBlankingSource */ MODIFY_REG(COMPx->CSR, COMP_CSR_PWRMODE | COMP_CSR_INPSEL | COMP_CSR_SCALEN | COMP_CSR_BRGEN | COMP_CSR_INMSEL | COMP_CSR_HYST | COMP_CSR_POLARITY | COMP_CSR_BLANKING , COMP_InitStruct->PowerMode | COMP_InitStruct->InputPlus | COMP_InitStruct->InputMinus | COMP_InitStruct->InputHysteresis | COMP_InitStruct->OutputPolarity | COMP_InitStruct->OutputBlankingSource ); } else { /* Initialization error: COMP instance is locked. */ status = ERROR; } return status; }
/** * @brief Return the output level (high or low) of the selected comparator. * The output level depends on the selected polarity. * 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 * 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 hcomp COMP handle * @retval Returns the selected comparator output level: * @arg @ref COMP_OUTPUTLEVEL_LOW * @arg @ref COMP_OUTPUTLEVEL_HIGH * */ uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp) { uint32_t level=0; /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); level = READ_BIT(hcomp->Instance->CSR, COMP_CSR_COMPxOUT); if(level != 0) { return(COMP_OUTPUTLEVEL_HIGH); } return(COMP_OUTPUTLEVEL_LOW); }
/** * @brief Return the output level (high or low) of the selected comparator. * The output level depends on the selected polarity. * - 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 * @param hcomp: COMP handle * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH. * */ uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp) { uint32_t level = 0; /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); /* Read output level of the selected comparator */ if(READ_BIT(COMP->CSR, __COMP_CSR_CMPXOUT(hcomp)) == RESET) { level = COMP_OUTPUTLEVEL_LOW; } else { level = COMP_OUTPUTLEVEL_HIGH; } return(level); }
/** * @brief Start the comparator * @param hcomp: COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp) { uint32_t wait_loop_index = 0; 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)); if(hcomp->State == HAL_COMP_STATE_READY) { /* Enable the selected comparator */ if(hcomp->Instance == COMP2) { regshift = COMP_CSR_COMP2_SHIFT; } SET_BIT(COMP->CSR, COMP_CSR_COMPxEN << regshift); /* Set HAL COMP handle state */ hcomp->State = HAL_COMP_STATE_BUSY; /* Delay for COMP startup time */ wait_loop_index = (LL_COMP_DELAY_STARTUP_US * (SystemCoreClock / 1000000)); while(wait_loop_index != 0) { wait_loop_index--; } } else { status = HAL_ERROR; } } return status; }
/** * @brief Return the output level (high or low) of the selected comparator. * The output level depends on the selected polarity. * 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 * 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 hcomp: COMP handle * @retval Returns the selected comparator output level: COMP_OUTPUTLEVEL_LOW or COMP_OUTPUTLEVEL_HIGH. * */ uint32_t HAL_COMP_GetOutputLevel(COMP_HandleTypeDef *hcomp) { uint32_t level=0; uint32_t regshift = COMP_CSR_COMP1_SHIFT; /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); if(hcomp->Instance == COMP2) { regshift = COMP_CSR_COMP2_SHIFT; } level = READ_BIT(COMP->CSR, (uint32_t)COMP_CSR_COMPxOUT << regshift); if(level != 0) { return(COMP_OUTPUTLEVEL_HIGH); } return(COMP_OUTPUTLEVEL_LOW); }
/** * @brief Lock the selected comparator configuration. * Caution: On STM32L1, HAL COMP lock is software lock only (not * hardware lock as on some other STM32 devices) * @param hcomp: COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef *hcomp) { HAL_StatusTypeDef status = HAL_OK; /* Check the COMP handle allocation and lock status */ if((hcomp == HAL_NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != RESET)) { status = HAL_ERROR; } else { /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); /* Set lock flag */ hcomp->State |= COMP_STATE_BIT_LOCK; } return status; }
/** * @brief Start the comparator. * @param hcomp COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp) { __IO uint32_t wait_loop_index = 0U; HAL_StatusTypeDef status = HAL_OK; /* Check the COMP handle allocation and lock status */ if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) { status = HAL_ERROR; } else { /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); if(hcomp->State == HAL_COMP_STATE_READY) { /* Enable the selected comparator */ SET_BIT(hcomp->Instance->CSR, COMP_CSR_COMPxEN); /* Set HAL COMP handle state */ hcomp->State = HAL_COMP_STATE_BUSY; /* Delay for COMP startup time */ /* Wait loop initialization and execution */ /* Note: Variable divided by 2 to compensate partially */ /* CPU processing cycles. */ wait_loop_index = (COMP_DELAY_STARTUP_US * (SystemCoreClock / (1000000U * 2U))); while(wait_loop_index != 0U) { wait_loop_index--; } } else { status = HAL_ERROR; } } return status; }
/** * @brief Lock the selected comparator configuration. * @note A system reset is required to unlock the comparator configuration. * @param hcomp COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Lock(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)); /* Set lock flag on state */ switch(hcomp->State) { case HAL_COMP_STATE_BUSY: hcomp->State = HAL_COMP_STATE_BUSY_LOCKED; break; case HAL_COMP_STATE_READY: hcomp->State = HAL_COMP_STATE_READY_LOCKED; break; default: /* unexpected state */ status = HAL_ERROR; break; } } if(status == HAL_OK) { /* Set the lock bit corresponding to selected comparator */ __HAL_COMP_LOCK(hcomp); } return status; }
/** * @brief De-initialize registers of the selected COMP instance * to their default reset values. * @note If comparator is locked, de-initialization by software is * not possible. * The only way to unlock the comparator is a device hardware reset. * @param COMPx COMP instance * @retval An ErrorStatus enumeration value: * - SUCCESS: COMP registers are de-initialized * - ERROR: COMP registers are not de-initialized */ ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx) { ErrorStatus status = SUCCESS; /* Check the parameters */ assert_param(IS_COMP_ALL_INSTANCE(COMPx)); /* Note: Hardware constraint (refer to description of this function): */ /* COMP instance must not be locked. */ if(LL_COMP_IsLocked(COMPx) == 0U) { LL_COMP_WriteReg(COMPx, CSR, 0x00000000U); } else { /* Comparator instance is locked: de-initialization by software is */ /* not possible. */ /* The only way to unlock the comparator is a device hardware reset. */ status = ERROR; } return status; }
/** * @brief Start the comparator in Interrupt mode. * @param hcomp COMP handle * @retval HAL status. */ HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp) { HAL_StatusTypeDef status = HAL_OK; uint32_t extiline = 0; /* 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->State == HAL_COMP_STATE_READY) { /* Configure the EXTI event generation */ if((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_IT_RISING|COMP_TRIGGERMODE_IT_FALLING)) != RESET) { /* Get the EXTI Line output configuration */ extiline = COMP_GET_EXTI_LINE(hcomp->Instance); /* Configure the trigger rising edge */ if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != RESET) { SET_BIT(EXTI->RTSR1, extiline); } else { CLEAR_BIT(EXTI->RTSR1, extiline); } /* Configure the trigger falling edge */ if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET) { SET_BIT(EXTI->FTSR1, extiline); } else { CLEAR_BIT(EXTI->FTSR1, extiline); } /* Clear COMP EXTI pending bit if any */ WRITE_REG(EXTI->PR1, extiline); /* Enable EXTI interrupt mode */ SET_BIT(EXTI->IMR1, extiline); /* Enable the selected comparator */ SET_BIT(hcomp->Instance->CSR, COMP_CSR_EN); hcomp->State = HAL_COMP_STATE_BUSY; } else { status = HAL_ERROR; } } else { status = HAL_ERROR; } } return status; }
/** * @brief Initialize the COMP according to the specified * parameters in the COMP_InitTypeDef and initialize 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) { uint32_t tmpcsr = 0; 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_OUTPUTPOL(hcomp->Init.OutputPol)); assert_param(IS_COMP_MODE(hcomp->Init.Mode)); assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis)); 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; } /* Change COMP peripheral state */ hcomp->State = HAL_COMP_STATE_BUSY; /* Set COMP parameters */ /* Set INMSEL bits according to hcomp->Init.InvertingInput value */ /* Set INPSEL bits according to hcomp->Init.NonInvertingInput value */ /* Set BLANKING bits according to hcomp->Init.BlankingSrce value */ /* Set HYST bits according to hcomp->Init.Hysteresis value */ /* Set POLARITY bit according to hcomp->Init.OutputPol value */ /* Set POWERMODE bits according to hcomp->Init.Mode value */ /* Set WINMODE bit according to hcomp->Init.WindowMode value */ tmpcsr = hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ hcomp->Init.BlankingSrce | \ hcomp->Init.Hysteresis | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode; /* Check VREFINT use for NonInvertingInput */ if(hcomp->Init.InvertingInput == COMP_INVERTINGINPUT_VREFINT) { /* Enable voltage scaler to output VREFINT */ tmpcsr |= COMP_CSR_SCALEN; } else { if((hcomp->Init.InvertingInput == COMP_INVERTINGINPUT_1_4VREFINT) || (hcomp->Init.InvertingInput == COMP_INVERTINGINPUT_1_2VREFINT) || (hcomp->Init.InvertingInput == COMP_INVERTINGINPUT_3_4VREFINT)) { /* Enable voltage & bandgap scaler to output VREFINT */ tmpcsr |= (COMP_CSR_BRGEN | COMP_CSR_SCALEN); } } MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, tmpcsr); /* Initialize the COMP state*/ hcomp->State = HAL_COMP_STATE_READY; } return status; }
/** * @brief Initialize the COMP according to the specified * parameters in the COMP_InitTypeDef and initialize the associated handle. * @note If the selected comparator is locked, initialization can't be performed. * To unlock the configuration, perform a system reset. * @note When the LPTIM connection is enabled, the following pins LPTIM_IN1(PB5, PC0) and LPTIM_IN2(PB7, PC2) should not be configured in alternate function. * @param hcomp COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef *hcomp) { uint32_t tmp_csr = 0U; uint32_t exti_line = 0U; uint32_t comp_voltage_scaler_not_initialized = 0U; __IO uint32_t wait_loop_index = 0U; HAL_StatusTypeDef status = HAL_OK; /* Check the COMP handle allocation and lock status */ if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) { status = HAL_ERROR; } else { /* Check the parameters */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput)); assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput)); assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol)); assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode)); assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode)); assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode)); if(hcomp->State == HAL_COMP_STATE_RESET) { /* Allocate lock resource and initialize it */ hcomp->Lock = HAL_UNLOCKED; /* Init SYSCFG and the low level hardware to access comparators */ /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */ /* to enable internal control clock of the comparators. */ /* However, this is a legacy strategy. In future STM32 families, */ /* COMP clock enable must be implemented by user */ /* in "HAL_COMP_MspInit()". */ /* Therefore, for compatibility anticipation, it is recommended */ /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */ /* in "HAL_COMP_MspInit()". */ __HAL_RCC_SYSCFG_CLK_ENABLE(); /* Init the low level hardware */ HAL_COMP_MspInit(hcomp); } /* Set COMP parameters */ tmp_csr = (hcomp->Init.InvertingInput | hcomp->Init.OutputPol ); /* Configuration specific to comparator instance: COMP2 */ if ((hcomp->Instance) == COMP2) { /* Comparator input plus configuration is available on COMP2 only */ /* Comparator power mode configuration is available on COMP2 only */ tmp_csr |= (hcomp->Init.NonInvertingInput | hcomp->Init.Mode ); /* COMP2 specificity: when using VrefInt or subdivision of VrefInt, */ /* specific path must be enabled. */ if((hcomp->Init.InvertingInput == COMP_INPUT_MINUS_VREFINT) || (hcomp->Init.InvertingInput == COMP_INPUT_MINUS_1_4VREFINT) || (hcomp->Init.InvertingInput == COMP_INPUT_MINUS_1_2VREFINT) || (hcomp->Init.InvertingInput == COMP_INPUT_MINUS_3_4VREFINT) ) { /* Memorize voltage scaler state before initialization */ comp_voltage_scaler_not_initialized = (READ_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP) == 0U); SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUFLP_VREFINT_COMP ); /* Delay for COMP scaler bridge voltage stabilization */ /* Apply the delay if voltage scaler bridge is enabled for the first time */ if (comp_voltage_scaler_not_initialized != 0U) { /* Wait loop initialization and execution */ /* Note: Variable divided by 2 to compensate partially */ /* CPU processing cycles. */ wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000U * 2U))); while(wait_loop_index != 0U) { wait_loop_index--; } } } } /* Set comparator output connection to LPTIM */ if (hcomp->Init.LPTIMConnection != COMP_LPTIMCONNECTION_DISABLED) { /* LPTIM connexion requested on COMP1 */ if ((hcomp->Instance) == COMP1) { /* Note : COMP1 can be connected to the input 1 of LPTIM if requested */ assert_param(IS_COMP1_LPTIMCONNECTION(hcomp->Init.LPTIMConnection)); if (hcomp->Init.LPTIMConnection == COMP_LPTIMCONNECTION_IN1_ENABLED) { tmp_csr |= (COMP_CSR_COMP1LPTIM1IN1); } } else { /* Check the MCU_ID in order to allow or not the COMP2 connection to LPTIM-input2 */ if (((HAL_GetDEVID() == C_DEV_ID_L073) && (HAL_GetREVID() == C_REV_ID_A)) || ((HAL_GetDEVID() == C_DEV_ID_L053) && (HAL_GetREVID() == C_REV_ID_A)) || ((HAL_GetDEVID() == C_DEV_ID_L053) && (HAL_GetREVID() == C_REV_ID_Z))) { /* Note : COMP2 can be connected only to input 1 of LPTIM if requested */ assert_param(IS_COMP2_LPTIMCONNECTION_RESTRICTED(hcomp->Init.LPTIMConnection)); tmp_csr |= (COMP_CSR_COMP2LPTIM1IN1); } /* LPTIM connexion requested on COMP2 */ else { /* Note : COMP2 can be connected to input 1 or input2 of LPTIM if requested */ assert_param(IS_COMP2_LPTIMCONNECTION(hcomp->Init.LPTIMConnection)); switch (hcomp->Init.LPTIMConnection) { case COMP_LPTIMCONNECTION_IN1_ENABLED : tmp_csr |= (COMP_CSR_COMP2LPTIM1IN1); break; case COMP_LPTIMCONNECTION_IN2_ENABLED : tmp_csr |= (COMP_CSR_COMP2LPTIM1IN2); break; default : break; } } } } /* Update comparator register */ if ((hcomp->Instance) == COMP1) { MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_COMP1INNSEL | COMP_CSR_COMP1WM | COMP_CSR_COMP1LPTIM1IN1 | COMP_CSR_COMP1POLARITY , tmp_csr ); } else /* Instance == COMP2 */ { MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_COMP2SPEED | COMP_CSR_COMP2INNSEL | COMP_CSR_COMP2INPSEL | COMP_CSR_COMP2POLARITY | COMP_CSR_COMP2LPTIM1IN2 | COMP_CSR_COMP2LPTIM1IN1 , tmp_csr ); } /* Set window mode */ /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */ /* instances. Therefore, this function can update another COMP */ /* instance that the one currently selected. */ if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON) { SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); } else { CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); } /* Get the EXTI line corresponding to the selected COMP instance */ exti_line = COMP_GET_EXTI_LINE(hcomp->Instance); /* Manage EXTI settings */ if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != RESET) { /* Configure EXTI rising edge */ if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != RESET) { SET_BIT(EXTI->RTSR, exti_line); } else { CLEAR_BIT(EXTI->RTSR, exti_line); } /* Configure EXTI falling edge */ if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != RESET) { SET_BIT(EXTI->FTSR, exti_line); } else { CLEAR_BIT(EXTI->FTSR, exti_line); } /* Clear COMP EXTI pending bit (if any) */ WRITE_REG(EXTI->PR, exti_line); /* Configure EXTI event mode */ if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != RESET) { SET_BIT(EXTI->EMR, exti_line); } else { CLEAR_BIT(EXTI->EMR, exti_line); } /* Configure EXTI interrupt mode */ if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != RESET) { SET_BIT(EXTI->IMR, exti_line); } else { CLEAR_BIT(EXTI->IMR, exti_line); } } else { /* Disable EXTI event generation */ CLEAR_BIT(EXTI->EMR, exti_line); } /* Set HAL COMP handle state */ /* Note: Transition from state reset to state ready, */ /* otherwise (coming from state ready or busy) no state update. */ if (hcomp->State == HAL_COMP_STATE_RESET) { hcomp->State = HAL_COMP_STATE_READY; } } return status; }
/** * @brief Enables the interrupt and starts the comparator * @param hcomp: COMP handle * @retval HAL status. */ HAL_StatusTypeDef HAL_COMP_Start_IT(COMP_HandleTypeDef *hcomp) { HAL_StatusTypeDef status = HAL_OK; uint32_t extiline = 0; /* Check the COMP handle allocation and lock status */ if((hcomp == NULL) || ((hcomp->State & COMP_STATE_BIT_LOCK) != 0x00)) { status = HAL_ERROR; } else { /* Check the parameter */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); if(hcomp->State == HAL_COMP_STATE_READY) { /* Check the Exti Line output configuration */ extiline = COMP_GET_EXTI_LINE(hcomp->Instance); /* Configure the rising edge */ if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_RISING) != 0x0) { if (extiline == COMP_EXTI_LINE_COMP1) { __HAL_COMP_COMP1_EXTI_ENABLE_RISING_EDGE(); } else { __HAL_COMP_COMP2_EXTI_ENABLE_RISING_EDGE(); } } else { if (extiline == COMP_EXTI_LINE_COMP1) { __HAL_COMP_COMP1_EXTI_DISABLE_RISING_EDGE(); } else { __HAL_COMP_COMP2_EXTI_DISABLE_RISING_EDGE(); } } /* Configure the falling edge */ if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != 0x0) { if (extiline == COMP_EXTI_LINE_COMP1) { __HAL_COMP_COMP1_EXTI_ENABLE_FALLING_EDGE(); } else { __HAL_COMP_COMP2_EXTI_ENABLE_FALLING_EDGE(); } } else { if (extiline == COMP_EXTI_LINE_COMP1) { __HAL_COMP_COMP1_EXTI_DISABLE_FALLING_EDGE(); } else { __HAL_COMP_COMP2_EXTI_DISABLE_FALLING_EDGE(); } } /* Configure the COMP module */ if (extiline == COMP_EXTI_LINE_COMP1) { /* Clear COMP Exti pending bit */ __HAL_COMP_COMP1_EXTI_CLEAR_FLAG(); /* Enable Exti interrupt mode */ __HAL_COMP_COMP1_EXTI_ENABLE_IT(); } else { /* Clear COMP Exti pending bit */ __HAL_COMP_COMP2_EXTI_CLEAR_FLAG(); /* Enable Exti interrupt mode */ __HAL_COMP_COMP2_EXTI_ENABLE_IT(); } /* Enable the selected comparator */ __HAL_COMP_ENABLE(hcomp); hcomp->State = HAL_COMP_STATE_BUSY; } else { status = HAL_ERROR; } } 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. * @note When the LPTIM connection is enabled, the following pins LPTIM_IN1(PB5, PC0) and LPTIM_IN2(PB7, PC2) should not be configured in AF. * @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) != 0x00)) { 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_OUTPUTPOL(hcomp->Init.OutputPol)); assert_param(IS_COMP_MODE(hcomp->Init.Mode)); if(hcomp->Init.WindowMode != COMP_WINDOWMODE_DISABLE) { assert_param(IS_COMP_WINDOWMODE_INSTANCE(hcomp->Instance)); assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode)); } if(hcomp->State == HAL_COMP_STATE_RESET) { /* Allocate lock resource and initialize it */ hcomp->Lock = HAL_UNLOCKED; /* 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); } /* Change COMP peripheral state */ hcomp->State = HAL_COMP_STATE_BUSY; /* Set COMP parameters */ /* Set COMPxINSEL bits according to hcomp->Init.InvertingInput value */ /* Set COMPxNONINSEL bits according to hcomp->Init.NonInvertingInput value */ /* Set COMPxLPTIMCONNECTION bits according to hcomp->Init.LPTIMConnection value */ /* Set COMPxPOL bit according to hcomp->Init.OutputPol value */ /* Set COMPxMODE bits according to hcomp->Init.Mode value */ /* Set COMP1WM bit according to hcomp->Init.WindowMode value */ /* No LPTIM connexion requested */ if (hcomp->Init.LPTIMConnection == COMP_LPTIMCONNECTION_DISABLED) { MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \ hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode); } else { /* LPTIM connexion requested on COMP2*/ if ((hcomp->Instance) == COMP2) { /* Check the MCU_ID in order to allow or not the COMP2 connection to LPTIM-input2 */ if (((HAL_GetDEVID() == C_DEV_ID_L073) && (HAL_GetREVID() == C_REV_ID_A)) || ((HAL_GetDEVID() == C_DEV_ID_L053) && (HAL_GetREVID() == C_REV_ID_A)) || ((HAL_GetDEVID() == C_DEV_ID_L053) && (HAL_GetREVID() == C_REV_ID_Z))) { /* Note : COMP2 can be connected only to input 1 of LPTIM if requested */ assert_param(IS_COMP2_LPTIMCONNECTION_RESTRICTED(hcomp->Init.LPTIMConnection)); MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \ hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ COMP_CSR_COMP2LPTIM1IN1 | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode); } else { /* Note : COMP2 can be connected to input 1 or input2 of LPTIM if requested */ assert_param(IS_COMP2_LPTIMCONNECTION(hcomp->Init.LPTIMConnection)); switch (hcomp->Init.LPTIMConnection) { case COMP_LPTIMCONNECTION_IN1_ENABLED : MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \ hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ COMP_CSR_COMP2LPTIM1IN1 | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode); break; case COMP_LPTIMCONNECTION_IN2_ENABLED : MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \ hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ COMP_CSR_COMP2LPTIM1IN2 | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode); break; default : MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \ hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode); break; } } } else /* LPTIM connexion requested on COMP1 */ { /* Note : COMP1 can be connected to the input 1 of LPTIM if requested */ assert_param(IS_COMP1_LPTIMCONNECTION(hcomp->Init.LPTIMConnection)); if (hcomp->Init.LPTIMConnection == COMP_LPTIMCONNECTION_IN1_ENABLED) MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \ hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ COMP_CSR_COMP1LPTIM1IN1 | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode); else MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_UPDATE_PARAMETERS_MASK, \ hcomp->Init.InvertingInput | \ hcomp->Init.NonInvertingInput | \ hcomp->Init.OutputPol | \ hcomp->Init.Mode | \ hcomp->Init.WindowMode); } } /* Initialize the COMP state*/ hcomp->State = HAL_COMP_STATE_READY; } return status; }
/** * @brief Initialize some features of COMP instance. * @note This function configures features of the selected COMP instance. * Some features are also available at scope COMP common instance * (common to several COMP instances). * Refer to functions having argument "COMPxy_COMMON" as parameter. * @param COMPx COMP instance * @param COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure * @retval An ErrorStatus enumeration value: * - SUCCESS: COMP registers are initialized * - ERROR: COMP registers are not initialized */ ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct) { ErrorStatus status = SUCCESS; /* Check the parameters */ assert_param(IS_COMP_ALL_INSTANCE(COMPx)); assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode)); assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus)); assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus)); assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis)); assert_param(IS_LL_COMP_OUTPUT_SELECTION(COMP_InitStruct->OutputSelection)); assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity)); /* Note: Hardware constraint (refer to description of this function) */ /* COMP instance must not be locked. */ if(LL_COMP_IsLocked(COMPx) == 0U) { /* Configuration of comparator instance : */ /* - PowerMode */ /* - InputPlus */ /* - InputMinus */ /* - InputHysteresis */ /* - OutputSelection */ /* - OutputPolarity */ /* Note: Connection switch is applicable only to COMP instance COMP1, */ /* therefore is COMP2 is selected the equivalent bit is */ /* kept unmodified. */ if(COMPx == COMP1) { MODIFY_REG(COMP->CSR, ( COMP_CSR_COMP1MODE | COMP_CSR_COMP1INSEL | COMP_CSR_COMP1SW1 | COMP_CSR_COMP1OUTSEL | COMP_CSR_COMP1HYST | COMP_CSR_COMP1POL ) << __COMP_BITOFFSET_INSTANCE(COMPx) , ( COMP_InitStruct->PowerMode | COMP_InitStruct->InputPlus | COMP_InitStruct->InputMinus | COMP_InitStruct->InputHysteresis | COMP_InitStruct->OutputSelection | COMP_InitStruct->OutputPolarity ) << __COMP_BITOFFSET_INSTANCE(COMPx) ); } else { MODIFY_REG(COMP->CSR, ( COMP_CSR_COMP1MODE | COMP_CSR_COMP1INSEL | COMP_CSR_COMP1OUTSEL | COMP_CSR_COMP1HYST | COMP_CSR_COMP1POL ) << __COMP_BITOFFSET_INSTANCE(COMPx) , ( COMP_InitStruct->PowerMode | COMP_InitStruct->InputPlus | COMP_InitStruct->InputMinus | COMP_InitStruct->InputHysteresis | COMP_InitStruct->OutputSelection | COMP_InitStruct->OutputPolarity ) << __COMP_BITOFFSET_INSTANCE(COMPx) ); } } else { /* Initialization error: COMP instance is locked. */ status = ERROR; } return status; }
/** * @brief Start the comparator. * @param hcomp COMP handle * @retval HAL status */ HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp) { HAL_StatusTypeDef status = HAL_OK; uint32_t extiline = 0; /* 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->State == HAL_COMP_STATE_READY) { /* Get the EXTI Line output configuration */ extiline = COMP_GET_EXTI_LINE(hcomp->Instance); /* Configure the event generation */ if((hcomp->Init.TriggerMode & (COMP_TRIGGERMODE_EVENT_RISING|COMP_TRIGGERMODE_EVENT_FALLING)) != RESET) { /* Configure the event trigger rising edge */ if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_RISING) != RESET) { COMP_EXTI_RISING_ENABLE(extiline); } else { COMP_EXTI_RISING_DISABLE(extiline); } /* Configure the trigger falling edge */ if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_EVENT_FALLING) != RESET) { COMP_EXTI_FALLING_ENABLE(extiline); } else { COMP_EXTI_FALLING_DISABLE(extiline); } /* Enable EXTI event generation */ COMP_EXTI_ENABLE_EVENT(extiline); /* Clear COMP EXTI pending bit */ COMP_EXTI_CLEAR_FLAG(extiline); } /* Enable the selected comparator */ __HAL_COMP_ENABLE(hcomp); hcomp->State = HAL_COMP_STATE_BUSY; } else { status = HAL_ERROR; } } return status; }
/** * @brief Initialize the COMP according to the specified * parameters in the COMP_InitTypeDef and initialize 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) { uint32_t tmp_csr = 0U; uint32_t exti_line = 0U; uint32_t comp_voltage_scaler_not_initialized = 0U; __IO uint32_t wait_loop_index = 0U; HAL_StatusTypeDef status = HAL_OK; /* Check the COMP handle allocation and lock status */ if((hcomp == NULL) || (__HAL_COMP_IS_LOCKED(hcomp))) { status = HAL_ERROR; } else { /* Check the parameters */ assert_param(IS_COMP_ALL_INSTANCE(hcomp->Instance)); assert_param(IS_COMP_INPUT_PLUS(hcomp->Instance, hcomp->Init.NonInvertingInput)); assert_param(IS_COMP_INPUT_MINUS(hcomp->Instance, hcomp->Init.InvertingInput)); assert_param(IS_COMP_OUTPUTPOL(hcomp->Init.OutputPol)); assert_param(IS_COMP_POWERMODE(hcomp->Init.Mode)); assert_param(IS_COMP_HYSTERESIS(hcomp->Init.Hysteresis)); assert_param(IS_COMP_BLANKINGSRC_INSTANCE(hcomp->Instance, hcomp->Init.BlankingSrce)); assert_param(IS_COMP_TRIGGERMODE(hcomp->Init.TriggerMode)); assert_param(IS_COMP_WINDOWMODE(hcomp->Init.WindowMode)); if(hcomp->State == HAL_COMP_STATE_RESET) { /* Allocate lock resource and initialize it */ hcomp->Lock = HAL_UNLOCKED; /* Init SYSCFG and the low level hardware to access comparators */ /* Note: HAL_COMP_Init() calls __HAL_RCC_SYSCFG_CLK_ENABLE() */ /* to enable internal control clock of the comparators. */ /* However, this is a legacy strategy. In future STM32 families, */ /* COMP clock enable must be implemented by user */ /* in "HAL_COMP_MspInit()". */ /* Therefore, for compatibility anticipation, it is recommended */ /* to implement __HAL_RCC_SYSCFG_CLK_ENABLE() */ /* in "HAL_COMP_MspInit()". */ __HAL_RCC_SYSCFG_CLK_ENABLE(); /* Init the low level hardware */ HAL_COMP_MspInit(hcomp); } /* Memorize voltage scaler state before initialization */ comp_voltage_scaler_not_initialized = (READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) == 0); /* Set COMP parameters */ tmp_csr = ( hcomp->Init.NonInvertingInput | hcomp->Init.InvertingInput | hcomp->Init.BlankingSrce | hcomp->Init.Hysteresis | hcomp->Init.OutputPol | hcomp->Init.Mode ); /* Set parameters in COMP register */ /* Note: Update all bits except read-only, lock and enable bits */ #if defined (COMP_CSR_INMESEL) MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL | COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST | COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN | COMP_CSR_INMESEL, tmp_csr ); #else MODIFY_REG(hcomp->Instance->CSR, COMP_CSR_PWRMODE | COMP_CSR_INMSEL | COMP_CSR_INPSEL | COMP_CSR_WINMODE | COMP_CSR_POLARITY | COMP_CSR_HYST | COMP_CSR_BLANKING | COMP_CSR_BRGEN | COMP_CSR_SCALEN, tmp_csr ); #endif /* Set window mode */ /* Note: Window mode bit is located into 1 out of the 2 pairs of COMP */ /* instances. Therefore, this function can update another COMP */ /* instance that the one currently selected. */ if(hcomp->Init.WindowMode == COMP_WINDOWMODE_COMP1_INPUT_PLUS_COMMON) { SET_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); } else { CLEAR_BIT(COMP12_COMMON->CSR, COMP_CSR_WINMODE); } /* Delay for COMP scaler bridge voltage stabilization */ /* Apply the delay if voltage scaler bridge is enabled for the first time */ if ((READ_BIT(hcomp->Instance->CSR, COMP_CSR_SCALEN) != 0U) && (comp_voltage_scaler_not_initialized != 0U) ) { /* Wait loop initialization and execution */ /* Note: Variable divided by 2 to compensate partially */ /* CPU processing cycles. */ wait_loop_index = (COMP_DELAY_VOLTAGE_SCALER_STAB_US * (SystemCoreClock / (1000000 * 2U))); while(wait_loop_index != 0U) { wait_loop_index--; } } /* Get the EXTI line corresponding to the selected COMP instance */ exti_line = COMP_GET_EXTI_LINE(hcomp->Instance); /* Manage EXTI settings */ if((hcomp->Init.TriggerMode & (COMP_EXTI_IT | COMP_EXTI_EVENT)) != RESET) { /* Configure EXTI rising edge */ if((hcomp->Init.TriggerMode & COMP_EXTI_RISING) != RESET) { LL_EXTI_EnableRisingTrig_0_31(exti_line); } else { LL_EXTI_DisableRisingTrig_0_31(exti_line); } /* Configure EXTI falling edge */ if((hcomp->Init.TriggerMode & COMP_EXTI_FALLING) != RESET) { LL_EXTI_EnableFallingTrig_0_31(exti_line); } else { LL_EXTI_DisableFallingTrig_0_31(exti_line); } /* Clear COMP EXTI pending bit (if any) */ LL_EXTI_ClearFlag_0_31(exti_line); /* Configure EXTI event mode */ if((hcomp->Init.TriggerMode & COMP_EXTI_EVENT) != RESET) { LL_EXTI_EnableEvent_0_31(exti_line); } else { LL_EXTI_DisableEvent_0_31(exti_line); } /* Configure EXTI interrupt mode */ if((hcomp->Init.TriggerMode & COMP_EXTI_IT) != RESET) { LL_EXTI_EnableIT_0_31(exti_line); } else { LL_EXTI_DisableIT_0_31(exti_line); } } else { /* Disable EXTI event mode */ LL_EXTI_DisableEvent_0_31(exti_line); /* Disable EXTI interrupt mode */ LL_EXTI_DisableIT_0_31(exti_line); } /* Set HAL COMP handle state */ /* Note: Transition from state reset to state ready, */ /* otherwise (coming from state ready or busy) no state update. */ 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; /* 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; }