00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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