test
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32F4/analogin_api.c@174:e131a1973e81, 2017-09-15 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Sep 15 14:59:18 2017 +0100
- Revision:
- 174:e131a1973e81
- Parent:
- 173:7d866c31b3c5
This updates the lib to the mbed lib v 151
Who changed what in which revision?
User | Revision | Line number | New 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 | void analogin_init(analogin_t *obj, PinName pin) |
<> | 149:156823d33999 | 40 | { |
Anna Bridge |
163:74e0ce7f98e8 | 41 | uint32_t function = (uint32_t)NC; |
Anna Bridge |
163:74e0ce7f98e8 | 42 | |
<> | 149:156823d33999 | 43 | #if defined(ADC1) |
<> | 149:156823d33999 | 44 | static int adc1_inited = 0; |
<> | 149:156823d33999 | 45 | #endif |
<> | 149:156823d33999 | 46 | #if defined(ADC2) |
<> | 149:156823d33999 | 47 | static int adc2_inited = 0; |
<> | 149:156823d33999 | 48 | #endif |
<> | 149:156823d33999 | 49 | #if defined(ADC3) |
<> | 149:156823d33999 | 50 | static int adc3_inited = 0; |
<> | 149:156823d33999 | 51 | #endif |
Anna Bridge |
163:74e0ce7f98e8 | 52 | // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) |
Anna Bridge |
163:74e0ce7f98e8 | 53 | // are described in PinNames.h and PeripheralPins.c |
AnnaBridge | 173:7d866c31b3c5 | 54 | // Pin value must be between 0xF0 and 0xFF |
AnnaBridge | 173:7d866c31b3c5 | 55 | if ((pin < 0xF0) || (pin >= 0x100)) { |
Anna Bridge |
163:74e0ce7f98e8 | 56 | // Normal channels |
Anna Bridge |
163:74e0ce7f98e8 | 57 | // Get the peripheral name from the pin and assign it to the object |
AnnaBridge | 172:89b338f31ef1 | 58 | obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC); |
Anna Bridge |
163:74e0ce7f98e8 | 59 | // Get the functions (adc channel) from the pin and assign it to the object |
Anna Bridge |
163:74e0ce7f98e8 | 60 | function = pinmap_function(pin, PinMap_ADC); |
Anna Bridge |
163:74e0ce7f98e8 | 61 | // Configure GPIO |
Anna Bridge |
163:74e0ce7f98e8 | 62 | pinmap_pinout(pin, PinMap_ADC); |
Anna Bridge |
163:74e0ce7f98e8 | 63 | } else { |
Anna Bridge |
163:74e0ce7f98e8 | 64 | // Internal channels |
AnnaBridge | 172:89b338f31ef1 | 65 | obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal); |
Anna Bridge |
163:74e0ce7f98e8 | 66 | function = pinmap_function(pin, PinMap_ADC_Internal); |
Anna Bridge |
163:74e0ce7f98e8 | 67 | // No GPIO configuration for internal channels |
Anna Bridge |
163:74e0ce7f98e8 | 68 | } |
AnnaBridge | 172:89b338f31ef1 | 69 | MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); |
<> | 149:156823d33999 | 70 | MBED_ASSERT(function != (uint32_t)NC); |
Anna Bridge |
163:74e0ce7f98e8 | 71 | |
<> | 149:156823d33999 | 72 | obj->channel = STM_PIN_CHANNEL(function); |
<> | 149:156823d33999 | 73 | |
<> | 149:156823d33999 | 74 | // Save pin number for the read function |
<> | 149:156823d33999 | 75 | obj->pin = pin; |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | // Check if ADC is already initialized |
<> | 149:156823d33999 | 78 | // Enable ADC clock |
<> | 149:156823d33999 | 79 | #if defined(ADC1) |
AnnaBridge | 172:89b338f31ef1 | 80 | if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return; |
AnnaBridge | 172:89b338f31ef1 | 81 | if ((ADCName)obj->handle.Instance == ADC_1) { |
AnnaBridge | 168:e84263d55307 | 82 | __HAL_RCC_ADC1_CLK_ENABLE(); |
<> | 149:156823d33999 | 83 | adc1_inited = 1; |
<> | 149:156823d33999 | 84 | } |
<> | 149:156823d33999 | 85 | #endif |
<> | 149:156823d33999 | 86 | #if defined(ADC2) |
AnnaBridge | 172:89b338f31ef1 | 87 | if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return; |
AnnaBridge | 172:89b338f31ef1 | 88 | if ((ADCName)obj->handle.Instance == ADC_2) { |
AnnaBridge | 168:e84263d55307 | 89 | __HAL_RCC_ADC2_CLK_ENABLE(); |
<> | 149:156823d33999 | 90 | adc2_inited = 1; |
<> | 149:156823d33999 | 91 | } |
<> | 149:156823d33999 | 92 | #endif |
<> | 149:156823d33999 | 93 | #if defined(ADC3) |
AnnaBridge | 172:89b338f31ef1 | 94 | if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return; |
AnnaBridge | 172:89b338f31ef1 | 95 | if ((ADCName)obj->handle.Instance == ADC_3) { |
AnnaBridge | 168:e84263d55307 | 96 | __HAL_RCC_ADC3_CLK_ENABLE(); |
<> | 149:156823d33999 | 97 | adc3_inited = 1; |
<> | 149:156823d33999 | 98 | } |
<> | 149:156823d33999 | 99 | #endif |
<> | 149:156823d33999 | 100 | // Configure ADC |
AnnaBridge | 172:89b338f31ef1 | 101 | obj->handle.State = HAL_ADC_STATE_RESET; |
AnnaBridge | 172:89b338f31ef1 | 102 | obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; |
AnnaBridge | 172:89b338f31ef1 | 103 | obj->handle.Init.Resolution = ADC_RESOLUTION_12B; |
AnnaBridge | 172:89b338f31ef1 | 104 | obj->handle.Init.ScanConvMode = DISABLE; |
AnnaBridge | 172:89b338f31ef1 | 105 | obj->handle.Init.ContinuousConvMode = DISABLE; |
AnnaBridge | 172:89b338f31ef1 | 106 | obj->handle.Init.DiscontinuousConvMode = DISABLE; |
AnnaBridge | 172:89b338f31ef1 | 107 | obj->handle.Init.NbrOfDiscConversion = 0; |
AnnaBridge | 172:89b338f31ef1 | 108 | obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
AnnaBridge | 172:89b338f31ef1 | 109 | obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; |
AnnaBridge | 172:89b338f31ef1 | 110 | obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
AnnaBridge | 172:89b338f31ef1 | 111 | obj->handle.Init.NbrOfConversion = 1; |
AnnaBridge | 172:89b338f31ef1 | 112 | obj->handle.Init.DMAContinuousRequests = DISABLE; |
AnnaBridge | 172:89b338f31ef1 | 113 | obj->handle.Init.EOCSelection = DISABLE; |
<> | 149:156823d33999 | 114 | |
AnnaBridge | 172:89b338f31ef1 | 115 | if (HAL_ADC_Init(&obj->handle) != HAL_OK) { |
<> | 149:156823d33999 | 116 | error("Cannot initialize ADC\n"); |
<> | 149:156823d33999 | 117 | } |
<> | 149:156823d33999 | 118 | } |
<> | 149:156823d33999 | 119 | |
<> | 149:156823d33999 | 120 | static inline uint16_t adc_read(analogin_t *obj) |
<> | 149:156823d33999 | 121 | { |
<> | 149:156823d33999 | 122 | ADC_ChannelConfTypeDef sConfig = {0}; |
<> | 149:156823d33999 | 123 | |
<> | 149:156823d33999 | 124 | // Configure ADC channel |
<> | 149:156823d33999 | 125 | sConfig.Rank = 1; |
<> | 149:156823d33999 | 126 | sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; |
<> | 149:156823d33999 | 127 | sConfig.Offset = 0; |
<> | 149:156823d33999 | 128 | |
<> | 149:156823d33999 | 129 | switch (obj->channel) { |
<> | 149:156823d33999 | 130 | case 0: |
<> | 149:156823d33999 | 131 | sConfig.Channel = ADC_CHANNEL_0; |
<> | 149:156823d33999 | 132 | break; |
<> | 149:156823d33999 | 133 | case 1: |
<> | 149:156823d33999 | 134 | sConfig.Channel = ADC_CHANNEL_1; |
<> | 149:156823d33999 | 135 | break; |
<> | 149:156823d33999 | 136 | case 2: |
<> | 149:156823d33999 | 137 | sConfig.Channel = ADC_CHANNEL_2; |
<> | 149:156823d33999 | 138 | break; |
<> | 149:156823d33999 | 139 | case 3: |
<> | 149:156823d33999 | 140 | sConfig.Channel = ADC_CHANNEL_3; |
<> | 149:156823d33999 | 141 | break; |
<> | 149:156823d33999 | 142 | case 4: |
<> | 149:156823d33999 | 143 | sConfig.Channel = ADC_CHANNEL_4; |
<> | 149:156823d33999 | 144 | break; |
<> | 149:156823d33999 | 145 | case 5: |
<> | 149:156823d33999 | 146 | sConfig.Channel = ADC_CHANNEL_5; |
<> | 149:156823d33999 | 147 | break; |
<> | 149:156823d33999 | 148 | case 6: |
<> | 149:156823d33999 | 149 | sConfig.Channel = ADC_CHANNEL_6; |
<> | 149:156823d33999 | 150 | break; |
<> | 149:156823d33999 | 151 | case 7: |
<> | 149:156823d33999 | 152 | sConfig.Channel = ADC_CHANNEL_7; |
<> | 149:156823d33999 | 153 | break; |
<> | 149:156823d33999 | 154 | case 8: |
<> | 149:156823d33999 | 155 | sConfig.Channel = ADC_CHANNEL_8; |
<> | 149:156823d33999 | 156 | break; |
<> | 149:156823d33999 | 157 | case 9: |
<> | 149:156823d33999 | 158 | sConfig.Channel = ADC_CHANNEL_9; |
<> | 149:156823d33999 | 159 | break; |
<> | 149:156823d33999 | 160 | case 10: |
<> | 149:156823d33999 | 161 | sConfig.Channel = ADC_CHANNEL_10; |
<> | 149:156823d33999 | 162 | break; |
<> | 149:156823d33999 | 163 | case 11: |
<> | 149:156823d33999 | 164 | sConfig.Channel = ADC_CHANNEL_11; |
<> | 149:156823d33999 | 165 | break; |
<> | 149:156823d33999 | 166 | case 12: |
<> | 149:156823d33999 | 167 | sConfig.Channel = ADC_CHANNEL_12; |
<> | 149:156823d33999 | 168 | break; |
<> | 149:156823d33999 | 169 | case 13: |
<> | 149:156823d33999 | 170 | sConfig.Channel = ADC_CHANNEL_13; |
<> | 149:156823d33999 | 171 | break; |
<> | 149:156823d33999 | 172 | case 14: |
<> | 149:156823d33999 | 173 | sConfig.Channel = ADC_CHANNEL_14; |
<> | 149:156823d33999 | 174 | break; |
<> | 149:156823d33999 | 175 | case 15: |
<> | 149:156823d33999 | 176 | sConfig.Channel = ADC_CHANNEL_15; |
<> | 149:156823d33999 | 177 | break; |
<> | 149:156823d33999 | 178 | case 16: |
<> | 149:156823d33999 | 179 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
<> | 149:156823d33999 | 180 | break; |
<> | 149:156823d33999 | 181 | case 17: |
<> | 149:156823d33999 | 182 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
AnnaBridge | 174:e131a1973e81 | 183 | /* From experiment, measurement needs max sampling time to be valid */ |
AnnaBridge | 174:e131a1973e81 | 184 | sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; |
<> | 149:156823d33999 | 185 | break; |
<> | 149:156823d33999 | 186 | case 18: |
<> | 149:156823d33999 | 187 | sConfig.Channel = ADC_CHANNEL_VBAT; |
AnnaBridge | 174:e131a1973e81 | 188 | /* From experiment, measurement needs max sampling time to be valid */ |
Kojto | 170:e3b6fe271b81 | 189 | sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; |
<> | 149:156823d33999 | 190 | break; |
<> | 149:156823d33999 | 191 | default: |
<> | 149:156823d33999 | 192 | return 0; |
<> | 149:156823d33999 | 193 | } |
<> | 149:156823d33999 | 194 | |
<> | 153:fa9ff456f731 | 195 | // Measuring VBAT sets the ADC_CCR_VBATE bit in ADC->CCR, and there is not |
<> | 153:fa9ff456f731 | 196 | // possibility with the ST HAL driver to clear it. If it isn't cleared, |
<> | 153:fa9ff456f731 | 197 | // VBAT remains connected to the ADC channel in preference to temperature, |
<> | 153:fa9ff456f731 | 198 | // so VBAT readings are returned in place of temperature. |
<> | 153:fa9ff456f731 | 199 | ADC->CCR &= ~(ADC_CCR_VBATE | ADC_CCR_TSVREFE); |
<> | 153:fa9ff456f731 | 200 | |
AnnaBridge | 172:89b338f31ef1 | 201 | HAL_ADC_ConfigChannel(&obj->handle, &sConfig); |
<> | 149:156823d33999 | 202 | |
AnnaBridge | 172:89b338f31ef1 | 203 | HAL_ADC_Start(&obj->handle); // Start conversion |
<> | 149:156823d33999 | 204 | |
<> | 149:156823d33999 | 205 | // Wait end of conversion and get value |
AnnaBridge | 172:89b338f31ef1 | 206 | if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { |
AnnaBridge | 172:89b338f31ef1 | 207 | return (uint16_t)HAL_ADC_GetValue(&obj->handle); |
<> | 149:156823d33999 | 208 | } else { |
<> | 149:156823d33999 | 209 | return 0; |
<> | 149:156823d33999 | 210 | } |
<> | 149:156823d33999 | 211 | } |
<> | 149:156823d33999 | 212 | |
<> | 149:156823d33999 | 213 | uint16_t analogin_read_u16(analogin_t *obj) |
<> | 149:156823d33999 | 214 | { |
<> | 149:156823d33999 | 215 | uint16_t value = adc_read(obj); |
<> | 149:156823d33999 | 216 | // 12-bit to 16-bit conversion |
<> | 149:156823d33999 | 217 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
<> | 149:156823d33999 | 218 | return value; |
<> | 149:156823d33999 | 219 | } |
<> | 149:156823d33999 | 220 | |
<> | 149:156823d33999 | 221 | float analogin_read(analogin_t *obj) |
<> | 149:156823d33999 | 222 | { |
<> | 149:156823d33999 | 223 | uint16_t value = adc_read(obj); |
<> | 149:156823d33999 | 224 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
<> | 149:156823d33999 | 225 | } |
<> | 149:156823d33999 | 226 | |
<> | 149:156823d33999 | 227 | #endif |