mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F051R8/analogin_api.c@227:7bd0639b8911, 2014-06-11 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 11 16:00:09 2014 +0100
- Revision:
- 227:7bd0639b8911
- Parent:
- 130:1dec54e4aec3
- Child:
- 240:9a7c54113eaf
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af
Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 130:1dec54e4aec3 | 1 | /* mbed Microcontroller Library |
mbed_official | 130:1dec54e4aec3 | 2 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 130:1dec54e4aec3 | 3 | * All rights reserved. |
mbed_official | 130:1dec54e4aec3 | 4 | * |
mbed_official | 130:1dec54e4aec3 | 5 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 130:1dec54e4aec3 | 6 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 130:1dec54e4aec3 | 7 | * |
mbed_official | 130:1dec54e4aec3 | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 130:1dec54e4aec3 | 9 | * this list of conditions and the following disclaimer. |
mbed_official | 130:1dec54e4aec3 | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 130:1dec54e4aec3 | 11 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 130:1dec54e4aec3 | 12 | * and/or other materials provided with the distribution. |
mbed_official | 130:1dec54e4aec3 | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 130:1dec54e4aec3 | 14 | * may be used to endorse or promote products derived from this software |
mbed_official | 130:1dec54e4aec3 | 15 | * without specific prior written permission. |
mbed_official | 130:1dec54e4aec3 | 16 | * |
mbed_official | 130:1dec54e4aec3 | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 130:1dec54e4aec3 | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 130:1dec54e4aec3 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 130:1dec54e4aec3 | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 130:1dec54e4aec3 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 130:1dec54e4aec3 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 130:1dec54e4aec3 | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 130:1dec54e4aec3 | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 130:1dec54e4aec3 | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 130:1dec54e4aec3 | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 130:1dec54e4aec3 | 27 | */ |
mbed_official | 227:7bd0639b8911 | 28 | #include "mbed_assert.h" |
mbed_official | 130:1dec54e4aec3 | 29 | #include "analogin_api.h" |
mbed_official | 130:1dec54e4aec3 | 30 | #include "wait_api.h" |
mbed_official | 130:1dec54e4aec3 | 31 | |
mbed_official | 130:1dec54e4aec3 | 32 | #if DEVICE_ANALOGIN |
mbed_official | 130:1dec54e4aec3 | 33 | |
mbed_official | 130:1dec54e4aec3 | 34 | #include "cmsis.h" |
mbed_official | 130:1dec54e4aec3 | 35 | #include "pinmap.h" |
mbed_official | 130:1dec54e4aec3 | 36 | |
mbed_official | 130:1dec54e4aec3 | 37 | static const PinMap PinMap_ADC[] = { |
mbed_official | 130:1dec54e4aec3 | 38 | {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0 |
mbed_official | 130:1dec54e4aec3 | 39 | {PA_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN1 |
mbed_official | 130:1dec54e4aec3 | 40 | {PA_4, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN4 |
mbed_official | 130:1dec54e4aec3 | 41 | {PB_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN8 |
mbed_official | 130:1dec54e4aec3 | 42 | {PC_1, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN11 |
mbed_official | 130:1dec54e4aec3 | 43 | {PC_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN10 |
mbed_official | 130:1dec54e4aec3 | 44 | {NC, NC, 0} |
mbed_official | 130:1dec54e4aec3 | 45 | }; |
mbed_official | 130:1dec54e4aec3 | 46 | |
mbed_official | 130:1dec54e4aec3 | 47 | int adc_inited = 0; |
mbed_official | 130:1dec54e4aec3 | 48 | |
mbed_official | 130:1dec54e4aec3 | 49 | void analogin_init(analogin_t *obj, PinName pin) { |
mbed_official | 130:1dec54e4aec3 | 50 | |
mbed_official | 130:1dec54e4aec3 | 51 | ADC_TypeDef *adc; |
mbed_official | 130:1dec54e4aec3 | 52 | ADC_InitTypeDef ADC_InitStructure; |
mbed_official | 130:1dec54e4aec3 | 53 | |
mbed_official | 130:1dec54e4aec3 | 54 | // Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object |
mbed_official | 130:1dec54e4aec3 | 55 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 227:7bd0639b8911 | 56 | MBED_ASSERT(obj->adc != (ADCName)NC); |
mbed_official | 130:1dec54e4aec3 | 57 | |
mbed_official | 130:1dec54e4aec3 | 58 | // Configure GPIO |
mbed_official | 130:1dec54e4aec3 | 59 | pinmap_pinout(pin, PinMap_ADC); |
mbed_official | 130:1dec54e4aec3 | 60 | |
mbed_official | 130:1dec54e4aec3 | 61 | // Save pin number for the read function |
mbed_official | 130:1dec54e4aec3 | 62 | obj->pin = pin; |
mbed_official | 130:1dec54e4aec3 | 63 | |
mbed_official | 130:1dec54e4aec3 | 64 | // The ADC initialization is done once |
mbed_official | 130:1dec54e4aec3 | 65 | if (adc_inited == 0) { |
mbed_official | 130:1dec54e4aec3 | 66 | adc_inited = 1; |
mbed_official | 130:1dec54e4aec3 | 67 | |
mbed_official | 130:1dec54e4aec3 | 68 | // Get ADC registers structure address |
mbed_official | 130:1dec54e4aec3 | 69 | adc = (ADC_TypeDef *)(obj->adc); |
mbed_official | 130:1dec54e4aec3 | 70 | |
mbed_official | 130:1dec54e4aec3 | 71 | // Enable ADC clock |
mbed_official | 130:1dec54e4aec3 | 72 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 73 | |
mbed_official | 130:1dec54e4aec3 | 74 | // Configure ADC |
mbed_official | 130:1dec54e4aec3 | 75 | ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; |
mbed_official | 227:7bd0639b8911 | 76 | ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; |
mbed_official | 130:1dec54e4aec3 | 77 | ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; |
mbed_official | 130:1dec54e4aec3 | 78 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO; |
mbed_official | 130:1dec54e4aec3 | 79 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; |
mbed_official | 130:1dec54e4aec3 | 80 | ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; |
mbed_official | 130:1dec54e4aec3 | 81 | ADC_Init(adc, &ADC_InitStructure); |
mbed_official | 130:1dec54e4aec3 | 82 | |
mbed_official | 130:1dec54e4aec3 | 83 | // Calibrate ADC |
mbed_official | 130:1dec54e4aec3 | 84 | ADC_GetCalibrationFactor(adc); |
mbed_official | 130:1dec54e4aec3 | 85 | |
mbed_official | 130:1dec54e4aec3 | 86 | // Enable ADC |
mbed_official | 130:1dec54e4aec3 | 87 | ADC_Cmd(adc, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 88 | } |
mbed_official | 130:1dec54e4aec3 | 89 | } |
mbed_official | 130:1dec54e4aec3 | 90 | |
mbed_official | 130:1dec54e4aec3 | 91 | static inline uint16_t adc_read(analogin_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 92 | // Get ADC registers structure address |
mbed_official | 130:1dec54e4aec3 | 93 | ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc); |
mbed_official | 130:1dec54e4aec3 | 94 | |
mbed_official | 130:1dec54e4aec3 | 95 | adc->CHSELR = 0; // Clear all channels first |
mbed_official | 130:1dec54e4aec3 | 96 | |
mbed_official | 130:1dec54e4aec3 | 97 | // Configure ADC channel |
mbed_official | 130:1dec54e4aec3 | 98 | switch (obj->pin) { |
mbed_official | 130:1dec54e4aec3 | 99 | case PA_0: |
mbed_official | 130:1dec54e4aec3 | 100 | ADC_ChannelConfig(adc, ADC_Channel_0, ADC_SampleTime_7_5Cycles); |
mbed_official | 130:1dec54e4aec3 | 101 | break; |
mbed_official | 130:1dec54e4aec3 | 102 | case PA_1: |
mbed_official | 130:1dec54e4aec3 | 103 | ADC_ChannelConfig(adc, ADC_Channel_1, ADC_SampleTime_7_5Cycles); |
mbed_official | 130:1dec54e4aec3 | 104 | break; |
mbed_official | 130:1dec54e4aec3 | 105 | case PA_4: |
mbed_official | 130:1dec54e4aec3 | 106 | ADC_ChannelConfig(adc, ADC_Channel_4, ADC_SampleTime_7_5Cycles); |
mbed_official | 130:1dec54e4aec3 | 107 | break; |
mbed_official | 130:1dec54e4aec3 | 108 | case PB_0: |
mbed_official | 130:1dec54e4aec3 | 109 | ADC_ChannelConfig(adc, ADC_Channel_8, ADC_SampleTime_7_5Cycles); |
mbed_official | 130:1dec54e4aec3 | 110 | break; |
mbed_official | 130:1dec54e4aec3 | 111 | case PC_1: |
mbed_official | 130:1dec54e4aec3 | 112 | ADC_ChannelConfig(adc, ADC_Channel_11, ADC_SampleTime_7_5Cycles); |
mbed_official | 130:1dec54e4aec3 | 113 | break; |
mbed_official | 130:1dec54e4aec3 | 114 | case PC_0: |
mbed_official | 130:1dec54e4aec3 | 115 | ADC_ChannelConfig(adc, ADC_Channel_10, ADC_SampleTime_7_5Cycles); |
mbed_official | 130:1dec54e4aec3 | 116 | break; |
mbed_official | 130:1dec54e4aec3 | 117 | default: |
mbed_official | 130:1dec54e4aec3 | 118 | return 0; |
mbed_official | 130:1dec54e4aec3 | 119 | } |
mbed_official | 130:1dec54e4aec3 | 120 | |
mbed_official | 130:1dec54e4aec3 | 121 | while(!ADC_GetFlagStatus(adc, ADC_FLAG_ADRDY)); // Wait ADC ready |
mbed_official | 130:1dec54e4aec3 | 122 | |
mbed_official | 130:1dec54e4aec3 | 123 | ADC_StartOfConversion(adc); // Start conversion |
mbed_official | 130:1dec54e4aec3 | 124 | |
mbed_official | 130:1dec54e4aec3 | 125 | while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion |
mbed_official | 130:1dec54e4aec3 | 126 | |
mbed_official | 130:1dec54e4aec3 | 127 | return(ADC_GetConversionValue(adc)); // Get conversion value |
mbed_official | 130:1dec54e4aec3 | 128 | } |
mbed_official | 130:1dec54e4aec3 | 129 | |
mbed_official | 130:1dec54e4aec3 | 130 | uint16_t analogin_read_u16(analogin_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 131 | return(adc_read(obj)); |
mbed_official | 130:1dec54e4aec3 | 132 | } |
mbed_official | 130:1dec54e4aec3 | 133 | |
mbed_official | 130:1dec54e4aec3 | 134 | float analogin_read(analogin_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 135 | uint16_t value = adc_read(obj); |
mbed_official | 130:1dec54e4aec3 | 136 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
mbed_official | 130:1dec54e4aec3 | 137 | } |
mbed_official | 130:1dec54e4aec3 | 138 | |
mbed_official | 130:1dec54e4aec3 | 139 | #endif |