inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 2 * Copyright (c) 2016, STMicroelectronics
NYX 0:85b3fd62ea1a 3 * All rights reserved.
NYX 0:85b3fd62ea1a 4 *
NYX 0:85b3fd62ea1a 5 * Redistribution and use in source and binary forms, with or without
NYX 0:85b3fd62ea1a 6 * modification, are permitted provided that the following conditions are met:
NYX 0:85b3fd62ea1a 7 *
NYX 0:85b3fd62ea1a 8 * 1. Redistributions of source code must retain the above copyright notice,
NYX 0:85b3fd62ea1a 9 * this list of conditions and the following disclaimer.
NYX 0:85b3fd62ea1a 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
NYX 0:85b3fd62ea1a 11 * this list of conditions and the following disclaimer in the documentation
NYX 0:85b3fd62ea1a 12 * and/or other materials provided with the distribution.
NYX 0:85b3fd62ea1a 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
NYX 0:85b3fd62ea1a 14 * may be used to endorse or promote products derived from this software
NYX 0:85b3fd62ea1a 15 * without specific prior written permission.
NYX 0:85b3fd62ea1a 16 *
NYX 0:85b3fd62ea1a 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
NYX 0:85b3fd62ea1a 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
NYX 0:85b3fd62ea1a 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
NYX 0:85b3fd62ea1a 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
NYX 0:85b3fd62ea1a 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
NYX 0:85b3fd62ea1a 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
NYX 0:85b3fd62ea1a 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
NYX 0:85b3fd62ea1a 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
NYX 0:85b3fd62ea1a 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
NYX 0:85b3fd62ea1a 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
NYX 0:85b3fd62ea1a 27 */
NYX 0:85b3fd62ea1a 28 #include "mbed_assert.h"
NYX 0:85b3fd62ea1a 29 #include "analogin_api.h"
NYX 0:85b3fd62ea1a 30
NYX 0:85b3fd62ea1a 31 #if DEVICE_ANALOGIN
NYX 0:85b3fd62ea1a 32
NYX 0:85b3fd62ea1a 33 #include "mbed_wait_api.h"
NYX 0:85b3fd62ea1a 34 #include "cmsis.h"
NYX 0:85b3fd62ea1a 35 #include "pinmap.h"
NYX 0:85b3fd62ea1a 36 #include "mbed_error.h"
NYX 0:85b3fd62ea1a 37 #include "PeripheralPins.h"
NYX 0:85b3fd62ea1a 38
NYX 0:85b3fd62ea1a 39 void analogin_init(analogin_t *obj, PinName pin)
NYX 0:85b3fd62ea1a 40 {
NYX 0:85b3fd62ea1a 41 uint32_t function = (uint32_t)NC;
NYX 0:85b3fd62ea1a 42
NYX 0:85b3fd62ea1a 43 #if defined(ADC1)
NYX 0:85b3fd62ea1a 44 static int adc1_inited = 0;
NYX 0:85b3fd62ea1a 45 #endif
NYX 0:85b3fd62ea1a 46 #if defined(ADC2)
NYX 0:85b3fd62ea1a 47 static int adc2_inited = 0;
NYX 0:85b3fd62ea1a 48 #endif
NYX 0:85b3fd62ea1a 49 #if defined(ADC3)
NYX 0:85b3fd62ea1a 50 static int adc3_inited = 0;
NYX 0:85b3fd62ea1a 51 #endif
NYX 0:85b3fd62ea1a 52 // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
NYX 0:85b3fd62ea1a 53 // are described in PinNames.h and PeripheralPins.c
NYX 0:85b3fd62ea1a 54 // Pin value must be between 0xF0 and 0xFF
NYX 0:85b3fd62ea1a 55 if ((pin < 0xF0) || (pin >= 0x100)) {
NYX 0:85b3fd62ea1a 56 // Normal channels
NYX 0:85b3fd62ea1a 57 // Get the peripheral name from the pin and assign it to the object
NYX 0:85b3fd62ea1a 58 obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
NYX 0:85b3fd62ea1a 59 // Get the functions (adc channel) from the pin and assign it to the object
NYX 0:85b3fd62ea1a 60 function = pinmap_function(pin, PinMap_ADC);
NYX 0:85b3fd62ea1a 61 // Configure GPIO
NYX 0:85b3fd62ea1a 62 pinmap_pinout(pin, PinMap_ADC);
NYX 0:85b3fd62ea1a 63 } else {
NYX 0:85b3fd62ea1a 64 // Internal channels
NYX 0:85b3fd62ea1a 65 obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal);
NYX 0:85b3fd62ea1a 66 function = pinmap_function(pin, PinMap_ADC_Internal);
NYX 0:85b3fd62ea1a 67 // No GPIO configuration for internal channels
NYX 0:85b3fd62ea1a 68 }
NYX 0:85b3fd62ea1a 69 MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
NYX 0:85b3fd62ea1a 70 MBED_ASSERT(function != (uint32_t)NC);
NYX 0:85b3fd62ea1a 71
NYX 0:85b3fd62ea1a 72 obj->channel = STM_PIN_CHANNEL(function);
NYX 0:85b3fd62ea1a 73
NYX 0:85b3fd62ea1a 74 // Save pin number for the read function
NYX 0:85b3fd62ea1a 75 obj->pin = pin;
NYX 0:85b3fd62ea1a 76
NYX 0:85b3fd62ea1a 77 // Check if ADC is already initialized
NYX 0:85b3fd62ea1a 78 // Enable ADC clock
NYX 0:85b3fd62ea1a 79 #if defined(ADC1)
NYX 0:85b3fd62ea1a 80 if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
NYX 0:85b3fd62ea1a 81 if ((ADCName)obj->handle.Instance == ADC_1) {
NYX 0:85b3fd62ea1a 82 __HAL_RCC_ADC1_CLK_ENABLE();
NYX 0:85b3fd62ea1a 83 adc1_inited = 1;
NYX 0:85b3fd62ea1a 84 }
NYX 0:85b3fd62ea1a 85 #endif
NYX 0:85b3fd62ea1a 86 #if defined(ADC2)
NYX 0:85b3fd62ea1a 87 if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return;
NYX 0:85b3fd62ea1a 88 if ((ADCName)obj->handle.Instance == ADC_2) {
NYX 0:85b3fd62ea1a 89 __HAL_RCC_ADC2_CLK_ENABLE();
NYX 0:85b3fd62ea1a 90 adc2_inited = 1;
NYX 0:85b3fd62ea1a 91 }
NYX 0:85b3fd62ea1a 92 #endif
NYX 0:85b3fd62ea1a 93 #if defined(ADC3)
NYX 0:85b3fd62ea1a 94 if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return;
NYX 0:85b3fd62ea1a 95 if ((ADCName)obj->handle.Instance == ADC_3) {
NYX 0:85b3fd62ea1a 96 __HAL_RCC_ADC3_CLK_ENABLE();
NYX 0:85b3fd62ea1a 97 adc3_inited = 1;
NYX 0:85b3fd62ea1a 98 }
NYX 0:85b3fd62ea1a 99 #endif
NYX 0:85b3fd62ea1a 100 // Configure ADC
NYX 0:85b3fd62ea1a 101 obj->handle.State = HAL_ADC_STATE_RESET;
NYX 0:85b3fd62ea1a 102 obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
NYX 0:85b3fd62ea1a 103 obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
NYX 0:85b3fd62ea1a 104 obj->handle.Init.ScanConvMode = DISABLE;
NYX 0:85b3fd62ea1a 105 obj->handle.Init.ContinuousConvMode = DISABLE;
NYX 0:85b3fd62ea1a 106 obj->handle.Init.DiscontinuousConvMode = DISABLE;
NYX 0:85b3fd62ea1a 107 obj->handle.Init.NbrOfDiscConversion = 0;
NYX 0:85b3fd62ea1a 108 obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
NYX 0:85b3fd62ea1a 109 obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
NYX 0:85b3fd62ea1a 110 obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
NYX 0:85b3fd62ea1a 111 obj->handle.Init.NbrOfConversion = 1;
NYX 0:85b3fd62ea1a 112 obj->handle.Init.DMAContinuousRequests = DISABLE;
NYX 0:85b3fd62ea1a 113 obj->handle.Init.EOCSelection = DISABLE;
NYX 0:85b3fd62ea1a 114
NYX 0:85b3fd62ea1a 115 if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
NYX 0:85b3fd62ea1a 116 error("Cannot initialize ADC\n");
NYX 0:85b3fd62ea1a 117 }
NYX 0:85b3fd62ea1a 118 }
NYX 0:85b3fd62ea1a 119
NYX 0:85b3fd62ea1a 120 static inline uint16_t adc_read(analogin_t *obj)
NYX 0:85b3fd62ea1a 121 {
NYX 0:85b3fd62ea1a 122 ADC_ChannelConfTypeDef sConfig = {0};
NYX 0:85b3fd62ea1a 123
NYX 0:85b3fd62ea1a 124 // Configure ADC channel
NYX 0:85b3fd62ea1a 125 sConfig.Rank = 1;
NYX 0:85b3fd62ea1a 126 sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
NYX 0:85b3fd62ea1a 127 sConfig.Offset = 0;
NYX 0:85b3fd62ea1a 128
NYX 0:85b3fd62ea1a 129 switch (obj->channel) {
NYX 0:85b3fd62ea1a 130 case 0:
NYX 0:85b3fd62ea1a 131 sConfig.Channel = ADC_CHANNEL_0;
NYX 0:85b3fd62ea1a 132 break;
NYX 0:85b3fd62ea1a 133 case 1:
NYX 0:85b3fd62ea1a 134 sConfig.Channel = ADC_CHANNEL_1;
NYX 0:85b3fd62ea1a 135 break;
NYX 0:85b3fd62ea1a 136 case 2:
NYX 0:85b3fd62ea1a 137 sConfig.Channel = ADC_CHANNEL_2;
NYX 0:85b3fd62ea1a 138 break;
NYX 0:85b3fd62ea1a 139 case 3:
NYX 0:85b3fd62ea1a 140 sConfig.Channel = ADC_CHANNEL_3;
NYX 0:85b3fd62ea1a 141 break;
NYX 0:85b3fd62ea1a 142 case 4:
NYX 0:85b3fd62ea1a 143 sConfig.Channel = ADC_CHANNEL_4;
NYX 0:85b3fd62ea1a 144 break;
NYX 0:85b3fd62ea1a 145 case 5:
NYX 0:85b3fd62ea1a 146 sConfig.Channel = ADC_CHANNEL_5;
NYX 0:85b3fd62ea1a 147 break;
NYX 0:85b3fd62ea1a 148 case 6:
NYX 0:85b3fd62ea1a 149 sConfig.Channel = ADC_CHANNEL_6;
NYX 0:85b3fd62ea1a 150 break;
NYX 0:85b3fd62ea1a 151 case 7:
NYX 0:85b3fd62ea1a 152 sConfig.Channel = ADC_CHANNEL_7;
NYX 0:85b3fd62ea1a 153 break;
NYX 0:85b3fd62ea1a 154 case 8:
NYX 0:85b3fd62ea1a 155 sConfig.Channel = ADC_CHANNEL_8;
NYX 0:85b3fd62ea1a 156 break;
NYX 0:85b3fd62ea1a 157 case 9:
NYX 0:85b3fd62ea1a 158 sConfig.Channel = ADC_CHANNEL_9;
NYX 0:85b3fd62ea1a 159 break;
NYX 0:85b3fd62ea1a 160 case 10:
NYX 0:85b3fd62ea1a 161 sConfig.Channel = ADC_CHANNEL_10;
NYX 0:85b3fd62ea1a 162 break;
NYX 0:85b3fd62ea1a 163 case 11:
NYX 0:85b3fd62ea1a 164 sConfig.Channel = ADC_CHANNEL_11;
NYX 0:85b3fd62ea1a 165 break;
NYX 0:85b3fd62ea1a 166 case 12:
NYX 0:85b3fd62ea1a 167 sConfig.Channel = ADC_CHANNEL_12;
NYX 0:85b3fd62ea1a 168 break;
NYX 0:85b3fd62ea1a 169 case 13:
NYX 0:85b3fd62ea1a 170 sConfig.Channel = ADC_CHANNEL_13;
NYX 0:85b3fd62ea1a 171 break;
NYX 0:85b3fd62ea1a 172 case 14:
NYX 0:85b3fd62ea1a 173 sConfig.Channel = ADC_CHANNEL_14;
NYX 0:85b3fd62ea1a 174 break;
NYX 0:85b3fd62ea1a 175 case 15:
NYX 0:85b3fd62ea1a 176 sConfig.Channel = ADC_CHANNEL_15;
NYX 0:85b3fd62ea1a 177 break;
NYX 0:85b3fd62ea1a 178 case 16:
NYX 0:85b3fd62ea1a 179 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
NYX 0:85b3fd62ea1a 180 break;
NYX 0:85b3fd62ea1a 181 case 17:
NYX 0:85b3fd62ea1a 182 sConfig.Channel = ADC_CHANNEL_VREFINT;
NYX 0:85b3fd62ea1a 183 /* From experiment, measurement needs max sampling time to be valid */
NYX 0:85b3fd62ea1a 184 sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
NYX 0:85b3fd62ea1a 185 break;
NYX 0:85b3fd62ea1a 186 case 18:
NYX 0:85b3fd62ea1a 187 sConfig.Channel = ADC_CHANNEL_VBAT;
NYX 0:85b3fd62ea1a 188 /* From experiment, measurement needs max sampling time to be valid */
NYX 0:85b3fd62ea1a 189 sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
NYX 0:85b3fd62ea1a 190 break;
NYX 0:85b3fd62ea1a 191 default:
NYX 0:85b3fd62ea1a 192 return 0;
NYX 0:85b3fd62ea1a 193 }
NYX 0:85b3fd62ea1a 194
NYX 0:85b3fd62ea1a 195 // Measuring VBAT sets the ADC_CCR_VBATE bit in ADC->CCR, and there is not
NYX 0:85b3fd62ea1a 196 // possibility with the ST HAL driver to clear it. If it isn't cleared,
NYX 0:85b3fd62ea1a 197 // VBAT remains connected to the ADC channel in preference to temperature,
NYX 0:85b3fd62ea1a 198 // so VBAT readings are returned in place of temperature.
NYX 0:85b3fd62ea1a 199 ADC->CCR &= ~(ADC_CCR_VBATE | ADC_CCR_TSVREFE);
NYX 0:85b3fd62ea1a 200
NYX 0:85b3fd62ea1a 201 HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
NYX 0:85b3fd62ea1a 202
NYX 0:85b3fd62ea1a 203 HAL_ADC_Start(&obj->handle); // Start conversion
NYX 0:85b3fd62ea1a 204
NYX 0:85b3fd62ea1a 205 // Wait end of conversion and get value
NYX 0:85b3fd62ea1a 206 if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
NYX 0:85b3fd62ea1a 207 return (uint16_t)HAL_ADC_GetValue(&obj->handle);
NYX 0:85b3fd62ea1a 208 } else {
NYX 0:85b3fd62ea1a 209 return 0;
NYX 0:85b3fd62ea1a 210 }
NYX 0:85b3fd62ea1a 211 }
NYX 0:85b3fd62ea1a 212
NYX 0:85b3fd62ea1a 213 uint16_t analogin_read_u16(analogin_t *obj)
NYX 0:85b3fd62ea1a 214 {
NYX 0:85b3fd62ea1a 215 uint16_t value = adc_read(obj);
NYX 0:85b3fd62ea1a 216 // 12-bit to 16-bit conversion
NYX 0:85b3fd62ea1a 217 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
NYX 0:85b3fd62ea1a 218 return value;
NYX 0:85b3fd62ea1a 219 }
NYX 0:85b3fd62ea1a 220
NYX 0:85b3fd62ea1a 221 float analogin_read(analogin_t *obj)
NYX 0:85b3fd62ea1a 222 {
NYX 0:85b3fd62ea1a 223 uint16_t value = adc_read(obj);
NYX 0:85b3fd62ea1a 224 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
NYX 0:85b3fd62ea1a 225 }
NYX 0:85b3fd62ea1a 226
NYX 0:85b3fd62ea1a 227 #endif