mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Thu Jan 08 12:00:08 2015 +0000
Revision:
442:d916d321e60f
Parent:
targets/hal/TARGET_STM/TARGET_DISCO_F429ZI/analogin_api.c@428:4ddf7f7eabbb
Child:
511:532f83b66a7f
Synchronized with git revision 2acefb66eb35837a685ce640b2b15e068d4ccf5b

Full URL: https://github.com/mbedmicro/mbed/commit/2acefb66eb35837a685ce640b2b15e068d4ccf5b/

NUCLEO_F334R8 - Fix issue with multiple ADC initialization

Who changed what in which revision?

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