]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F4/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F4 / analogin_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2014, STMicroelectronics
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
16 *
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.
27 */
28 #include "mbed_assert.h"
29 #include "analogin_api.h"
30
31 #if DEVICE_ANALOGIN
32
33 #include "wait_api.h"
34 #include "cmsis.h"
35 #include "pinmap.h"
36 #include "PeripheralPins.h"
37
38 ADC_HandleTypeDef AdcHandle;
39
40 int adc_inited = 0;
41
42 void analogin_init(analogin_t *obj, PinName pin)
43 {
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);
47
48 // Get the functions (adc channel) from the pin and assign it to the object
49 uint32_t function = pinmap_function(pin, PinMap_ADC);
50 MBED_ASSERT(function != (uint32_t)NC);
51 obj->channel = STM_PIN_CHANNEL(function);
52
53 // Configure GPIO
54 pinmap_pinout(pin, PinMap_ADC);
55
56 // Save pin number for the read function
57 obj->pin = pin;
58
59 // The ADC initialization is done once
60 if (adc_inited == 0) {
61 adc_inited = 1;
62
63 // Enable ADC clock
64 __ADC1_CLK_ENABLE();
65
66 // Configure ADC
67 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
68 AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
69 AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
70 AdcHandle.Init.ScanConvMode = DISABLE;
71 AdcHandle.Init.ContinuousConvMode = DISABLE;
72 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
73 AdcHandle.Init.NbrOfDiscConversion = 0;
74 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
75 AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
76 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
77 AdcHandle.Init.NbrOfConversion = 1;
78 AdcHandle.Init.DMAContinuousRequests = DISABLE;
79 AdcHandle.Init.EOCSelection = DISABLE;
80 HAL_ADC_Init(&AdcHandle);
81 }
82 }
83
84 static inline uint16_t adc_read(analogin_t *obj)
85 {
86 ADC_ChannelConfTypeDef sConfig;
87
88 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
89
90 // Configure ADC channel
91 sConfig.Rank = 1;
92 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
93 sConfig.Offset = 0;
94
95 switch (obj->channel) {
96 case 0:
97 sConfig.Channel = ADC_CHANNEL_0;
98 break;
99 case 1:
100 sConfig.Channel = ADC_CHANNEL_1;
101 break;
102 case 2:
103 sConfig.Channel = ADC_CHANNEL_2;
104 break;
105 case 3:
106 sConfig.Channel = ADC_CHANNEL_3;
107 break;
108 case 4:
109 sConfig.Channel = ADC_CHANNEL_4;
110 break;
111 case 5:
112 sConfig.Channel = ADC_CHANNEL_5;
113 break;
114 case 6:
115 sConfig.Channel = ADC_CHANNEL_6;
116 break;
117 case 7:
118 sConfig.Channel = ADC_CHANNEL_7;
119 break;
120 case 8:
121 sConfig.Channel = ADC_CHANNEL_8;
122 break;
123 case 9:
124 sConfig.Channel = ADC_CHANNEL_9;
125 break;
126 case 10:
127 sConfig.Channel = ADC_CHANNEL_10;
128 break;
129 case 11:
130 sConfig.Channel = ADC_CHANNEL_11;
131 break;
132 case 12:
133 sConfig.Channel = ADC_CHANNEL_12;
134 break;
135 case 13:
136 sConfig.Channel = ADC_CHANNEL_13;
137 break;
138 case 14:
139 sConfig.Channel = ADC_CHANNEL_14;
140 break;
141 case 15:
142 sConfig.Channel = ADC_CHANNEL_15;
143 break;
144 default:
145 return 0;
146 }
147
148 HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
149
150 HAL_ADC_Start(&AdcHandle); // Start conversion
151
152 // Wait end of conversion and get value
153 if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
154 return (HAL_ADC_GetValue(&AdcHandle));
155 } else {
156 return 0;
157 }
158 }
159
160 uint16_t analogin_read_u16(analogin_t *obj)
161 {
162 uint16_t value = adc_read(obj);
163 // 12-bit to 16-bit conversion
164 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
165 return value;
166 }
167
168 float analogin_read(analogin_t *obj)
169 {
170 uint16_t value = adc_read(obj);
171 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
172 }
173
174 #endif
Imprint / Impressum