Subversion Repositories DashDisplay

Rev

Rev 61 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32l1xx_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.   *             ++ Start, stop, get result of conversions of injected
  10.   *                group, using 2 possible modes: polling, interruption.
  11.   *           + Control functions
  12.   *             ++ Channels configuration on injected group
  13.   *          Other functions (generic functions) are available in file
  14.   *          "stm32l1xx_hal_adc.c".
  15.   *
  16.   @verbatim
  17.   [..]
  18.   (@) Sections "ADC peripheral features" and "How to use this driver" are
  19.       available in file of generic functions "stm32l1xx_hal_adc.c".
  20.   [..]
  21.   @endverbatim
  22.   ******************************************************************************
  23.   * @attention
  24.   *
  25.   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  26.   * All rights reserved.</center></h2>
  27.   *
  28.   * This software component is licensed by ST under BSD 3-Clause license,
  29.   * the "License"; You may not use this file except in compliance with the
  30.   * License. You may obtain a copy of the License at:
  31.   *                        opensource.org/licenses/BSD-3-Clause
  32.   *
  33.   ******************************************************************************
  34.   */
  35.  
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l1xx_hal.h"
  38.  
  39. /** @addtogroup STM32L1xx_HAL_Driver
  40.   * @{
  41.   */
  42.  
  43. /** @defgroup ADCEx ADCEx
  44.   * @brief ADC Extension HAL module driver
  45.   * @{
  46.   */
  47.  
  48. #ifdef HAL_ADC_MODULE_ENABLED
  49.  
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
  53.   * @{
  54.   */
  55.  
  56.   /* ADC conversion cycles (unit: ADC clock cycles)                           */
  57.   /* (selected sampling time + conversion time of 12 ADC clock cycles, with   */
  58.   /* resolution 12 bits)                                                      */
  59.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_4CYCLE5   ( 16U)
  60.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_9CYCLES   ( 21U)
  61.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_16CYCLES  ( 28U)
  62.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_24CYCLES  ( 36U)
  63.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_48CYCLES  ( 60U)
  64.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_96CYCLES  (108U)
  65.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_192CYCLES (204U)
  66.   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_384CYCLES (396U)
  67.  
  68.   /* Delay for temperature sensor stabilization time.                         */
  69.   /* Maximum delay is 10us (refer to device datasheet, parameter tSTART).     */
  70.   /* Unit: us                                                                 */
  71.   #define ADC_TEMPSENSOR_DELAY_US         (10U)
  72.  
  73. /**
  74.   * @}
  75.   */
  76.  
  77. /* Private macro -------------------------------------------------------------*/
  78. /* Private variables ---------------------------------------------------------*/
  79. /* Private function prototypes -----------------------------------------------*/
  80. /* Private functions ---------------------------------------------------------*/
  81.  
  82. /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
  83.   * @{
  84.   */
  85.  
  86. /** @defgroup ADCEx_Exported_Functions_Group1 ADC Extended IO operation functions
  87.  *  @brief    ADC Extended Input and Output operation functions
  88.  *
  89. @verbatim
  90.  ===============================================================================
  91.                       ##### IO operation functions #####
  92.  ===============================================================================
  93.     [..]  This section provides functions allowing to:
  94.       (+) Start conversion of injected group.
  95.       (+) Stop conversion of injected group.
  96.       (+) Poll for conversion complete on injected group.
  97.       (+) Get result of injected channel conversion.
  98.       (+) Start conversion of injected group and enable interruptions.
  99.       (+) Stop conversion of injected group and disable interruptions.
  100.      
  101. @endverbatim
  102.   * @{
  103.   */
  104.  
  105. /**
  106.   * @brief  Enables ADC, starts conversion of injected group.
  107.   *         Interruptions enabled in this function: None.
  108.   * @param  hadc ADC handle
  109.   * @retval HAL status
  110.   */
  111. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  112. {
  113.   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  114.  
  115.   /* Check the parameters */
  116.   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  117.  
  118.   /* Process locked */
  119.   __HAL_LOCK(hadc);
  120.    
  121.   /* Enable the ADC peripheral */
  122.   tmp_hal_status = ADC_Enable(hadc);
  123.  
  124.   /* Start conversion if ADC is effectively enabled */
  125.   if (tmp_hal_status == HAL_OK)
  126.   {
  127.     /* Set ADC state                                                          */
  128.     /* - Clear state bitfield related to injected group conversion results    */
  129.     /* - Set state bitfield related to injected operation                     */
  130.     ADC_STATE_CLR_SET(hadc->State,
  131.                       HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  132.                       HAL_ADC_STATE_INJ_BUSY);
  133.    
  134.     /* Check if a regular conversion is ongoing */
  135.     /* Note: On this device, there is no ADC error code fields related to     */
  136.     /*       conversions on group injected only. In case of conversion on     */
  137.     /*       going on group regular, no error code is reset.                  */
  138.     if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  139.     {
  140.       /* Reset ADC all error code fields */
  141.       ADC_CLEAR_ERRORCODE(hadc);
  142.     }
  143.    
  144.     /* Process unlocked */
  145.     /* Unlock before starting ADC conversions: in case of potential           */
  146.     /* interruption, to let the process to ADC IRQ Handler.                   */
  147.     __HAL_UNLOCK(hadc);
  148.    
  149.     /* Clear injected group conversion flag */
  150.     /* (To ensure of no unknown state from potential previous ADC operations) */
  151.     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  152.    
  153.     /* Enable conversion of injected group.                                   */
  154.     /* If software start has been selected, conversion starts immediately.    */
  155.     /* If external trigger has been selected, conversion will start at next   */
  156.     /* trigger event.                                                         */
  157.     /* If automatic injected conversion is enabled, conversion will start     */
  158.     /* after next regular group conversion.                                   */
  159.     if (ADC_IS_SOFTWARE_START_INJECTED(hadc)              &&
  160.         HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)  )
  161.     {
  162.       /* Enable ADC software conversion for injected channels */
  163.       SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
  164.     }
  165.   }
  166.  
  167.   /* Return function status */
  168.   return tmp_hal_status;
  169. }
  170.  
  171. /**
  172.   * @brief  Stop conversion of injected channels. Disable ADC peripheral if
  173.   *         no regular conversion is on going.
  174.   * @note   If ADC must be disabled and if conversion is on going on
  175.   *         regular group, function HAL_ADC_Stop must be used to stop both
  176.   *         injected and regular groups, and disable the ADC.
  177.   * @note   If injected group mode auto-injection is enabled,
  178.   *         function HAL_ADC_Stop must be used.
  179.   * @note   In case of auto-injection mode, HAL_ADC_Stop must be used.
  180.   * @param  hadc ADC handle
  181.   * @retval None
  182.   */
  183. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  184. {
  185.   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  186.  
  187.   /* Check the parameters */
  188.   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  189.  
  190.   /* Process locked */
  191.   __HAL_LOCK(hadc);
  192.    
  193.   /* Stop potential conversion and disable ADC peripheral                     */
  194.   /* Conditioned to:                                                          */
  195.   /* - No conversion on the other group (regular group) is intended to        */
  196.   /*   continue (injected and regular groups stop conversion and ADC disable  */
  197.   /*   are common)                                                            */
  198.   /* - In case of auto-injection mode, HAL_ADC_Stop must be used.             */
  199.   if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET)  &&
  200.      HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)   )
  201.   {
  202.     /* Stop potential conversion on going, on regular and injected groups */
  203.     /* Disable ADC peripheral */
  204.     tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  205.    
  206.     /* Check if ADC is effectively disabled */
  207.     if (tmp_hal_status == HAL_OK)
  208.     {
  209.       /* Set ADC state */
  210.       ADC_STATE_CLR_SET(hadc->State,
  211.                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  212.                         HAL_ADC_STATE_READY);
  213.     }
  214.   }
  215.   else
  216.   {
  217.     /* Update ADC state machine to error */
  218.     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  219.      
  220.     tmp_hal_status = HAL_ERROR;
  221.   }
  222.  
  223.   /* Process unlocked */
  224.   __HAL_UNLOCK(hadc);
  225.  
  226.   /* Return function status */
  227.   return tmp_hal_status;
  228. }
  229.  
  230. /**
  231.   * @brief  Wait for injected group conversion to be completed.
  232.   * @param  hadc ADC handle
  233.   * @param  Timeout Timeout value in millisecond.
  234.   * @retval HAL status
  235.   */
  236. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  237. {
  238.   uint32_t tickstart;
  239.  
  240.   /* Variables for polling in case of scan mode enabled and polling for each  */
  241.   /* conversion.                                                              */
  242.   /* Note: Variable "conversion_timeout_cpu_cycles" set to offset 28 CPU      */
  243.   /* cycles to compensate number of CPU cycles for processing of variable     */
  244.   /* "conversion_timeout_cpu_cycles_max"                                      */
  245.   uint32_t conversion_timeout_cpu_cycles = 28;
  246.   uint32_t conversion_timeout_cpu_cycles_max = 0;
  247.  
  248.   /* Check the parameters */
  249.   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  250.  
  251.   /* Get timeout */
  252.   tickstart = HAL_GetTick();  
  253.      
  254.   /* Polling for end of conversion: differentiation if single/sequence        */
  255.   /* conversion.                                                              */
  256.   /* For injected group, flag JEOC is set only at the end of the sequence,    */
  257.   /* not for each conversion within the sequence.                             */
  258.   /* If setting "EOCSelection" is set to poll for each single conversion,     */
  259.   /* management of polling depends on setting of injected group sequencer:    */
  260.   /*  - If single conversion for injected group (scan mode disabled or        */
  261.   /*    InjectedNbrOfConversion ==1), flag JEOC is used to determine the      */
  262.   /*    conversion completion.                                                */
  263.   /*  - If sequence conversion for injected group (scan mode enabled and      */
  264.   /*    InjectedNbrOfConversion >=2), flag JEOC is set only at the end of the */
  265.   /*    sequence.                                                             */
  266.   /*    To poll for each conversion, the maximum conversion time is computed  */
  267.   /*    from ADC conversion time (selected sampling time + conversion time of */
  268.   /*    12 ADC clock cycles) and APB2/ADC clock prescalers (depending on      */
  269.   /*    settings, conversion time range can vary from 8 to several thousands  */
  270.   /*    of CPU cycles).                                                       */
  271.  
  272.   /* Note: On STM32L1, setting "EOCSelection" is related to regular group     */
  273.   /*       only, by hardware. For compatibility with other STM32 devices,     */
  274.   /*       this setting is related also to injected group by software.        */
  275.   if (((hadc->Instance->JSQR & ADC_JSQR_JL) == RESET)  ||
  276.       (hadc->Init.EOCSelection != ADC_EOC_SINGLE_CONV)   )
  277.   {
  278.     /* Wait until End of Conversion flag is raised */
  279.     while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  280.     {
  281.       /* Check if timeout is disabled (set to infinite wait) */
  282.       if(Timeout != HAL_MAX_DELAY)
  283.       {
  284.         if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout))
  285.         {
  286.           /* New check to avoid false timeout detection in case of preemption */
  287.           if(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  288.           {
  289.             /* Update ADC state machine to timeout */
  290.             SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  291.  
  292.             /* Process unlocked */
  293.             __HAL_UNLOCK(hadc);
  294.  
  295.             return HAL_TIMEOUT;
  296.           }
  297.         }
  298.       }
  299.     }
  300.   }
  301.   else
  302.   {
  303.     /* Computation of CPU cycles corresponding to ADC conversion cycles.      */
  304.     /* Retrieve ADC clock prescaler and ADC maximum conversion cycles on all  */
  305.     /* channels.                                                              */
  306.     conversion_timeout_cpu_cycles_max = ADC_GET_CLOCK_PRESCALER_DECIMAL(hadc);
  307.     conversion_timeout_cpu_cycles_max *= ADC_CONVCYCLES_MAX_RANGE(hadc);
  308.  
  309.     /* Poll with maximum conversion time */
  310.     while(conversion_timeout_cpu_cycles < conversion_timeout_cpu_cycles_max)
  311.     {
  312.       /* Check if timeout is disabled (set to infinite wait) */
  313.       if(Timeout != HAL_MAX_DELAY)
  314.       {
  315.         if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout))
  316.         {
  317.           /* New check to avoid false timeout detection in case of preemption */
  318.           if(conversion_timeout_cpu_cycles < conversion_timeout_cpu_cycles_max)
  319.           {
  320.             /* Update ADC state machine to timeout */
  321.             SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  322.  
  323.             /* Process unlocked */
  324.             __HAL_UNLOCK(hadc);
  325.  
  326.             return HAL_TIMEOUT;
  327.           }
  328.         }
  329.       }
  330.       conversion_timeout_cpu_cycles ++;
  331.     }
  332.   }
  333.  
  334.   /* Clear end of conversion flag of injected group if low power feature      */
  335.   /* "Auto Wait" is disabled, to not interfere with this feature until data   */
  336.   /* register is read using function HAL_ADCEx_InjectedGetValue().            */
  337.   if (hadc->Init.LowPowerAutoWait == DISABLE)
  338.   {
  339.     /* Clear injected group conversion flag */
  340.     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC);
  341.   }
  342.  
  343.   /* Update ADC state machine */
  344.   SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  345.  
  346.   /* Determine whether any further conversion upcoming on group injected      */
  347.   /* by external trigger, continuous mode or scan sequence on going.          */
  348.   /* Note: On STM32L1, there is no independent flag of end of sequence.       */
  349.   /*       The test of scan sequence on going is done either with scan        */
  350.   /*       sequence disabled or with end of conversion flag set to            */
  351.   /*       of end of sequence.                                                */
  352.   if(ADC_IS_SOFTWARE_START_INJECTED(hadc)                    &&
  353.      (HAL_IS_BIT_CLR(hadc->Instance->JSQR, ADC_JSQR_JL)  ||
  354.       HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS)    ) &&
  355.      (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  356.       (ADC_IS_SOFTWARE_START_REGULAR(hadc)       &&
  357.       (hadc->Init.ContinuousConvMode == DISABLE)   )       )   )
  358.   {
  359.     /* Set ADC state */
  360.     CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  361.    
  362.     if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  363.     {
  364.       SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  365.     }
  366.   }
  367.  
  368.   /* Return ADC state */
  369.   return HAL_OK;
  370. }
  371.  
  372. /**
  373.   * @brief  Enables ADC, starts conversion of injected group with interruption.
  374.   *          - JEOC (end of conversion of injected group)
  375.   *         Each of these interruptions has its dedicated callback function.
  376.   * @param  hadc ADC handle
  377.   * @retval HAL status.
  378.   */
  379. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  380. {
  381.   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  382.  
  383.   /* Check the parameters */
  384.   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  385.  
  386.   /* Process locked */
  387.   __HAL_LOCK(hadc);
  388.    
  389.   /* Enable the ADC peripheral */
  390.   tmp_hal_status = ADC_Enable(hadc);
  391.  
  392.   /* Start conversion if ADC is effectively enabled */
  393.   if (tmp_hal_status == HAL_OK)
  394.   {
  395.     /* Set ADC state                                                          */
  396.     /* - Clear state bitfield related to injected group conversion results    */
  397.     /* - Set state bitfield related to injected operation                     */
  398.     ADC_STATE_CLR_SET(hadc->State,
  399.                       HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  400.                       HAL_ADC_STATE_INJ_BUSY);
  401.    
  402.     /* Check if a regular conversion is ongoing */
  403.     /* Note: On this device, there is no ADC error code fields related to     */
  404.     /*       conversions on group injected only. In case of conversion on     */
  405.     /*       going on group regular, no error code is reset.                  */
  406.     if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  407.     {
  408.       /* Reset ADC all error code fields */
  409.       ADC_CLEAR_ERRORCODE(hadc);
  410.     }
  411.    
  412.     /* Process unlocked */
  413.     /* Unlock before starting ADC conversions: in case of potential           */
  414.     /* interruption, to let the process to ADC IRQ Handler.                   */
  415.     __HAL_UNLOCK(hadc);
  416.    
  417.     /* Clear injected group conversion flag */
  418.     /* (To ensure of no unknown state from potential previous ADC operations) */
  419.     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  420.    
  421.     /* Enable end of conversion interrupt for injected channels */
  422.     __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  423.    
  424.     /* Enable conversion of injected group.                                   */
  425.     /* If software start has been selected, conversion starts immediately.    */
  426.     /* If external trigger has been selected, conversion will start at next   */
  427.     /* trigger event.                                                         */
  428.     /* If automatic injected conversion is enabled, conversion will start     */
  429.     /* after next regular group conversion.                                   */
  430.     if (ADC_IS_SOFTWARE_START_INJECTED(hadc)              &&
  431.         HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)  )
  432.     {
  433.       /* Enable ADC software conversion for injected channels */
  434.       SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
  435.     }
  436.   }
  437.  
  438.   /* Return function status */
  439.   return tmp_hal_status;
  440. }
  441.  
  442. /**
  443.   * @brief  Stop conversion of injected channels, disable interruption of
  444.   *         end-of-conversion. Disable ADC peripheral if no regular conversion
  445.   *         is on going.
  446.   * @note   If ADC must be disabled and if conversion is on going on
  447.   *         regular group, function HAL_ADC_Stop must be used to stop both
  448.   *         injected and regular groups, and disable the ADC.
  449.   * @note   If injected group mode auto-injection is enabled,
  450.   *         function HAL_ADC_Stop must be used.
  451.   * @param  hadc ADC handle
  452.   * @retval None
  453.   */
  454. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  455. {
  456.   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  457.  
  458.   /* Check the parameters */
  459.   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  460.  
  461.   /* Process locked */
  462.   __HAL_LOCK(hadc);
  463.    
  464.   /* Stop potential conversion and disable ADC peripheral                     */
  465.   /* Conditioned to:                                                          */
  466.   /* - No conversion on the other group (regular group) is intended to        */
  467.   /*   continue (injected and regular groups stop conversion and ADC disable  */
  468.   /*   are common)                                                            */
  469.   /* - In case of auto-injection mode, HAL_ADC_Stop must be used.             */
  470.   if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET)  &&
  471.      HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)   )
  472.   {
  473.     /* Stop potential conversion on going, on regular and injected groups */
  474.     /* Disable ADC peripheral */
  475.     tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  476.    
  477.     /* Check if ADC is effectively disabled */
  478.     if (tmp_hal_status == HAL_OK)
  479.     {
  480.       /* Disable ADC end of conversion interrupt for injected channels */
  481.       __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  482.      
  483.       /* Set ADC state */
  484.       ADC_STATE_CLR_SET(hadc->State,
  485.                         HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  486.                         HAL_ADC_STATE_READY);
  487.     }
  488.   }
  489.   else
  490.   {
  491.     /* Update ADC state machine to error */
  492.     SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  493.      
  494.     tmp_hal_status = HAL_ERROR;
  495.   }
  496.  
  497.   /* Process unlocked */
  498.   __HAL_UNLOCK(hadc);
  499.  
  500.   /* Return function status */
  501.   return tmp_hal_status;
  502. }
  503.  
  504. /**
  505.   * @brief  Get ADC injected group conversion result.
  506.   * @note   Reading register JDRx automatically clears ADC flag JEOC
  507.   *         (ADC group injected end of unitary conversion).
  508.   * @note   This function does not clear ADC flag JEOS
  509.   *         (ADC group injected end of sequence conversion)
  510.   *         Occurrence of flag JEOS rising:
  511.   *          - If sequencer is composed of 1 rank, flag JEOS is equivalent
  512.   *            to flag JEOC.
  513.   *          - If sequencer is composed of several ranks, during the scan
  514.   *            sequence flag JEOC only is raised, at the end of the scan sequence
  515.   *            both flags JEOC and EOS are raised.
  516.   *         Flag JEOS must not be cleared by this function because
  517.   *         it would not be compliant with low power features
  518.   *         (feature low power auto-wait, not available on all STM32 families).
  519.   *         To clear this flag, either use function:
  520.   *         in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  521.   *         model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  522.   *         or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  523.   * @param  hadc ADC handle
  524.   * @param  InjectedRank the converted ADC injected rank.
  525.   *          This parameter can be one of the following values:
  526.   *            @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
  527.   *            @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
  528.   *            @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
  529.   *            @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
  530.   * @retval ADC group injected conversion data
  531.   */
  532. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  533. {
  534.   uint32_t tmp_jdr = 0;
  535.  
  536.   /* Check the parameters */
  537.   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  538.   assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  539.  
  540.   /* Get ADC converted value */
  541.   switch(InjectedRank)
  542.   {  
  543.     case ADC_INJECTED_RANK_4:
  544.       tmp_jdr = hadc->Instance->JDR4;
  545.       break;
  546.     case ADC_INJECTED_RANK_3:
  547.       tmp_jdr = hadc->Instance->JDR3;
  548.       break;
  549.     case ADC_INJECTED_RANK_2:
  550.       tmp_jdr = hadc->Instance->JDR2;
  551.       break;
  552.     case ADC_INJECTED_RANK_1:
  553.     default:
  554.       tmp_jdr = hadc->Instance->JDR1;
  555.       break;
  556.   }
  557.  
  558.   /* Return ADC converted value */
  559.   return tmp_jdr;
  560. }
  561.  
  562. /**
  563.   * @brief  Injected conversion complete callback in non blocking mode
  564.   * @param  hadc ADC handle
  565.   * @retval None
  566.   */
  567. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
  568. {
  569.   /* Prevent unused argument(s) compilation warning */
  570.   UNUSED(hadc);
  571.  
  572.   /* NOTE : This function Should not be modified, when the callback is needed,
  573.             the HAL_ADCEx_InjectedConvCpltCallback could be implemented in the user file
  574.   */
  575. }
  576.  
  577. /**
  578.   * @}
  579.   */
  580.  
  581. /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
  582.   * @brief    ADC Extended Peripheral Control functions
  583.   *
  584. @verbatim  
  585.  ===============================================================================
  586.              ##### Peripheral Control functions #####
  587.  ===============================================================================  
  588.     [..]  This section provides functions allowing to:
  589.       (+) Configure channels on injected group
  590.      
  591. @endverbatim
  592.   * @{
  593.   */
  594.  
  595. /**
  596.   * @brief  Configures the ADC injected group and the selected channel to be
  597.   *         linked to the injected group.
  598.   * @note   Possibility to update parameters on the fly:
  599.   *         This function initializes injected group, following calls to this
  600.   *         function can be used to reconfigure some parameters of structure
  601.   *         "ADC_InjectionConfTypeDef" on the fly, without reseting the ADC.
  602.   *         The setting of these parameters is conditioned to ADC state:
  603.   *         this function must be called when ADC is not under conversion.
  604.   * @param  hadc ADC handle
  605.   * @param  sConfigInjected Structure of ADC injected group and ADC channel for
  606.   *         injected group.
  607.   * @retval None
  608.   */
  609. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  610. {  
  611.   HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  612.   __IO uint32_t wait_loop_index = 0;
  613.  
  614.   /* Check the parameters */
  615.   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  616.   assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  617.   assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  618.   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  619.   assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  620.   assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, sConfigInjected->InjectedOffset));
  621.  
  622.   if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  623.   {
  624.     assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  625.     assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  626.     assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  627.   }
  628.  
  629.   if(sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  630.   {
  631.     assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  632.   }
  633.  
  634.   /* Process locked */
  635.   __HAL_LOCK(hadc);
  636.  
  637.   /* Configuration of injected group sequencer:                               */
  638.   /* - if scan mode is disabled, injected channels sequence length is set to  */
  639.   /*   0x00: 1 channel converted (channel on regular rank 1)                  */
  640.   /*   Parameter "InjectedNbrOfConversion" is discarded.                      */
  641.   /*   Note: Scan mode is present by hardware on this device and, if          */
  642.   /*   disabled, discards automatically nb of conversions. Anyway, nb of      */
  643.   /*   conversions is forced to 0x00 for alignment over all STM32 devices.    */
  644.   /* - if scan mode is enabled, injected channels sequence length is set to   */
  645.   /*   parameter ""InjectedNbrOfConversion".                                  */
  646.   if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
  647.   {
  648.     if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  649.     {
  650.       /* Clear the old SQx bits for all injected ranks */
  651.         MODIFY_REG(hadc->Instance->JSQR                              ,
  652.                    ADC_JSQR_JL   |
  653.                    ADC_JSQR_JSQ4 |
  654.                    ADC_JSQR_JSQ3 |
  655.                    ADC_JSQR_JSQ2 |
  656.                    ADC_JSQR_JSQ1                                     ,
  657.                    ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  658.                                     ADC_INJECTED_RANK_1,
  659.                                     0x01)                             );
  660.     }
  661.     /* If another injected rank than rank1 was intended to be set, and could  */
  662.     /* not due to ScanConvMode disabled, error is reported.                   */
  663.     else
  664.     {
  665.       /* Update ADC state machine to error */
  666.       SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  667.        
  668.       tmp_hal_status = HAL_ERROR;
  669.     }
  670.   }
  671.   else
  672.   {  
  673.     /* Since injected channels rank conv. order depends on total number of   */
  674.     /* injected conversions, selected rank must be below or equal to total   */
  675.     /* number of injected conversions to be updated.                         */
  676.     if (sConfigInjected->InjectedRank <= sConfigInjected->InjectedNbrOfConversion)
  677.     {
  678.       /* Clear the old SQx bits for the selected rank */
  679.       /* Set the SQx bits for the selected rank */
  680.       MODIFY_REG(hadc->Instance->JSQR                                                     ,
  681.                  
  682.                  ADC_JSQR_JL                                               |
  683.                  ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,                        
  684.                                   sConfigInjected->InjectedRank,        
  685.                                   sConfigInjected->InjectedNbrOfConversion)               ,
  686.                  
  687.                  ADC_JSQR_JL_SHIFT(sConfigInjected->InjectedNbrOfConversion)             |
  688.                  ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,      
  689.                                                 sConfigInjected->InjectedRank,        
  690.                                                 sConfigInjected->InjectedNbrOfConversion)  );
  691.     }
  692.     else
  693.     {
  694.       /* Clear the old SQx bits for the selected rank */
  695.       MODIFY_REG(hadc->Instance->JSQR                                       ,
  696.                  
  697.                  ADC_JSQR_JL                                               |
  698.                  ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,                        
  699.                                   sConfigInjected->InjectedRank,        
  700.                                   sConfigInjected->InjectedNbrOfConversion) ,
  701.                  
  702.                  0x00000000                                                  );
  703.     }
  704.   }
  705.    
  706.   /* Enable external trigger if trigger selection is different of software    */
  707.   /* start.                                                                   */
  708.   /* Note: This configuration keeps the hardware feature of parameter         */
  709.   /*       ExternalTrigConvEdge "trigger edge none" equivalent to             */
  710.   /*       software start.                                                    */
  711.  
  712.   if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  713.   {    
  714.     MODIFY_REG(hadc->Instance->CR2                        ,
  715.                ADC_CR2_JEXTEN  |
  716.                ADC_CR2_JEXTSEL                            ,
  717.                sConfigInjected->ExternalTrigInjecConv    |
  718.                sConfigInjected->ExternalTrigInjecConvEdge  );
  719.   }
  720.   else
  721.   {
  722.     MODIFY_REG(hadc->Instance->CR2,
  723.                ADC_CR2_JEXTEN  |
  724.                ADC_CR2_JEXTSEL    ,
  725.                0x00000000          );
  726.   }
  727.  
  728.   /* Configuration of injected group                                          */
  729.   /* Parameters update conditioned to ADC state:                              */
  730.   /* Parameters that can be updated only when ADC is disabled:                */
  731.   /*  - Automatic injected conversion                                         */
  732.   /*  - Injected discontinuous mode                                           */
  733.   if ((ADC_IS_ENABLE(hadc) == RESET))
  734.   {
  735.     hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO   |
  736.                              ADC_CR1_JDISCEN  );
  737.    
  738.     /* Automatic injected conversion can be enabled if injected group         */
  739.     /* external triggers are disabled.                                        */
  740.     if (sConfigInjected->AutoInjectedConv == ENABLE)
  741.     {
  742.       if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  743.       {
  744.         SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
  745.       }
  746.       else
  747.       {
  748.         /* Update ADC state machine to error */
  749.         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  750.        
  751.         tmp_hal_status = HAL_ERROR;
  752.       }
  753.     }
  754.    
  755.     /* Injected discontinuous can be enabled only if auto-injected mode is    */
  756.     /* disabled.                                                              */  
  757.     if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
  758.     {
  759.       if (sConfigInjected->AutoInjectedConv == DISABLE)
  760.       {
  761.         SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
  762.       }
  763.       else
  764.       {
  765.         /* Update ADC state machine to error */
  766.         SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  767.        
  768.         tmp_hal_status = HAL_ERROR;
  769.       }
  770.     }
  771.   }
  772.  
  773.   /* Channel sampling time configuration */
  774.   /* For InjectedChannels 0 to 9 */
  775.   if (sConfigInjected->InjectedChannel < ADC_CHANNEL_10)
  776.   {
  777.     MODIFY_REG(hadc->Instance->SMPR3,
  778.                ADC_SMPR3(ADC_SMPR3_SMP0, sConfigInjected->InjectedChannel),
  779.                ADC_SMPR3(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  780.   }
  781.   /* For InjectedChannels 10 to 19 */
  782.   else if (sConfigInjected->InjectedChannel < ADC_CHANNEL_20)
  783.   {
  784.     MODIFY_REG(hadc->Instance->SMPR2,
  785.                ADC_SMPR2(ADC_SMPR2_SMP10, sConfigInjected->InjectedChannel),
  786.                ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  787.   }
  788.   /* For InjectedChannels 20 to 26 for devices Cat.1, Cat.2, Cat.3 */
  789.   /* For InjectedChannels 20 to 29 for devices Cat4, Cat.5 */
  790.   else if (sConfigInjected->InjectedChannel <= ADC_SMPR1_CHANNEL_MAX)
  791.   {  
  792.     MODIFY_REG(hadc->Instance->SMPR1,
  793.                ADC_SMPR1(ADC_SMPR1_SMP20, sConfigInjected->InjectedChannel),
  794.                ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  795.   }
  796.   /* For InjectedChannels 30 to 31 for devices Cat4, Cat.5 */
  797.   else
  798.   {
  799.     ADC_SMPR0_CHANNEL_SET(hadc, sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
  800.   }
  801.  
  802.  
  803.   /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
  804.   switch(sConfigInjected->InjectedRank)
  805.   {
  806.     case 1:
  807.       /* Set injected channel 1 offset */
  808.       MODIFY_REG(hadc->Instance->JOFR1,
  809.                  ADC_JOFR1_JOFFSET1,
  810.                  sConfigInjected->InjectedOffset);
  811.       break;
  812.     case 2:
  813.       /* Set injected channel 2 offset */
  814.       MODIFY_REG(hadc->Instance->JOFR2,
  815.                  ADC_JOFR2_JOFFSET2,
  816.                  sConfigInjected->InjectedOffset);
  817.       break;
  818.     case 3:
  819.       /* Set injected channel 3 offset */
  820.       MODIFY_REG(hadc->Instance->JOFR3,
  821.                  ADC_JOFR3_JOFFSET3,
  822.                  sConfigInjected->InjectedOffset);
  823.       break;
  824.     case 4:
  825.     default:
  826.       MODIFY_REG(hadc->Instance->JOFR4,
  827.                  ADC_JOFR4_JOFFSET4,
  828.                  sConfigInjected->InjectedOffset);
  829.       break;
  830.   }
  831.  
  832.   /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor  */
  833.   /* and VREFINT measurement path.                                            */
  834.   if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
  835.       (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)      )
  836.   {
  837.     SET_BIT(ADC->CCR, ADC_CCR_TSVREFE);
  838.    
  839.     if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR))
  840.     {
  841.       /* Delay for temperature sensor stabilization time */
  842.       /* Compute number of CPU cycles to wait for */
  843.       wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000));
  844.       while(wait_loop_index != 0)
  845.       {
  846.         wait_loop_index--;
  847.       }
  848.     }
  849.   }
  850.  
  851.   /* Process unlocked */
  852.   __HAL_UNLOCK(hadc);
  853.  
  854.   /* Return function status */
  855.   return tmp_hal_status;
  856. }
  857.  
  858. /**
  859.   * @}
  860.   */  
  861.  
  862. /**
  863.   * @}
  864.   */
  865.  
  866. #endif /* HAL_ADC_MODULE_ENABLED */
  867. /**
  868.   * @}
  869.   */
  870.  
  871. /**
  872.   * @}
  873.   */
  874.  
  875. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  876.