36
36
#include "mbed_error.h"
37
37
#include "mbed_debug.h"
38
38
#include "PeripheralPins.h"
39
+ #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
39
76
40
77
#if STATIC_PINMAP_READY
41
78
#define ANALOGIN_INIT_DIRECT analogin_init_direct
@@ -72,6 +109,7 @@ static void _analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
72
109
73
110
// Configure ADC object structures
74
111
obj -> handle .State = HAL_ADC_STATE_RESET ;
112
+ memset (& obj -> handle .Init , 0 , sizeof (obj -> handle .Init ));
75
113
obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2 ;
76
114
obj -> handle .Init .Resolution = ADC_RESOLUTION_12B ;
77
115
obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
@@ -82,40 +120,48 @@ static void _analogin_init_direct(analogin_t *obj, const PinMap *pinmap)
82
120
obj -> handle .Init .NbrOfConversion = 1 ;
83
121
obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
84
122
obj -> handle .Init .NbrOfDiscConversion = 0 ;
85
- obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1 ;
123
+ obj -> handle .Init .ExternalTrigConv = ADC_SOFTWARE_START ;
86
124
obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
87
125
obj -> handle .Init .DMAContinuousRequests = DISABLE ;
88
126
obj -> handle .Init .Overrun = ADC_OVR_DATA_OVERWRITTEN ;
127
+ obj -> handle .Init .GainCompensation = 0 ;
128
+ obj -> handle .Init .OversamplingMode = DISABLE ;
129
+ obj -> handle .Init .SamplingMode = ADC_SAMPLING_MODE_NORMAL ;
89
130
90
131
#if defined(ADC1 )
91
132
if ((ADCName )obj -> handle .Instance == ADC_1 ) {
92
- __HAL_RCC_ADC12_CONFIG (RCC_ADC12CLKSOURCE_SYSCLK ); // TODO - which clock?
93
- // 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 ++ ;
94
136
}
95
137
#endif
96
138
#if defined(ADC2 )
97
139
if ((ADCName )obj -> handle .Instance == ADC_2 ) {
98
- __HAL_RCC_ADC12_CONFIG (RCC_ADC12CLKSOURCE_SYSCLK ); // TODO - which clock?
99
- // 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 ++ ;
100
143
}
101
144
#endif
102
145
#if defined(ADC3 )
103
146
if ((ADCName )obj -> handle .Instance == ADC_3 ) {
104
- __HAL_RCC_ADC345_CONFIG (RCC_ADC345CLKSOURCE_SYSCLK ); // TODO - which clock?
105
- // 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 ++ ;
106
150
}
107
151
#endif
108
152
#if defined(ADC4 )
109
153
if ((ADCName )obj -> handle .Instance == ADC_4 ) {
110
- __HAL_RCC_ADC345_CONFIG (RCC_ADC345CLKSOURCE_SYSCLK ); // TODO - which clock?
111
- // 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 ++ ;
112
157
}
113
158
#endif
114
159
115
160
#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?
161
+ if ((ADCName )obj -> handle .Instance == ADC_5 ) {
162
+ LL_AHB2_GRP1_EnableClock (LL_AHB2_GRP1_PERIPH_ADC345 );
163
+ __HAL_RCC_ADC345_CONFIG (RCC_ADC345CLKSOURCE_SYSCLK );
164
+ adc5_en_counter ++ ;
119
165
}
120
166
121
167
#endif
@@ -153,7 +199,7 @@ uint16_t adc_read(analogin_t *obj)
153
199
154
200
// Configure ADC channel
155
201
sConfig .Rank = ADC_REGULAR_RANK_1 ;
156
- sConfig .SamplingTime = ADC_SAMPLETIME_24CYCLES_5 ;
202
+ sConfig .SamplingTime = ADC_SAMPLETIME_247CYCLES_5 ;
157
203
sConfig .SingleDiff = ADC_SINGLE_ENDED ;
158
204
sConfig .OffsetNumber = ADC_OFFSET_NONE ;
159
205
sConfig .Offset = 0 ;
@@ -202,45 +248,31 @@ uint16_t adc_read(analogin_t *obj)
202
248
sConfig .Channel = ADC_CHANNEL_14 ;
203
249
break ;
204
250
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
- }
251
+ sConfig .Channel = ADC_CHANNEL_15 ;
211
252
break ;
212
253
case 16 :
254
+ sConfig .Channel = ADC_CHANNEL_16 ;
255
+
213
256
if ((ADCName )obj -> handle .Instance == ADC_1 ) {
214
257
sConfig .Channel = ADC_CHANNEL_TEMPSENSOR_ADC1 ;
215
- sConfig .SamplingTime = ADC_SAMPLETIME_247CYCLES_5 ;
216
- } else {
217
- sConfig .Channel = ADC_CHANNEL_16 ;
258
+ sConfig .SamplingTime = ADC_SAMPLETIME_640CYCLES_5 ;
218
259
}
219
260
break ;
220
261
case 17 :
221
- sConfig .SamplingTime = ADC_SAMPLETIME_247CYCLES_5 ;
262
+ sConfig .Channel = ADC_CHANNEL_17 ;
263
+
222
264
if ((ADCName )obj -> handle .Instance == ADC_1 ) {
223
265
sConfig .Channel = ADC_CHANNEL_VBAT ;
266
+ sConfig .SamplingTime = ADC_SAMPLETIME_640CYCLES_5 ;
224
267
}
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
268
break ;
241
269
case 18 :
242
- sConfig .SamplingTime = ADC_SAMPLETIME_247CYCLES_5 ;
243
- sConfig .Channel = ADC_CHANNEL_VREFINT ;
270
+ sConfig .Channel = ADC_CHANNEL_18 ;
271
+
272
+ if ((ADCName )obj -> handle .Instance == ADC_1 ) {
273
+ sConfig .Channel = ADC_CHANNEL_VREFINT ;
274
+ sConfig .SamplingTime = ADC_SAMPLETIME_640CYCLES_5 ;
275
+ }
244
276
break ;
245
277
default :
246
278
return 0 ;
@@ -261,7 +293,9 @@ uint16_t adc_read(analogin_t *obj)
261
293
} else {
262
294
debug ("HAL_ADC_PollForConversion issue\n" );
263
295
}
296
+
264
297
LL_ADC_SetCommonPathInternalCh (__LL_ADC_COMMON_INSTANCE ((& obj -> handle )-> Instance ), LL_ADC_PATH_INTERNAL_NONE );
298
+
265
299
if (HAL_ADC_Stop (& obj -> handle ) != HAL_OK ) {
266
300
debug ("HAL_ADC_Stop issue\n" );;
267
301
}
@@ -274,4 +308,79 @@ const PinMap *analogin_pinmap()
274
308
return PinMap_ADC ;
275
309
}
276
310
311
+ void analogin_free (analogin_t * obj )
312
+ {
313
+ #if defined(ADC1 )
314
+ if ((ADCName )obj -> handle .Instance == ADC_1 ) {
315
+ adc1_en_counter -- ;
316
+ if (ADC1_EN_CTR == 0 )
317
+ {
318
+ HAL_ADC_DeInit (& obj -> handle );
319
+
320
+ // Disable clock if ADC2 is also unused
321
+ if (ADC2_EN_CTR == 0 ) {
322
+ LL_AHB2_GRP1_DisableClock (LL_AHB2_GRP1_PERIPH_ADC12 );
323
+ }
324
+ }
325
+ }
326
+ #endif
327
+ #if defined(ADC2 )
328
+ if ((ADCName )obj -> handle .Instance == ADC_2 ) {
329
+ adc2_en_counter -- ;
330
+ if (ADC2_EN_CTR == 0 )
331
+ {
332
+ HAL_ADC_DeInit (& obj -> handle );
333
+
334
+ // Disable clock if ADC1 is also unused
335
+ if (ADC1_EN_CTR == 0 ) {
336
+ LL_AHB2_GRP1_DisableClock (LL_AHB2_GRP1_PERIPH_ADC12 );
337
+ }
338
+ }
339
+ }
340
+ #endif
341
+ #if defined(ADC3 )
342
+ if ((ADCName )obj -> handle .Instance == ADC_3 ) {
343
+ adc3_en_counter -- ;
344
+ if (ADC3_EN_CTR == 0 )
345
+ {
346
+ HAL_ADC_DeInit (& obj -> handle );
347
+
348
+ // Disable clock if ADC4 and ADC5 are also unused
349
+ if ((ADC4_EN_CTR + ADC5_EN_CTR ) == 0 ) {
350
+ LL_AHB2_GRP1_DisableClock (LL_AHB2_GRP1_PERIPH_ADC345 );
351
+ }
352
+ }
353
+ }
354
+ #endif
355
+ #if defined(ADC4 )
356
+ if ((ADCName )obj -> handle .Instance == ADC_4 ) {
357
+ adc4_en_counter -- ;
358
+ if (ADC4_EN_CTR == 0 )
359
+ {
360
+ HAL_ADC_DeInit (& obj -> handle );
361
+
362
+ // Disable clock if ADC3 and ADC5 are also unused
363
+ if ((ADC3_EN_CTR + ADC5_EN_CTR ) == 0 ) {
364
+ LL_AHB2_GRP1_DisableClock (LL_AHB2_GRP1_PERIPH_ADC345 );
365
+ }
366
+ }
367
+ }
368
+ #endif
369
+
370
+ #if defined(ADC5 )
371
+ if ((ADCName )obj -> handle .Instance == ADC_5 ) {
372
+ adc5_en_counter -- ;
373
+ if (ADC5_EN_CTR == 0 )
374
+ {
375
+ HAL_ADC_DeInit (& obj -> handle );
376
+
377
+ // Disable clock if ADC3 and ADC4 are also unused
378
+ if ((ADC3_EN_CTR + ADC4_EN_CTR ) == 0 ) {
379
+ LL_AHB2_GRP1_DisableClock (LL_AHB2_GRP1_PERIPH_ADC345 );
380
+ }
381
+ }
382
+ }
383
+ #endif
384
+ }
385
+
277
386
#endif
0 commit comments