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:
Wed Mar 19 10:15:22 2014 +0000
Revision:
125:23cc3068a9e4
Child:
135:067cc8ba23da
Synchronized with git revision ace35dfba3748c7cdc102eb38ec6b9e1067c3252

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

[NUCLEO_F302R8] Add cmsis and hal files + change F401RE clock to 84MHz

Who changed what in which revision?

UserRevisionLine numberNew 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 125:23cc3068a9e4 28 #include "analogin_api.h"
mbed_official 125:23cc3068a9e4 29 #include "wait_api.h"
mbed_official 125:23cc3068a9e4 30
mbed_official 125:23cc3068a9e4 31 #if DEVICE_ANALOGIN
mbed_official 125:23cc3068a9e4 32
mbed_official 125:23cc3068a9e4 33 #include "cmsis.h"
mbed_official 125:23cc3068a9e4 34 #include "pinmap.h"
mbed_official 125:23cc3068a9e4 35 #include "error.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 125:23cc3068a9e4 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 125:23cc3068a9e4 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 125:23cc3068a9e4 66
mbed_official 125:23cc3068a9e4 67 if (obj->adc == (ADCName)NC) {
mbed_official 125:23cc3068a9e4 68 error("ADC pin mapping failed");
mbed_official 125:23cc3068a9e4 69 }
mbed_official 125:23cc3068a9e4 70
mbed_official 125:23cc3068a9e4 71 // Configure GPIO
mbed_official 125:23cc3068a9e4 72 pinmap_pinout(pin, PinMap_ADC);
mbed_official 125:23cc3068a9e4 73
mbed_official 125:23cc3068a9e4 74 // Save pin number for the read function
mbed_official 125:23cc3068a9e4 75 obj->pin = pin;
mbed_official 125:23cc3068a9e4 76
mbed_official 125:23cc3068a9e4 77 // The ADC initialization is done once
mbed_official 125:23cc3068a9e4 78 if (adc_inited == 0) {
mbed_official 125:23cc3068a9e4 79 adc_inited = 1;
mbed_official 125:23cc3068a9e4 80
mbed_official 125:23cc3068a9e4 81 // Get ADC registers structure address
mbed_official 125:23cc3068a9e4 82 adc = (ADC_TypeDef *)(obj->adc);
mbed_official 125:23cc3068a9e4 83
mbed_official 125:23cc3068a9e4 84 // Enable ADC clock
mbed_official 125:23cc3068a9e4 85 RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1);
mbed_official 125:23cc3068a9e4 86 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
mbed_official 125:23cc3068a9e4 87
mbed_official 125:23cc3068a9e4 88 // Calibration
mbed_official 125:23cc3068a9e4 89 ADC_VoltageRegulatorCmd(adc, ENABLE);
mbed_official 125:23cc3068a9e4 90 wait_us(10);
mbed_official 125:23cc3068a9e4 91 ADC_SelectCalibrationMode(adc, ADC_CalibrationMode_Single);
mbed_official 125:23cc3068a9e4 92 ADC_StartCalibration(adc);
mbed_official 125:23cc3068a9e4 93 while (ADC_GetCalibrationStatus(adc) != RESET ) {}
mbed_official 125:23cc3068a9e4 94
mbed_official 125:23cc3068a9e4 95 // Configure ADC
mbed_official 125:23cc3068a9e4 96 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
mbed_official 125:23cc3068a9e4 97 ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
mbed_official 125:23cc3068a9e4 98 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
mbed_official 125:23cc3068a9e4 99 ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
mbed_official 125:23cc3068a9e4 100 ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
mbed_official 125:23cc3068a9e4 101 ADC_CommonInit(adc, &ADC_CommonInitStructure);
mbed_official 125:23cc3068a9e4 102
mbed_official 125:23cc3068a9e4 103 ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
mbed_official 125:23cc3068a9e4 104 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
mbed_official 125:23cc3068a9e4 105 ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
mbed_official 125:23cc3068a9e4 106 ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
mbed_official 125:23cc3068a9e4 107 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
mbed_official 125:23cc3068a9e4 108 ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
mbed_official 125:23cc3068a9e4 109 ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
mbed_official 125:23cc3068a9e4 110 ADC_InitStructure.ADC_NbrOfRegChannel = 1;
mbed_official 125:23cc3068a9e4 111 ADC_Init(adc, &ADC_InitStructure);
mbed_official 125:23cc3068a9e4 112
mbed_official 125:23cc3068a9e4 113 // Enable ADC
mbed_official 125:23cc3068a9e4 114 ADC_Cmd(adc, ENABLE);
mbed_official 125:23cc3068a9e4 115
mbed_official 125:23cc3068a9e4 116 while(!ADC_GetFlagStatus(adc, ADC_FLAG_RDY)) {}
mbed_official 125:23cc3068a9e4 117 }
mbed_official 125:23cc3068a9e4 118 }
mbed_official 125:23cc3068a9e4 119
mbed_official 125:23cc3068a9e4 120 static inline uint16_t adc_read(analogin_t *obj) {
mbed_official 125:23cc3068a9e4 121 // Get ADC registers structure address
mbed_official 125:23cc3068a9e4 122 ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc);
mbed_official 125:23cc3068a9e4 123 uint8_t channel = 0;
mbed_official 125:23cc3068a9e4 124
mbed_official 125:23cc3068a9e4 125 // Configure ADC channel
mbed_official 125:23cc3068a9e4 126 switch (obj->pin) {
mbed_official 125:23cc3068a9e4 127 case PA_0:
mbed_official 125:23cc3068a9e4 128 channel = ADC_Channel_1;
mbed_official 125:23cc3068a9e4 129 break;
mbed_official 125:23cc3068a9e4 130 case PA_1:
mbed_official 125:23cc3068a9e4 131 channel = ADC_Channel_2;
mbed_official 125:23cc3068a9e4 132 break;
mbed_official 125:23cc3068a9e4 133 case PA_2:
mbed_official 125:23cc3068a9e4 134 channel = ADC_Channel_3;
mbed_official 125:23cc3068a9e4 135 break;
mbed_official 125:23cc3068a9e4 136 case PA_3:
mbed_official 125:23cc3068a9e4 137 channel = ADC_Channel_4;
mbed_official 125:23cc3068a9e4 138 break;
mbed_official 125:23cc3068a9e4 139 case PA_4:
mbed_official 125:23cc3068a9e4 140 channel = ADC_Channel_5;
mbed_official 125:23cc3068a9e4 141 break;
mbed_official 125:23cc3068a9e4 142 case PC_0:
mbed_official 125:23cc3068a9e4 143 channel = ADC_Channel_6;
mbed_official 125:23cc3068a9e4 144 break;
mbed_official 125:23cc3068a9e4 145 case PC_1:
mbed_official 125:23cc3068a9e4 146 channel = ADC_Channel_7;
mbed_official 125:23cc3068a9e4 147 break;
mbed_official 125:23cc3068a9e4 148 case PC_2:
mbed_official 125:23cc3068a9e4 149 channel = ADC_Channel_8;
mbed_official 125:23cc3068a9e4 150 break;
mbed_official 125:23cc3068a9e4 151 case PC_3:
mbed_official 125:23cc3068a9e4 152 channel = ADC_Channel_9;
mbed_official 125:23cc3068a9e4 153 break;
mbed_official 125:23cc3068a9e4 154 case PA_6:
mbed_official 125:23cc3068a9e4 155 channel = ADC_Channel_10;
mbed_official 125:23cc3068a9e4 156 break;
mbed_official 125:23cc3068a9e4 157 case PB_0:
mbed_official 125:23cc3068a9e4 158 channel = ADC_Channel_11;
mbed_official 125:23cc3068a9e4 159 break;
mbed_official 125:23cc3068a9e4 160 case PB_1:
mbed_official 125:23cc3068a9e4 161 channel = ADC_Channel_12;
mbed_official 125:23cc3068a9e4 162 break;
mbed_official 125:23cc3068a9e4 163 case PB_13:
mbed_official 125:23cc3068a9e4 164 channel = ADC_Channel_13;
mbed_official 125:23cc3068a9e4 165 break;
mbed_official 125:23cc3068a9e4 166 case PB_11:
mbed_official 125:23cc3068a9e4 167 channel = ADC_Channel_14;
mbed_official 125:23cc3068a9e4 168 break;
mbed_official 125:23cc3068a9e4 169 case PA_7:
mbed_official 125:23cc3068a9e4 170 channel = ADC_Channel_15;
mbed_official 125:23cc3068a9e4 171 break;
mbed_official 125:23cc3068a9e4 172 default:
mbed_official 125:23cc3068a9e4 173 return 0;
mbed_official 125:23cc3068a9e4 174 }
mbed_official 125:23cc3068a9e4 175
mbed_official 125:23cc3068a9e4 176 ADC_RegularChannelConfig(adc, channel, 1, ADC_SampleTime_7Cycles5);
mbed_official 125:23cc3068a9e4 177
mbed_official 125:23cc3068a9e4 178 ADC_StartConversion(adc); // Start conversion
mbed_official 125:23cc3068a9e4 179
mbed_official 125:23cc3068a9e4 180 while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion
mbed_official 125:23cc3068a9e4 181
mbed_official 125:23cc3068a9e4 182 return(ADC_GetConversionValue(adc)); // Get conversion value
mbed_official 125:23cc3068a9e4 183 }
mbed_official 125:23cc3068a9e4 184
mbed_official 125:23cc3068a9e4 185 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 125:23cc3068a9e4 186 return(adc_read(obj));
mbed_official 125:23cc3068a9e4 187 }
mbed_official 125:23cc3068a9e4 188
mbed_official 125:23cc3068a9e4 189 float analogin_read(analogin_t *obj) {
mbed_official 125:23cc3068a9e4 190 uint16_t value = adc_read(obj);
mbed_official 125:23cc3068a9e4 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