@@ -29,6 +29,7 @@ LOG_MODULE_REGISTER(adc_stm32);
29
29
#include <pinmux/stm32/pinmux_stm32.h>
30
30
31
31
#if !defined(CONFIG_SOC_SERIES_STM32F0X ) && \
32
+ !defined(CONFIG_SOC_SERIES_STM32G0X ) && \
32
33
!defined(CONFIG_SOC_SERIES_STM32L0X )
33
34
#define RANK (n ) LL_ADC_REG_RANK_##n
34
35
static const uint32_t table_rank [] = {
@@ -148,7 +149,8 @@ static const uint32_t table_samp_time[] = {
148
149
SMP_TIME (239 , S_5 ),
149
150
};
150
151
#endif /* ADC5_V1_1 */
151
- #elif defined(CONFIG_SOC_SERIES_STM32L0X )
152
+ #elif defined(CONFIG_SOC_SERIES_STM32L0X ) || \
153
+ defined(CONFIG_SOC_SERIES_STM32G0X )
152
154
static const uint16_t acq_time_tbl [8 ] = {2 , 4 , 8 , 13 , 20 , 40 , 80 , 161 };
153
155
static const uint32_t table_samp_time [] = {
154
156
SMP_TIME (1 , _5 ),
@@ -211,7 +213,9 @@ struct adc_stm32_data {
211
213
212
214
uint8_t resolution ;
213
215
uint8_t channel_count ;
214
- #if defined(CONFIG_SOC_SERIES_STM32F0X ) || defined(CONFIG_SOC_SERIES_STM32L0X )
216
+ #if defined(CONFIG_SOC_SERIES_STM32F0X ) || \
217
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
218
+ defined(CONFIG_SOC_SERIES_STM32L0X )
215
219
int8_t acq_time_index ;
216
220
#endif
217
221
};
@@ -256,6 +260,7 @@ static void adc_stm32_start_conversion(const struct device *dev)
256
260
defined(CONFIG_SOC_SERIES_STM32L0X ) || \
257
261
defined(CONFIG_SOC_SERIES_STM32L4X ) || \
258
262
defined(CONFIG_SOC_SERIES_STM32WBX ) || \
263
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
259
264
defined(CONFIG_SOC_SERIES_STM32G4X ) || \
260
265
defined(CONFIG_SOC_SERIES_STM32H7X )
261
266
LL_ADC_REG_StartConversion (adc );
@@ -332,6 +337,11 @@ static int start_read(const struct device *dev,
332
337
#if defined(CONFIG_SOC_SERIES_STM32F0X ) || \
333
338
defined(CONFIG_SOC_SERIES_STM32L0X )
334
339
LL_ADC_REG_SetSequencerChannels (adc , channel );
340
+ #elif defined(CONFIG_SOC_SERIES_STM32G0X )
341
+ /* STM32G0 in "not fully configurable" sequencer mode */
342
+ LL_ADC_REG_SetSequencerChannels (adc , channel );
343
+ while (LL_ADC_IsActiveFlag_CCRDY (adc ) == 0 ) {
344
+ }
335
345
#else
336
346
LL_ADC_REG_SetSequencerRanks (adc , table_rank [0 ], channel );
337
347
LL_ADC_REG_SetSequencerLength (adc , table_seq_len [0 ]);
@@ -343,7 +353,21 @@ static int start_read(const struct device *dev,
343
353
return err ;
344
354
}
345
355
346
- #if !defined(CONFIG_SOC_SERIES_STM32F1X )
356
+ #if defined(CONFIG_SOC_SERIES_STM32G0X )
357
+ /*
358
+ * Errata: Writing ADC_CFGR1 register while ADEN bit is set
359
+ * resets RES[1:0] bitfield. We need to disable and enable adc.
360
+ */
361
+ if (LL_ADC_IsEnabled (adc ) == 1UL ) {
362
+ LL_ADC_Disable (adc );
363
+ }
364
+ while (LL_ADC_IsEnabled (adc ) == 1UL ) {
365
+ }
366
+ LL_ADC_SetResolution (adc , resolution );
367
+ LL_ADC_Enable (adc );
368
+ while (LL_ADC_IsActiveFlag_ADRDY (adc ) != 1UL ) {
369
+ }
370
+ #elif !defined(CONFIG_SOC_SERIES_STM32F1X )
347
371
LL_ADC_SetResolution (adc , resolution );
348
372
#endif
349
373
@@ -352,6 +376,7 @@ static int start_read(const struct device *dev,
352
376
defined(CONFIG_SOC_SERIES_STM32L0X ) || \
353
377
defined(CONFIG_SOC_SERIES_STM32L4X ) || \
354
378
defined(CONFIG_SOC_SERIES_STM32WBX ) || \
379
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
355
380
defined(CONFIG_SOC_SERIES_STM32G4X ) || \
356
381
defined(CONFIG_SOC_SERIES_STM32H7X )
357
382
LL_ADC_EnableIT_EOC (adc );
@@ -457,6 +482,9 @@ static void adc_stm32_setup_speed(const struct device *dev, uint8_t id,
457
482
#if defined(CONFIG_SOC_SERIES_STM32F0X ) || defined(CONFIG_SOC_SERIES_STM32L0X )
458
483
LL_ADC_SetSamplingTimeCommonChannels (adc ,
459
484
table_samp_time [acq_time_index ]);
485
+ #elif defined(CONFIG_SOC_SERIES_STM32G0X )
486
+ LL_ADC_SetSamplingTimeCommonChannels (adc , LL_ADC_SAMPLINGTIME_COMMON_1 ,
487
+ table_samp_time [acq_time_index ]);
460
488
#else
461
489
LL_ADC_SetChannelSamplingTime (adc ,
462
490
__LL_ADC_DECIMAL_NB_TO_CHANNEL (id ),
@@ -467,7 +495,9 @@ static void adc_stm32_setup_speed(const struct device *dev, uint8_t id,
467
495
static int adc_stm32_channel_setup (const struct device * dev ,
468
496
const struct adc_channel_cfg * channel_cfg )
469
497
{
470
- #if defined(CONFIG_SOC_SERIES_STM32F0X ) || defined(CONFIG_SOC_SERIES_STM32L0X )
498
+ #if defined(CONFIG_SOC_SERIES_STM32F0X ) || \
499
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
500
+ defined(CONFIG_SOC_SERIES_STM32L0X )
471
501
struct adc_stm32_data * data = dev -> data ;
472
502
#endif
473
503
int acq_time_index ;
@@ -482,7 +512,9 @@ static int adc_stm32_channel_setup(const struct device *dev,
482
512
if (acq_time_index < 0 ) {
483
513
return acq_time_index ;
484
514
}
485
- #if defined(CONFIG_SOC_SERIES_STM32F0X ) || defined(CONFIG_SOC_SERIES_STM32L0X )
515
+ #if defined(CONFIG_SOC_SERIES_STM32F0X ) || \
516
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
517
+ defined(CONFIG_SOC_SERIES_STM32L0X )
486
518
if (data -> acq_time_index == -1 ) {
487
519
data -> acq_time_index = acq_time_index ;
488
520
} else {
@@ -533,6 +565,7 @@ static void adc_stm32_calib(const struct device *dev)
533
565
defined(CONFIG_SOC_SERIES_STM32G4X )
534
566
LL_ADC_StartCalibration (adc , LL_ADC_SINGLE_ENDED );
535
567
#elif defined(CONFIG_SOC_SERIES_STM32F0X ) || \
568
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
536
569
defined(CONFIG_SOC_SERIES_STM32L0X )
537
570
LL_ADC_StartCalibration (adc );
538
571
#elif defined(CONFIG_SOC_SERIES_STM32H7X )
@@ -555,12 +588,15 @@ static int adc_stm32_init(const struct device *dev)
555
588
LOG_DBG ("Initializing...." );
556
589
557
590
data -> dev = dev ;
558
- #if defined(CONFIG_SOC_SERIES_STM32F0X ) || defined(CONFIG_SOC_SERIES_STM32L0X )
591
+ #if defined(CONFIG_SOC_SERIES_STM32F0X ) || \
592
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
593
+ defined(CONFIG_SOC_SERIES_STM32L0X )
559
594
/*
560
595
* All conversion time for all channels on one ADC instance for F0 and
561
- * L0 series chips has to be the same. This additional variable is for
562
- * checking if the conversion time selection of all channels on one ADC
563
- * instance is the same.
596
+ * L0 series chips has to be the same. For STM32G0 currently only one
597
+ * of the two available common channel conversion times is used.
598
+ * This additional variable is for checking if the conversion time
599
+ * selection of all channels on one ADC instance is the same.
564
600
*/
565
601
data -> acq_time_index = -1 ;
566
602
#endif
@@ -591,12 +627,13 @@ static int adc_stm32_init(const struct device *dev)
591
627
LL_ADC_DisableDeepPowerDown (adc );
592
628
#endif
593
629
/*
594
- * F3, L4, WB and G4 ADC modules need some time to be stabilized before
595
- * performing any enable or calibration actions.
630
+ * F3, L4, WB, G0 and G4 ADC modules need some time
631
+ * to be stabilized before performing any enable or calibration actions.
596
632
*/
597
633
#if defined(CONFIG_SOC_SERIES_STM32F3X ) || \
598
634
defined(CONFIG_SOC_SERIES_STM32L4X ) || \
599
635
defined(CONFIG_SOC_SERIES_STM32WBX ) || \
636
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
600
637
defined(CONFIG_SOC_SERIES_STM32G4X ) || \
601
638
defined(CONFIG_SOC_SERIES_STM32H7X )
602
639
LL_ADC_EnableInternalRegulator (adc );
@@ -609,6 +646,7 @@ static int adc_stm32_init(const struct device *dev)
609
646
#elif defined(CONFIG_SOC_SERIES_STM32F3X ) || \
610
647
defined(CONFIG_SOC_SERIES_STM32L4X ) || \
611
648
defined(CONFIG_SOC_SERIES_STM32WBX ) || \
649
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
612
650
defined(CONFIG_SOC_SERIES_STM32G4X ) || \
613
651
defined(CONFIG_SOC_SERIES_STM32H7X )
614
652
LL_ADC_SetCommonClock (__LL_ADC_COMMON_INSTANCE (adc ),
@@ -634,6 +672,7 @@ static int adc_stm32_init(const struct device *dev)
634
672
defined(CONFIG_SOC_SERIES_STM32L0X ) || \
635
673
defined(CONFIG_SOC_SERIES_STM32L4X ) || \
636
674
defined(CONFIG_SOC_SERIES_STM32WBX ) || \
675
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
637
676
defined(CONFIG_SOC_SERIES_STM32G4X ) || \
638
677
defined(CONFIG_SOC_SERIES_STM32H7X )
639
678
if (LL_ADC_IsActiveFlag_ADRDY (adc )) {
@@ -653,6 +692,7 @@ static int adc_stm32_init(const struct device *dev)
653
692
defined(CONFIG_SOC_SERIES_STM32L0X ) || \
654
693
defined(CONFIG_SOC_SERIES_STM32L4X ) || \
655
694
defined(CONFIG_SOC_SERIES_STM32WBX ) || \
695
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
656
696
defined(CONFIG_SOC_SERIES_STM32G4X ) || \
657
697
defined(CONFIG_SOC_SERIES_STM32H7X )
658
698
/*
@@ -677,10 +717,11 @@ static int adc_stm32_init(const struct device *dev)
677
717
678
718
#if defined(CONFIG_SOC_SERIES_STM32L4X ) || \
679
719
defined(CONFIG_SOC_SERIES_STM32WBX ) || \
720
+ defined(CONFIG_SOC_SERIES_STM32G0X ) || \
680
721
defined(CONFIG_SOC_SERIES_STM32G4X ) || \
681
722
defined(CONFIG_SOC_SERIES_STM32H7X )
682
723
/*
683
- * Enabling ADC modules in L4, WB and G4 series may fail if they are
724
+ * Enabling ADC modules in L4, WB, G0 and G4 series may fail if they are
684
725
* still not stabilized, this will wait for a short time to ensure ADC
685
726
* modules are properly enabled.
686
727
*/
0 commit comments