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