4545

Dependents:   LSS_Rev_1

Fork of mbed-dev by Umar Naeem

Committer:
iftaziz
Date:
Wed Aug 23 10:32:38 2017 +0000
Revision:
166:33361e55dd8c
Parent:
163:74e0ce7f98e8
r1

Who changed what in which revision?

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