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