@@ -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_16B )
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_16B )
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+
3566int _writeResolution = PWM_RESOLUTION ;
3667static int _internalWriteResolution =
3768#if PWM_RESOLUTION > MAX_PWM_RESOLUTION
@@ -45,7 +76,39 @@ 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+ #ifndef ADC_RESOLUTION_16B
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_16B
99+ else if (_internalReadResolution <= 14 ) {
100+ _internalReadResolution = 14 ;
101+ } else if (_internalReadResolution <= 16 ) {
102+ _internalReadResolution = 16 ;
103+ }
104+ #endif
105+ #else
106+ _internalReadResolution = 12 ;
107+ #endif
108+ }
109+ } else {
110+ Error_Handler ();
111+ }
49112}
50113
51114void analogWriteResolution (int res )
@@ -86,16 +149,16 @@ void analogReference(eAnalogReference ulMode)
86149 UNUSED (ulMode );
87150}
88151
89- //perform the read operation on the selected analog pin.
90- //the initialization of the analog PIN is done through this function
152+ // Perform the read operation on the selected analog pin.
153+ // the initialization of the analog PIN is done through this function
91154uint32_t analogRead (uint32_t ulPin )
92155{
93156 uint32_t value = 0 ;
94157#if defined(HAL_ADC_MODULE_ENABLED ) && !defined(HAL_ADC_MODULE_ONLY )
95158 PinName p = analogInputToPinName (ulPin );
96159 if (p != NC ) {
97- value = adc_read_value (p );
98- value = mapResolution (value , ADC_RESOLUTION , _readResolution );
160+ value = adc_read_value (p , _internalReadResolution );
161+ value = mapResolution (value , _internalReadResolution , _readResolution );
99162 }
100163#else
101164 UNUSED (ulPin );
0 commit comments