Forked.
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32L4/analogin_api.c@163:74e0ce7f98e8, 2017-04-28 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Apr 28 14:04:18 2017 +0100
- Revision:
- 163:74e0ce7f98e8
- Parent:
- 160:d5399cc887bb
- Child:
- 170:19eb464bc2be
This updates the lib to the mbed lib v141
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 | ADC_HandleTypeDef AdcHandle; |
<> | 149:156823d33999 | 40 | |
<> | 149:156823d33999 | 41 | int adc_inited = 0; |
<> | 149:156823d33999 | 42 | |
<> | 149:156823d33999 | 43 | void analogin_init(analogin_t *obj, PinName pin) |
<> | 149:156823d33999 | 44 | { |
Anna Bridge |
163:74e0ce7f98e8 | 45 | uint32_t function = (uint32_t)NC; |
Anna Bridge |
163:74e0ce7f98e8 | 46 | obj->adc = (ADCName)NC; |
Anna Bridge |
163:74e0ce7f98e8 | 47 | |
Anna Bridge |
163:74e0ce7f98e8 | 48 | // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) |
Anna Bridge |
163:74e0ce7f98e8 | 49 | // are described in PinNames.h and PeripheralPins.c |
Anna Bridge |
163:74e0ce7f98e8 | 50 | // Pin value must be >= 0xF0 |
Anna Bridge |
163:74e0ce7f98e8 | 51 | if (pin < 0xF0) { |
Anna Bridge |
163:74e0ce7f98e8 | 52 | // Normal channels |
Anna Bridge |
163:74e0ce7f98e8 | 53 | // Get the peripheral name from the pin and assign it to the object |
Anna Bridge |
163:74e0ce7f98e8 | 54 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
Anna Bridge |
163:74e0ce7f98e8 | 55 | // Get the functions (adc channel) from the pin and assign it to the object |
Anna Bridge |
163:74e0ce7f98e8 | 56 | function = pinmap_function(pin, PinMap_ADC); |
Anna Bridge |
163:74e0ce7f98e8 | 57 | // Configure GPIO |
Anna Bridge |
163:74e0ce7f98e8 | 58 | pinmap_pinout(pin, PinMap_ADC); |
Anna Bridge |
163:74e0ce7f98e8 | 59 | } else { |
Anna Bridge |
163:74e0ce7f98e8 | 60 | // Internal channels |
Anna Bridge |
163:74e0ce7f98e8 | 61 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC_Internal); |
Anna Bridge |
163:74e0ce7f98e8 | 62 | function = pinmap_function(pin, PinMap_ADC_Internal); |
Anna Bridge |
163:74e0ce7f98e8 | 63 | // No GPIO configuration for internal channels |
Anna Bridge |
163:74e0ce7f98e8 | 64 | } |
<> | 149:156823d33999 | 65 | MBED_ASSERT(obj->adc != (ADCName)NC); |
<> | 149:156823d33999 | 66 | MBED_ASSERT(function != (uint32_t)NC); |
Anna Bridge |
163:74e0ce7f98e8 | 67 | |
<> | 149:156823d33999 | 68 | obj->channel = STM_PIN_CHANNEL(function); |
<> | 149:156823d33999 | 69 | |
<> | 149:156823d33999 | 70 | // Save pin number for the read function |
<> | 149:156823d33999 | 71 | obj->pin = pin; |
<> | 149:156823d33999 | 72 | |
<> | 149:156823d33999 | 73 | // The ADC initialization is done once |
<> | 149:156823d33999 | 74 | if (adc_inited == 0) { |
<> | 149:156823d33999 | 75 | adc_inited = 1; |
<> | 149:156823d33999 | 76 | |
<> | 149:156823d33999 | 77 | // Enable ADC clock |
<> | 149:156823d33999 | 78 | __HAL_RCC_ADC_CLK_ENABLE(); |
<> | 149:156823d33999 | 79 | __HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK); |
<> | 149:156823d33999 | 80 | |
<> | 149:156823d33999 | 81 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
<> | 149:156823d33999 | 82 | |
<> | 149:156823d33999 | 83 | // Configure ADC |
<> | 149:156823d33999 | 84 | AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock |
<> | 149:156823d33999 | 85 | AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; |
<> | 149:156823d33999 | 86 | AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
<> | 149:156823d33999 | 87 | AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) |
<> | 149:156823d33999 | 88 | AdcHandle.Init.EOCSelection = ADC_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). |
<> | 149:156823d33999 | 89 | AdcHandle.Init.LowPowerAutoWait = DISABLE; |
<> | 149:156823d33999 | 90 | AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig |
<> | 149:156823d33999 | 91 | AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled |
<> | 149:156823d33999 | 92 | AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled |
<> | 149:156823d33999 | 93 | AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled |
<> | 149:156823d33999 | 94 | AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event |
<> | 149:156823d33999 | 95 | AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
<> | 149:156823d33999 | 96 | AdcHandle.Init.DMAContinuousRequests = DISABLE; |
<> | 149:156823d33999 | 97 | AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun |
<> | 149:156823d33999 | 98 | AdcHandle.Init.OversamplingMode = DISABLE; // No oversampling |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | if (HAL_ADC_Init(&AdcHandle) != HAL_OK) { |
<> | 149:156823d33999 | 101 | error("Cannot initialize ADC\n"); |
<> | 149:156823d33999 | 102 | } |
<> | 149:156823d33999 | 103 | } |
<> | 149:156823d33999 | 104 | } |
<> | 149:156823d33999 | 105 | |
<> | 149:156823d33999 | 106 | static inline uint16_t adc_read(analogin_t *obj) |
<> | 149:156823d33999 | 107 | { |
<> | 149:156823d33999 | 108 | ADC_ChannelConfTypeDef sConfig = {0}; |
<> | 149:156823d33999 | 109 | |
<> | 149:156823d33999 | 110 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | // Configure ADC channel |
<> | 149:156823d33999 | 113 | switch (obj->channel) { |
<> | 149:156823d33999 | 114 | case 0: |
<> | 149:156823d33999 | 115 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
<> | 149:156823d33999 | 116 | break; |
<> | 149:156823d33999 | 117 | case 1: |
<> | 149:156823d33999 | 118 | sConfig.Channel = ADC_CHANNEL_1; |
<> | 149:156823d33999 | 119 | break; |
<> | 149:156823d33999 | 120 | case 2: |
<> | 149:156823d33999 | 121 | sConfig.Channel = ADC_CHANNEL_2; |
<> | 149:156823d33999 | 122 | break; |
<> | 149:156823d33999 | 123 | case 3: |
<> | 149:156823d33999 | 124 | sConfig.Channel = ADC_CHANNEL_3; |
<> | 149:156823d33999 | 125 | break; |
<> | 149:156823d33999 | 126 | case 4: |
<> | 149:156823d33999 | 127 | sConfig.Channel = ADC_CHANNEL_4; |
<> | 149:156823d33999 | 128 | break; |
<> | 149:156823d33999 | 129 | case 5: |
<> | 149:156823d33999 | 130 | sConfig.Channel = ADC_CHANNEL_5; |
<> | 149:156823d33999 | 131 | break; |
<> | 149:156823d33999 | 132 | case 6: |
<> | 149:156823d33999 | 133 | sConfig.Channel = ADC_CHANNEL_6; |
<> | 149:156823d33999 | 134 | break; |
<> | 149:156823d33999 | 135 | case 7: |
<> | 149:156823d33999 | 136 | sConfig.Channel = ADC_CHANNEL_7; |
<> | 149:156823d33999 | 137 | break; |
<> | 149:156823d33999 | 138 | case 8: |
<> | 149:156823d33999 | 139 | sConfig.Channel = ADC_CHANNEL_8; |
<> | 149:156823d33999 | 140 | break; |
<> | 149:156823d33999 | 141 | case 9: |
<> | 149:156823d33999 | 142 | sConfig.Channel = ADC_CHANNEL_9; |
<> | 149:156823d33999 | 143 | break; |
<> | 149:156823d33999 | 144 | case 10: |
<> | 149:156823d33999 | 145 | sConfig.Channel = ADC_CHANNEL_10; |
<> | 149:156823d33999 | 146 | break; |
<> | 149:156823d33999 | 147 | case 11: |
<> | 149:156823d33999 | 148 | sConfig.Channel = ADC_CHANNEL_11; |
<> | 149:156823d33999 | 149 | break; |
<> | 149:156823d33999 | 150 | case 12: |
<> | 149:156823d33999 | 151 | sConfig.Channel = ADC_CHANNEL_12; |
<> | 149:156823d33999 | 152 | break; |
<> | 149:156823d33999 | 153 | case 13: |
<> | 149:156823d33999 | 154 | sConfig.Channel = ADC_CHANNEL_13; |
<> | 149:156823d33999 | 155 | break; |
<> | 149:156823d33999 | 156 | case 14: |
<> | 149:156823d33999 | 157 | sConfig.Channel = ADC_CHANNEL_14; |
<> | 149:156823d33999 | 158 | break; |
<> | 149:156823d33999 | 159 | case 15: |
<> | 149:156823d33999 | 160 | sConfig.Channel = ADC_CHANNEL_15; |
<> | 149:156823d33999 | 161 | break; |
<> | 149:156823d33999 | 162 | case 16: |
<> | 149:156823d33999 | 163 | sConfig.Channel = ADC_CHANNEL_16; |
<> | 149:156823d33999 | 164 | break; |
<> | 149:156823d33999 | 165 | case 17: |
<> | 149:156823d33999 | 166 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
<> | 149:156823d33999 | 167 | break; |
<> | 149:156823d33999 | 168 | case 18: |
<> | 149:156823d33999 | 169 | sConfig.Channel = ADC_CHANNEL_VBAT; |
<> | 149:156823d33999 | 170 | break; |
<> | 149:156823d33999 | 171 | default: |
<> | 149:156823d33999 | 172 | return 0; |
<> | 149:156823d33999 | 173 | } |
<> | 149:156823d33999 | 174 | |
<> | 149:156823d33999 | 175 | sConfig.Rank = ADC_REGULAR_RANK_1; |
<> | 149:156823d33999 | 176 | sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5; |
<> | 149:156823d33999 | 177 | sConfig.SingleDiff = ADC_SINGLE_ENDED; |
<> | 149:156823d33999 | 178 | sConfig.OffsetNumber = ADC_OFFSET_NONE; |
<> | 149:156823d33999 | 179 | sConfig.Offset = 0; |
<> | 149:156823d33999 | 180 | |
<> | 149:156823d33999 | 181 | HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); |
<> | 149:156823d33999 | 182 | |
<> | 149:156823d33999 | 183 | HAL_ADC_Start(&AdcHandle); // Start conversion |
<> | 149:156823d33999 | 184 | |
<> | 149:156823d33999 | 185 | // Wait end of conversion and get value |
<> | 149:156823d33999 | 186 | if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) { |
<> | 149:156823d33999 | 187 | return (HAL_ADC_GetValue(&AdcHandle)); |
<> | 149:156823d33999 | 188 | } else { |
<> | 149:156823d33999 | 189 | return 0; |
<> | 149:156823d33999 | 190 | } |
<> | 149:156823d33999 | 191 | } |
<> | 149:156823d33999 | 192 | |
<> | 149:156823d33999 | 193 | uint16_t analogin_read_u16(analogin_t *obj) |
<> | 149:156823d33999 | 194 | { |
<> | 149:156823d33999 | 195 | uint16_t value = adc_read(obj); |
<> | 149:156823d33999 | 196 | // 12-bit to 16-bit conversion |
<> | 149:156823d33999 | 197 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
<> | 149:156823d33999 | 198 | return value; |
<> | 149:156823d33999 | 199 | } |
<> | 149:156823d33999 | 200 | |
<> | 149:156823d33999 | 201 | float analogin_read(analogin_t *obj) |
<> | 149:156823d33999 | 202 | { |
<> | 149:156823d33999 | 203 | uint16_t value = adc_read(obj); |
<> | 149:156823d33999 | 204 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
<> | 149:156823d33999 | 205 | } |
<> | 149:156823d33999 | 206 | |
<> | 149:156823d33999 | 207 | #endif |