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