mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Apr 08 09:15:06 2014 +0100
Revision:
155:8435094ec241
Child:
227:7bd0639b8911
Synchronized with git revision d616c415fc62f490b915ff0ff39c8d24b2917c0f

Full URL: https://github.com/mbedmicro/mbed/commit/d616c415fc62f490b915ff0ff39c8d24b2917c0f/

[STM32F3XX] Initial port

Who changed what in which revision?

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