@@ -67,18 +67,28 @@ void analogin_init(analogin_t *obj, PinName pin)
67
67
68
68
// Configure ADC object structures
69
69
obj -> handle .State = HAL_ADC_STATE_RESET ;
70
- obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4 ;
71
- obj -> handle .Init .Resolution = ADC_RESOLUTION_12B ;
72
- obj -> handle .Init .ScanConvMode = DISABLE ;
73
- obj -> handle .Init .ContinuousConvMode = DISABLE ;
74
- obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
75
- obj -> handle .Init .NbrOfDiscConversion = 0 ;
76
- obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
70
+ obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4 ;
71
+ obj -> handle .Init .Resolution = ADC_RESOLUTION_16B ;
72
+ obj -> handle .Init .ScanConvMode = DISABLE ;
73
+ obj -> handle .Init .ContinuousConvMode = DISABLE ;
74
+ obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
75
+ obj -> handle .Init .NbrOfDiscConversion = 0 ;
76
+ obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
77
77
obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1 ;
78
78
obj -> handle .Init .LeftBitShift = 0 ;
79
79
obj -> handle .Init .NbrOfConversion = 1 ;
80
80
obj -> handle .Init .ConversionDataManagement = ADC_CONVERSIONDATA_DR ;
81
81
obj -> handle .Init .EOCSelection = DISABLE ;
82
+ obj -> handle .Init .LowPowerAutoWait = DISABLE ;
83
+ obj -> handle .Init .Overrun = ADC_OVR_DATA_OVERWRITTEN ;
84
+ obj -> handle .Init .OversamplingMode = DISABLE ;
85
+
86
+ RCC_PeriphCLKInitTypeDef PeriphClkInitStruct ;
87
+ PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_ADC ;
88
+ PeriphClkInitStruct .AdcClockSelection = RCC_ADCCLKSOURCE_CLKP ;
89
+ PeriphClkInitStruct .PLL2 .PLL2P = 4 ;
90
+ HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
91
+ __HAL_RCC_ADC_CONFIG (RCC_ADCCLKSOURCE_CLKP );
82
92
83
93
#if defined(ADC1 )
84
94
if ((ADCName )obj -> handle .Instance == ADC_1 ) {
@@ -99,16 +109,20 @@ void analogin_init(analogin_t *obj, PinName pin)
99
109
if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
100
110
error ("Cannot initialize ADC" );
101
111
}
112
+
113
+ HAL_ADCEx_Calibration_Start (& obj -> handle , ADC_CALIB_OFFSET , ADC_SINGLE_ENDED );
102
114
}
103
115
104
116
uint16_t adc_read (analogin_t * obj )
105
117
{
106
118
ADC_ChannelConfTypeDef sConfig = {0 };
107
119
108
120
// Configure ADC channel
109
- sConfig .Rank = 1 ;
110
- sConfig .SamplingTime = ADC_SAMPLETIME_1CYCLE_5 ;
121
+ sConfig .Rank = ADC_REGULAR_RANK_1 ;
122
+ sConfig .SamplingTime = ADC_SAMPLETIME_64CYCLES_5 ;
111
123
sConfig .Offset = 0 ;
124
+ sConfig .SingleDiff = ADC_SINGLE_ENDED ;
125
+ sConfig .OffsetNumber = ADC_OFFSET_NONE ;
112
126
113
127
switch (obj -> channel ) {
114
128
case 0 :
0 commit comments