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 RCC_OscInitTypeDef RCC_OscInitStruct;
46 // Get the peripheral name from the pin and assign it to the object
47 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
49 MBED_ASSERT(obj->adc != (ADCName)NC);
52 pinmap_pinout(pin, PinMap_ADC);
54 // Save pin number for the read function
57 // The ADC initialization is done once
58 if (adc_inited == 0) {
61 // Enable the HSI (to clock the ADC)
62 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
63 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
64 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
65 HAL_RCC_OscConfig(&RCC_OscInitStruct);
67 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
73 AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
74 AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
75 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
76 AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
77 AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
78 AdcHandle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
79 AdcHandle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
80 AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
81 AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
82 AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
83 AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
84 AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
85 AdcHandle.Init.ExternalTrigConv = 0; // Not used
86 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
87 AdcHandle.Init.DMAContinuousRequests = DISABLE;
88 HAL_ADC_Init(&AdcHandle);
92 static inline uint16_t adc_read(analogin_t *obj)
94 ADC_ChannelConfTypeDef sConfig;
96 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
98 // Configure ADC channel
101 sConfig.Channel = ADC_CHANNEL_0;
104 sConfig.Channel = ADC_CHANNEL_1;
107 sConfig.Channel = ADC_CHANNEL_2;
110 sConfig.Channel = ADC_CHANNEL_3;
113 sConfig.Channel = ADC_CHANNEL_4;
116 sConfig.Channel = ADC_CHANNEL_5;
119 sConfig.Channel = ADC_CHANNEL_6;
122 sConfig.Channel = ADC_CHANNEL_7;
125 sConfig.Channel = ADC_CHANNEL_8;
128 sConfig.Channel = ADC_CHANNEL_9;
131 sConfig.Channel = ADC_CHANNEL_10;
134 sConfig.Channel = ADC_CHANNEL_11;
137 sConfig.Channel = ADC_CHANNEL_12;
140 sConfig.Channel = ADC_CHANNEL_13;
143 sConfig.Channel = ADC_CHANNEL_14;
146 sConfig.Channel = ADC_CHANNEL_15;
149 sConfig.Channel = ADC_CHANNEL_18;
152 sConfig.Channel = ADC_CHANNEL_19;
155 sConfig.Channel = ADC_CHANNEL_20;
158 sConfig.Channel = ADC_CHANNEL_21;
164 sConfig.Rank = ADC_REGULAR_RANK_1;
165 sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES;
167 HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
169 HAL_ADC_Start(&AdcHandle); // Start conversion
171 // Wait end of conversion and get value
172 if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
173 return (HAL_ADC_GetValue(&AdcHandle));
179 uint16_t analogin_read_u16(analogin_t *obj)
181 uint16_t value = adc_read(obj);
182 // 12-bit to 16-bit conversion
183 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
187 float analogin_read(analogin_t *obj)
189 uint16_t value = adc_read(obj);
190 return (float)value * (1.0f / (float)0xFFF); // 12 bits range