test
targets/TARGET_STM/TARGET_STM32L0/analogin_api.c@2:4364577b5ad8, 2020-11-09 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Nov 09 00:33:19 2020 -0500
- Revision:
- 2:4364577b5ad8
- Parent:
- 1:8a094db1347f
copied mbed library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 1:8a094db1347f | 1 | /* mbed Microcontroller Library |
elijahsj | 1:8a094db1347f | 2 | * Copyright (c) 2015, STMicroelectronics |
elijahsj | 1:8a094db1347f | 3 | * All rights reserved. |
elijahsj | 1:8a094db1347f | 4 | * |
elijahsj | 1:8a094db1347f | 5 | * Redistribution and use in source and binary forms, with or without |
elijahsj | 1:8a094db1347f | 6 | * modification, are permitted provided that the following conditions are met: |
elijahsj | 1:8a094db1347f | 7 | * |
elijahsj | 1:8a094db1347f | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
elijahsj | 1:8a094db1347f | 9 | * this list of conditions and the following disclaimer. |
elijahsj | 1:8a094db1347f | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elijahsj | 1:8a094db1347f | 11 | * this list of conditions and the following disclaimer in the documentation |
elijahsj | 1:8a094db1347f | 12 | * and/or other materials provided with the distribution. |
elijahsj | 1:8a094db1347f | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elijahsj | 1:8a094db1347f | 14 | * may be used to endorse or promote products derived from this software |
elijahsj | 1:8a094db1347f | 15 | * without specific prior written permission. |
elijahsj | 1:8a094db1347f | 16 | * |
elijahsj | 1:8a094db1347f | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elijahsj | 1:8a094db1347f | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elijahsj | 1:8a094db1347f | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elijahsj | 1:8a094db1347f | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elijahsj | 1:8a094db1347f | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elijahsj | 1:8a094db1347f | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elijahsj | 1:8a094db1347f | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elijahsj | 1:8a094db1347f | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elijahsj | 1:8a094db1347f | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elijahsj | 1:8a094db1347f | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elijahsj | 1:8a094db1347f | 27 | */ |
elijahsj | 1:8a094db1347f | 28 | #include "mbed_assert.h" |
elijahsj | 1:8a094db1347f | 29 | #include "analogin_api.h" |
elijahsj | 1:8a094db1347f | 30 | |
elijahsj | 1:8a094db1347f | 31 | #if DEVICE_ANALOGIN |
elijahsj | 1:8a094db1347f | 32 | |
elijahsj | 1:8a094db1347f | 33 | #include "mbed_wait_api.h" |
elijahsj | 1:8a094db1347f | 34 | #include "cmsis.h" |
elijahsj | 1:8a094db1347f | 35 | #include "pinmap.h" |
elijahsj | 1:8a094db1347f | 36 | #include "mbed_error.h" |
elijahsj | 1:8a094db1347f | 37 | #include "PeripheralPins.h" |
elijahsj | 1:8a094db1347f | 38 | |
elijahsj | 1:8a094db1347f | 39 | int adc_inited = 0; |
elijahsj | 1:8a094db1347f | 40 | |
elijahsj | 1:8a094db1347f | 41 | void analogin_init(analogin_t *obj, PinName pin) |
elijahsj | 1:8a094db1347f | 42 | { |
elijahsj | 1:8a094db1347f | 43 | uint32_t function = (uint32_t)NC; |
elijahsj | 1:8a094db1347f | 44 | obj->handle.Instance = (ADC_TypeDef *)NC; |
elijahsj | 1:8a094db1347f | 45 | |
elijahsj | 1:8a094db1347f | 46 | // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) |
elijahsj | 1:8a094db1347f | 47 | // are described in PinNames.h and PeripheralPins.c |
elijahsj | 1:8a094db1347f | 48 | // Pin value must be between 0xF0 and 0xFF |
elijahsj | 1:8a094db1347f | 49 | if ((pin < 0xF0) || (pin >= 0x100)) { |
elijahsj | 1:8a094db1347f | 50 | // Normal channels |
elijahsj | 1:8a094db1347f | 51 | // Get the peripheral name from the pin and assign it to the object |
elijahsj | 1:8a094db1347f | 52 | obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC); |
elijahsj | 1:8a094db1347f | 53 | // Get the functions (adc channel) from the pin and assign it to the object |
elijahsj | 1:8a094db1347f | 54 | function = pinmap_function(pin, PinMap_ADC); |
elijahsj | 1:8a094db1347f | 55 | // Configure GPIO |
elijahsj | 1:8a094db1347f | 56 | pinmap_pinout(pin, PinMap_ADC); |
elijahsj | 1:8a094db1347f | 57 | } else { |
elijahsj | 1:8a094db1347f | 58 | // Internal channels |
elijahsj | 1:8a094db1347f | 59 | obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal); |
elijahsj | 1:8a094db1347f | 60 | function = pinmap_function(pin, PinMap_ADC_Internal); |
elijahsj | 1:8a094db1347f | 61 | // No GPIO configuration for internal channels |
elijahsj | 1:8a094db1347f | 62 | } |
elijahsj | 1:8a094db1347f | 63 | MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC); |
elijahsj | 1:8a094db1347f | 64 | MBED_ASSERT(function != (uint32_t)NC); |
elijahsj | 1:8a094db1347f | 65 | |
elijahsj | 1:8a094db1347f | 66 | obj->channel = STM_PIN_CHANNEL(function); |
elijahsj | 1:8a094db1347f | 67 | |
elijahsj | 1:8a094db1347f | 68 | // Save pin number for the read function |
elijahsj | 1:8a094db1347f | 69 | obj->pin = pin; |
elijahsj | 1:8a094db1347f | 70 | |
elijahsj | 1:8a094db1347f | 71 | // The ADC initialization is done once |
elijahsj | 1:8a094db1347f | 72 | if (adc_inited == 0) { |
elijahsj | 1:8a094db1347f | 73 | adc_inited = 1; |
elijahsj | 1:8a094db1347f | 74 | |
elijahsj | 1:8a094db1347f | 75 | obj->handle.State = HAL_ADC_STATE_RESET; |
elijahsj | 1:8a094db1347f | 76 | // Enable ADC clock |
elijahsj | 1:8a094db1347f | 77 | __ADC1_CLK_ENABLE(); |
elijahsj | 1:8a094db1347f | 78 | |
elijahsj | 1:8a094db1347f | 79 | // Configure ADC |
elijahsj | 1:8a094db1347f | 80 | obj->handle.Init.OversamplingMode = DISABLE; |
elijahsj | 1:8a094db1347f | 81 | obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1; |
elijahsj | 1:8a094db1347f | 82 | obj->handle.Init.Resolution = ADC_RESOLUTION12b; |
elijahsj | 1:8a094db1347f | 83 | obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; |
elijahsj | 1:8a094db1347f | 84 | obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; |
elijahsj | 1:8a094db1347f | 85 | obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
elijahsj | 1:8a094db1347f | 86 | obj->handle.Init.ContinuousConvMode = DISABLE; |
elijahsj | 1:8a094db1347f | 87 | obj->handle.Init.DiscontinuousConvMode = DISABLE; |
elijahsj | 1:8a094db1347f | 88 | obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; |
elijahsj | 1:8a094db1347f | 89 | obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here |
elijahsj | 1:8a094db1347f | 90 | obj->handle.Init.DMAContinuousRequests = DISABLE; |
elijahsj | 1:8a094db1347f | 91 | obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; |
elijahsj | 1:8a094db1347f | 92 | obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN; |
elijahsj | 1:8a094db1347f | 93 | obj->handle.Init.LowPowerAutoWait = ENABLE; |
elijahsj | 1:8a094db1347f | 94 | obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz |
elijahsj | 1:8a094db1347f | 95 | obj->handle.Init.LowPowerAutoPowerOff = DISABLE; |
elijahsj | 1:8a094db1347f | 96 | |
elijahsj | 1:8a094db1347f | 97 | if (HAL_ADC_Init(&obj->handle) != HAL_OK) { |
elijahsj | 1:8a094db1347f | 98 | error("Cannot initialize ADC"); |
elijahsj | 1:8a094db1347f | 99 | } |
elijahsj | 1:8a094db1347f | 100 | |
elijahsj | 1:8a094db1347f | 101 | // Calibration |
elijahsj | 1:8a094db1347f | 102 | HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED); |
elijahsj | 1:8a094db1347f | 103 | |
elijahsj | 1:8a094db1347f | 104 | __HAL_ADC_ENABLE(&obj->handle); |
elijahsj | 1:8a094db1347f | 105 | } |
elijahsj | 1:8a094db1347f | 106 | } |
elijahsj | 1:8a094db1347f | 107 | |
elijahsj | 1:8a094db1347f | 108 | static inline uint16_t adc_read(analogin_t *obj) |
elijahsj | 1:8a094db1347f | 109 | { |
elijahsj | 1:8a094db1347f | 110 | ADC_ChannelConfTypeDef sConfig = {0}; |
elijahsj | 1:8a094db1347f | 111 | |
elijahsj | 1:8a094db1347f | 112 | // Configure ADC channel |
elijahsj | 1:8a094db1347f | 113 | sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; |
elijahsj | 1:8a094db1347f | 114 | switch (obj->channel) { |
elijahsj | 1:8a094db1347f | 115 | case 0: |
elijahsj | 1:8a094db1347f | 116 | sConfig.Channel = ADC_CHANNEL_0; |
elijahsj | 1:8a094db1347f | 117 | break; |
elijahsj | 1:8a094db1347f | 118 | case 1: |
elijahsj | 1:8a094db1347f | 119 | sConfig.Channel = ADC_CHANNEL_1; |
elijahsj | 1:8a094db1347f | 120 | break; |
elijahsj | 1:8a094db1347f | 121 | case 2: |
elijahsj | 1:8a094db1347f | 122 | sConfig.Channel = ADC_CHANNEL_2; |
elijahsj | 1:8a094db1347f | 123 | break; |
elijahsj | 1:8a094db1347f | 124 | case 3: |
elijahsj | 1:8a094db1347f | 125 | sConfig.Channel = ADC_CHANNEL_3; |
elijahsj | 1:8a094db1347f | 126 | break; |
elijahsj | 1:8a094db1347f | 127 | case 4: |
elijahsj | 1:8a094db1347f | 128 | sConfig.Channel = ADC_CHANNEL_4; |
elijahsj | 1:8a094db1347f | 129 | break; |
elijahsj | 1:8a094db1347f | 130 | case 5: |
elijahsj | 1:8a094db1347f | 131 | sConfig.Channel = ADC_CHANNEL_5; |
elijahsj | 1:8a094db1347f | 132 | break; |
elijahsj | 1:8a094db1347f | 133 | case 6: |
elijahsj | 1:8a094db1347f | 134 | sConfig.Channel = ADC_CHANNEL_6; |
elijahsj | 1:8a094db1347f | 135 | break; |
elijahsj | 1:8a094db1347f | 136 | case 7: |
elijahsj | 1:8a094db1347f | 137 | sConfig.Channel = ADC_CHANNEL_7; |
elijahsj | 1:8a094db1347f | 138 | break; |
elijahsj | 1:8a094db1347f | 139 | case 8: |
elijahsj | 1:8a094db1347f | 140 | sConfig.Channel = ADC_CHANNEL_8; |
elijahsj | 1:8a094db1347f | 141 | break; |
elijahsj | 1:8a094db1347f | 142 | case 9: |
elijahsj | 1:8a094db1347f | 143 | sConfig.Channel = ADC_CHANNEL_9; |
elijahsj | 1:8a094db1347f | 144 | break; |
elijahsj | 1:8a094db1347f | 145 | case 10: |
elijahsj | 1:8a094db1347f | 146 | sConfig.Channel = ADC_CHANNEL_10; |
elijahsj | 1:8a094db1347f | 147 | break; |
elijahsj | 1:8a094db1347f | 148 | case 11: |
elijahsj | 1:8a094db1347f | 149 | sConfig.Channel = ADC_CHANNEL_11; |
elijahsj | 1:8a094db1347f | 150 | break; |
elijahsj | 1:8a094db1347f | 151 | case 12: |
elijahsj | 1:8a094db1347f | 152 | sConfig.Channel = ADC_CHANNEL_12; |
elijahsj | 1:8a094db1347f | 153 | break; |
elijahsj | 1:8a094db1347f | 154 | case 13: |
elijahsj | 1:8a094db1347f | 155 | sConfig.Channel = ADC_CHANNEL_13; |
elijahsj | 1:8a094db1347f | 156 | break; |
elijahsj | 1:8a094db1347f | 157 | case 14: |
elijahsj | 1:8a094db1347f | 158 | sConfig.Channel = ADC_CHANNEL_14; |
elijahsj | 1:8a094db1347f | 159 | break; |
elijahsj | 1:8a094db1347f | 160 | case 15: |
elijahsj | 1:8a094db1347f | 161 | sConfig.Channel = ADC_CHANNEL_15; |
elijahsj | 1:8a094db1347f | 162 | break; |
elijahsj | 1:8a094db1347f | 163 | #ifdef ADC_CHANNEL_VLCD |
elijahsj | 1:8a094db1347f | 164 | case 16: |
elijahsj | 1:8a094db1347f | 165 | sConfig.Channel = ADC_CHANNEL_VLCD; |
elijahsj | 1:8a094db1347f | 166 | break; |
elijahsj | 1:8a094db1347f | 167 | #endif |
elijahsj | 1:8a094db1347f | 168 | case 17: |
elijahsj | 1:8a094db1347f | 169 | sConfig.Channel = ADC_CHANNEL_VREFINT; |
elijahsj | 1:8a094db1347f | 170 | break; |
elijahsj | 1:8a094db1347f | 171 | case 18: |
elijahsj | 1:8a094db1347f | 172 | sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; |
elijahsj | 1:8a094db1347f | 173 | break; |
elijahsj | 1:8a094db1347f | 174 | default: |
elijahsj | 1:8a094db1347f | 175 | return 0; |
elijahsj | 1:8a094db1347f | 176 | } |
elijahsj | 1:8a094db1347f | 177 | |
elijahsj | 1:8a094db1347f | 178 | ADC1->CHSELR = 0; // [TODO] Workaround. To be removed after Cube driver is corrected. |
elijahsj | 1:8a094db1347f | 179 | HAL_ADC_ConfigChannel(&obj->handle, &sConfig); |
elijahsj | 1:8a094db1347f | 180 | |
elijahsj | 1:8a094db1347f | 181 | HAL_ADC_Start(&obj->handle); // Start conversion |
elijahsj | 1:8a094db1347f | 182 | |
elijahsj | 1:8a094db1347f | 183 | // Wait end of conversion and get value |
elijahsj | 1:8a094db1347f | 184 | if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { |
elijahsj | 1:8a094db1347f | 185 | return (HAL_ADC_GetValue(&obj->handle)); |
elijahsj | 1:8a094db1347f | 186 | } else { |
elijahsj | 1:8a094db1347f | 187 | return 0; |
elijahsj | 1:8a094db1347f | 188 | } |
elijahsj | 1:8a094db1347f | 189 | } |
elijahsj | 1:8a094db1347f | 190 | |
elijahsj | 1:8a094db1347f | 191 | uint16_t analogin_read_u16(analogin_t *obj) |
elijahsj | 1:8a094db1347f | 192 | { |
elijahsj | 1:8a094db1347f | 193 | uint16_t value = adc_read(obj); |
elijahsj | 1:8a094db1347f | 194 | // 12-bit to 16-bit conversion |
elijahsj | 1:8a094db1347f | 195 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
elijahsj | 1:8a094db1347f | 196 | return value; |
elijahsj | 1:8a094db1347f | 197 | } |
elijahsj | 1:8a094db1347f | 198 | |
elijahsj | 1:8a094db1347f | 199 | float analogin_read(analogin_t *obj) |
elijahsj | 1:8a094db1347f | 200 | { |
elijahsj | 1:8a094db1347f | 201 | uint16_t value = adc_read(obj); |
elijahsj | 1:8a094db1347f | 202 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
elijahsj | 1:8a094db1347f | 203 | } |
elijahsj | 1:8a094db1347f | 204 | |
elijahsj | 1:8a094db1347f | 205 | #endif |