Subversion Repositories FuelGauge

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_ll_comp.c
  4.   * @author  MCD Application Team
  5.   * @brief   COMP LL module driver
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19.  
  20. #if defined(USE_FULL_LL_DRIVER)
  21.  
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f0xx_ll_comp.h"
  24.  
  25. #ifdef  USE_FULL_ASSERT
  26.   #include "stm32_assert.h"
  27. #else
  28.   #define assert_param(expr) ((void)0U)
  29. #endif
  30.  
  31. /** @addtogroup STM32F0xx_LL_Driver
  32.   * @{
  33.   */
  34.  
  35. #if defined (COMP1) || defined (COMP2)
  36.  
  37. /** @addtogroup COMP_LL COMP
  38.   * @{
  39.   */
  40.  
  41. /* Private types -------------------------------------------------------------*/
  42. /* Private variables ---------------------------------------------------------*/
  43. /* Private constants ---------------------------------------------------------*/
  44. /* Private macros ------------------------------------------------------------*/
  45.  
  46. /** @addtogroup COMP_LL_Private_Macros
  47.   * @{
  48.   */
  49.  
  50. /* Check of parameters for configuration of COMP hierarchical scope:          */
  51. /* COMP instance.                                                             */
  52.  
  53. #define IS_LL_COMP_POWER_MODE(__POWER_MODE__)                                  \
  54.   (   ((__POWER_MODE__) == LL_COMP_POWERMODE_HIGHSPEED)                        \
  55.    || ((__POWER_MODE__) == LL_COMP_POWERMODE_MEDIUMSPEED)                      \
  56.    || ((__POWER_MODE__) == LL_COMP_POWERMODE_LOWPOWER)                         \
  57.    || ((__POWER_MODE__) == LL_COMP_POWERMODE_ULTRALOWPOWER)                    \
  58.   )
  59.  
  60. /* Note: On this STM32 serie, comparator input plus parameters are            */
  61. /*       the different depending on COMP instances.                           */
  62. #define IS_LL_COMP_INPUT_PLUS(__COMP_INSTANCE__, __INPUT_PLUS__)               \
  63.   (((__COMP_INSTANCE__) == COMP1)                                              \
  64.     ? (                                                                        \
  65.           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
  66.        || ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_DAC1_CH1)                    \
  67.       )                                                                        \
  68.       :                                                                        \
  69.       (                                                                        \
  70.           ((__INPUT_PLUS__) == LL_COMP_INPUT_PLUS_IO1)                         \
  71.       )                                                                        \
  72.   )
  73.  
  74. /* Note: On this STM32 serie, comparator input minus parameters are           */
  75. /*       the same on all COMP instances.                                      */
  76. /*       However, comparator instance kept as macro parameter for             */
  77. /*       compatibility with other STM32 families.                             */
  78. #define IS_LL_COMP_INPUT_MINUS(__COMP_INSTANCE__, __INPUT_MINUS__)             \
  79.   (   ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_4VREFINT)                    \
  80.    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_1_2VREFINT)                    \
  81.    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_3_4VREFINT)                    \
  82.    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_VREFINT)                       \
  83.    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH1)                      \
  84.    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_DAC1_CH2)                      \
  85.    || ((__INPUT_MINUS__) == LL_COMP_INPUT_MINUS_IO1)                           \
  86.   )
  87.  
  88. #define IS_LL_COMP_INPUT_HYSTERESIS(__INPUT_HYSTERESIS__)                      \
  89.   (   ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_NONE)                      \
  90.    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_LOW)                       \
  91.    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_MEDIUM)                    \
  92.    || ((__INPUT_HYSTERESIS__) == LL_COMP_HYSTERESIS_HIGH)                      \
  93.   )
  94.  
  95. #define IS_LL_COMP_OUTPUT_SELECTION(__OUTPUT_SELECTION__)                      \
  96.   (   ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_NONE)                          \
  97.    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_BKIN)                     \
  98.    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_IC1)                      \
  99.    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM1_OCCLR)                    \
  100.    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_IC4)                      \
  101.    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM2_OCCLR)                    \
  102.    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_IC1)                      \
  103.    || ((__OUTPUT_SELECTION__) == LL_COMP_OUTPUT_TIM3_OCCLR)                    \
  104.   )
  105.  
  106. #define IS_LL_COMP_OUTPUT_POLARITY(__POLARITY__)                               \
  107.   (   ((__POLARITY__) == LL_COMP_OUTPUTPOL_NONINVERTED)                        \
  108.    || ((__POLARITY__) == LL_COMP_OUTPUTPOL_INVERTED)                           \
  109.   )
  110.  
  111. /**
  112.   * @}
  113.   */
  114.  
  115.  
  116. /* Private function prototypes -----------------------------------------------*/
  117.  
  118. /* Exported functions --------------------------------------------------------*/
  119. /** @addtogroup COMP_LL_Exported_Functions
  120.   * @{
  121.   */
  122.  
  123. /** @addtogroup COMP_LL_EF_Init
  124.   * @{
  125.   */
  126.  
  127. /**
  128.   * @brief  De-initialize registers of the selected COMP instance
  129.   *         to their default reset values.
  130.   * @note   If comparator is locked, de-initialization by software is
  131.   *         not possible.
  132.   *         The only way to unlock the comparator is a device hardware reset.
  133.   * @param  COMPx COMP instance
  134.   * @retval An ErrorStatus enumeration value:
  135.   *          - SUCCESS: COMP registers are de-initialized
  136.   *          - ERROR: COMP registers are not de-initialized
  137.   */
  138. ErrorStatus LL_COMP_DeInit(COMP_TypeDef *COMPx)
  139. {
  140.   ErrorStatus status = SUCCESS;
  141.  
  142.   /* Check the parameters */
  143.   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  144.  
  145.   /* Note: Hardware constraint (refer to description of this function):       */
  146.   /*       COMP instance must not be locked.                                  */
  147.   if(LL_COMP_IsLocked(COMPx) == 0U)
  148.   {
  149.     /* Note: Connection switch is applicable only to COMP instance COMP1,     */
  150.     /*       therefore is COMP2 is selected the equivalent bit is             */
  151.     /*       kept unmodified.                                                 */
  152.     if(COMPx == COMP1)
  153.     {
  154.       CLEAR_BIT(COMP->CSR,
  155.                 (  COMP_CSR_COMP1MODE
  156.                  | COMP_CSR_COMP1INSEL
  157.                  | COMP_CSR_COMP1SW1
  158.                  | COMP_CSR_COMP1OUTSEL
  159.                  | COMP_CSR_COMP1HYST
  160.                  | COMP_CSR_COMP1POL
  161.                  | COMP_CSR_COMP1EN
  162.                 ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  163.                );
  164.     }
  165.     else
  166.     {
  167.       CLEAR_BIT(COMP->CSR,
  168.                 (  COMP_CSR_COMP1MODE
  169.                  | COMP_CSR_COMP1INSEL
  170.                  | COMP_CSR_COMP1OUTSEL
  171.                  | COMP_CSR_COMP1HYST
  172.                  | COMP_CSR_COMP1POL
  173.                  | COMP_CSR_COMP1EN
  174.                 ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  175.                );
  176.     }
  177.    
  178.   }
  179.   else
  180.   {
  181.     /* Comparator instance is locked: de-initialization by software is         */
  182.     /* not possible.                                                           */
  183.     /* The only way to unlock the comparator is a device hardware reset.       */
  184.     status = ERROR;
  185.   }
  186.  
  187.   return status;
  188. }
  189.  
  190. /**
  191.   * @brief  Initialize some features of COMP instance.
  192.   * @note   This function configures features of the selected COMP instance.
  193.   *         Some features are also available at scope COMP common instance
  194.   *         (common to several COMP instances).
  195.   *         Refer to functions having argument "COMPxy_COMMON" as parameter.
  196.   * @param  COMPx COMP instance
  197.   * @param  COMP_InitStruct Pointer to a @ref LL_COMP_InitTypeDef structure
  198.   * @retval An ErrorStatus enumeration value:
  199.   *          - SUCCESS: COMP registers are initialized
  200.   *          - ERROR: COMP registers are not initialized
  201.   */
  202. ErrorStatus LL_COMP_Init(COMP_TypeDef *COMPx, LL_COMP_InitTypeDef *COMP_InitStruct)
  203. {
  204.   ErrorStatus status = SUCCESS;
  205.  
  206.   /* Check the parameters */
  207.   assert_param(IS_COMP_ALL_INSTANCE(COMPx));
  208.   assert_param(IS_LL_COMP_POWER_MODE(COMP_InitStruct->PowerMode));
  209.   assert_param(IS_LL_COMP_INPUT_PLUS(COMPx, COMP_InitStruct->InputPlus));
  210.   assert_param(IS_LL_COMP_INPUT_MINUS(COMPx, COMP_InitStruct->InputMinus));
  211.   assert_param(IS_LL_COMP_INPUT_HYSTERESIS(COMP_InitStruct->InputHysteresis));
  212.   assert_param(IS_LL_COMP_OUTPUT_SELECTION(COMP_InitStruct->OutputSelection));
  213.   assert_param(IS_LL_COMP_OUTPUT_POLARITY(COMP_InitStruct->OutputPolarity));
  214.  
  215.   /* Note: Hardware constraint (refer to description of this function)        */
  216.   /*       COMP instance must not be locked.                                  */
  217.   if(LL_COMP_IsLocked(COMPx) == 0U)
  218.   {
  219.     /* Configuration of comparator instance :                                 */
  220.     /*  - PowerMode                                                           */
  221.     /*  - InputPlus                                                           */
  222.     /*  - InputMinus                                                          */
  223.     /*  - InputHysteresis                                                     */
  224.     /*  - OutputSelection                                                     */
  225.     /*  - OutputPolarity                                                      */
  226.     /* Note: Connection switch is applicable only to COMP instance COMP1,     */
  227.     /*       therefore is COMP2 is selected the equivalent bit is             */
  228.     /*       kept unmodified.                                                 */
  229.     if(COMPx == COMP1)
  230.     {
  231.       MODIFY_REG(COMP->CSR,
  232.                  (  COMP_CSR_COMP1MODE
  233.                   | COMP_CSR_COMP1INSEL
  234.                   | COMP_CSR_COMP1SW1
  235.                   | COMP_CSR_COMP1OUTSEL
  236.                   | COMP_CSR_COMP1HYST
  237.                   | COMP_CSR_COMP1POL
  238.                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  239.                 ,
  240.                  (  COMP_InitStruct->PowerMode
  241.                   | COMP_InitStruct->InputPlus
  242.                   | COMP_InitStruct->InputMinus
  243.                   | COMP_InitStruct->InputHysteresis
  244.                   | COMP_InitStruct->OutputSelection
  245.                   | COMP_InitStruct->OutputPolarity
  246.                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  247.                 );
  248.     }
  249.     else
  250.     {
  251.       MODIFY_REG(COMP->CSR,
  252.                  (  COMP_CSR_COMP1MODE
  253.                   | COMP_CSR_COMP1INSEL
  254.                   | COMP_CSR_COMP1OUTSEL
  255.                   | COMP_CSR_COMP1HYST
  256.                   | COMP_CSR_COMP1POL
  257.                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  258.                 ,
  259.                  (  COMP_InitStruct->PowerMode
  260.                   | COMP_InitStruct->InputPlus
  261.                   | COMP_InitStruct->InputMinus
  262.                   | COMP_InitStruct->InputHysteresis
  263.                   | COMP_InitStruct->OutputSelection
  264.                   | COMP_InitStruct->OutputPolarity
  265.                  ) << __COMP_BITOFFSET_INSTANCE(COMPx)
  266.                 );
  267.     }
  268.    
  269.   }
  270.   else
  271.   {
  272.     /* Initialization error: COMP instance is locked.                         */
  273.     status = ERROR;
  274.   }
  275.  
  276.   return status;
  277. }
  278.  
  279. /**
  280.   * @brief Set each @ref LL_COMP_InitTypeDef field to default value.
  281.   * @param COMP_InitStruct pointer to a @ref LL_COMP_InitTypeDef structure
  282.   *                         whose fields will be set to default values.
  283.   * @retval None
  284.   */
  285. void LL_COMP_StructInit(LL_COMP_InitTypeDef *COMP_InitStruct)
  286. {
  287.   /* Set COMP_InitStruct fields to default values */
  288.   COMP_InitStruct->PowerMode            = LL_COMP_POWERMODE_ULTRALOWPOWER;
  289.   COMP_InitStruct->InputPlus            = LL_COMP_INPUT_PLUS_IO1;
  290.   COMP_InitStruct->InputMinus           = LL_COMP_INPUT_MINUS_VREFINT;
  291.   COMP_InitStruct->InputHysteresis      = LL_COMP_HYSTERESIS_NONE;
  292.   COMP_InitStruct->OutputSelection      = LL_COMP_OUTPUT_NONE;
  293.   COMP_InitStruct->OutputPolarity       = LL_COMP_OUTPUTPOL_NONINVERTED;
  294. }
  295.  
  296. /**
  297.   * @}
  298.   */
  299.  
  300. /**
  301.   * @}
  302.   */
  303.  
  304. /**
  305.   * @}
  306.   */
  307.  
  308. #endif /* COMP1 || COMP2 */
  309.  
  310. /**
  311.   * @}
  312.   */
  313.  
  314. #endif /* USE_FULL_LL_DRIVER */
  315.  
  316. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  317.