1 /* mbed Microcontroller Library
2 * Copyright (c) 2014, STMicroelectronics
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "mbed_assert.h"
29 #include "analogin_api.h"
36 #include "PeripheralPins.h"
38 ADC_HandleTypeDef AdcHandle;
42 void analogin_init(analogin_t *obj, PinName pin)
44 // Get the peripheral name from the pin and assign it to the object
45 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
46 MBED_ASSERT(obj->adc != (ADCName)NC);
49 pinmap_pinout(pin, PinMap_ADC);
51 // Save pin number for the read function
54 // The ADC initialization is done once
55 if (adc_inited == 0) {
58 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
64 AdcHandle.Init.OversamplingMode = DISABLE;
65 AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
66 AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
67 AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_41CYCLES_5;
68 AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
69 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
70 AdcHandle.Init.ContinuousConvMode = DISABLE;
71 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
72 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
73 AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
74 AdcHandle.Init.DMAContinuousRequests = DISABLE;
75 AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
76 AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
77 AdcHandle.Init.LowPowerAutoWait = ENABLE;
78 AdcHandle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
79 AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
80 HAL_ADC_Init(&AdcHandle);
83 HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED);
85 __HAL_ADC_ENABLE(&AdcHandle);
89 static inline uint16_t adc_read(analogin_t *obj)
91 ADC_ChannelConfTypeDef sConfig;
93 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
95 // Configure ADC channel
98 sConfig.Channel = ADC_CHANNEL_0;
101 sConfig.Channel = ADC_CHANNEL_1;
104 sConfig.Channel = ADC_CHANNEL_2;
107 sConfig.Channel = ADC_CHANNEL_3;
110 sConfig.Channel = ADC_CHANNEL_4;
113 sConfig.Channel = ADC_CHANNEL_5;
116 sConfig.Channel = ADC_CHANNEL_6;
119 sConfig.Channel = ADC_CHANNEL_7;
122 sConfig.Channel = ADC_CHANNEL_8;
125 sConfig.Channel = ADC_CHANNEL_9;
128 sConfig.Channel = ADC_CHANNEL_10;
131 sConfig.Channel = ADC_CHANNEL_11;
134 sConfig.Channel = ADC_CHANNEL_12;
137 sConfig.Channel = ADC_CHANNEL_13;
140 sConfig.Channel = ADC_CHANNEL_14;
143 sConfig.Channel = ADC_CHANNEL_15;
149 ADC1->CHSELR = 0; // [TODO] Workaround. To be removed after Cube driver is corrected.
150 HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
152 HAL_ADC_Start(&AdcHandle); // Start conversion
154 // Wait end of conversion and get value
155 if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
156 return (HAL_ADC_GetValue(&AdcHandle));
162 uint16_t analogin_read_u16(analogin_t *obj)
164 uint16_t value = adc_read(obj);
165 // 12-bit to 16-bit conversion
166 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
170 float analogin_read(analogin_t *obj)
172 uint16_t value = adc_read(obj);
173 return (float)value * (1.0f / (float)0xFFF); // 12 bits range