Skip to content

Commit bbc15f6

Browse files
committed
Implemented adc deinitialization functionality
1 parent e9d6c9c commit bbc15f6

File tree

1 file changed

+127
-16
lines changed

1 file changed

+127
-16
lines changed

targets/TARGET_STM/TARGET_STM32G4/analogin_device.c

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,42 @@
3737
#include "mbed_debug.h"
3838
#include "PeripheralPins.h"
3939
#include "stm32g4xx_ll_bus.h"
40+
#include "string.h"
41+
42+
#if defined(ADC1)
43+
static uint8_t adc1_en_counter = 0;
44+
#define ADC1_EN_CTR adc1_en_counter
45+
#else
46+
#define ADC1_EN_CTR 0
47+
#endif
48+
49+
#if defined(ADC2)
50+
static uint8_t adc2_en_counter = 0;
51+
#define ADC2_EN_CTR adc2_en_counter
52+
#else
53+
#define ADC2_EN_CTR 0
54+
#endif
55+
56+
#if defined(ADC3)
57+
static uint8_t adc3_en_counter = 0;
58+
#define ADC3_EN_CTR adc3_en_counter
59+
#else
60+
#define ADC3_EN_CTR 0
61+
#endif
62+
63+
#if defined(ADC4)
64+
static uint8_t adc4_en_counter = 0;
65+
#define ADC4_EN_CTR adc4_en_counter
66+
#else
67+
#define ADC4_EN_CTR 0
68+
#endif
69+
70+
#if defined(ADC5)
71+
static uint8_t adc5_en_counter = 0;
72+
#define ADC5_EN_CTR adc5_en_counter
73+
#else
74+
#define ADC5_EN_CTR 0
75+
#endif
4076

4177
#if STATIC_PINMAP_READY
4278
#define ANALOGIN_INIT_DIRECT analogin_init_direct
@@ -94,38 +130,38 @@ static void _analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
94130

95131
#if defined(ADC1)
96132
if ((ADCName)obj->handle.Instance == ADC_1) {
97-
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12); // TODO - disable clock with deinit?
98-
__HAL_RCC_ADC12_CONFIG(RCC_ADC12CLKSOURCE_SYSCLK); // TODO - which clock?
99-
// SYSCLK or PLL?
133+
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12);
134+
__HAL_RCC_ADC12_CONFIG(RCC_ADC12CLKSOURCE_SYSCLK);
135+
adc1_en_counter++;
100136
}
101137
#endif
102138
#if defined(ADC2)
103139
if ((ADCName)obj->handle.Instance == ADC_2) {
104-
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12); // TODO - disable clock with deinit?
105-
__HAL_RCC_ADC12_CONFIG(RCC_ADC12CLKSOURCE_SYSCLK); // TODO - which clock?
106-
// SYSCLK or PLL?
140+
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12);
141+
__HAL_RCC_ADC12_CONFIG(RCC_ADC12CLKSOURCE_SYSCLK);
142+
adc2_en_counter++;
107143
}
108144
#endif
109145
#if defined(ADC3)
110146
if ((ADCName)obj->handle.Instance == ADC_3) {
111-
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC345); // TODO - disable clock with deinit?
112-
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK); // TODO - which clock?
113-
// SYSCLK or PLL?
147+
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC345);
148+
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK);
149+
adc3_en_counter++;
114150
}
115151
#endif
116152
#if defined(ADC4)
117153
if ((ADCName)obj->handle.Instance == ADC_4) {
118-
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC345); // TODO - disable clock with deinit?
119-
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK); // TODO - which clock?
120-
// SYSCLK or PLL?
154+
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC345);
155+
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK);
156+
adc4_en_counter++;
121157
}
122158
#endif
123159

124160
#if defined(ADC5)
125161
if ((ADCName)obj->handle.Instance == ADC_5) {
126-
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC345); // TODO - disable clock with deinit?
127-
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK); // TODO - which clock?
128-
// SYSCLK or PLL?
162+
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC345);
163+
__HAL_RCC_ADC345_CONFIG(RCC_ADC345CLKSOURCE_SYSCLK);
164+
adc5_en_counter++;
129165
}
130166

