LELEC2811 - I&S / Mbed OS LELEC2811_IKS01A3_Multisensors

Dependencies:   X_NUCLEO_IKS01A3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers adc.cpp Source File

adc.cpp

00001 /**
00002   ******************************************************************************
00003   * File Name          : adc.c
00004   * Description        : This file provides code for the configuration
00005   *                      of the ADC instances.
00006   ******************************************************************************
00007   ** This notice applies to any and all portions of this file
00008   * that are not between comment pairs USER CODE BEGIN and
00009   * USER CODE END. Other portions of this file, whether 
00010   * inserted by the user or by software development tools
00011   * are owned by their respective copyright owners.
00012   *
00013   * COPYRIGHT(c) 2019 STMicroelectronics
00014   *
00015   * Redistribution and use in source and binary forms, with or without modification,
00016   * are permitted provided that the following conditions are met:
00017   *   1. Redistributions of source code must retain the above copyright notice,
00018   *      this list of conditions and the following disclaimer.
00019   *   2. Redistributions in binary form must reproduce the above copyright notice,
00020   *      this list of conditions and the following disclaimer in the documentation
00021   *      and/or other materials provided with the distribution.
00022   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00023   *      may be used to endorse or promote products derived from this software
00024   *      without specific prior written permission.
00025   *
00026   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00029   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00030   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00031   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00032   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00033   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00034   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036   *
00037   ******************************************************************************
00038   */
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 #include "adc.h"
00042 #include "gpio.h"
00043 
00044 ADC_HandleTypeDef hadc;
00045 
00046 /* ADC init function */
00047 void MX_ADC_Init(void)
00048 {
00049     ADC_ChannelConfTypeDef sConfig;
00050 
00051     /* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
00052     hadc.Instance = ADC1;
00053 
00054     hadc.Init.OversamplingMode = DISABLE;
00055     hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_16;
00056     hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_4;
00057     hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
00058 
00059     /* ADC SAMPLING FREQUENCY = ((12.5+ADC_SAMPLETIME_xCYCLES_x)*ADC_CLK)/OVERSAMPLING_RATIO */
00060     hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
00061     hadc.Init.LowPowerAutoWait = DISABLE;
00062     hadc.Init.LowPowerFrequencyMode = DISABLE;
00063     hadc.Init.LowPowerAutoPowerOff = DISABLE;
00064 
00065     hadc.Init.Resolution = ADC_RESOLUTION_12B;
00066     hadc.Init.SamplingTime = ADC_SAMPLETIME_3CYCLES_5;
00067     hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
00068     hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
00069     hadc.Init.ContinuousConvMode = ENABLE;
00070     hadc.Init.DiscontinuousConvMode = DISABLE;
00071     hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
00072     hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
00073     hadc.Init.DMAContinuousRequests = DISABLE;
00074     hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
00075     hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
00076 
00077     if (HAL_ADC_Init(&hadc) != HAL_OK)
00078     {
00079         _Error_Handler(__FILE__, __LINE__);
00080     }
00081 
00082     /* Start calibration */
00083     if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK)
00084     {
00085         Error_Handler();
00086     }
00087 
00088     /* Configure for the selected ADC regular channel to be converted. */
00089     sConfig.Channel = ADC_CHANNEL_0;
00090 
00091     if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
00092     {
00093         _Error_Handler(__FILE__, __LINE__);
00094     }
00095 }
00096 
00097 void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
00098 {
00099   GPIO_InitTypeDef GPIO_InitStruct;
00100   if (adcHandle->Instance == ADC1)
00101   {
00102     /* ADC1 clock enable */
00103     __HAL_RCC_ADC1_CLK_ENABLE();
00104   
00105     /* ADC GPIO Configuration PA0 ------> ADC_IN0 */
00106     GPIO_InitStruct.Pin = GPIO_PIN_0;
00107     GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
00108     GPIO_InitStruct.Pull = GPIO_NOPULL;
00109     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
00110 
00111     /* Configure the NVIC */
00112     /* NVIC configuration for ADC EOC interrupt */
00113     HAL_NVIC_SetPriority(ADC1_COMP_IRQn, 0, 0);
00114     HAL_NVIC_EnableIRQ(ADC1_COMP_IRQn);
00115   }
00116 }
00117 
00118 void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
00119 {
00120   if(adcHandle->Instance == ADC1)
00121   {
00122     /* Peripheral clock disable */
00123     __HAL_RCC_ADC1_CLK_DISABLE();
00124   
00125     /* ADC GPIO Configuration PA0 ------> ADC_IN0 */
00126     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
00127 
00128     /* Configure the NVIC */
00129     /* NVIC configuration for ADC EOC interrupt */
00130     HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn);
00131   }
00132 }
00133 
00134 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/