예제 #1
0
/**
  * @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 wait_loop_cycles = 0;
  __IO uint32_t wait_loop_index = 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)
    {
      
      /* Note: For comparator 2, inverting input (parameter                   */
      /*       "hcomp->Init.InvertingInput") is configured into this          */
      /*       function instead of function "HAL_COMP_Init()" since           */
      /*       inverting input selection also enables the comparator 2.       */
      __HAL_COMP_ENABLE(hcomp);

      /* Set delay for COMP start-up time */
      if (hcomp->Instance == COMP1)
      {
        wait_loop_cycles = COMP1_START_DELAY_CPU_CYCLES;
      }
      else /* if (hcomp->Instance == COMP2) */
      {
        wait_loop_cycles = COMP2_START_DELAY_CPU_CYCLES;
      }

      /* Delay for COMP start-up time.                                         */
      /* Delay fixed to worst case: maximum CPU frequency                     */
      while(wait_loop_index < wait_loop_cycles)
      {
        wait_loop_index++;
      }

      /* Update COMP state */
      hcomp->State = HAL_COMP_STATE_BUSY;
      
    }
    else
    {
      status = HAL_ERROR;
    }
  }

  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;
}
예제 #3
0
/**
  * @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)
        {
          COMP_EXTI_RISING_ENABLE(extiline);
        }
        else
        {
          COMP_EXTI_RISING_DISABLE(extiline);
        }
        /* Configure the trigger falling edge */
        if((hcomp->Init.TriggerMode & COMP_TRIGGERMODE_IT_FALLING) != RESET)
        {
          COMP_EXTI_FALLING_ENABLE(extiline);
        }
        else
        {
          COMP_EXTI_FALLING_DISABLE(extiline);
        }

        /* Clear COMP EXTI pending bit if any */
        COMP_EXTI_CLEAR_FLAG(extiline);

        /* Enable EXTI interrupt mode */
        COMP_EXTI_ENABLE_IT(extiline);

        /* Enable the selected comparator */
        __HAL_COMP_ENABLE(hcomp);

        hcomp->State = HAL_COMP_STATE_BUSY;
      }
      else
      {
        status = HAL_ERROR;
      }
    }
    else
    {
      status = HAL_ERROR;
    }
  }

  return status;
}