]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F1/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F1 / 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     RCC_PeriphCLKInitTypeDef  PeriphClkInit;
45
46     // Get the peripheral name from the pin and assign it to the object
47     obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
48     MBED_ASSERT(obj->adc != (ADCName)NC);
49
50     // Configure GPIO
51     pinmap_pinout(pin, PinMap_ADC);
52
53     // Save pin number for the read function
54     obj->pin = pin;
55
56     // The ADC initialization is done once
57     if (adc_inited == 0) {
58         adc_inited = 1;
59
60         // Enable ADC clock
61         __HAL_RCC_ADC1_CLK_ENABLE();
62
63         // Configure ADC clock prescaler
64         // Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet).
65         // Therefore, ADC clock prescaler must be configured in function
66         // of ADC clock source frequency to remain below this maximum frequency.
67         // with 8 MHz external xtal: PCLK2 = 72 MHz --> ADC clock = 72/6 = 12 MHz
68         // with internal clock     : PCLK2 = 64 MHz --> ADC clock = 64/6 = 10.67 MHz
69         PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
70         PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
71         HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
72
73         // Configure ADC
74         AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
75         AdcHandle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
76         AdcHandle.Init.ScanConvMode          = DISABLE;
77         AdcHandle.Init.ContinuousConvMode    = DISABLE;
78         AdcHandle.Init.NbrOfConversion       = 1;
79         AdcHandle.Init.DiscontinuousConvMode = DISABLE;
80         AdcHandle.Init.NbrOfDiscConversion   = 0;
81         AdcHandle.Init.ExternalTrigConv      = ADC_EXTERNALTRIGCONV_T1_CC1;
82         HAL_ADC_Init(&AdcHandle);
83     }
84 }
85
86 static inline uint16_t adc_read(analogin_t *obj)
87 {
88     ADC_ChannelConfTypeDef sConfig;
89
90     AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
91
92     // Configure ADC channel
93     sConfig.Rank         = 1;
94     sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
95
96     switch (obj->pin) {
97         case PA_0:
98             sConfig.Channel = ADC_CHANNEL_0;
99             break;
100         case PA_1:
101             sConfig.Channel = ADC_CHANNEL_1;
102             break;
103         case PA_2:
104             sConfig.Channel = ADC_CHANNEL_2;
105             break;
106         case PA_3:
107             sConfig.Channel = ADC_CHANNEL_3;
108             break;
109         case PA_4:
110             sConfig.Channel = ADC_CHANNEL_4;
111             break;
112         case PA_5:
113             sConfig.Channel = ADC_CHANNEL_5;
114             break;
115         case PA_6:
116             sConfig.Channel = ADC_CHANNEL_6;
117             break;
118         case PA_7:
119             sConfig.Channel = ADC_CHANNEL_7;
120             break;
121         case PB_0:
122             sConfig.Channel = ADC_CHANNEL_8;
123             break;
124         case PB_1:
125             sConfig.Channel = ADC_CHANNEL_9;
126             break;
127         case PC_0:
128             sConfig.Channel = ADC_CHANNEL_10;
129             break;
130         case PC_1:
131             sConfig.Channel = ADC_CHANNEL_11;
132             break;
133         case PC_2:
134             sConfig.Channel = ADC_CHANNEL_12;
135             break;
136         case PC_3:
137             sConfig.Channel = ADC_CHANNEL_13;
138             break;
139         case PC_4:
140             sConfig.Channel = ADC_CHANNEL_14;
141             break;
142         case PC_5:
143             sConfig.Channel = ADC_CHANNEL_15;
144             break;
145         default:
146             return 0;
147     }
148
149     HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
150
151     HAL_ADC_Start(&AdcHandle); // Start conversion
152
153     // Wait end of conversion and get value
154     if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
155         return (HAL_ADC_GetValue(&AdcHandle));
156     } else {
157         return 0;
158     }
159 }
160
161 uint16_t analogin_read_u16(analogin_t *obj)
162 {
163     uint16_t value = adc_read(obj);
164     // 12-bit to 16-bit conversion
165     value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
166     return value;
167 }
168
169 float analogin_read(analogin_t *obj)
170 {
171     uint16_t value = adc_read(obj);
172     return (float)value * (1.0f / (float)0xFFF); // 12 bits range
173 }
174
175 #endif