Subversion Repositories FuelGauge

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f0xx_hal_adc_ex.c
4
  * @author  MCD Application Team
5
  * @brief   This file provides firmware functions to manage the following
6
  *          functionalities of the Analog to Digital Convertor (ADC)
7
  *          peripheral:
8
  *           + Operation functions
9
  *             ++ Calibration (ADC automatic self-calibration)
10
  *          Other functions (generic functions) are available in file
11
  *          "stm32f0xx_hal_adc.c".
12
  *
13
  @verbatim
14
  [..]
15
  (@) Sections "ADC peripheral features" and "How to use this driver" are
16
      available in file of generic functions "stm32l1xx_hal_adc.c".
17
  [..]
18
  @endverbatim
19
  ******************************************************************************
20
  * @attention
21
  *
22
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
23
  * All rights reserved.</center></h2>
24
  *
25
  * This software component is licensed by ST under BSD 3-Clause license,
26
  * the "License"; You may not use this file except in compliance with the
27
  * License. You may obtain a copy of the License at:
28
  *                        opensource.org/licenses/BSD-3-Clause
29
  *
30
  ******************************************************************************
31
  */
32
 
33
/* Includes ------------------------------------------------------------------*/
34
#include "stm32f0xx_hal.h"
35
 
36
/** @addtogroup STM32F0xx_HAL_Driver
37
  * @{
38
  */
39
 
40
/** @defgroup ADCEx ADCEx
41
  * @brief ADC HAL module driver
42
  * @{
43
  */
44
 
45
#ifdef HAL_ADC_MODULE_ENABLED
46
 
47
/* Private typedef -----------------------------------------------------------*/
48
/* Private define ------------------------------------------------------------*/
49
/** @defgroup ADCEx_Private_Constants ADCEx Private Constants
50
  * @{
51
  */
52
 
53
/* Fixed timeout values for ADC calibration, enable settling time, disable  */
54
  /* settling time.                                                           */
55
  /* Values defined to be higher than worst cases: low clock frequency,       */
56
  /* maximum prescaler.                                                       */
57
  /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock         */
58
  /* prescaler 4.                                                             */
59
  /* Unit: ms                                                                 */
60
  #define ADC_DISABLE_TIMEOUT           2
61
  #define ADC_CALIBRATION_TIMEOUT       2U      
62
/**
63
  * @}
64
  */
65
 
66
/* Private macros -------------------------------------------------------------*/
67
/* Private variables ---------------------------------------------------------*/
68
/* Private function prototypes -----------------------------------------------*/
69
/* Private functions ---------------------------------------------------------*/
70
 
71
/** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
72
  * @{
73
  */
74
 
75
/** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
76
 *  @brief    Extended Initialization and Configuration functions
77
 *
78
@verbatim
79
 ===============================================================================
80
                      ##### IO operation functions #####
81
 ===============================================================================
82
    [..]  This section provides functions allowing to:
83
      (+) Perform the ADC calibration.
84
@endverbatim
85
  * @{
86
  */
87
 
88
/**
89
  * @brief  Perform an ADC automatic self-calibration
90
  *         Calibration prerequisite: ADC must be disabled (execute this
91
  *         function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
92
  * @note   Calibration factor can be read after calibration, using function
93
  *         HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
94
  * @param  hadc ADC handle
95
  * @retval HAL status
96
  */
97
HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
98
{
99
  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
100
  uint32_t tickstart = 0U;
101
  uint32_t backup_setting_adc_dma_transfer = 0; /* Note: Variable not declared as volatile because register read is already declared as volatile */
102
 
103
  /* Check the parameters */
104
  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
105
 
106
  /* Process locked */
107
  __HAL_LOCK(hadc);
108
 
109
  /* Calibration prerequisite: ADC must be disabled. */
110
  if (ADC_IS_ENABLE(hadc) == RESET)
111
  {
112
    /* Set ADC state */
113
    ADC_STATE_CLR_SET(hadc->State,
114
                      HAL_ADC_STATE_REG_BUSY,
115
                      HAL_ADC_STATE_BUSY_INTERNAL);
116
 
117
    /* Disable ADC DMA transfer request during calibration */
118
    /* Note: Specificity of this STM32 serie: Calibration factor is           */
119
    /*       available in data register and also transfered by DMA.           */
120
    /*       To not insert ADC calibration factor among ADC conversion data   */
121
    /*       in array variable, DMA transfer must be disabled during          */
122
    /*       calibration.                                                     */
123
    backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
124
    CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
125
 
126
    /* Start ADC calibration */
127
    hadc->Instance->CR |= ADC_CR_ADCAL;
128
 
129
    tickstart = HAL_GetTick();  
130
 
131
    /* Wait for calibration completion */
132
    while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
133
    {
134
      if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
135
      {
136
        /* Update ADC state machine to error */
137
        ADC_STATE_CLR_SET(hadc->State,
138
                          HAL_ADC_STATE_BUSY_INTERNAL,
139
                          HAL_ADC_STATE_ERROR_INTERNAL);
140
 
141
        /* Process unlocked */
142
        __HAL_UNLOCK(hadc);
143
 
144
        return HAL_ERROR;
145
      }
146
    }
147
 
148
    /* Restore ADC DMA transfer request after calibration */
149
    SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer);
150
 
151
    /* Set ADC state */
152
    ADC_STATE_CLR_SET(hadc->State,
153
                      HAL_ADC_STATE_BUSY_INTERNAL,
154
                      HAL_ADC_STATE_READY);
155
  }
156
  else
157
  {
158
    /* Update ADC state machine to error */
159
    SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
160
 
161
    tmp_hal_status = HAL_ERROR;
162
  }
163
 
164
  /* Process unlocked */
165
  __HAL_UNLOCK(hadc);
166
 
167
  /* Return function status */
168
  return tmp_hal_status;
169
}
170
 
171
/**
172
  * @}
173
  */  
174
 
175
/**
176
  * @}
177
  */
178
 
179
#endif /* HAL_ADC_MODULE_ENABLED */
180
/**
181
  * @}
182
  */
183
 
184
/**
185
  * @}
186
  */
187
 
188
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/