mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jan 07 11:00:05 2014 +0000
Revision:
70:c1fbde68b492
Parent:
56:99eb381a3269
Child:
167:d5744491c362
Synchronized with git revision 3f438a307904431f2782db3c8fa49946b9fc1d85

Full URL: https://github.com/mbedmicro/mbed/commit/3f438a307904431f2782db3c8fa49946b9fc1d85/

[NUCLEO_F103RB] license text changed + sleep hal updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 52:a51c77007319 1 /* mbed Microcontroller Library
mbed_official 70:c1fbde68b492 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 70:c1fbde68b492 3 * All rights reserved.
mbed_official 52:a51c77007319 4 *
mbed_official 70:c1fbde68b492 5 * Redistribution and use in source and binary forms, with or without
mbed_official 70:c1fbde68b492 6 * modification, are permitted provided that the following conditions are met:
mbed_official 52:a51c77007319 7 *
mbed_official 70:c1fbde68b492 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 70:c1fbde68b492 9 * this list of conditions and the following disclaimer.
mbed_official 70:c1fbde68b492 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 70:c1fbde68b492 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 70:c1fbde68b492 12 * and/or other materials provided with the distribution.
mbed_official 70:c1fbde68b492 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 70:c1fbde68b492 14 * may be used to endorse or promote products derived from this software
mbed_official 70:c1fbde68b492 15 * without specific prior written permission.
mbed_official 52:a51c77007319 16 *
mbed_official 70:c1fbde68b492 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 70:c1fbde68b492 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 70:c1fbde68b492 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 70:c1fbde68b492 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 70:c1fbde68b492 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 70:c1fbde68b492 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 70:c1fbde68b492 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 70:c1fbde68b492 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 70:c1fbde68b492 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 70:c1fbde68b492 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 52:a51c77007319 27 */
mbed_official 52:a51c77007319 28 #include "analogin_api.h"
mbed_official 52:a51c77007319 29 #include "wait_api.h"
mbed_official 52:a51c77007319 30
mbed_official 52:a51c77007319 31 #if DEVICE_ANALOGIN
mbed_official 52:a51c77007319 32
mbed_official 52:a51c77007319 33 #include "cmsis.h"
mbed_official 52:a51c77007319 34 #include "pinmap.h"
mbed_official 52:a51c77007319 35 #include "error.h"
mbed_official 52:a51c77007319 36
mbed_official 52:a51c77007319 37 static const PinMap PinMap_ADC[] = {
mbed_official 52:a51c77007319 38 {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)},
mbed_official 52:a51c77007319 39 {PA_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)},
mbed_official 52:a51c77007319 40 {PA_4, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)},
mbed_official 52:a51c77007319 41 {PB_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)},
mbed_official 52:a51c77007319 42 {PC_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)},
mbed_official 52:a51c77007319 43 {PC_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AIN, 0)},
mbed_official 52:a51c77007319 44 {NC, NC, 0}
mbed_official 52:a51c77007319 45 };
mbed_official 52:a51c77007319 46
mbed_official 52:a51c77007319 47 int adc_inited = 0;
mbed_official 52:a51c77007319 48
mbed_official 52:a51c77007319 49 void analogin_init(analogin_t *obj, PinName pin) {
mbed_official 52:a51c77007319 50
mbed_official 52:a51c77007319 51 ADC_TypeDef *adc;
mbed_official 52:a51c77007319 52 ADC_InitTypeDef ADC_InitStructure;
mbed_official 52:a51c77007319 53
mbed_official 52:a51c77007319 54 // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object
mbed_official 52:a51c77007319 55 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
mbed_official 52:a51c77007319 56
mbed_official 52:a51c77007319 57 if (obj->adc == (ADCName)NC) {
mbed_official 52:a51c77007319 58 error("ADC pin mapping failed");
mbed_official 52:a51c77007319 59 }
mbed_official 52:a51c77007319 60
mbed_official 52:a51c77007319 61 // Configure GPIO
mbed_official 52:a51c77007319 62 pinmap_pinout(pin, PinMap_ADC);
mbed_official 52:a51c77007319 63
mbed_official 52:a51c77007319 64 // Save pin number for the read function
mbed_official 52:a51c77007319 65 obj->pin = pin;
mbed_official 52:a51c77007319 66
mbed_official 52:a51c77007319 67 // The ADC initialization is done once
mbed_official 52:a51c77007319 68 if (adc_inited == 0) {
mbed_official 52:a51c77007319 69 adc_inited = 1;
mbed_official 52:a51c77007319 70
mbed_official 52:a51c77007319 71 // Get ADC registers structure address
mbed_official 52:a51c77007319 72 adc = (ADC_TypeDef *)(obj->adc);
mbed_official 52:a51c77007319 73
mbed_official 52:a51c77007319 74 // Enable ADC clock
mbed_official 52:a51c77007319 75 RCC_ADCCLKConfig(RCC_PCLK2_Div4);
mbed_official 52:a51c77007319 76 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
mbed_official 52:a51c77007319 77
mbed_official 52:a51c77007319 78 // Configure ADC
mbed_official 52:a51c77007319 79 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
mbed_official 52:a51c77007319 80 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
mbed_official 52:a51c77007319 81 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
mbed_official 52:a51c77007319 82 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
mbed_official 52:a51c77007319 83 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
mbed_official 52:a51c77007319 84 ADC_InitStructure.ADC_NbrOfChannel = 1;
mbed_official 52:a51c77007319 85 ADC_Init(adc, &ADC_InitStructure);
mbed_official 52:a51c77007319 86
mbed_official 52:a51c77007319 87 // Enable ADC
mbed_official 52:a51c77007319 88 ADC_Cmd(adc, ENABLE);
mbed_official 52:a51c77007319 89
mbed_official 52:a51c77007319 90 // Calibrate ADC
mbed_official 52:a51c77007319 91 ADC_ResetCalibration(adc);
mbed_official 52:a51c77007319 92 while(ADC_GetResetCalibrationStatus(adc));
mbed_official 52:a51c77007319 93 ADC_StartCalibration(adc);
mbed_official 52:a51c77007319 94 while(ADC_GetCalibrationStatus(adc));
mbed_official 52:a51c77007319 95 }
mbed_official 52:a51c77007319 96 }
mbed_official 52:a51c77007319 97
mbed_official 52:a51c77007319 98 static inline uint16_t adc_read(analogin_t *obj) {
mbed_official 52:a51c77007319 99 // Get ADC registers structure address
mbed_official 52:a51c77007319 100 ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc);
mbed_official 52:a51c77007319 101
mbed_official 52:a51c77007319 102 // Configure ADC channel
mbed_official 52:a51c77007319 103 switch (obj->pin) {
mbed_official 52:a51c77007319 104 case PA_0:
mbed_official 52:a51c77007319 105 ADC_RegularChannelConfig(adc, ADC_Channel_0, 1, ADC_SampleTime_7Cycles5);
mbed_official 52:a51c77007319 106 break;
mbed_official 52:a51c77007319 107 case PA_1:
mbed_official 52:a51c77007319 108 ADC_RegularChannelConfig(adc, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5);
mbed_official 52:a51c77007319 109 break;
mbed_official 52:a51c77007319 110 case PA_4:
mbed_official 52:a51c77007319 111 ADC_RegularChannelConfig(adc, ADC_Channel_4, 1, ADC_SampleTime_7Cycles5);
mbed_official 52:a51c77007319 112 break;
mbed_official 52:a51c77007319 113 case PB_0:
mbed_official 52:a51c77007319 114 ADC_RegularChannelConfig(adc, ADC_Channel_8, 1, ADC_SampleTime_7Cycles5);
mbed_official 52:a51c77007319 115 break;
mbed_official 52:a51c77007319 116 case PC_1:
mbed_official 52:a51c77007319 117 ADC_RegularChannelConfig(adc, ADC_Channel_11, 1, ADC_SampleTime_7Cycles5);
mbed_official 52:a51c77007319 118 break;
mbed_official 52:a51c77007319 119 case PC_0:
mbed_official 52:a51c77007319 120 ADC_RegularChannelConfig(adc, ADC_Channel_10, 1, ADC_SampleTime_7Cycles5);
mbed_official 52:a51c77007319 121 break;
mbed_official 52:a51c77007319 122 default:
mbed_official 52:a51c77007319 123 return 0;
mbed_official 52:a51c77007319 124 }
mbed_official 52:a51c77007319 125
mbed_official 52:a51c77007319 126 ADC_SoftwareStartConvCmd(adc, ENABLE); // Start conversion
mbed_official 52:a51c77007319 127
mbed_official 52:a51c77007319 128 while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion
mbed_official 52:a51c77007319 129
mbed_official 52:a51c77007319 130 return(ADC_GetConversionValue(adc)); // Get conversion value
mbed_official 52:a51c77007319 131 }
mbed_official 52:a51c77007319 132
mbed_official 52:a51c77007319 133 uint16_t analogin_read_u16(analogin_t *obj) {
mbed_official 52:a51c77007319 134 return(adc_read(obj));
mbed_official 52:a51c77007319 135 }
mbed_official 52:a51c77007319 136
mbed_official 52:a51c77007319 137 float analogin_read(analogin_t *obj) {
mbed_official 52:a51c77007319 138 uint16_t value = adc_read(obj);
mbed_official 52:a51c77007319 139 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
mbed_official 52:a51c77007319 140 }
mbed_official 52:a51c77007319 141
mbed_official 52:a51c77007319 142 #endif