]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F334R8/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F3 / TARGET_NUCLEO_F334R8 / 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 void analogin_init(analogin_t *obj, PinName pin)
41 {
42 static int adc1_inited = 0;
43 static int adc2_inited = 0;
44
45 // Get the peripheral name from the pin and assign it to the object
46 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
47 MBED_ASSERT(obj->adc != (ADCName)NC);
48
49 // Configure GPIO
50 pinmap_pinout(pin, PinMap_ADC);
51
52 // Save pin number for the read function
53 obj->pin = pin;
54
55 // Check if ADC is already initialized
56 if ((obj->adc == ADC_1) && adc1_inited) return;
57 if ((obj->adc == ADC_2) && adc2_inited) return;
58 if (obj->adc == ADC_1) adc1_inited = 1;
59 if (obj->adc == ADC_2) adc2_inited = 1;
60
61 // Enable ADC clock
62 __ADC12_CLK_ENABLE();
63
64 // Configure ADC
65 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
66 AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
67 AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
68 AdcHandle.Init.ScanConvMode = DISABLE;
69 AdcHandle.Init.ContinuousConvMode = DISABLE;
70 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
71 AdcHandle.Init.NbrOfDiscConversion = 0;
72 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
73 AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
74 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
75 AdcHandle.Init.NbrOfConversion = 1;
76 AdcHandle.Init.DMAContinuousRequests = DISABLE;
77 AdcHandle.Init.EOCSelection = DISABLE;
78 HAL_ADC_Init(&AdcHandle);
79 }
80
81 static inline uint16_t adc_read(analogin_t *obj)
82 {
83 ADC_ChannelConfTypeDef sConfig;
84
85 AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
86
87 // Configure ADC channel
88 sConfig.Rank = ADC_REGULAR_RANK_1;
89 sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5;
90 sConfig.SingleDiff = ADC_SINGLE_ENDED;
91 sConfig.OffsetNumber = ADC_OFFSET_NONE;
92 sConfig.Offset = 0;
93
94 switch (obj->pin) {
95 case PA_0:
96 sConfig.Channel = ADC_CHANNEL_1;
97 break;
98 case PA_1:
99 sConfig.Channel = ADC_CHANNEL_2;
100 break;
101 case PA_2:
102 sConfig.Channel = ADC_CHANNEL_3;
103 break;
104 case PA_3:
105 sConfig.Channel = ADC_CHANNEL_4;
106 break;
107 case PA_4:
108 sConfig.Channel = ADC_CHANNEL_1;
109 break;
110 case PA_5:
111 sConfig.Channel = ADC_CHANNEL_2;
112 break;
113 case PA_6:
114 sConfig.Channel = ADC_CHANNEL_3;
115 break;
116 case PA_7:
117 sConfig.Channel = ADC_CHANNEL_4;
118 break;
119 case PB_0:
120 sConfig.Channel = ADC_CHANNEL_11;
121 break;
122 case PB_1:
123 sConfig.Channel = ADC_CHANNEL_12;
124 break;
125 case PB_2:
126 sConfig.Channel = ADC_CHANNEL_12;
127 break;
128 case PB_12:
129 sConfig.Channel = ADC_CHANNEL_13;
130 break;
131 case PB_13:
132 sConfig.Channel = ADC_CHANNEL_13;
133 break;
134 case PB_14:
135 sConfig.Channel = ADC_CHANNEL_14;
136 break;
137 case PB_15:
138 sConfig.Channel = ADC_CHANNEL_15;
139 break;
140 case PC_0:
141 sConfig.Channel = ADC_CHANNEL_6;
142 break;
143 case PC_1:
144 sConfig.Channel = ADC_CHANNEL_7;
145 break;
146 case PC_2:
147 sConfig.Channel = ADC_CHANNEL_8;
148 break;
149 case PC_3:
150 sConfig.Channel = ADC_CHANNEL_9;
151 break;
152 case PC_4:
153 sConfig.Channel = ADC_CHANNEL_5;
154 break;
155 case PC_5:
156 sConfig.Channel = ADC_CHANNEL_11;
157 break;
158 default:
159 return 0;
160 }
161
162 HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
163
164 HAL_ADC_Start(&AdcHandle); // Start conversion
165
166 // Wait end of conversion and get value
167 if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
168 return (HAL_ADC_GetValue(&AdcHandle));
169 } else {
170 return 0;
171 }
172 }
173
174 uint16_t analogin_read_u16(analogin_t *obj)
175 {
176 uint16_t value = adc_read(obj);
177 // 12-bit to 16-bit conversion
178 value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
179 return value;
180 }
181
182 float analogin_read(analogin_t *obj)
183 {
184 uint16_t value = adc_read(obj);
185 return (float)value * (1.0f / (float)0xFFF); // 12 bits range
186 }
187
188 #endif
Imprint / Impressum