Forked.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Aug 31 17:27:04 2017 +0100
Revision:
172:7d866c31b3c5
Parent:
171:89b338f31ef1
This updates the lib to the mbed lib v 150

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 int adc_inited = 0;
<> 149:156823d33999 40
<> 149:156823d33999 41 void analogin_init(analogin_t *obj, PinName pin)
<> 149:156823d33999 42 {
<> 149:156823d33999 43 RCC_OscInitTypeDef RCC_OscInitStruct;
AnnaBridge 172:7d866c31b3c5 44 uint32_t function = (uint32_t)NC;
<> 149:156823d33999 45
AnnaBridge 172:7d866c31b3c5 46 // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
AnnaBridge 172:7d866c31b3c5 47 // are described in PinNames.h and PeripheralPins.c
AnnaBridge 172:7d866c31b3c5 48 // Pin value must be between 0xF0 and 0xFF
AnnaBridge 172:7d866c31b3c5 49 if ((pin < 0xF0) || (pin >= 0x100)) {
AnnaBridge 172:7d866c31b3c5 50 // Normal channels
AnnaBridge 172:7d866c31b3c5 51 // Get the peripheral name from the pin and assign it to the object
AnnaBridge 172:7d866c31b3c5 52 obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
AnnaBridge 172:7d866c31b3c5 53 // Get the functions (adc channel) from the pin and assign it to the object
AnnaBridge 172:7d866c31b3c5 54 function = pinmap_function(pin, PinMap_ADC);
AnnaBridge 172:7d866c31b3c5 55 // Configure GPIO
AnnaBridge 172:7d866c31b3c5 56 pinmap_pinout(pin, PinMap_ADC);
AnnaBridge 172:7d866c31b3c5 57 } else {
AnnaBridge 172:7d866c31b3c5 58 // Internal channels
AnnaBridge 172:7d866c31b3c5 59 obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal);
AnnaBridge 172:7d866c31b3c5 60 function = pinmap_function(pin, PinMap_ADC_Internal);
AnnaBridge 172:7d866c31b3c5 61 // No GPIO configuration for internal channels
AnnaBridge 172:7d866c31b3c5 62 }
AnnaBridge 171:89b338f31ef1 63 MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
<> 149:156823d33999 64 MBED_ASSERT(function != (uint32_t)NC);
AnnaBridge 172:7d866c31b3c5 65
<> 149:156823d33999 66 obj->channel = STM_PIN_CHANNEL(function);
<> 149:156823d33999 67
<> 149:156823d33999 68 // Save pin number for the read function
<> 149:156823d33999 69 obj->pin = pin;
<> 149:156823d33999 70
<> 149:156823d33999 71 // The ADC initialization is done once
<> 149:156823d33999 72 if (adc_inited == 0) {
<> 149:156823d33999 73 adc_inited = 1;
<> 149:156823d33999 74
<> 149:156823d33999 75 // Enable the HSI (to clock the ADC)
<> 149:156823d33999 76 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
<> 149:156823d33999 77 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
<> 149:156823d33999 78 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
<> 153:fa9ff456f731 79 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
<> 149:156823d33999 80 HAL_RCC_OscConfig(&RCC_OscInitStruct);
<> 149:156823d33999 81
AnnaBridge 171:89b338f31ef1 82 obj->handle.State = HAL_ADC_STATE_RESET;
<> 149:156823d33999 83 // Enable ADC clock
<> 149:156823d33999 84 __ADC1_CLK_ENABLE();
<> 149:156823d33999 85
<> 149:156823d33999 86 // Configure ADC
AnnaBridge 171:89b338f31ef1 87 obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
AnnaBridge 171:89b338f31ef1 88 obj->handle.Init.Resolution = ADC_RESOLUTION12b;
AnnaBridge 171:89b338f31ef1 89 obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AnnaBridge 171:89b338f31ef1 90 obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
AnnaBridge 171:89b338f31ef1 91 obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
AnnaBridge 171:89b338f31ef1 92 obj->handle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
AnnaBridge 171:89b338f31ef1 93 obj->handle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
AnnaBridge 171:89b338f31ef1 94 obj->handle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
AnnaBridge 171:89b338f31ef1 95 obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
AnnaBridge 171:89b338f31ef1 96 obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
AnnaBridge 171:89b338f31ef1 97 obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
AnnaBridge 171:89b338f31ef1 98 obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
AnnaBridge 171:89b338f31ef1 99 obj->handle.Init.ExternalTrigConv = 0; // Not used
AnnaBridge 171:89b338f31ef1 100 obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AnnaBridge 171:89b338f31ef1 101 obj->handle.Init.DMAContinuousRequests = DISABLE;
<> 149:156823d33999 102
AnnaBridge 171:89b338f31ef1 103 if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
<> 149:156823d33999 104 error("Cannot initialize ADC");
<> 149:156823d33999 105 }
<> 149:156823d33999 106 }
<> 149:156823d33999 107 }
<> 149:156823d33999 108
<> 149:156823d33999 109 static inline uint16_t adc_read(analogin_t *obj)
<> 149:156823d33999 110 {
<> 149:156823d33999 111 ADC_ChannelConfTypeDef sConfig = {0};
<> 149:156823d33999 112
<> 149:156823d33999 113 // Configure ADC channel
<> 149:156823d33999 114 switch (obj->channel) {
<> 149:156823d33999 115 case 0:
<> 149:156823d33999 116 sConfig.Channel = ADC_CHANNEL_0;
<> 149:156823d33999 117 break;
<> 149:156823d33999 118 case 1:
<> 149:156823d33999 119 sConfig.Channel = ADC_CHANNEL_1;
<> 149:156823d33999 120 break;
<> 149:156823d33999 121 case 2:
<> 149:156823d33999 122 sConfig.Channel = ADC_CHANNEL_2;
<> 149:156823d33999 123 break;
<> 149:156823d33999 124 case 3:
<> 149:156823d33999 125 sConfig.Channel = ADC_CHANNEL_3;
<> 149:156823d33999 126 break;
<> 149:156823d33999 127 case 4:
<> 149:156823d33999 128 sConfig.Channel = ADC_CHANNEL_4;
<> 149:156823d33999 129 break;
<> 149:156823d33999 130 case 5:
<> 149:156823d33999 131 sConfig.Channel = ADC_CHANNEL_5;
<> 149:156823d33999 132 break;
<> 149:156823d33999 133 case 6:
<> 149:156823d33999 134 sConfig.Channel = ADC_CHANNEL_6;
<> 149:156823d33999 135 break;
<> 149:156823d33999 136 case 7:
<> 149:156823d33999 137 sConfig.Channel = ADC_CHANNEL_7;
<> 149:156823d33999 138 break;
<> 149:156823d33999 139 case 8:
<> 149:156823d33999 140 sConfig.Channel = ADC_CHANNEL_8;
<> 149:156823d33999 141 break;
<> 149:156823d33999 142 case 9:
<> 149:156823d33999 143 sConfig.Channel = ADC_CHANNEL_9;
<> 149:156823d33999 144 break;
<> 149:156823d33999 145 case 10:
<> 149:156823d33999 146 sConfig.Channel = ADC_CHANNEL_10;
<> 149:156823d33999 147 break;
<> 149:156823d33999 148 case 11:
<> 149:156823d33999 149 sConfig.Channel = ADC_CHANNEL_11;
<> 149:156823d33999 150 break;
<> 149:156823d33999 151 case 12:
<> 149:156823d33999 152 sConfig.Channel = ADC_CHANNEL_12;
<> 149:156823d33999 153 break;
<> 149:156823d33999 154 case 13:
<> 149:156823d33999 155 sConfig.Channel = ADC_CHANNEL_13;
<> 149:156823d33999 156 break;
<> 149:156823d33999 157 case 14:
<> 149:156823d33999 158 sConfig.Channel = ADC_CHANNEL_14;
<> 149:156823d33999 159 break;
<> 149:156823d33999 160 case 15:
<> 149:156823d33999 161 sConfig.Channel = ADC_CHANNEL_15;
<> 149:156823d33999 162 break;
<> 149:156823d33999 163 case 16:
<> 149:156823d33999 164 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
<> 149:156823d33999 165 break;
<> 149:156823d33999 166 case 17:
<> 149:156823d33999 167 sConfig.Channel = ADC_CHANNEL_VREFINT;
<> 149:156823d33999 168 break;
<> 149:156823d33999 169 case 18:
<> 149:156823d33999 170 sConfig.Channel = ADC_CHANNEL_18;
<> 149:156823d33999 171 break;
<> 149:156823d33999 172 case 19:
<> 149:156823d33999 173 sConfig.Channel = ADC_CHANNEL_19;
<> 149:156823d33999 174 break;
<> 149:156823d33999 175 case 20:
<> 149:156823d33999 176 sConfig.Channel = ADC_CHANNEL_20;
<> 149:156823d33999 177 break;
<> 149:156823d33999 178 case 21:
<> 149:156823d33999 179 sConfig.Channel = ADC_CHANNEL_21;
<> 149:156823d33999 180 break;
<> 149:156823d33999 181 case 22:
<> 149:156823d33999 182 sConfig.Channel = ADC_CHANNEL_22;
<> 149:156823d33999 183 break;
<> 149:156823d33999 184 case 23:
<> 149:156823d33999 185 sConfig.Channel = ADC_CHANNEL_23;
<> 149:156823d33999 186 break;
<> 149:156823d33999 187 case 24:
<> 149:156823d33999 188 sConfig.Channel = ADC_CHANNEL_24;
<> 149:156823d33999 189 break;
<> 149:156823d33999 190 case 25:
<> 149:156823d33999 191 sConfig.Channel = ADC_CHANNEL_25;
<> 149:156823d33999 192 break;
<> 149:156823d33999 193 case 26:
<> 149:156823d33999 194 sConfig.Channel = ADC_CHANNEL_26;
<> 149:156823d33999 195 break;
<> 149:156823d33999 196 #ifdef ADC_CHANNEL_27
<> 149:156823d33999 197 case 27:
<> 149:156823d33999 198 sConfig.Channel = ADC_CHANNEL_27;
<> 149:156823d33999 199 break;
<> 149:156823d33999 200 #endif
<> 149:156823d33999 201 #ifdef ADC_CHANNEL_28
<> 149:156823d33999 202 case 28:
<> 149:156823d33999 203 sConfig.Channel = ADC_CHANNEL_28;
<> 149:156823d33999 204 break;
<> 149:156823d33999 205 #endif
<> 149:156823d33999 206 #ifdef ADC_CHANNEL_29
<> 149:156823d33999 207 case 29:
<> 149:156823d33999 208 sConfig.Channel = ADC_CHANNEL_29;
<> 149:156823d33999 209 break;
<> 149:156823d33999 210 #endif
<> 149:156823d33999 211 #ifdef ADC_CHANNEL_30
<> 149:156823d33999 212 case 30:
<> 149:156823d33999 213 sConfig.Channel = ADC_CHANNEL_30;
<> 149:156823d33999 214 break;
<> 149:156823d33999 215 #endif
<> 149:156823d33999 216 #ifdef ADC_CHANNEL_31
<> 149:156823d33999 217 case 31:
<> 149:156823d33999 218 sConfig.Channel = ADC_CHANNEL_31;
<> 149:156823d33999 219 break;
<> 149:156823d33999 220 #endif
<> 149:156823d33999 221 default:
<> 149:156823d33999 222 return 0;
<> 149:156823d33999 223 }
<> 149:156823d33999 224
<> 149:156823d33999 225 sConfig.Rank = ADC_REGULAR_RANK_1;
<> 149:156823d33999 226 sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES;
<> 149:156823d33999 227
AnnaBridge 171:89b338f31ef1 228 HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
<> 149:156823d33999 229
AnnaBridge 171:89b338f31ef1 230 HAL_ADC_Start(&obj->handle); // Start conversion
<> 149:156823d33999 231
<> 149:156823d33999 232 // Wait end of conversion and get value
AnnaBridge 171:89b338f31ef1 233 if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
AnnaBridge 171:89b338f31ef1 234 return (HAL_ADC_GetValue(&obj->handle));
<> 149:156823d33999 235 } else {
<> 149:156823d33999 236 return 0;
<> 149:156823d33999 237 }
<> 149:156823d33999 238 }
<> 149:156823d33999 239
<> 149:156823d33999 240 uint16_t analogin_read_u16(analogin_t *obj)
<> 149:156823d33999 241 {
<> 149:156823d33999 242 uint16_t value = adc_read(obj);
<> 149:156823d33999 243 // 12-bit to 16-bit conversion
<> 149:156823d33999 244 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
<> 149:156823d33999 245 return value;
<> 149:156823d33999 246 }
<> 149:156823d33999 247
<> 149:156823d33999 248 float analogin_read(analogin_t *obj)
<> 149:156823d33999 249 {
<> 149:156823d33999 250 uint16_t value = adc_read(obj);
<> 149:156823d33999 251 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
<> 149:156823d33999 252 }
<> 149:156823d33999 253
<> 149:156823d33999 254 #endif