mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F411RE/analogin_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 298:7557d401dbc3
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 235:685d5f11838f | 1 | /* mbed Microcontroller Library |
mbed_official | 235:685d5f11838f | 2 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 235:685d5f11838f | 3 | * All rights reserved. |
mbed_official | 235:685d5f11838f | 4 | * |
mbed_official | 235:685d5f11838f | 5 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 235:685d5f11838f | 6 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 235:685d5f11838f | 7 | * |
mbed_official | 235:685d5f11838f | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 235:685d5f11838f | 9 | * this list of conditions and the following disclaimer. |
mbed_official | 235:685d5f11838f | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 235:685d5f11838f | 11 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 235:685d5f11838f | 12 | * and/or other materials provided with the distribution. |
mbed_official | 235:685d5f11838f | 13 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 235:685d5f11838f | 14 | * may be used to endorse or promote products derived from this software |
mbed_official | 235:685d5f11838f | 15 | * without specific prior written permission. |
mbed_official | 235:685d5f11838f | 16 | * |
mbed_official | 235:685d5f11838f | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 235:685d5f11838f | 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 235:685d5f11838f | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 235:685d5f11838f | 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 235:685d5f11838f | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 235:685d5f11838f | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 235:685d5f11838f | 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 235:685d5f11838f | 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 235:685d5f11838f | 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 235:685d5f11838f | 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 235:685d5f11838f | 27 | */ |
mbed_official | 235:685d5f11838f | 28 | #include "mbed_assert.h" |
mbed_official | 235:685d5f11838f | 29 | #include "analogin_api.h" |
mbed_official | 235:685d5f11838f | 30 | |
mbed_official | 235:685d5f11838f | 31 | #if DEVICE_ANALOGIN |
mbed_official | 235:685d5f11838f | 32 | |
mbed_official | 235:685d5f11838f | 33 | #include "wait_api.h" |
mbed_official | 235:685d5f11838f | 34 | #include "cmsis.h" |
mbed_official | 235:685d5f11838f | 35 | #include "pinmap.h" |
mbed_official | 235:685d5f11838f | 36 | |
mbed_official | 235:685d5f11838f | 37 | static const PinMap PinMap_ADC[] = { |
mbed_official | 235:685d5f11838f | 38 | {PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN0 |
mbed_official | 235:685d5f11838f | 39 | {PA_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN1 |
mbed_official | 235:685d5f11838f | 40 | {PA_2, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN2 |
mbed_official | 235:685d5f11838f | 41 | {PA_3, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN3 |
mbed_official | 235:685d5f11838f | 42 | {PA_4, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN4 |
mbed_official | 235:685d5f11838f | 43 | {PA_5, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN5 |
mbed_official | 235:685d5f11838f | 44 | {PA_6, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN6 |
mbed_official | 235:685d5f11838f | 45 | {PA_7, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN7 |
mbed_official | 235:685d5f11838f | 46 | {PB_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN8 |
mbed_official | 235:685d5f11838f | 47 | {PB_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN9 |
mbed_official | 235:685d5f11838f | 48 | {PC_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN10 |
mbed_official | 235:685d5f11838f | 49 | {PC_1, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN11 |
mbed_official | 235:685d5f11838f | 50 | {PC_2, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN12 |
mbed_official | 235:685d5f11838f | 51 | {PC_3, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN13 |
mbed_official | 235:685d5f11838f | 52 | {PC_4, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN14 |
mbed_official | 235:685d5f11838f | 53 | {PC_5, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN15 |
mbed_official | 235:685d5f11838f | 54 | {NC, NC, 0} |
mbed_official | 235:685d5f11838f | 55 | }; |
mbed_official | 235:685d5f11838f | 56 | |
mbed_official | 235:685d5f11838f | 57 | ADC_HandleTypeDef AdcHandle; |
mbed_official | 235:685d5f11838f | 58 | |
mbed_official | 235:685d5f11838f | 59 | int adc_inited = 0; |
mbed_official | 235:685d5f11838f | 60 | |
mbed_official | 235:685d5f11838f | 61 | void analogin_init(analogin_t *obj, PinName pin) |
mbed_official | 235:685d5f11838f | 62 | { |
mbed_official | 235:685d5f11838f | 63 | // Get the peripheral name from the pin and assign it to the object |
mbed_official | 235:685d5f11838f | 64 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); |
mbed_official | 235:685d5f11838f | 65 | MBED_ASSERT(obj->adc != (ADCName)NC); |
mbed_official | 235:685d5f11838f | 66 | |
mbed_official | 235:685d5f11838f | 67 | // Configure GPIO |
mbed_official | 235:685d5f11838f | 68 | pinmap_pinout(pin, PinMap_ADC); |
mbed_official | 235:685d5f11838f | 69 | |
mbed_official | 235:685d5f11838f | 70 | // Save pin number for the read function |
mbed_official | 235:685d5f11838f | 71 | obj->pin = pin; |
mbed_official | 235:685d5f11838f | 72 | |
mbed_official | 235:685d5f11838f | 73 | // The ADC initialization is done once |
mbed_official | 235:685d5f11838f | 74 | if (adc_inited == 0) { |
mbed_official | 235:685d5f11838f | 75 | adc_inited = 1; |
mbed_official | 235:685d5f11838f | 76 | |
mbed_official | 235:685d5f11838f | 77 | // Enable ADC clock |
mbed_official | 235:685d5f11838f | 78 | __ADC1_CLK_ENABLE(); |
mbed_official | 235:685d5f11838f | 79 | |
mbed_official | 235:685d5f11838f | 80 | // Configure ADC |
mbed_official | 235:685d5f11838f | 81 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
mbed_official | 235:685d5f11838f | 82 | AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; |
mbed_official | 235:685d5f11838f | 83 | AdcHandle.Init.Resolution = ADC_RESOLUTION12b; |
mbed_official | 235:685d5f11838f | 84 | AdcHandle.Init.ScanConvMode = DISABLE; |
mbed_official | 235:685d5f11838f | 85 | AdcHandle.Init.ContinuousConvMode = DISABLE; |
mbed_official | 235:685d5f11838f | 86 | AdcHandle.Init.DiscontinuousConvMode = DISABLE; |
mbed_official | 235:685d5f11838f | 87 | AdcHandle.Init.NbrOfDiscConversion = 0; |
mbed_official | 235:685d5f11838f | 88 | AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
mbed_official | 235:685d5f11838f | 89 | AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; |
mbed_official | 235:685d5f11838f | 90 | AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
mbed_official | 235:685d5f11838f | 91 | AdcHandle.Init.NbrOfConversion = 1; |
mbed_official | 235:685d5f11838f | 92 | AdcHandle.Init.DMAContinuousRequests = DISABLE; |
mbed_official | 235:685d5f11838f | 93 | AdcHandle.Init.EOCSelection = DISABLE; |
mbed_official | 235:685d5f11838f | 94 | HAL_ADC_Init(&AdcHandle); |
mbed_official | 235:685d5f11838f | 95 | } |
mbed_official | 235:685d5f11838f | 96 | } |
mbed_official | 235:685d5f11838f | 97 | |
mbed_official | 235:685d5f11838f | 98 | static inline uint16_t adc_read(analogin_t *obj) |
mbed_official | 235:685d5f11838f | 99 | { |
mbed_official | 235:685d5f11838f | 100 | ADC_ChannelConfTypeDef sConfig; |
mbed_official | 235:685d5f11838f | 101 | |
mbed_official | 235:685d5f11838f | 102 | AdcHandle.Instance = (ADC_TypeDef *)(obj->adc); |
mbed_official | 235:685d5f11838f | 103 | |
mbed_official | 235:685d5f11838f | 104 | // Configure ADC channel |
mbed_official | 235:685d5f11838f | 105 | sConfig.Rank = 1; |
mbed_official | 235:685d5f11838f | 106 | sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; |
mbed_official | 235:685d5f11838f | 107 | sConfig.Offset = 0; |
mbed_official | 235:685d5f11838f | 108 | |
mbed_official | 235:685d5f11838f | 109 | switch (obj->pin) { |
mbed_official | 235:685d5f11838f | 110 | case PA_0: |
mbed_official | 235:685d5f11838f | 111 | sConfig.Channel = ADC_CHANNEL_0; |
mbed_official | 235:685d5f11838f | 112 | break; |
mbed_official | 235:685d5f11838f | 113 | case PA_1: |
mbed_official | 235:685d5f11838f | 114 | sConfig.Channel = ADC_CHANNEL_1; |
mbed_official | 235:685d5f11838f | 115 | break; |
mbed_official | 235:685d5f11838f | 116 | case PA_2: |
mbed_official | 235:685d5f11838f | 117 | sConfig.Channel = ADC_CHANNEL_2; |
mbed_official | 235:685d5f11838f | 118 | break; |
mbed_official | 235:685d5f11838f | 119 | case PA_3: |
mbed_official | 235:685d5f11838f | 120 | sConfig.Channel = ADC_CHANNEL_3; |
mbed_official | 235:685d5f11838f | 121 | break; |
mbed_official | 235:685d5f11838f | 122 | case PA_4: |
mbed_official | 235:685d5f11838f | 123 | sConfig.Channel = ADC_CHANNEL_4; |
mbed_official | 235:685d5f11838f | 124 | break; |
mbed_official | 235:685d5f11838f | 125 | case PA_5: |
mbed_official | 235:685d5f11838f | 126 | sConfig.Channel = ADC_CHANNEL_5; |
mbed_official | 235:685d5f11838f | 127 | break; |
mbed_official | 235:685d5f11838f | 128 | case PA_6: |
mbed_official | 235:685d5f11838f | 129 | sConfig.Channel = ADC_CHANNEL_6; |
mbed_official | 235:685d5f11838f | 130 | break; |
mbed_official | 235:685d5f11838f | 131 | case PA_7: |
mbed_official | 235:685d5f11838f | 132 | sConfig.Channel = ADC_CHANNEL_7; |
mbed_official | 235:685d5f11838f | 133 | break; |
mbed_official | 235:685d5f11838f | 134 | case PB_0: |
mbed_official | 235:685d5f11838f | 135 | sConfig.Channel = ADC_CHANNEL_8; |
mbed_official | 235:685d5f11838f | 136 | break; |
mbed_official | 235:685d5f11838f | 137 | case PB_1: |
mbed_official | 235:685d5f11838f | 138 | sConfig.Channel = ADC_CHANNEL_9; |
mbed_official | 235:685d5f11838f | 139 | break; |
mbed_official | 235:685d5f11838f | 140 | case PC_0: |
mbed_official | 235:685d5f11838f | 141 | sConfig.Channel = ADC_CHANNEL_10; |
mbed_official | 235:685d5f11838f | 142 | break; |
mbed_official | 235:685d5f11838f | 143 | case PC_1: |
mbed_official | 235:685d5f11838f | 144 | sConfig.Channel = ADC_CHANNEL_11; |
mbed_official | 235:685d5f11838f | 145 | break; |
mbed_official | 235:685d5f11838f | 146 | case PC_2: |
mbed_official | 235:685d5f11838f | 147 | sConfig.Channel = ADC_CHANNEL_12; |
mbed_official | 235:685d5f11838f | 148 | break; |
mbed_official | 235:685d5f11838f | 149 | case PC_3: |
mbed_official | 235:685d5f11838f | 150 | sConfig.Channel = ADC_CHANNEL_13; |
mbed_official | 235:685d5f11838f | 151 | break; |
mbed_official | 235:685d5f11838f | 152 | case PC_4: |
mbed_official | 235:685d5f11838f | 153 | sConfig.Channel = ADC_CHANNEL_14; |
mbed_official | 235:685d5f11838f | 154 | break; |
mbed_official | 235:685d5f11838f | 155 | case PC_5: |
mbed_official | 235:685d5f11838f | 156 | sConfig.Channel = ADC_CHANNEL_15; |
mbed_official | 235:685d5f11838f | 157 | break; |
mbed_official | 235:685d5f11838f | 158 | default: |
mbed_official | 235:685d5f11838f | 159 | return 0; |
mbed_official | 235:685d5f11838f | 160 | } |
mbed_official | 235:685d5f11838f | 161 | |
mbed_official | 235:685d5f11838f | 162 | HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); |
mbed_official | 235:685d5f11838f | 163 | |
mbed_official | 235:685d5f11838f | 164 | HAL_ADC_Start(&AdcHandle); // Start conversion |
mbed_official | 235:685d5f11838f | 165 | |
mbed_official | 235:685d5f11838f | 166 | // Wait end of conversion and get value |
mbed_official | 235:685d5f11838f | 167 | if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) { |
mbed_official | 235:685d5f11838f | 168 | return (HAL_ADC_GetValue(&AdcHandle)); |
mbed_official | 235:685d5f11838f | 169 | } else { |
mbed_official | 235:685d5f11838f | 170 | return 0; |
mbed_official | 235:685d5f11838f | 171 | } |
mbed_official | 235:685d5f11838f | 172 | } |
mbed_official | 235:685d5f11838f | 173 | |
mbed_official | 235:685d5f11838f | 174 | uint16_t analogin_read_u16(analogin_t *obj) |
mbed_official | 235:685d5f11838f | 175 | { |
mbed_official | 298:7557d401dbc3 | 176 | uint16_t value = adc_read(obj); |
mbed_official | 298:7557d401dbc3 | 177 | // 12-bit to 16-bit conversion |
mbed_official | 298:7557d401dbc3 | 178 | value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F); |
mbed_official | 298:7557d401dbc3 | 179 | return value; |
mbed_official | 235:685d5f11838f | 180 | } |
mbed_official | 235:685d5f11838f | 181 | |
mbed_official | 235:685d5f11838f | 182 | float analogin_read(analogin_t *obj) |
mbed_official | 235:685d5f11838f | 183 | { |
mbed_official | 235:685d5f11838f | 184 | uint16_t value = adc_read(obj); |
mbed_official | 235:685d5f11838f | 185 | return (float)value * (1.0f / (float)0xFFF); // 12 bits range |
mbed_official | 235:685d5f11838f | 186 | } |
mbed_official | 235:685d5f11838f | 187 | |
mbed_official | 235:685d5f11838f | 188 | #endif |