Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
targets/hal/TARGET_STM/TARGET_STM32F3XX/analogin_api.c@0:0a673c671a56, 2016-07-27 (annotated)
- Committer:
- ebrus
- Date:
- Wed Jul 27 18:35:32 2016 +0000
- Revision:
- 0:0a673c671a56
4
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ebrus | 0:0a673c671a56 | 1 | /* mbed Microcontroller Library |
| ebrus | 0:0a673c671a56 | 2 | * Copyright (c) 2014, STMicroelectronics |
| ebrus | 0:0a673c671a56 | 3 | * All rights reserved. |
| ebrus | 0:0a673c671a56 | 4 | * |
| ebrus | 0:0a673c671a56 | 5 | * Redistribution and use in source and binary forms, with or without |
| ebrus | 0:0a673c671a56 | 6 | * modification, are permitted provided that the following conditions are met: |
| ebrus | 0:0a673c671a56 | 7 | * |
| ebrus | 0:0a673c671a56 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| ebrus | 0:0a673c671a56 | 9 | * this list of conditions and the following disclaimer. |
| ebrus | 0:0a673c671a56 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| ebrus | 0:0a673c671a56 | 11 | * this list of conditions and the following disclaimer in the documentation |
| ebrus | 0:0a673c671a56 | 12 | * and/or other materials provided with the distribution. |
| ebrus | 0:0a673c671a56 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| ebrus | 0:0a673c671a56 | 14 | * may be used to endorse or promote products derived from this software |
| ebrus | 0:0a673c671a56 | 15 | * without specific prior written permission. |
| ebrus | 0:0a673c671a56 | 16 | * |
| ebrus | 0:0a673c671a56 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| ebrus | 0:0a673c671a56 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| ebrus | 0:0a673c671a56 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| ebrus | 0:0a673c671a56 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| ebrus | 0:0a673c671a56 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| ebrus | 0:0a673c671a56 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| ebrus | 0:0a673c671a56 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| ebrus | 0:0a673c671a56 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| ebrus | 0:0a673c671a56 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| ebrus | 0:0a673c671a56 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| ebrus | 0:0a673c671a56 | 27 | */ |
| ebrus | 0:0a673c671a56 | 28 | #include "mbed_assert.h" |
| ebrus | 0:0a673c671a56 | 29 | #include "analogin_api.h" |
| ebrus | 0:0a673c671a56 | 30 | #include "wait_api.h" |
| ebrus | 0:0a673c671a56 | 31 | |
| ebrus | 0:0a673c671a56 | 32 | #if DEVICE_ANALOGIN |
| ebrus | 0:0a673c671a56 | 33 | |
| ebrus | 0:0a673c671a56 | 34 | #include "cmsis.h" |
| ebrus | 0:0a673c671a56 | 35 | #include "pinmap.h" |
| ebrus | 0:0a673c671a56 | 36 | |
| ebrus | 0:0a673c671a56 | 37 | static const PinMap PinMap_ADC[] = { |
| ebrus | 0:0a673c671a56 | 38 | {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1 |
| ebrus | 0:0a673c671a56 | 39 | {PA_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN2 |
| ebrus | 0:0a673c671a56 | 40 | {PA_2, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN3 |
| ebrus | 0:0a673c671a56 | 41 | {PA_3, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN4 |
| ebrus | 0:0a673c671a56 | 42 | {PA_4, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN5 |
| ebrus | 0:0a673c671a56 | 43 | {PC_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN6 |
| ebrus | 0:0a673c671a56 | 44 | {PC_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN7 |
| ebrus | 0:0a673c671a56 | 45 | {PC_2, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN8 |
| ebrus | 0:0a673c671a56 | 46 | {PC_3, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN9 |
| ebrus | 0:0a673c671a56 | 47 | {PA_6, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN10 |
| ebrus | 0:0a673c671a56 | 48 | {PB_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN11 |
| ebrus | 0:0a673c671a56 | 49 | {PB_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN12 |
| ebrus | 0:0a673c671a56 | 50 | {PB_13, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN13 |
| ebrus | 0:0a673c671a56 | 51 | {PB_11, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN14 |
| ebrus | 0:0a673c671a56 | 52 | {PA_7, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN15 |
| ebrus | 0:0a673c671a56 | 53 | {NC, NC, 0} |
| ebrus | 0:0a673c671a56 | 54 | }; |
| ebrus | 0:0a673c671a56 | 55 | |
| ebrus | 0:0a673c671a56 | 56 | int adc_inited = 0; |
| ebrus | 0:0a673c671a56 | 57 | |
| ebrus | 0:0a673c671a56 | 58 | void analogin_init(analogin_t *obj, PinName pin) { |
| ebrus | 0:0a673c671a56 | 59 | |
| ebrus | 0:0a673c671a56 | 60 | ADC_TypeDef *adc; |
| ebrus | 0:0a673c671a56 | 61 | ADC_InitTypeDef ADC_InitStructure; |
| ebrus | 0:0a673c671a56 | 62 | ADC_CommonInitTypeDef ADC_CommonInitStructure; |
| ebrus | 0:0a673c671a56 | 63 | |
| ebrus | 0:0a673c671a56 | 64 | // Get the peripheral name from the pin and assign it to the object |
| ebrus | 0:0a673c671a56 | 65 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
| ebrus | 0:0a673c671a56 | 66 | MBED_ASSERT(obj->adc == (ADCName)NC); |
| ebrus | 0:0a673c671a56 | 67 | |
| ebrus | 0:0a673c671a56 | 68 | // Configure GPIO |
| ebrus | 0:0a673c671a56 | 69 | pinmap_pinout(pin, PinMap_ADC); |
| ebrus | 0:0a673c671a56 | 70 | |
| ebrus | 0:0a673c671a56 | 71 | // Save pin number for the read function |
| ebrus | 0:0a673c671a56 | 72 | obj->pin = pin; |
| ebrus | 0:0a673c671a56 | 73 | |
| ebrus | 0:0a673c671a56 | 74 | // The ADC initialization is done once |
| ebrus | 0:0a673c671a56 | 75 | if (adc_inited == 0) { |
| ebrus | 0:0a673c671a56 | 76 | adc_inited = 1; |
| ebrus | 0:0a673c671a56 | 77 | |
| ebrus | 0:0a673c671a56 | 78 | // Get ADC registers structure address |
| ebrus | 0:0a673c671a56 | 79 | adc = (ADC_TypeDef *)(obj->adc); |
| ebrus | 0:0a673c671a56 | 80 | |
| ebrus | 0:0a673c671a56 | 81 | // Enable ADC clock |
| ebrus | 0:0a673c671a56 | 82 | RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1); |
| ebrus | 0:0a673c671a56 | 83 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); |
| ebrus | 0:0a673c671a56 | 84 | |
| ebrus | 0:0a673c671a56 | 85 | // Calibration |
| ebrus | 0:0a673c671a56 | 86 | ADC_VoltageRegulatorCmd(adc, ENABLE); |
| ebrus | 0:0a673c671a56 | 87 | wait_us(10); |
| ebrus | 0:0a673c671a56 | 88 | ADC_SelectCalibrationMode(adc, ADC_CalibrationMode_Single); |
| ebrus | 0:0a673c671a56 | 89 | ADC_StartCalibration(adc); |
| ebrus | 0:0a673c671a56 | 90 | while (ADC_GetCalibrationStatus(adc) != RESET) {} |
| ebrus | 0:0a673c671a56 | 91 | |
| ebrus | 0:0a673c671a56 | 92 | // Configure ADC |
| ebrus | 0:0a673c671a56 | 93 | ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; |
| ebrus | 0:0a673c671a56 | 94 | ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; |
| ebrus | 0:0a673c671a56 | 95 | ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; |
| ebrus | 0:0a673c671a56 | 96 | ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; |
| ebrus | 0:0a673c671a56 | 97 | ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; |
| ebrus | 0:0a673c671a56 | 98 | ADC_CommonInit(adc, &ADC_CommonInitStructure); |
| ebrus | 0:0a673c671a56 | 99 | |
| ebrus | 0:0a673c671a56 | 100 | ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; |
| ebrus | 0:0a673c671a56 | 101 | ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; |
| ebrus | 0:0a673c671a56 | 102 | ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; |
| ebrus | 0:0a673c671a56 | 103 | ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; |
| ebrus | 0:0a673c671a56 | 104 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; |
| ebrus | 0:0a673c671a56 | 105 | ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; |
| ebrus | 0:0a673c671a56 | 106 | ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; |
| ebrus | 0:0a673c671a56 | 107 | ADC_InitStructure.ADC_NbrOfRegChannel = 1; |
| ebrus | 0:0a673c671a56 | 108 | ADC_Init(adc, &ADC_InitStructure); |
| ebrus | 0:0a673c671a56 | 109 | |
| ebrus | 0:0a673c671a56 | 110 | // Enable ADC |
| ebrus | 0:0a673c671a56 | 111 | ADC_Cmd(adc, ENABLE); |
| ebrus | 0:0a673c671a56 | 112 | |
| ebrus | 0:0a673c671a56 | 113 | while (!ADC_GetFlagStatus(adc, ADC_FLAG_RDY)) {} |
| ebrus | 0:0a673c671a56 | 114 | } |
| ebrus | 0:0a673c671a56 | 115 | } |
| ebrus | 0:0a673c671a56 | 116 | |
| ebrus | 0:0a673c671a56 | 117 | static inline uint16_t adc_read(analogin_t *obj) { |
| ebrus | 0:0a673c671a56 | 118 | // Get ADC registers structure address |
| ebrus | 0:0a673c671a56 | 119 | ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc); |
| ebrus | 0:0a673c671a56 | 120 | uint8_t channel = 0; |
| ebrus | 0:0a673c671a56 | 121 | |
| ebrus | 0:0a673c671a56 | 122 | // Configure ADC channel |
| ebrus | 0:0a673c671a56 | 123 | switch (obj->pin) { |
| ebrus | 0:0a673c671a56 | 124 | case PA_0: |
| ebrus | 0:0a673c671a56 | 125 | channel = ADC_Channel_1; |
| ebrus | 0:0a673c671a56 | 126 | break; |
| ebrus | 0:0a673c671a56 | 127 | case PA_1: |
| ebrus | 0:0a673c671a56 | 128 | channel = ADC_Channel_2; |
| ebrus | 0:0a673c671a56 | 129 | break; |
| ebrus | 0:0a673c671a56 | 130 | case PA_2: |
| ebrus | 0:0a673c671a56 | 131 | channel = ADC_Channel_3; |
| ebrus | 0:0a673c671a56 | 132 | break; |
| ebrus | 0:0a673c671a56 | 133 | case PA_3: |
| ebrus | 0:0a673c671a56 | 134 | channel = ADC_Channel_4; |
| ebrus | 0:0a673c671a56 | 135 | break; |
| ebrus | 0:0a673c671a56 | 136 | case PA_4: |
| ebrus | 0:0a673c671a56 | 137 | channel = ADC_Channel_5; |
| ebrus | 0:0a673c671a56 | 138 | break; |
| ebrus | 0:0a673c671a56 | 139 | case PC_0: |
| ebrus | 0:0a673c671a56 | 140 | channel = ADC_Channel_6; |
| ebrus | 0:0a673c671a56 | 141 | break; |
| ebrus | 0:0a673c671a56 | 142 | case PC_1: |
| ebrus | 0:0a673c671a56 | 143 | channel = ADC_Channel_7; |
| ebrus | 0:0a673c671a56 | 144 | break; |
| ebrus | 0:0a673c671a56 | 145 | case PC_2: |
| ebrus | 0:0a673c671a56 | 146 | channel = ADC_Channel_8; |
| ebrus | 0:0a673c671a56 | 147 | break; |
| ebrus | 0:0a673c671a56 | 148 | case PC_3: |
| ebrus | 0:0a673c671a56 | 149 | channel = ADC_Channel_9; |
| ebrus | 0:0a673c671a56 | 150 | break; |
| ebrus | 0:0a673c671a56 | 151 | case PA_6: |
| ebrus | 0:0a673c671a56 | 152 | channel = ADC_Channel_10; |
| ebrus | 0:0a673c671a56 | 153 | break; |
| ebrus | 0:0a673c671a56 | 154 | case PB_0: |
| ebrus | 0:0a673c671a56 | 155 | channel = ADC_Channel_11; |
| ebrus | 0:0a673c671a56 | 156 | break; |
| ebrus | 0:0a673c671a56 | 157 | case PB_1: |
| ebrus | 0:0a673c671a56 | 158 | channel = ADC_Channel_12; |
| ebrus | 0:0a673c671a56 | 159 | break; |
| ebrus | 0:0a673c671a56 | 160 | case PB_13: |
| ebrus | 0:0a673c671a56 | 161 | channel = ADC_Channel_13; |
| ebrus | 0:0a673c671a56 | 162 | break; |
| ebrus | 0:0a673c671a56 | 163 | case PB_11: |
| ebrus | 0:0a673c671a56 | 164 | channel = ADC_Channel_14; |
| ebrus | 0:0a673c671a56 | 165 | break; |
| ebrus | 0:0a673c671a56 | 166 | case PA_7: |
| ebrus | 0:0a673c671a56 | 167 | channel = ADC_Channel_15; |
| ebrus | 0:0a673c671a56 | 168 | break; |
| ebrus | 0:0a673c671a56 | 169 | default: |
| ebrus | 0:0a673c671a56 | 170 | return 0; |
| ebrus | 0:0a673c671a56 | 171 | } |
| ebrus | 0:0a673c671a56 | 172 | |
| ebrus | 0:0a673c671a56 | 173 | ADC_RegularChannelConfig(adc, channel, 1, ADC_SampleTime_7Cycles5); |
| ebrus | 0:0a673c671a56 | 174 | |
| ebrus | 0:0a673c671a56 | 175 | ADC_StartConversion(adc); // Start conversion |
| ebrus | 0:0a673c671a56 | 176 | |
| ebrus | 0:0a673c671a56 | 177 | while (ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion |
| ebrus | 0:0a673c671a56 | 178 | |
| ebrus | 0:0a673c671a56 | 179 | return (ADC_GetConversionValue(adc)); // Get conversion value |
| ebrus | 0:0a673c671a56 | 180 | } |
| ebrus | 0:0a673c671a56 | 181 | |
| ebrus | 0:0a673c671a56 | 182 | uint16_t analogin_read_u16(analogin_t *obj) { |
| ebrus | 0:0a673c671a56 | 183 | return (adc_read(obj)); |
| ebrus | 0:0a673c671a56 | 184 | } |
| ebrus | 0:0a673c671a56 | 185 | |
| ebrus | 0:0a673c671a56 | 186 | float analogin_read(analogin_t *obj) { |
| ebrus | 0:0a673c671a56 | 187 | uint16_t value = adc_read(obj); |
| ebrus | 0:0a673c671a56 | 188 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
| ebrus | 0:0a673c671a56 | 189 | } |
| ebrus | 0:0a673c671a56 | 190 | |
| ebrus | 0:0a673c671a56 | 191 | #endif |