131167
#endif
@@ -163,7 +199,7 @@ uint16_t adc_read(analogin_t *obj)
163199

164200
// Configure ADC channel
165201
sConfig.Rank = ADC_REGULAR_RANK_1;
166-
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
202+
sConfig.SamplingTime = ADC_SAMPLETIME_24CYCLES_5;
167203
sConfig.SingleDiff = ADC_SINGLE_ENDED;
168204
sConfig.OffsetNumber = ADC_OFFSET_NONE;
169205
sConfig.Offset = 0;
@@ -259,4 +295,79 @@ const PinMap *analogin_pinmap()
259295
return PinMap_ADC;
260296
}
261297

298+
void analogin_free(analogin_t *obj)
299+
{
300+
#if defined(ADC1)
301+
if ((ADCName)obj->handle.Instance == ADC_1) {
302+
adc1_en_counter--;
303+
if(ADC1_EN_CTR == 0)
304+
{
305+
HAL_ADC_DeInit(&obj->handle);
306+
307+
// Disable clock if ADC2 is also unused
308+
if(ADC2_EN_CTR == 0) {
309+
LL_AHB2_GRP1_DisableClock(LL_AHB2_GRP1_PERIPH_ADC12);
310+
}
311+
}
312+
}
313+
#endif
314+
#if defined(ADC2)
315+
if ((ADCName)obj->handle.Instance == ADC_2) {
316+
adc2_en_counter--;
317+
if(ADC2_EN_CTR == 0)
318+
{
319+
HAL_ADC_DeInit(&obj->handle);
320+
321+
// Disable clock if ADC1 is also unused
322+
if(ADC1_EN_CTR == 0) {
323+
LL_AHB2_GRP1_DisableClock(LL_AHB2_GRP1_PERIPH_ADC12);
324+
}
325+
}
326+
}
327+
#endif
328+
#if defined(ADC3)
329+
if ((ADCName)obj->handle.Instance == ADC_3) {
330+
adc3_en_counter--;
331+
if(ADC3_EN_CTR == 0)
332+
{
333+
HAL_ADC_DeInit(&obj->handle);
334+
335+
// Disable clock if ADC4 and ADC5 are also unused
336+
if((ADC4_EN_CTR + ADC5_EN_CTR) == 0) {
337+
LL_AHB2_GRP1_DisableClock(LL_AHB2_GRP1_PERIPH_ADC345);
338+
}
339+
}
340+
}
341+
#endif
342+
#if defined(ADC4)
343+
if ((ADCName)obj->handle.Instance == ADC_4) {
344+
adc4_en_counter--;
345+
if(ADC4_EN_CTR == 0)
346+
{
347+
HAL_ADC_DeInit(&obj->handle);
348+
349+
// Disable clock if ADC3 and ADC5 are also unused
350+
if((ADC3_EN_CTR + ADC5_EN_CTR) == 0) {
351+
LL_AHB2_GRP1_DisableClock(LL_AHB2_GRP1_PERIPH_ADC345);
352+
}
353+
}
354+
}
355+
#endif
356+
357+
#if defined(ADC5)
358+
if ((ADCName)obj->handle.Instance == ADC_5) {
359+
adc5_en_counter--;
360+
if(ADC5_EN_CTR == 0)
361+
{
362+
HAL_ADC_DeInit(&obj->handle);
363+
364+
// Disable clock if ADC3 and ADC4 are also unused
365+
if((ADC3_EN_CTR + ADC4_EN_CTR) == 0) {
366+
LL_AHB2_GRP1_DisableClock(LL_AHB2_GRP1_PERIPH_ADC345);
367+
}
368+
}
369+
}
370+
#endif
371+
}
372+
262373
#endif

0 commit comments

Comments
 (0)