STM32F103xB HAL User Manual
stm32f1xx_hal_adc_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal_adc_ex.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides firmware functions to manage the following 
00006   *          functionalities of the Analog to Digital Convertor (ADC)
00007   *          peripheral:
00008   *           + Operation functions
00009   *             ++ Start, stop, get result of conversions of injected
00010   *                group, using 2 possible modes: polling, interruption.
00011   *             ++ Multimode feature (available on devices with 2 ADCs or more)
00012   *             ++ Calibration (ADC automatic self-calibration)
00013   *           + Control functions
00014   *             ++ Channels configuration on injected group
00015   *          Other functions (generic functions) are available in file 
00016   *          "stm32f1xx_hal_adc.c".
00017   *
00018   @verbatim
00019   [..] 
00020   (@) Sections "ADC peripheral features" and "How to use this driver" are
00021       available in file of generic functions "stm32f1xx_hal_adc.c".
00022   [..]
00023   @endverbatim
00024   ******************************************************************************
00025   * @attention
00026   *
00027   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00028   * All rights reserved.</center></h2>
00029   *
00030   * This software component is licensed by ST under BSD 3-Clause license,
00031   * the "License"; You may not use this file except in compliance with the
00032   * License. You may obtain a copy of the License at:
00033   *                        opensource.org/licenses/BSD-3-Clause
00034   *
00035   ******************************************************************************
00036   */
00037 
00038 /* Includes ------------------------------------------------------------------*/
00039 #include "stm32f1xx_hal.h"
00040 
00041 /** @addtogroup STM32F1xx_HAL_Driver
00042   * @{
00043   */
00044 
00045 /** @defgroup ADCEx ADCEx
00046   * @brief ADC Extension HAL module driver
00047   * @{
00048   */
00049 
00050 #ifdef HAL_ADC_MODULE_ENABLED
00051 
00052 /* Private typedef -----------------------------------------------------------*/
00053 /* Private define ------------------------------------------------------------*/
00054 /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
00055   * @{
00056   */
00057 
00058   /* Delay for ADC calibration:                                               */
00059   /* Hardware prerequisite before starting a calibration: the ADC must have   */
00060   /* been in power-on state for at least two ADC clock cycles.                */
00061   /* Unit: ADC clock cycles                                                   */
00062   #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES       2U
00063 
00064   /* Timeout value for ADC calibration                                        */
00065   /* Value defined to be higher than worst cases: low clocks freq,            */
00066   /* maximum prescaler.                                                       */
00067   /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock         */
00068   /* prescaler 4, sampling time 12.5 ADC clock cycles, resolution 12 bits.    */
00069   /* Unit: ms                                                                 */
00070   #define ADC_CALIBRATION_TIMEOUT          10U
00071 
00072   /* Delay for temperature sensor stabilization time.                         */
00073   /* Maximum delay is 10us (refer to device datasheet, parameter tSTART).     */
00074   /* Unit: us                                                                 */
00075   #define ADC_TEMPSENSOR_DELAY_US         10U
00076 
00077 /**
00078   * @}
00079   */
00080 
00081 /* Private macro -------------------------------------------------------------*/
00082 /* Private variables ---------------------------------------------------------*/
00083 /* Private function prototypes -----------------------------------------------*/
00084 /* Private functions ---------------------------------------------------------*/
00085 
00086 /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
00087   * @{
00088   */
00089 
00090 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Extended IO operation functions
00091  *  @brief    Extended Extended Input and Output operation functions
00092  *
00093 @verbatim    
00094  ===============================================================================
00095                       ##### IO operation functions #####
00096  ===============================================================================
00097     [..]  This section provides functions allowing to:
00098       (+) Start conversion of injected group.
00099       (+) Stop conversion of injected group.
00100       (+) Poll for conversion complete on injected group.
00101       (+) Get result of injected channel conversion.
00102       (+) Start conversion of injected group and enable interruptions.
00103       (+) Stop conversion of injected group and disable interruptions.
00104 
00105       (+) Start multimode and enable DMA transfer.
00106       (+) Stop multimode and disable ADC DMA transfer.
00107       (+) Get result of multimode conversion.
00108 
00109       (+) Perform the ADC self-calibration for single or differential ending.
00110       (+) Get calibration factors for single or differential ending.
00111       (+) Set calibration factors for single or differential ending.
00112       
00113 @endverbatim
00114   * @{
00115   */
00116 
00117 /**
00118   * @brief  Perform an ADC automatic self-calibration
00119   *         Calibration prerequisite: ADC must be disabled (execute this
00120   *         function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
00121   *         During calibration process, ADC is enabled. ADC is let enabled at
00122   *         the completion of this function.
00123   * @param  hadc: ADC handle
00124   * @retval HAL status
00125   */
00126 HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
00127 {
00128   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00129   uint32_t tickstart;
00130   __IO uint32_t wait_loop_index = 0U;
00131   
00132   /* Check the parameters */
00133   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00134 
00135   /* Process locked */
00136   __HAL_LOCK(hadc);
00137     
00138   /* 1. Calibration prerequisite:                                             */
00139   /*    - ADC must be disabled for at least two ADC clock cycles in disable   */
00140   /*      mode before ADC enable                                              */
00141   /* Stop potential conversion on going, on regular and injected groups       */
00142   /* Disable ADC peripheral */
00143   tmp_hal_status = ADC_ConversionStop_Disable(hadc);
00144   
00145   /* Check if ADC is effectively disabled */
00146   if (tmp_hal_status == HAL_OK)
00147   {
00148     /* Set ADC state */
00149     ADC_STATE_CLR_SET(hadc->State,
00150                       HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00151                       HAL_ADC_STATE_BUSY_INTERNAL);
00152     
00153     /* Hardware prerequisite: delay before starting the calibration.          */
00154     /*  - Computation of CPU clock cycles corresponding to ADC clock cycles.  */
00155     /*  - Wait for the expected ADC clock cycles delay */
00156     wait_loop_index = ((SystemCoreClock
00157                         / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC))
00158                        * ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES        );
00159 
00160     while(wait_loop_index != 0U)
00161     {
00162       wait_loop_index--;
00163     }
00164     
00165     /* 2. Enable the ADC peripheral */
00166     ADC_Enable(hadc);
00167     
00168     /* 3. Resets ADC calibration registers */  
00169     SET_BIT(hadc->Instance->CR2, ADC_CR2_RSTCAL);
00170     
00171     tickstart = HAL_GetTick();  
00172 
00173     /* Wait for calibration reset completion */
00174     while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_RSTCAL))
00175     {
00176       if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
00177       {
00178         /* New check to avoid false timeout detection in case of preemption */
00179         if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_RSTCAL))
00180         {
00181           /* Update ADC state machine to error */
00182           ADC_STATE_CLR_SET(hadc->State,
00183                             HAL_ADC_STATE_BUSY_INTERNAL,
00184                             HAL_ADC_STATE_ERROR_INTERNAL);
00185 
00186           /* Process unlocked */
00187           __HAL_UNLOCK(hadc);
00188 
00189           return HAL_ERROR;
00190         }
00191       }
00192     }
00193     
00194     /* 4. Start ADC calibration */
00195     SET_BIT(hadc->Instance->CR2, ADC_CR2_CAL);
00196     
00197     tickstart = HAL_GetTick();  
00198 
00199     /* Wait for calibration completion */
00200     while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_CAL))
00201     {
00202       if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
00203       {
00204         /* New check to avoid false timeout detection in case of preemption */
00205         if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_CAL))
00206         {
00207           /* Update ADC state machine to error */
00208           ADC_STATE_CLR_SET(hadc->State,
00209                             HAL_ADC_STATE_BUSY_INTERNAL,
00210                             HAL_ADC_STATE_ERROR_INTERNAL);
00211 
00212           /* Process unlocked */
00213           __HAL_UNLOCK(hadc);
00214 
00215           return HAL_ERROR;
00216         }
00217       }
00218     }
00219     
00220     /* Set ADC state */
00221     ADC_STATE_CLR_SET(hadc->State,
00222                       HAL_ADC_STATE_BUSY_INTERNAL,
00223                       HAL_ADC_STATE_READY);
00224   }
00225   
00226   /* Process unlocked */
00227   __HAL_UNLOCK(hadc);
00228   
00229   /* Return function status */
00230   return tmp_hal_status;
00231 }
00232 
00233 /**
00234   * @brief  Enables ADC, starts conversion of injected group.
00235   *         Interruptions enabled in this function: None.
00236   * @param  hadc: ADC handle
00237   * @retval HAL status
00238   */
00239 HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
00240 {
00241   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00242   
00243   /* Check the parameters */
00244   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00245   
00246   /* Process locked */
00247   __HAL_LOCK(hadc);
00248     
00249   /* Enable the ADC peripheral */
00250   tmp_hal_status = ADC_Enable(hadc);
00251   
00252   /* Start conversion if ADC is effectively enabled */
00253   if (tmp_hal_status == HAL_OK)
00254   {
00255     /* Set ADC state                                                          */
00256     /* - Clear state bitfield related to injected group conversion results    */
00257     /* - Set state bitfield related to injected operation                     */
00258     ADC_STATE_CLR_SET(hadc->State,
00259                       HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
00260                       HAL_ADC_STATE_INJ_BUSY);
00261     
00262     /* Case of independent mode or multimode (for devices with several ADCs): */
00263     /* Set multimode state.                                                   */
00264     if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
00265     {
00266       CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00267     }
00268     else
00269     {
00270       SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00271     }
00272     
00273     /* Check if a regular conversion is ongoing */
00274     /* Note: On this device, there is no ADC error code fields related to     */
00275     /*       conversions on group injected only. In case of conversion on     */
00276     /*       going on group regular, no error code is reset.                  */
00277     if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
00278     {
00279       /* Reset ADC all error code fields */
00280       ADC_CLEAR_ERRORCODE(hadc);
00281     }
00282     
00283     /* Process unlocked */
00284     /* Unlock before starting ADC conversions: in case of potential           */
00285     /* interruption, to let the process to ADC IRQ Handler.                   */
00286     __HAL_UNLOCK(hadc);
00287     
00288     /* Clear injected group conversion flag */
00289     /* (To ensure of no unknown state from potential previous ADC operations) */
00290     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
00291         
00292     /* Enable conversion of injected group.                                   */
00293     /* If software start has been selected, conversion starts immediately.    */
00294     /* If external trigger has been selected, conversion will start at next   */
00295     /* trigger event.                                                         */
00296     /* If automatic injected conversion is enabled, conversion will start     */
00297     /* after next regular group conversion.                                   */
00298     /* Case of multimode enabled (for devices with several ADCs): if ADC is   */
00299     /* slave, ADC is enabled only (conversion is not started). If ADC is      */
00300     /* master, ADC is enabled and conversion is started.                      */
00301     if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
00302     {
00303       if (ADC_IS_SOFTWARE_START_INJECTED(hadc)     &&
00304           ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)  )
00305       {
00306         /* Start ADC conversion on injected group with SW start */
00307         SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
00308       }
00309       else
00310       {
00311         /* Start ADC conversion on injected group with external trigger */
00312         SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
00313       }
00314     }
00315   }
00316   else
00317   {
00318     /* Process unlocked */
00319     __HAL_UNLOCK(hadc);
00320   }
00321   
00322   /* Return function status */
00323   return tmp_hal_status;
00324 }
00325 
00326 /**
00327   * @brief  Stop conversion of injected channels. Disable ADC peripheral if
00328   *         no regular conversion is on going.
00329   * @note   If ADC must be disabled and if conversion is on going on 
00330   *         regular group, function HAL_ADC_Stop must be used to stop both
00331   *         injected and regular groups, and disable the ADC.
00332   * @note   If injected group mode auto-injection is enabled,
00333   *         function HAL_ADC_Stop must be used.
00334   * @note   In case of auto-injection mode, HAL_ADC_Stop must be used.
00335   * @param  hadc: ADC handle
00336   * @retval None
00337   */
00338 HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
00339 {
00340   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00341   
00342   /* Check the parameters */
00343   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00344 
00345   /* Process locked */
00346   __HAL_LOCK(hadc);
00347     
00348   /* Stop potential conversion and disable ADC peripheral                     */
00349   /* Conditioned to:                                                          */
00350   /* - No conversion on the other group (regular group) is intended to        */
00351   /*   continue (injected and regular groups stop conversion and ADC disable  */
00352   /*   are common)                                                            */
00353   /* - In case of auto-injection mode, HAL_ADC_Stop must be used.             */
00354   if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET)  &&
00355      HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)   )
00356   {
00357     /* Stop potential conversion on going, on regular and injected groups */
00358     /* Disable ADC peripheral */
00359     tmp_hal_status = ADC_ConversionStop_Disable(hadc);
00360     
00361     /* Check if ADC is effectively disabled */
00362     if (tmp_hal_status == HAL_OK)
00363     {
00364       /* Set ADC state */
00365       ADC_STATE_CLR_SET(hadc->State,
00366                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00367                         HAL_ADC_STATE_READY);
00368     }
00369   }
00370   else
00371   {
00372     /* Update ADC state machine to error */
00373     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
00374       
00375     tmp_hal_status = HAL_ERROR;
00376   }
00377   
00378   /* Process unlocked */
00379   __HAL_UNLOCK(hadc);
00380   
00381   /* Return function status */
00382   return tmp_hal_status;
00383 }
00384 
00385 /**
00386   * @brief  Wait for injected group conversion to be completed.
00387   * @param  hadc: ADC handle
00388   * @param  Timeout: Timeout value in millisecond.
00389   * @retval HAL status
00390   */
00391 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
00392 {
00393   uint32_t tickstart;
00394 
00395   /* Variables for polling in case of scan mode enabled and polling for each  */
00396   /* conversion.                                                              */
00397   __IO uint32_t Conversion_Timeout_CPU_cycles = 0U;
00398   uint32_t Conversion_Timeout_CPU_cycles_max = 0U;
00399   
00400   /* Check the parameters */
00401   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00402 
00403   /* Get timeout */
00404   tickstart = HAL_GetTick();  
00405      
00406   /* Polling for end of conversion: differentiation if single/sequence        */
00407   /* conversion.                                                              */
00408   /* For injected group, flag JEOC is set only at the end of the sequence,    */
00409   /* not for each conversion within the sequence.                             */
00410   /*  - If single conversion for injected group (scan mode disabled or        */
00411   /*    InjectedNbrOfConversion ==1), flag JEOC is used to determine the      */
00412   /*    conversion completion.                                                */
00413   /*  - If sequence conversion for injected group (scan mode enabled and      */
00414   /*    InjectedNbrOfConversion >=2), flag JEOC is set only at the end of the */
00415   /*    sequence.                                                             */
00416   /*    To poll for each conversion, the maximum conversion time is computed  */
00417   /*    from ADC conversion time (selected sampling time + conversion time of */
00418   /*    12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on    */
00419   /*    settings, conversion time range can be from 28 to 32256 CPU cycles).  */
00420   /*    As flag JEOC is not set after each conversion, no timeout status can  */
00421   /*    be set.                                                               */
00422   if ((hadc->Instance->JSQR & ADC_JSQR_JL) == RESET)
00423   {
00424     /* Wait until End of Conversion flag is raised */
00425     while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
00426     {
00427       /* Check if timeout is disabled (set to infinite wait) */
00428       if(Timeout != HAL_MAX_DELAY)
00429       {
00430         if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
00431         {
00432           /* New check to avoid false timeout detection in case of preemption */
00433           if(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
00434           {
00435             /* Update ADC state machine to timeout */
00436             SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
00437 
00438             /* Process unlocked */
00439             __HAL_UNLOCK(hadc);
00440 
00441             return HAL_TIMEOUT;
00442           }
00443         }
00444       }
00445     }
00446   }
00447   else
00448   {
00449     /* Replace polling by wait for maximum conversion time */
00450     /*  - Computation of CPU clock cycles corresponding to ADC clock cycles   */
00451     /*    and ADC maximum conversion cycles on all channels.                  */
00452     /*  - Wait for the expected ADC clock cycles delay                        */
00453     Conversion_Timeout_CPU_cycles_max = ((SystemCoreClock
00454                                           / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC))
00455                                          * ADC_CONVCYCLES_MAX_RANGE(hadc)                 );
00456     
00457     while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
00458     {
00459       /* Check if timeout is disabled (set to infinite wait) */
00460       if(Timeout != HAL_MAX_DELAY)
00461       {
00462         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
00463         {
00464           /* New check to avoid false timeout detection in case of preemption */
00465           if(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
00466           {
00467             /* Update ADC state machine to timeout */
00468             SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
00469 
00470             /* Process unlocked */
00471             __HAL_UNLOCK(hadc);
00472 
00473             return HAL_TIMEOUT;
00474           }
00475         }
00476       }
00477       Conversion_Timeout_CPU_cycles ++;
00478     }
00479   }
00480 
00481   /* Clear injected group conversion flag */
00482   /* Note: On STM32F1 ADC, clear regular conversion flag raised               */
00483   /* simultaneously.                                                          */
00484   __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC | ADC_FLAG_EOC);
00485   
00486   /* Update ADC state machine */
00487   SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
00488   
00489   /* Determine whether any further conversion upcoming on group injected      */
00490   /* by external trigger or by automatic injected conversion                  */
00491   /* from group regular.                                                      */
00492   if(ADC_IS_SOFTWARE_START_INJECTED(hadc)                     || 
00493      (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&     
00494      (ADC_IS_SOFTWARE_START_REGULAR(hadc)        &&
00495       (hadc->Init.ContinuousConvMode == DISABLE)   )        )   )
00496   {
00497     /* Set ADC state */
00498     CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);   
00499     
00500     if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
00501     {
00502       SET_BIT(hadc->State, HAL_ADC_STATE_READY);
00503     }
00504   }
00505   
00506   /* Return ADC state */
00507   return HAL_OK;
00508 }
00509 
00510 /**
00511   * @brief  Enables ADC, starts conversion of injected group with interruption.
00512   *          - JEOC (end of conversion of injected group)
00513   *         Each of these interruptions has its dedicated callback function.
00514   * @param  hadc: ADC handle
00515   * @retval HAL status.
00516   */
00517 HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
00518 {
00519   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00520   
00521   /* Check the parameters */
00522   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00523   
00524   /* Process locked */
00525   __HAL_LOCK(hadc);
00526     
00527   /* Enable the ADC peripheral */
00528   tmp_hal_status = ADC_Enable(hadc);
00529   
00530   /* Start conversion if ADC is effectively enabled */
00531   if (tmp_hal_status == HAL_OK)
00532   {
00533     /* Set ADC state                                                          */
00534     /* - Clear state bitfield related to injected group conversion results    */
00535     /* - Set state bitfield related to injected operation                     */
00536     ADC_STATE_CLR_SET(hadc->State,
00537                       HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
00538                       HAL_ADC_STATE_INJ_BUSY);
00539     
00540     /* Case of independent mode or multimode (for devices with several ADCs): */
00541     /* Set multimode state.                                                   */
00542     if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
00543     {
00544       CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00545     }
00546     else
00547     {
00548       SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
00549     }
00550     
00551     /* Check if a regular conversion is ongoing */
00552     /* Note: On this device, there is no ADC error code fields related to     */
00553     /*       conversions on group injected only. In case of conversion on     */
00554     /*       going on group regular, no error code is reset.                  */
00555     if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
00556     {
00557       /* Reset ADC all error code fields */
00558       ADC_CLEAR_ERRORCODE(hadc);
00559     }
00560     
00561     /* Process unlocked */
00562     /* Unlock before starting ADC conversions: in case of potential           */
00563     /* interruption, to let the process to ADC IRQ Handler.                   */
00564     __HAL_UNLOCK(hadc);
00565     
00566     /* Clear injected group conversion flag */
00567     /* (To ensure of no unknown state from potential previous ADC operations) */
00568     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
00569     
00570     /* Enable end of conversion interrupt for injected channels */
00571     __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
00572     
00573     /* Start conversion of injected group if software start has been selected */
00574     /* and if automatic injected conversion is disabled.                      */
00575     /* If external trigger has been selected, conversion will start at next   */
00576     /* trigger event.                                                         */
00577     /* If automatic injected conversion is enabled, conversion will start     */
00578     /* after next regular group conversion.                                   */
00579     if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
00580     {
00581       if (ADC_IS_SOFTWARE_START_INJECTED(hadc)     &&
00582           ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)  )
00583       {
00584         /* Start ADC conversion on injected group with SW start */
00585         SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
00586       }
00587       else
00588       {
00589         /* Start ADC conversion on injected group with external trigger */
00590         SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
00591       }
00592     }
00593   }
00594   else
00595   {
00596     /* Process unlocked */
00597     __HAL_UNLOCK(hadc);
00598   }
00599   
00600   /* Return function status */
00601   return tmp_hal_status;
00602 }
00603 
00604 /**
00605   * @brief  Stop conversion of injected channels, disable interruption of 
00606   *         end-of-conversion. Disable ADC peripheral if no regular conversion
00607   *         is on going.
00608   * @note   If ADC must be disabled and if conversion is on going on 
00609   *         regular group, function HAL_ADC_Stop must be used to stop both
00610   *         injected and regular groups, and disable the ADC.
00611   * @note   If injected group mode auto-injection is enabled,
00612   *         function HAL_ADC_Stop must be used.
00613   * @param  hadc: ADC handle
00614   * @retval None
00615   */
00616 HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
00617 {
00618   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00619   
00620   /* Check the parameters */
00621   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00622 
00623   /* Process locked */
00624   __HAL_LOCK(hadc);
00625     
00626   /* Stop potential conversion and disable ADC peripheral                     */
00627   /* Conditioned to:                                                          */
00628   /* - No conversion on the other group (regular group) is intended to        */
00629   /*   continue (injected and regular groups stop conversion and ADC disable  */
00630   /*   are common)                                                            */
00631   /* - In case of auto-injection mode, HAL_ADC_Stop must be used.             */ 
00632   if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET)  &&
00633      HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)   )
00634   {
00635     /* Stop potential conversion on going, on regular and injected groups */
00636     /* Disable ADC peripheral */
00637     tmp_hal_status = ADC_ConversionStop_Disable(hadc);
00638     
00639     /* Check if ADC is effectively disabled */
00640     if (tmp_hal_status == HAL_OK)
00641     {
00642       /* Disable ADC end of conversion interrupt for injected channels */
00643       __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
00644       
00645       /* Set ADC state */
00646       ADC_STATE_CLR_SET(hadc->State,
00647                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00648                         HAL_ADC_STATE_READY);
00649     }
00650   }
00651   else
00652   {
00653     /* Update ADC state machine to error */
00654     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
00655       
00656     tmp_hal_status = HAL_ERROR;
00657   }
00658   
00659   /* Process unlocked */
00660   __HAL_UNLOCK(hadc);
00661   
00662   /* Return function status */
00663   return tmp_hal_status;
00664 }
00665 
00666 #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
00667 /**
00668   * @brief  Enables ADC, starts conversion of regular group and transfers result
00669   *         through DMA.
00670   *         Multimode must have been previously configured using 
00671   *         HAL_ADCEx_MultiModeConfigChannel() function.
00672   *         Interruptions enabled in this function:
00673   *          - DMA transfer complete
00674   *          - DMA half transfer
00675   *         Each of these interruptions has its dedicated callback function.
00676   * @note:  On STM32F1 devices, ADC slave regular group must be configured 
00677   *         with conversion trigger ADC_SOFTWARE_START.
00678   * @note:  ADC slave can be enabled preliminarily using single-mode  
00679   *         HAL_ADC_Start() function.
00680   * @param  hadc: ADC handle of ADC master (handle of ADC slave must not be used)
00681   * @param  pData: The destination Buffer address.
00682   * @param  Length: The length of data to be transferred from ADC peripheral to memory.
00683   * @retval None
00684   */
00685 HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
00686 {
00687   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00688   ADC_HandleTypeDef tmphadcSlave={0};
00689 
00690   /* Check the parameters */
00691   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
00692   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
00693   
00694   /* Process locked */
00695   __HAL_LOCK(hadc);
00696 
00697   /* Set a temporary handle of the ADC slave associated to the ADC master     */
00698   ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
00699   
00700   /* On STM32F1 devices, ADC slave regular group must be configured with      */
00701   /* conversion trigger ADC_SOFTWARE_START.                                   */
00702   /* Note: External trigger of ADC slave must be enabled, it is already done  */
00703   /*       into function "HAL_ADC_Init()".                                    */
00704   if(!ADC_IS_SOFTWARE_START_REGULAR(&tmphadcSlave))  
00705   {
00706     /* Update ADC state machine to error */
00707     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
00708     
00709     /* Process unlocked */
00710     __HAL_UNLOCK(hadc);
00711     
00712     return HAL_ERROR;
00713   }
00714   
00715   /* Enable the ADC peripherals: master and slave (in case if not already     */
00716   /* enabled previously)                                                      */
00717   tmp_hal_status = ADC_Enable(hadc);
00718   if (tmp_hal_status == HAL_OK)
00719   {
00720     tmp_hal_status = ADC_Enable(&tmphadcSlave);
00721   }
00722   
00723   /* Start conversion if all ADCs of multimode are effectively enabled */
00724   if (tmp_hal_status == HAL_OK)
00725   {
00726     /* Set ADC state (ADC master)                                             */
00727     /* - Clear state bitfield related to regular group conversion results     */
00728     /* - Set state bitfield related to regular operation                      */
00729     ADC_STATE_CLR_SET(hadc->State,
00730                       HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_MULTIMODE_SLAVE,
00731                       HAL_ADC_STATE_REG_BUSY);
00732       
00733     /* If conversions on group regular are also triggering group injected,    */
00734     /* update ADC state.                                                      */
00735     if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
00736     {
00737       ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);  
00738     }
00739     
00740     /* Process unlocked */
00741     /* Unlock before starting ADC conversions: in case of potential           */
00742     /* interruption, to let the process to ADC IRQ Handler.                   */
00743     __HAL_UNLOCK(hadc);
00744     
00745     /* Set ADC error code to none */
00746     ADC_CLEAR_ERRORCODE(hadc);
00747     
00748     
00749     /* Set the DMA transfer complete callback */
00750     hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
00751        
00752     /* Set the DMA half transfer complete callback */
00753     hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
00754     
00755     /* Set the DMA error callback */
00756     hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
00757 
00758     
00759     /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC     */
00760     /* start (in case of SW start):                                           */
00761     
00762     /* Clear regular group conversion flag and overrun flag */
00763     /* (To ensure of no unknown state from potential previous ADC operations) */
00764     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
00765     
00766     /* Enable ADC DMA mode of ADC master */
00767     SET_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
00768     
00769     /* Start the DMA channel */
00770     HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
00771     
00772     /* Start conversion of regular group if software start has been selected. */
00773     /* If external trigger has been selected, conversion will start at next   */
00774     /* trigger event.                                                         */
00775     /* Note: Alternate trigger for single conversion could be to force an     */
00776     /*       additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/
00777     if (ADC_IS_SOFTWARE_START_REGULAR(hadc))
00778     {
00779       /* Start ADC conversion on regular group with SW start */
00780       SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
00781     }
00782     else
00783     {
00784       /* Start ADC conversion on regular group with external trigger */
00785       SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
00786     }
00787   }
00788   else
00789   {
00790     /* Process unlocked */
00791     __HAL_UNLOCK(hadc);
00792   }
00793   
00794   /* Return function status */
00795   return tmp_hal_status;
00796 }
00797 
00798 /**
00799   * @brief  Stop ADC conversion of regular group (and injected channels in 
00800   *         case of auto_injection mode), disable ADC DMA transfer, disable 
00801   *         ADC peripheral.
00802   * @note   Multimode is kept enabled after this function. To disable multimode 
00803   *         (set with HAL_ADCEx_MultiModeConfigChannel(), ADC must be 
00804   *         reinitialized using HAL_ADC_Init() or HAL_ADC_ReInit().
00805   * @note   In case of DMA configured in circular mode, function 
00806   *         HAL_ADC_Stop_DMA must be called after this function with handle of
00807   *         ADC slave, to properly disable the DMA channel.
00808   * @param  hadc: ADC handle of ADC master (handle of ADC slave must not be used)
00809   * @retval None
00810   */
00811 HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)
00812 {
00813   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
00814   ADC_HandleTypeDef tmphadcSlave={0};
00815   
00816   /* Check the parameters */
00817   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
00818   
00819   /* Process locked */
00820   __HAL_LOCK(hadc);
00821 
00822   /* Stop potential conversion on going, on regular and injected groups */
00823   /* Disable ADC master peripheral */
00824   tmp_hal_status = ADC_ConversionStop_Disable(hadc);
00825   
00826   /* Check if ADC is effectively disabled */
00827   if(tmp_hal_status == HAL_OK)
00828   {
00829     /* Set a temporary handle of the ADC slave associated to the ADC master   */
00830     ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
00831 
00832     /* Disable ADC slave peripheral */
00833     tmp_hal_status = ADC_ConversionStop_Disable(&tmphadcSlave);
00834 
00835     /* Check if ADC is effectively disabled */
00836     if(tmp_hal_status != HAL_OK)
00837     {
00838       /* Update ADC state machine to error */
00839       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
00840 
00841       /* Process unlocked */
00842       __HAL_UNLOCK(hadc);
00843 
00844       return HAL_ERROR;
00845     }
00846 
00847     /* Disable ADC DMA mode */
00848     CLEAR_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
00849     
00850     /* Reset configuration of ADC DMA continuous request for dual mode */
00851     CLEAR_BIT(hadc->Instance->CR1, ADC_CR1_DUALMOD);
00852         
00853     /* Disable the DMA channel (in case of DMA in circular mode or stop while */
00854     /* while DMA transfer is on going)                                        */
00855     tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
00856 
00857     /* Change ADC state (ADC master) */
00858     ADC_STATE_CLR_SET(hadc->State,
00859                       HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00860                       HAL_ADC_STATE_READY);
00861   }
00862   
00863   /* Process unlocked */
00864   __HAL_UNLOCK(hadc);
00865   
00866   /* Return function status */
00867   return tmp_hal_status;
00868 }
00869 #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
00870 
00871 /**
00872   * @brief  Get ADC injected group conversion result.
00873   * @note   Reading register JDRx automatically clears ADC flag JEOC
00874   *         (ADC group injected end of unitary conversion).
00875   * @note   This function does not clear ADC flag JEOS 
00876   *         (ADC group injected end of sequence conversion)
00877   *         Occurrence of flag JEOS rising:
00878   *          - If sequencer is composed of 1 rank, flag JEOS is equivalent
00879   *            to flag JEOC.
00880   *          - If sequencer is composed of several ranks, during the scan
00881   *            sequence flag JEOC only is raised, at the end of the scan sequence
00882   *            both flags JEOC and EOS are raised.
00883   *         Flag JEOS must not be cleared by this function because
00884   *         it would not be compliant with low power features
00885   *         (feature low power auto-wait, not available on all STM32 families).
00886   *         To clear this flag, either use function: 
00887   *         in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
00888   *         model polling: @ref HAL_ADCEx_InjectedPollForConversion() 
00889   *         or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
00890   * @param  hadc: ADC handle
00891   * @param  InjectedRank: the converted ADC injected rank.
00892   *          This parameter can be one of the following values:
00893   *            @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
00894   *            @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
00895   *            @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
00896   *            @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
00897   * @retval ADC group injected conversion data
00898   */
00899 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
00900 {
00901   uint32_t tmp_jdr = 0U;
00902   
00903   /* Check the parameters */
00904   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00905   assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
00906   
00907   /* Get ADC converted value */ 
00908   switch(InjectedRank)
00909   {  
00910     case ADC_INJECTED_RANK_4: 
00911       tmp_jdr = hadc->Instance->JDR4;
00912       break;
00913     case ADC_INJECTED_RANK_3: 
00914       tmp_jdr = hadc->Instance->JDR3;
00915       break;
00916     case ADC_INJECTED_RANK_2: 
00917       tmp_jdr = hadc->Instance->JDR2;
00918       break;
00919     case ADC_INJECTED_RANK_1:
00920     default:
00921       tmp_jdr = hadc->Instance->JDR1;
00922       break;
00923   }
00924   
00925   /* Return ADC converted value */ 
00926   return tmp_jdr;
00927 }
00928 
00929 #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
00930 /**
00931   * @brief  Returns the last ADC Master&Slave regular conversions results data
00932   *         in the selected multi mode.
00933   * @param  hadc: ADC handle of ADC master (handle of ADC slave must not be used)
00934   * @retval The converted data value.
00935   */
00936 uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)
00937 {
00938   uint32_t tmpDR = 0U;
00939   
00940   /* Check the parameters */
00941   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
00942   
00943   /* Check the parameters */
00944   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00945 
00946   /* Note: EOC flag is not cleared here by software because automatically     */
00947   /*       cleared by hardware when reading register DR.                      */
00948   
00949   /* On STM32F1 devices, ADC1 data register DR contains ADC2 conversions      */
00950   /* only if ADC1 DMA mode is enabled.                                        */
00951   tmpDR = hadc->Instance->DR;
00952 
00953   if (HAL_IS_BIT_CLR(ADC1->CR2, ADC_CR2_DMA))
00954   {
00955     tmpDR |= (ADC2->DR << 16U);
00956   }
00957     
00958   /* Return ADC converted value */ 
00959   return tmpDR;
00960 }
00961 #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
00962 
00963 /**
00964   * @brief  Injected conversion complete callback in non blocking mode 
00965   * @param  hadc: ADC handle
00966   * @retval None
00967   */
00968 __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
00969 {
00970   /* Prevent unused argument(s) compilation warning */
00971   UNUSED(hadc);
00972   /* NOTE : This function Should not be modified, when the callback is needed,
00973             the HAL_ADCEx_InjectedConvCpltCallback could be implemented in the user file
00974   */
00975 }
00976 
00977 /**
00978   * @}
00979   */
00980 
00981 /** @defgroup ADCEx_Exported_Functions_Group2 Extended Peripheral Control functions
00982   * @brief    Extended Peripheral Control functions
00983   *
00984 @verbatim   
00985  ===============================================================================
00986              ##### Peripheral Control functions #####
00987  ===============================================================================  
00988     [..]  This section provides functions allowing to:
00989       (+) Configure channels on injected group
00990       (+) Configure multimode
00991 
00992 @endverbatim
00993   * @{
00994   */
00995 
00996 /**
00997   * @brief  Configures the ADC injected group and the selected channel to be
00998   *         linked to the injected group.
00999   * @note   Possibility to update parameters on the fly:
01000   *         This function initializes injected group, following calls to this 
01001   *         function can be used to reconfigure some parameters of structure
01002   *         "ADC_InjectionConfTypeDef" on the fly, without reseting the ADC.
01003   *         The setting of these parameters is conditioned to ADC state: 
01004   *         this function must be called when ADC is not under conversion.
01005   * @param  hadc: ADC handle
01006   * @param  sConfigInjected: Structure of ADC injected group and ADC channel for
01007   *         injected group.
01008   * @retval None
01009   */
01010 HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
01011 {
01012   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
01013   __IO uint32_t wait_loop_index = 0U;
01014   
01015   /* Check the parameters */
01016   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
01017   assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
01018   assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
01019   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
01020   assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
01021   assert_param(IS_ADC_RANGE(sConfigInjected->InjectedOffset));
01022   
01023   if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
01024   {
01025     assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
01026     assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
01027     assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
01028   }
01029   
01030   /* Process locked */
01031   __HAL_LOCK(hadc);
01032   
01033   /* Configuration of injected group sequencer:                               */
01034   /* - if scan mode is disabled, injected channels sequence length is set to  */
01035   /*   0x00: 1 channel converted (channel on regular rank 1)                  */
01036   /*   Parameter "InjectedNbrOfConversion" is discarded.                      */
01037   /*   Note: Scan mode is present by hardware on this device and, if          */
01038   /*   disabled, discards automatically nb of conversions. Anyway, nb of      */
01039   /*   conversions is forced to 0x00 for alignment over all STM32 devices.    */
01040   /* - if scan mode is enabled, injected channels sequence length is set to   */
01041   /*   parameter "InjectedNbrOfConversion".                                   */
01042   if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
01043   {
01044     if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
01045     {
01046       /* Clear the old SQx bits for all injected ranks */
01047       MODIFY_REG(hadc->Instance->JSQR                             ,
01048                  ADC_JSQR_JL   |
01049                  ADC_JSQR_JSQ4 |
01050                  ADC_JSQR_JSQ3 |
01051                  ADC_JSQR_JSQ2 |
01052                  ADC_JSQR_JSQ1                                    ,
01053                  ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
01054                                   ADC_INJECTED_RANK_1,
01055                                   0x01U));
01056     }
01057     /* If another injected rank than rank1 was intended to be set, and could  */
01058     /* not due to ScanConvMode disabled, error is reported.                   */
01059     else
01060     {
01061       /* Update ADC state machine to error */
01062       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01063       
01064       tmp_hal_status = HAL_ERROR;
01065     }
01066   }
01067   else
01068   {
01069     /* Since injected channels rank conv. order depends on total number of   */
01070     /* injected conversions, selected rank must be below or equal to total   */
01071     /* number of injected conversions to be updated.                         */
01072     if (sConfigInjected->InjectedRank <= sConfigInjected->InjectedNbrOfConversion)
01073     {
01074       /* Clear the old SQx bits for the selected rank */
01075       /* Set the SQx bits for the selected rank */
01076       MODIFY_REG(hadc->Instance->JSQR                                         ,
01077                  
01078                  ADC_JSQR_JL                                               |
01079                  ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,                         
01080                                   sConfigInjected->InjectedRank,         
01081                                   sConfigInjected->InjectedNbrOfConversion)   ,
01082                  
01083                  ADC_JSQR_JL_SHIFT(sConfigInjected->InjectedNbrOfConversion) |
01084                  ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,      
01085                                   sConfigInjected->InjectedRank,         
01086                                   sConfigInjected->InjectedNbrOfConversion)    );
01087     }
01088     else
01089     {
01090       /* Clear the old SQx bits for the selected rank */
01091       MODIFY_REG(hadc->Instance->JSQR                                       ,
01092                  
01093                  ADC_JSQR_JL                                               |
01094                  ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,                         
01095                                   sConfigInjected->InjectedRank,         
01096                                   sConfigInjected->InjectedNbrOfConversion) ,
01097                  
01098                  0x00000000U);
01099     }
01100   } 
01101     
01102   /* Configuration of injected group                                          */
01103   /* Parameters update conditioned to ADC state:                              */
01104   /* Parameters that can be updated only when ADC is disabled:                */
01105   /*  - external trigger to start conversion                                  */
01106   /* Parameters update not conditioned to ADC state:                          */
01107   /*  - Automatic injected conversion                                         */
01108   /*  - Injected discontinuous mode                                           */
01109   /* Note: In case of ADC already enabled, caution to not launch an unwanted  */
01110   /*       conversion while modifying register CR2 by writing 1 to bit ADON.  */
01111   if (ADC_IS_ENABLE(hadc) == RESET)
01112   {    
01113     MODIFY_REG(hadc->Instance->CR2                                           ,
01114                ADC_CR2_JEXTSEL |
01115                ADC_CR2_ADON                                                  ,
01116                ADC_CFGR_JEXTSEL(hadc, sConfigInjected->ExternalTrigInjecConv) );
01117   }
01118   
01119   
01120   /* Configuration of injected group                                          */
01121   /*  - Automatic injected conversion                                         */
01122   /*  - Injected discontinuous mode                                           */
01123   
01124     /* Automatic injected conversion can be enabled if injected group         */
01125     /* external triggers are disabled.                                        */
01126     if (sConfigInjected->AutoInjectedConv == ENABLE)
01127     {
01128       if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
01129       {
01130         SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
01131       }
01132       else
01133       {
01134         /* Update ADC state machine to error */
01135         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01136         
01137         tmp_hal_status = HAL_ERROR;
01138       }
01139     }
01140     
01141     /* Injected discontinuous can be enabled only if auto-injected mode is    */
01142     /* disabled.                                                              */  
01143     if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
01144     {
01145       if (sConfigInjected->AutoInjectedConv == DISABLE)
01146       {
01147         SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
01148       } 
01149       else
01150       {
01151         /* Update ADC state machine to error */
01152         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01153         
01154         tmp_hal_status = HAL_ERROR;
01155       }
01156     }
01157 
01158 
01159   /* InjectedChannel sampling time configuration */
01160   /* For channels 10 to 17 */
01161   if (sConfigInjected->InjectedChannel >= ADC_CHANNEL_10)
01162   {
01163     MODIFY_REG(hadc->Instance->SMPR1                                                             ,
01164                ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel)                      ,
01165                ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
01166   }
01167   else /* For channels 0 to 9 */
01168   {
01169     MODIFY_REG(hadc->Instance->SMPR2                                                             ,
01170                ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel)                       ,
01171                ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
01172   }
01173   
01174   /* If ADC1 InjectedChannel_16 or InjectedChannel_17 is selected, enable Temperature sensor  */
01175   /* and VREFINT measurement path.                                            */
01176   if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
01177       (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)      )
01178   {
01179     SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
01180   }
01181   
01182   
01183   /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
01184   switch(sConfigInjected->InjectedRank)
01185   {
01186     case 1:
01187       /* Set injected channel 1 offset */
01188       MODIFY_REG(hadc->Instance->JOFR1,
01189                  ADC_JOFR1_JOFFSET1,
01190                  sConfigInjected->InjectedOffset);
01191       break;
01192     case 2:
01193       /* Set injected channel 2 offset */
01194       MODIFY_REG(hadc->Instance->JOFR2,
01195                  ADC_JOFR2_JOFFSET2,
01196                  sConfigInjected->InjectedOffset);
01197       break;
01198     case 3:
01199       /* Set injected channel 3 offset */
01200       MODIFY_REG(hadc->Instance->JOFR3,
01201                  ADC_JOFR3_JOFFSET3,
01202                  sConfigInjected->InjectedOffset);
01203       break;
01204     case 4:
01205     default:
01206       MODIFY_REG(hadc->Instance->JOFR4,
01207                  ADC_JOFR4_JOFFSET4,
01208                  sConfigInjected->InjectedOffset);
01209       break;
01210   }
01211   
01212   /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor  */
01213   /* and VREFINT measurement path.                                            */
01214   if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
01215       (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)      )
01216   {
01217     /* For STM32F1 devices with several ADC: Only ADC1 can access internal    */
01218     /* measurement channels (VrefInt/TempSensor). If these channels are       */
01219     /* intended to be set on other ADC instances, an error is reported.       */
01220     if (hadc->Instance == ADC1)
01221     {
01222       if (READ_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE) == RESET)
01223       {
01224         SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
01225         
01226         if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR))
01227         {
01228           /* Delay for temperature sensor stabilization time */
01229           /* Compute number of CPU cycles to wait for */
01230           wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
01231           while(wait_loop_index != 0U)
01232           {
01233             wait_loop_index--;
01234           }
01235         }
01236       }
01237     }
01238     else
01239     {
01240       /* Update ADC state machine to error */
01241       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01242       
01243       tmp_hal_status = HAL_ERROR;
01244     }
01245   }
01246   
01247   /* Process unlocked */
01248   __HAL_UNLOCK(hadc);
01249   
01250   /* Return function status */
01251   return tmp_hal_status;
01252 }
01253 
01254 #if defined (STM32F103x6) || defined (STM32F103xB) || defined (STM32F105xC) || defined (STM32F107xC) || defined (STM32F103xE) || defined (STM32F103xG)
01255 /**
01256   * @brief  Enable ADC multimode and configure multimode parameters
01257   * @note   Possibility to update parameters on the fly:
01258   *         This function initializes multimode parameters, following  
01259   *         calls to this function can be used to reconfigure some parameters 
01260   *         of structure "ADC_MultiModeTypeDef" on the fly, without reseting 
01261   *         the ADCs (both ADCs of the common group).
01262   *         The setting of these parameters is conditioned to ADC state.
01263   *         For parameters constraints, see comments of structure 
01264   *         "ADC_MultiModeTypeDef".
01265   * @note   To change back configuration from multimode to single mode, ADC must
01266   *         be reset (using function HAL_ADC_Init() ).
01267   * @param  hadc: ADC handle
01268   * @param  multimode: Structure of ADC multimode configuration
01269   * @retval HAL status
01270   */
01271 HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)
01272 {
01273   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
01274   ADC_HandleTypeDef tmphadcSlave={0};
01275   
01276   /* Check the parameters */
01277   assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
01278   assert_param(IS_ADC_MODE(multimode->Mode));
01279   
01280   /* Process locked */
01281   __HAL_LOCK(hadc);
01282   
01283   /* Set a temporary handle of the ADC slave associated to the ADC master     */
01284   ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
01285   
01286   /* Parameters update conditioned to ADC state:                              */
01287   /* Parameters that can be updated when ADC is disabled or enabled without   */
01288   /* conversion on going on regular group:                                    */
01289   /*  - ADC master and ADC slave DMA configuration                            */
01290   /* Parameters that can be updated only when ADC is disabled:                */
01291   /*  - Multimode mode selection                                              */
01292   /* To optimize code, all multimode settings can be set when both ADCs of    */
01293   /* the common group are in state: disabled.                                 */
01294   if ((ADC_IS_ENABLE(hadc) == RESET)                     &&
01295       (ADC_IS_ENABLE(&tmphadcSlave) == RESET)            &&
01296       (IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance))   )
01297   {
01298     MODIFY_REG(hadc->Instance->CR1,
01299                ADC_CR1_DUALMOD    ,
01300                multimode->Mode     );
01301   }
01302   /* If one of the ADC sharing the same common group is enabled, no update    */
01303   /* could be done on neither of the multimode structure parameters.          */
01304   else
01305   {
01306     /* Update ADC state machine to error */
01307     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
01308     
01309     tmp_hal_status = HAL_ERROR;
01310   }
01311     
01312     
01313   /* Process unlocked */
01314   __HAL_UNLOCK(hadc);
01315   
01316   /* Return function status */
01317   return tmp_hal_status;
01318 } 
01319 #endif /* defined STM32F103x6 || defined STM32F103xB || defined STM32F105xC || defined STM32F107xC || defined STM32F103xE || defined STM32F103xG */
01320 /**
01321   * @}
01322   */  
01323 
01324 /**
01325   * @}
01326   */
01327 
01328 #endif /* HAL_ADC_MODULE_ENABLED */
01329 /**
01330   * @}
01331   */
01332 
01333 /**
01334   * @}
01335   */
01336 
01337 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/