@@ -29,15 +29,78 @@ 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 uint32_t _writeFreq = PWM_FREQUENCY ;
3768
3869void analogReadResolution (int res )
3970{
40- _readResolution = res ;
71+ if ((res > 0 ) && (res <= 32 )) {
72+ _readResolution = res ;
73+ _internalReadResolution = _readResolution ;
74+ if (_readResolution > MAX_ADC_RESOLUTION ) {
75+ _internalReadResolution = MAX_ADC_RESOLUTION ;
76+ } else {
77+ #ifdef ADC_RESOLUTION_12B
78+ #ifndef ADC_RESOLUTION_16B
79+ if (_internalReadResolution <= 6 ) {
80+ _internalReadResolution = 6 ;
81+ } else
82+ #endif
83+ if (_internalReadResolution <= 8 ) {
84+ _internalReadResolution = 8 ;
85+ } else if (_internalReadResolution <= 10 ) {
86+ _internalReadResolution = 10 ;
87+ } else if (_internalReadResolution <= 12 ) {
88+ _internalReadResolution = 12 ;
89+ }
90+ #ifdef ADC_RESOLUTION_16B
91+ else if (_internalReadResolution <= 14 ) {
92+ _internalReadResolution = 14 ;
93+ } else if (_internalReadResolution <= 16 ) {
94+ _internalReadResolution = 16 ;
95+ }
96+ #endif
97+ #else
98+ _internalReadResolution = 12 ;
99+ #endif
100+ }
101+ } else {
102+ Error_Handler ();
103+ }
41104}
42105
43106void analogWriteResolution (int res )
@@ -73,16 +136,16 @@ void analogReference(eAnalogReference ulMode)
73136 UNUSED (ulMode );
74137}
75138
76- //perform the read operation on the selected analog pin.
77- //the initialization of the analog PIN is done through this function
139+ // Perform the read operation on the selected analog pin.
140+ // the initialization of the analog PIN is done through this function
78141uint32_t analogRead (uint32_t ulPin )
79142{
80143 uint32_t value = 0 ;
81144#if defined(HAL_ADC_MODULE_ENABLED ) && !defined(HAL_ADC_MODULE_ONLY )
82145 PinName p = analogInputToPinName (ulPin );
83146 if (p != NC ) {
84- value = adc_read_value (p );
85- value = mapResolution (value , ADC_RESOLUTION , _readResolution );
147+ value = adc_read_value (p , _internalReadResolution );
148+ value = mapResolution (value , _internalReadResolution , _readResolution );
86149 }
87150#else
88151 UNUSED (ulPin );
0 commit comments