@@ -29,9 +29,40 @@ extern "C" {
29
29
uint32_t g_anOutputPinConfigured [MAX_NB_PORT ] = {0 };
30
30
#endif
31
31
32
+ #if !defined(ADC_RESOLUTION_16B )
33
+ #define MAX_ADC_RESOLUTION 12
34
+ #else
35
+ #define MAX_ADC_RESOLUTION 16
36
+ #endif
32
37
#define MAX_PWM_RESOLUTION 16
33
38
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
+
35
66
static int _writeResolution = PWM_RESOLUTION ;
36
67
static int _internalWriteResolution =
37
68
#if PWM_RESOLUTION > MAX_PWM_RESOLUTION
@@ -45,7 +76,43 @@ static uint32_t _writeFreq = PWM_FREQUENCY;
45
76
46
77
void analogReadResolution (int res )
47
78
{
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
+ }
49
116
}
50
117
51
118
void analogWriteResolution (int res )
@@ -86,16 +153,16 @@ void analogReference(eAnalogReference ulMode)
86
153
UNUSED (ulMode );
87
154
}
88
155
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
91
158
uint32_t analogRead (uint32_t ulPin )
92
159
{
93
160
uint32_t value = 0 ;
94
161
#if defined(HAL_ADC_MODULE_ENABLED ) && !defined(HAL_ADC_MODULE_ONLY )
95
162
PinName p = analogInputToPinName (ulPin );
96
163
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 );
99
166
}
100
167
#else
101
168
UNUSED (ulPin );
0 commit comments