@@ -29,9 +29,40 @@ extern "C" {
2929uint32_t g_anOutputPinConfigured [MAX_NB_PORT ] = {0 };
3030#endif
3131
32+ #if !defined(ADC_RESOLUTION_16B )
33+ #define MAX_ADC_RESOLUTION 12
34+ #else
35+ #define MAX_ADC_RESOLUTION 16
36+ #endif
3237#define MAX_PWM_RESOLUTION 16
3338
34- static int _readResolution = 10 ;
39+ static int _readResolution = ADC_RESOLUTION ;
40+ static int _internalReadResolution =
41+ #if ADC_RESOLUTION > MAX_ADC_RESOLUTION
42+ MAX_ADC_RESOLUTION
43+ #else
44+
45+ #ifdef ADC_RESOLUTION_12B
46+
47+ #if ADC_RESOLUTION <= 6 && defined(ADC_RESOLUTION_6B )
48+ 6
49+ #elif ADC_RESOLUTION <= 8
50+ 8
51+ #elif ADC_RESOLUTION <= 10
52+ 10
53+ #elif ADC_RESOLUTION <= 12
54+ 12
55+ #elif ADC_RESOLUTION <= 14 && defined(ADC_RESOLUTION_14B )
56+ 14
57+ #elif defined(ADC_RESOLUTION_16B )
58+ 16
59+ #endif
60+ #else /* ADC_RESOLUTION_12B */
61+ 12
62+ #endif /* ADC_RESOLUTION_12B */
63+ #endif /* ADC_RESOLUTION > MAX_ADC_RESOLUTION */
64+ ;
65+
3566static int _writeResolution = PWM_RESOLUTION ;
3667static int _internalWriteResolution =
3768#if PWM_RESOLUTION > MAX_PWM_RESOLUTION
@@ -45,7 +76,43 @@ static uint32_t _writeFreq = PWM_FREQUENCY;
4576
4677void analogReadResolution (int res )
4778{
48- _readResolution = res ;
79+ if ((res > 0 ) && (res <= 32 )) {
80+ _readResolution = res ;
81+ _internalReadResolution = _readResolution ;
82+ if (_readResolution > MAX_ADC_RESOLUTION ) {
83+ _internalReadResolution = MAX_ADC_RESOLUTION ;
84+ } else {
85+ #ifdef ADC_RESOLUTION_12B
86+ #ifdef ADC_RESOLUTION_6B
87+ if (_internalReadResolution <= 6 ) {
88+ _internalReadResolution = 6 ;
89+ } else
90+ #endif
91+ if (_internalReadResolution <= 8 ) {
92+ _internalReadResolution = 8 ;
93+ } else if (_internalReadResolution <= 10 ) {
94+ _internalReadResolution = 10 ;
95+ } else if (_internalReadResolution <= 12 ) {
96+ _internalReadResolution = 12 ;
97+ }
98+ #ifdef ADC_RESOLUTION_14B
99+ else if (_internalReadResolution <= 14 ) {
100+ _internalReadResolution = 14 ;
101+ }
102+ #endif
103+ #ifdef ADC_RESOLUTION_16B
104+ else if (_internalReadResolution <= 16 ) {
105+ _internalReadResolution = 16 ;
106+ }
107+ #endif
108+ #else
109+ /* STM32F1xx have no ADC_RESOLUTION_xB */
110+ _internalReadResolution = 12 ;
111+ #endif
112+ }
113+ } else {
114+ Error_Handler ();
115+ }
49116}
50117
51118void analogWriteResolution (int res )
@@ -86,16 +153,16 @@ void analogReference(eAnalogReference ulMode)
86153 UNUSED (ulMode );
87154}
88155
89- //perform the read operation on the selected analog pin.
90- //the initialization of the analog PIN is done through this function
156+ // Perform the read operation on the selected analog pin.
157+ // the initialization of the analog PIN is done through this function
91158uint32_t analogRead (uint32_t ulPin )
92159{
93160 uint32_t value = 0 ;
94161#if defined(HAL_ADC_MODULE_ENABLED ) && !defined(HAL_ADC_MODULE_ONLY )
95162 PinName p = analogInputToPinName (ulPin );
96163 if (p != NC ) {
97- value = adc_read_value (p );
98- value = mapResolution (value , ADC_RESOLUTION , _readResolution );
164+ value = adc_read_value (p , _internalReadResolution );
165+ value = mapResolution (value , _internalReadResolution , _readResolution );
99166 }
100167#else
101168 UNUSED (ulPin );
0 commit comments