2019-2020 Multisensor project using the X_NUCLEO_IKS01A3 sensor platform
Dependencies: X_NUCLEO_IKS01A3
adc.cpp@19:35f7b17d4bb5, 2019-11-26 (annotated)
- Committer:
- martlefebvre94
- Date:
- Tue Nov 26 11:45:01 2019 +0000
- Revision:
- 19:35f7b17d4bb5
- Parent:
- 18:17f5509ac69b
Code cleaned
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martlefebvre94 | 18:17f5509ac69b | 1 | /** |
martlefebvre94 | 18:17f5509ac69b | 2 | ****************************************************************************** |
martlefebvre94 | 18:17f5509ac69b | 3 | * File Name : adc.c |
martlefebvre94 | 18:17f5509ac69b | 4 | * Description : This file provides code for the configuration |
martlefebvre94 | 18:17f5509ac69b | 5 | * of the ADC instances. |
martlefebvre94 | 18:17f5509ac69b | 6 | ****************************************************************************** |
martlefebvre94 | 18:17f5509ac69b | 7 | ** This notice applies to any and all portions of this file |
martlefebvre94 | 18:17f5509ac69b | 8 | * that are not between comment pairs USER CODE BEGIN and |
martlefebvre94 | 18:17f5509ac69b | 9 | * USER CODE END. Other portions of this file, whether |
martlefebvre94 | 18:17f5509ac69b | 10 | * inserted by the user or by software development tools |
martlefebvre94 | 18:17f5509ac69b | 11 | * are owned by their respective copyright owners. |
martlefebvre94 | 18:17f5509ac69b | 12 | * |
martlefebvre94 | 18:17f5509ac69b | 13 | * COPYRIGHT(c) 2019 STMicroelectronics |
martlefebvre94 | 18:17f5509ac69b | 14 | * |
martlefebvre94 | 18:17f5509ac69b | 15 | * Redistribution and use in source and binary forms, with or without modification, |
martlefebvre94 | 18:17f5509ac69b | 16 | * are permitted provided that the following conditions are met: |
martlefebvre94 | 18:17f5509ac69b | 17 | * 1. Redistributions of source code must retain the above copyright notice, |
martlefebvre94 | 18:17f5509ac69b | 18 | * this list of conditions and the following disclaimer. |
martlefebvre94 | 18:17f5509ac69b | 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
martlefebvre94 | 18:17f5509ac69b | 20 | * this list of conditions and the following disclaimer in the documentation |
martlefebvre94 | 18:17f5509ac69b | 21 | * and/or other materials provided with the distribution. |
martlefebvre94 | 18:17f5509ac69b | 22 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
martlefebvre94 | 18:17f5509ac69b | 23 | * may be used to endorse or promote products derived from this software |
martlefebvre94 | 18:17f5509ac69b | 24 | * without specific prior written permission. |
martlefebvre94 | 18:17f5509ac69b | 25 | * |
martlefebvre94 | 18:17f5509ac69b | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
martlefebvre94 | 18:17f5509ac69b | 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
martlefebvre94 | 18:17f5509ac69b | 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
martlefebvre94 | 18:17f5509ac69b | 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
martlefebvre94 | 18:17f5509ac69b | 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
martlefebvre94 | 18:17f5509ac69b | 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
martlefebvre94 | 18:17f5509ac69b | 32 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
martlefebvre94 | 18:17f5509ac69b | 33 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
martlefebvre94 | 18:17f5509ac69b | 34 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
martlefebvre94 | 18:17f5509ac69b | 35 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
martlefebvre94 | 18:17f5509ac69b | 36 | * |
martlefebvre94 | 18:17f5509ac69b | 37 | ****************************************************************************** |
martlefebvre94 | 18:17f5509ac69b | 38 | */ |
martlefebvre94 | 18:17f5509ac69b | 39 | |
martlefebvre94 | 18:17f5509ac69b | 40 | /* Includes ------------------------------------------------------------------*/ |
martlefebvre94 | 18:17f5509ac69b | 41 | #include "adc.h" |
martlefebvre94 | 18:17f5509ac69b | 42 | #include "gpio.h" |
martlefebvre94 | 18:17f5509ac69b | 43 | |
martlefebvre94 | 18:17f5509ac69b | 44 | ADC_HandleTypeDef hadc; |
martlefebvre94 | 18:17f5509ac69b | 45 | |
martlefebvre94 | 18:17f5509ac69b | 46 | /* ADC init function */ |
martlefebvre94 | 18:17f5509ac69b | 47 | void MX_ADC_Init(void) |
martlefebvre94 | 18:17f5509ac69b | 48 | { |
martlefebvre94 | 18:17f5509ac69b | 49 | ADC_ChannelConfTypeDef sConfig; |
martlefebvre94 | 18:17f5509ac69b | 50 | |
martlefebvre94 | 18:17f5509ac69b | 51 | /* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ |
martlefebvre94 | 18:17f5509ac69b | 52 | hadc.Instance = ADC1; |
martlefebvre94 | 18:17f5509ac69b | 53 | |
martlefebvre94 | 18:17f5509ac69b | 54 | hadc.Init.OversamplingMode = DISABLE; |
martlefebvre94 | 18:17f5509ac69b | 55 | hadc.Init.Oversample.Ratio = ADC_OVERSAMPLING_RATIO_16; |
martlefebvre94 | 18:17f5509ac69b | 56 | hadc.Init.Oversample.RightBitShift = ADC_RIGHTBITSHIFT_4; |
martlefebvre94 | 18:17f5509ac69b | 57 | hadc.Init.Oversample.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER; |
martlefebvre94 | 18:17f5509ac69b | 58 | |
martlefebvre94 | 18:17f5509ac69b | 59 | /* ADC SAMPLING FREQUENCY = ((12.5+ADC_SAMPLETIME_xCYCLES_x)*ADC_CLK)/OVERSAMPLING_RATIO */ |
martlefebvre94 | 18:17f5509ac69b | 60 | hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1; |
martlefebvre94 | 18:17f5509ac69b | 61 | hadc.Init.LowPowerAutoWait = DISABLE; |
martlefebvre94 | 18:17f5509ac69b | 62 | hadc.Init.LowPowerFrequencyMode = DISABLE; |
martlefebvre94 | 18:17f5509ac69b | 63 | hadc.Init.LowPowerAutoPowerOff = DISABLE; |
martlefebvre94 | 18:17f5509ac69b | 64 | |
martlefebvre94 | 18:17f5509ac69b | 65 | hadc.Init.Resolution = ADC_RESOLUTION_12B; |
martlefebvre94 | 18:17f5509ac69b | 66 | hadc.Init.SamplingTime = ADC_SAMPLETIME_3CYCLES_5; |
martlefebvre94 | 18:17f5509ac69b | 67 | hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; |
martlefebvre94 | 18:17f5509ac69b | 68 | hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
martlefebvre94 | 18:17f5509ac69b | 69 | hadc.Init.ContinuousConvMode = ENABLE; |
martlefebvre94 | 18:17f5509ac69b | 70 | hadc.Init.DiscontinuousConvMode = DISABLE; |
martlefebvre94 | 18:17f5509ac69b | 71 | hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
martlefebvre94 | 18:17f5509ac69b | 72 | hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; |
martlefebvre94 | 18:17f5509ac69b | 73 | hadc.Init.DMAContinuousRequests = DISABLE; |
martlefebvre94 | 18:17f5509ac69b | 74 | hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV; |
martlefebvre94 | 18:17f5509ac69b | 75 | hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED; |
martlefebvre94 | 18:17f5509ac69b | 76 | |
martlefebvre94 | 18:17f5509ac69b | 77 | if (HAL_ADC_Init(&hadc) != HAL_OK) |
martlefebvre94 | 18:17f5509ac69b | 78 | { |
martlefebvre94 | 18:17f5509ac69b | 79 | _Error_Handler(__FILE__, __LINE__); |
martlefebvre94 | 18:17f5509ac69b | 80 | } |
martlefebvre94 | 18:17f5509ac69b | 81 | |
martlefebvre94 | 18:17f5509ac69b | 82 | /* Start calibration */ |
martlefebvre94 | 18:17f5509ac69b | 83 | if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK) |
martlefebvre94 | 18:17f5509ac69b | 84 | { |
martlefebvre94 | 18:17f5509ac69b | 85 | Error_Handler(); |
martlefebvre94 | 18:17f5509ac69b | 86 | } |
martlefebvre94 | 18:17f5509ac69b | 87 | |
martlefebvre94 | 18:17f5509ac69b | 88 | /* Configure for the selected ADC regular channel to be converted. */ |
martlefebvre94 | 18:17f5509ac69b | 89 | sConfig.Channel = ADC_CHANNEL_0; |
martlefebvre94 | 18:17f5509ac69b | 90 | |
martlefebvre94 | 18:17f5509ac69b | 91 | if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) |
martlefebvre94 | 18:17f5509ac69b | 92 | { |
martlefebvre94 | 18:17f5509ac69b | 93 | _Error_Handler(__FILE__, __LINE__); |
martlefebvre94 | 18:17f5509ac69b | 94 | } |
martlefebvre94 | 18:17f5509ac69b | 95 | } |
martlefebvre94 | 18:17f5509ac69b | 96 | |
martlefebvre94 | 18:17f5509ac69b | 97 | void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) |
martlefebvre94 | 18:17f5509ac69b | 98 | { |
martlefebvre94 | 18:17f5509ac69b | 99 | GPIO_InitTypeDef GPIO_InitStruct; |
martlefebvre94 | 18:17f5509ac69b | 100 | if (adcHandle->Instance == ADC1) |
martlefebvre94 | 18:17f5509ac69b | 101 | { |
martlefebvre94 | 18:17f5509ac69b | 102 | /* ADC1 clock enable */ |
martlefebvre94 | 18:17f5509ac69b | 103 | __HAL_RCC_ADC1_CLK_ENABLE(); |
martlefebvre94 | 18:17f5509ac69b | 104 | |
martlefebvre94 | 18:17f5509ac69b | 105 | /* ADC GPIO Configuration PA0 ------> ADC_IN0 */ |
martlefebvre94 | 18:17f5509ac69b | 106 | GPIO_InitStruct.Pin = GPIO_PIN_0; |
martlefebvre94 | 18:17f5509ac69b | 107 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
martlefebvre94 | 18:17f5509ac69b | 108 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
martlefebvre94 | 18:17f5509ac69b | 109 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
martlefebvre94 | 18:17f5509ac69b | 110 | |
martlefebvre94 | 18:17f5509ac69b | 111 | /* Configure the NVIC */ |
martlefebvre94 | 18:17f5509ac69b | 112 | /* NVIC configuration for ADC EOC interrupt */ |
martlefebvre94 | 18:17f5509ac69b | 113 | HAL_NVIC_SetPriority(ADC1_COMP_IRQn, 0, 0); |
martlefebvre94 | 18:17f5509ac69b | 114 | HAL_NVIC_EnableIRQ(ADC1_COMP_IRQn); |
martlefebvre94 | 18:17f5509ac69b | 115 | } |
martlefebvre94 | 18:17f5509ac69b | 116 | } |
martlefebvre94 | 18:17f5509ac69b | 117 | |
martlefebvre94 | 18:17f5509ac69b | 118 | void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) |
martlefebvre94 | 18:17f5509ac69b | 119 | { |
martlefebvre94 | 18:17f5509ac69b | 120 | if(adcHandle->Instance == ADC1) |
martlefebvre94 | 18:17f5509ac69b | 121 | { |
martlefebvre94 | 18:17f5509ac69b | 122 | /* Peripheral clock disable */ |
martlefebvre94 | 18:17f5509ac69b | 123 | __HAL_RCC_ADC1_CLK_DISABLE(); |
martlefebvre94 | 18:17f5509ac69b | 124 | |
martlefebvre94 | 18:17f5509ac69b | 125 | /* ADC GPIO Configuration PA0 ------> ADC_IN0 */ |
martlefebvre94 | 18:17f5509ac69b | 126 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0); |
martlefebvre94 | 18:17f5509ac69b | 127 | |
martlefebvre94 | 18:17f5509ac69b | 128 | /* Configure the NVIC */ |
martlefebvre94 | 18:17f5509ac69b | 129 | /* NVIC configuration for ADC EOC interrupt */ |
martlefebvre94 | 18:17f5509ac69b | 130 | HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn); |
martlefebvre94 | 18:17f5509ac69b | 131 | } |
martlefebvre94 | 18:17f5509ac69b | 132 | } |
martlefebvre94 | 18:17f5509ac69b | 133 | |
martlefebvre94 | 18:17f5509ac69b | 134 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |