@@ -29,15 +29,78 @@ 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_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
+
35
66
int _writeResolution = PWM_RESOLUTION ;
36
67
static uint32_t _writeFreq = PWM_FREQUENCY ;
37
68
38
69
void analogReadResolution (int res )
39
70
{
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
+ }
41
104
}
42
105
43
106
void analogWriteResolution (int res )
@@ -73,16 +136,16 @@ void analogReference(eAnalogReference ulMode)
73
136
UNUSED (ulMode );
74
137
}
75
138
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
78
141
uint32_t analogRead (uint32_t ulPin )
79
142
{
80
143
uint32_t value = 0 ;
81
144
#if defined(HAL_ADC_MODULE_ENABLED ) && !defined(HAL_ADC_MODULE_ONLY )
82
145
PinName p = analogInputToPinName (ulPin );
83
146
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 );
86
149
}
87
150
#else
88
151
UNUSED (ulPin );
0 commit comments