Skip to content

Commit 4f2bd57

Browse files
committed
Added missing analogin_device.c file and configured for STM32G4xx series
1 parent b40dab8 commit 4f2bd57

File tree

2 files changed

+278
-1
lines changed

2 files changed

+278
-1
lines changed
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016, 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 "mbed_wait_api.h"
34+
#include "cmsis.h"
35+
#include "pinmap.h"
36+
#include "mbed_error.h"
37+
#include "mbed_debug.h"
38+
#include "PeripheralPins.h"
39+
40+
#if STATIC_PINMAP_READY
41+
#define ANALOGIN_INIT_DIRECT analogin_init_direct
42+
void analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
43+
#else
44+
#define ANALOGIN_INIT_DIRECT _analogin_init_direct
45+
static void _analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
46+
#endif
47+
{
48+
uint32_t function = (uint32_t)pinmap->function;
49+
50+
// Get the peripheral name from the pin and assign it to the object
51+
obj->handle.Instance = (ADC_TypeDef *)pinmap->peripheral;
52+
53+
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
54+
// are described in PinNames.h and PeripheralPins.c
55+
// Pin value must be between 0xF0 and 0xFF
56+
if ((pinmap->pin < 0xF0) || (pinmap->pin >= 0x100)) {
57+
// Normal channels
58+
// Configure GPIO
59+
pin_function(pinmap->pin, pinmap->function);
60+
pin_mode(pinmap->pin, PullNone);
61+
} else {
62+
// Internal channels
63+
// No GPIO configuration for internal channels
64+
}
65+
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
66+
MBED_ASSERT(function != (uint32_t)NC);
67+
68+
obj->channel = STM_PIN_CHANNEL(function);
69+
70+
// Save pin number for the read function
71+
obj->pin = pinmap->pin;
72+
73+
// Configure ADC object structures
74+
obj->handle.State = HAL_ADC_STATE_RESET;
75+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
76+
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
77+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
78+
obj->handle.Init.ScanConvMode = DISABLE;
79+
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
80+
obj->handle.Init.LowPowerAutoWait = DISABLE;
81+
obj->handle.Init.ContinuousConvMode = DISABLE;
82+
obj->handle.Init.NbrOfConversion = 1;
83+
obj->handle.Init.DiscontinuousConvMode = DISABLE;
84+
obj->handle.Init.NbrOfDiscConversion = 0;
85+
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1;
86+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
87+
obj->handle.Init.DMAContinuousRequests = DISABLE;
88+
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
89+
90+
#if defined(ADC1)
91+
if ((ADCName)obj->handle.Instance == ADC_1) {
92+
__HAL_RCC_ADC12_CONFIG(RCC_ADC12CLKSOURCE_SYSCLK); // TODO - which clock?
93+
// SYSCLK or PLL?
94+
}
95+
#endif
96+
#if defined(ADC2)
97+
if ((ADCName)obj->handle.Instance == ADC_2) {
98+
__HAL_RCC_ADC12_CONFIG(RCC_ADC12CLKSOURCE_SYSCLK); // TODO - which clock?
99+
// SYSCLK or PLL?
100+
}
101+
#endif
102+
#if defined(ADC3)
103+
if ((ADCName)obj->handle.Instance == ADC_3) {
104+
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK); // TODO - which clock?
105+
// SYSCLK or PLL?
106+
}
107+
#endif
108+
#if defined(ADC4)
109+
if ((ADCName)obj->handle.Instance == ADC_4) {
110+
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK); // TODO - which clock?
111+
// SYSCLK or PLL?
112+
}
113+
#endif
114+
115+
#if defined(ADC5)
116+
if ((ADCName)obj->handle.Instance == ADC_4) {
117+
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK); // TODO - which clock?
118+
// SYSCLK or PLL?
119+
}
120+
121+
#endif
122+
123+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
124+
error("Cannot initialize ADC\n");
125+
}
126+
127+
if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) {
128+
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
129+
}
130+
}
131+
132+
void analogin_init(analogin_t *obj, PinName pin)
133+
{
134+
int peripheral;
135+
int function;
136+
137+
if ((pin < 0xF0) || (pin >= 0x100)) {
138+
peripheral = (int)pinmap_peripheral(pin, PinMap_ADC);
139+
function = (int)pinmap_find_function(pin, PinMap_ADC);
140+
} else {
141+
peripheral = (int)pinmap_peripheral(pin, PinMap_ADC_Internal);
142+
function = (int)pinmap_find_function(pin, PinMap_ADC_Internal);
143+
}
144+
145+
const PinMap static_pinmap = {pin, peripheral, function};
146+
147+
ANALOGIN_INIT_DIRECT(obj, &static_pinmap);
148+
}
149+
150+
uint16_t adc_read(analogin_t *obj)
151+
{
152+
ADC_ChannelConfTypeDef sConfig = {0};
153+
154+
// Configure ADC channel
155+
sConfig.Rank = ADC_REGULAR_RANK_1;
156+
sConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES_5;
157+
sConfig.SingleDiff = ADC_SINGLE_ENDED;
158+
sConfig.OffsetNumber = ADC_OFFSET_NONE;
159+
sConfig.Offset = 0;
160+
161+
switch (obj->channel) {
162+
case 1:
163+
sConfig.Channel = ADC_CHANNEL_1;
164+
break;
165+
case 2:
166+
sConfig.Channel = ADC_CHANNEL_2;
167+
break;
168+
case 3:
169+
sConfig.Channel = ADC_CHANNEL_3;
170+
break;
171+
case 4:
172+
sConfig.Channel = ADC_CHANNEL_4;
173+
break;
174+
case 5:
175+
sConfig.Channel = ADC_CHANNEL_5;
176+
break;
177+
case 6:
178+
sConfig.Channel = ADC_CHANNEL_6;
179+
break;
180+
case 7:
181+
sConfig.Channel = ADC_CHANNEL_7;
182+
break;
183+
case 8:
184+
sConfig.Channel = ADC_CHANNEL_8;
185+
break;
186+
case 9:
187+
sConfig.Channel = ADC_CHANNEL_9;
188+
break;
189+
case 10:
190+
sConfig.Channel = ADC_CHANNEL_10;
191+
break;
192+
case 11:
193+
sConfig.Channel = ADC_CHANNEL_11;
194+
break;
195+
case 12:
196+
sConfig.Channel = ADC_CHANNEL_12;
197+
break;
198+
case 13:
199+
sConfig.Channel = ADC_CHANNEL_13;
200+
break;
201+
case 14:
202+
sConfig.Channel = ADC_CHANNEL_14;
203+
break;
204+
case 15:
205+
if ((ADCName)obj->handle.Instance == ADC_1) {
206+
sConfig.Channel = ADC_CHANNEL_VOPAMP1;
207+
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
208+
} else {
209+
sConfig.Channel = ADC_CHANNEL_15;
210+
}
211+
break;
212+
case 16:
213+
if ((ADCName)obj->handle.Instance == ADC_1) {
214+
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR_ADC1;
215+
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
216+
} else {
217+
sConfig.Channel = ADC_CHANNEL_16;
218+
}
219+
break;
220+
case 17:
221+
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
222+
if ((ADCName)obj->handle.Instance == ADC_1) {
223+
sConfig.Channel = ADC_CHANNEL_VBAT;
224+
}
225+
#if defined(ADC2)
226+
if ((ADCName)obj->handle.Instance == ADC_2) {
227+
sConfig.Channel = ADC_CHANNEL_VOPAMP2;
228+
}
229+
#endif
230+
#if defined(ADC3)
231+
if ((ADCName)obj->handle.Instance == ADC_3) {
232+
sConfig.Channel = ADC_CHANNEL_VOPAMP3_ADC3;
233+
}
234+
#endif
235+
#if defined(ADC4)
236+
if ((ADCName)obj->handle.Instance == ADC_4) {
237+
sConfig.Channel = ADC_CHANNEL_VOPAMP4;
238+
}
239+
#endif
240+
break;
241+
case 18:
242+
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
243+
sConfig.Channel = ADC_CHANNEL_VREFINT;
244+
break;
245+
default:
246+
return 0;
247+
}
248+
249+
if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
250+
debug("HAL_ADC_ConfigChannel issue\n");;
251+
}
252+
253+
if (HAL_ADC_Start(&obj->handle) != HAL_OK) {
254+
debug("HAL_ADC_Start issue\n");;
255+
}
256+
257+
uint16_t MeasuredValue = 0;
258+
259+
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
260+
MeasuredValue = (uint16_t)HAL_ADC_GetValue(&obj->handle);
261+
} else {
262+
debug("HAL_ADC_PollForConversion issue\n");
263+
}
264+
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE);
265+
if (HAL_ADC_Stop(&obj->handle) != HAL_OK) {
266+
debug("HAL_ADC_Stop issue\n");;
267+
}
268+
269+
return MeasuredValue;
270+
}
271+
272+
const PinMap *analogin_pinmap()
273+
{
274+
return PinMap_ADC;
275+
}
276+
277+
#endif
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Still to be added: CAN, USBDEVICE, ANALOGOUT, QSPI, CRC(?), TRNG
1+
Still to be added: CAN, USBDEVICE, QSPI, CRC(?), TRNG

0 commit comments

Comments
 (0)