mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Apr 29 11:15:07 2014 +0100
Revision:
174:8bb9f3a33240
Parent:
129:0182c99221bc
Child:
227:7bd0639b8911
Synchronized with git revision 5bf985ebc651a2c31cefabd9d62c51dc465ef60a

Full URL: https://github.com/mbedmicro/mbed/commit/5bf985ebc651a2c31cefabd9d62c51dc465ef60a/

[NUCLEO_L152RE/F103RB] Add LSE configuration for RTC

Who changed what in which revision?

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