@@ -29,18 +29,104 @@ extern "C" {
29
29
uint32_t g_anOutputPinConfigured [MAX_NB_PORT ] = {0 };
30
30
#endif
31
31
32
- static int _readResolution = 10 ;
33
- int _writeResolution = PWM_RESOLUTION ;
32
+ #if !defined(ADC_RESOLUTION_16B )
33
+ #define MAX_ADC_RESOLUTION 12
34
+ #else
35
+ #define MAX_ADC_RESOLUTION 16
36
+ #endif
37
+ #define MAX_PWM_RESOLUTION 16
38
+
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
+
66
+ static int _writeResolution = PWM_RESOLUTION ;
67
+ static int _internalWriteResolution =
68
+ #if PWM_RESOLUTION > MAX_PWM_RESOLUTION
69
+ MAX_PWM_RESOLUTION
70
+ #else
71
+ PWM_RESOLUTION
72
+ #endif
73
+ ;
74
+
34
75
static uint32_t _writeFreq = PWM_FREQUENCY ;
35
76
36
77
void analogReadResolution (int res )
37
78
{
38
- _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
+ }
39
116
}
40
117
41
118
void analogWriteResolution (int res )
42
119
{
43
- _writeResolution = res ;
120
+ if ((res > 0 ) && (res <= 32 )) {
121
+ _writeResolution = res ;
122
+ if (_writeResolution > MAX_ADC_RESOLUTION ) {
123
+ _internalWriteResolution = MAX_ADC_RESOLUTION ;
124
+ } else {
125
+ _internalWriteResolution = _writeResolution ;
126
+ }
127
+ } else {
128
+ Error_Handler ();
129
+ }
44
130
}
45
131
46
132
void analogWriteFrequency (uint32_t freq )
@@ -67,16 +153,16 @@ void analogReference(eAnalogReference ulMode)
67
153
UNUSED (ulMode );
68
154
}
69
155
70
- //perform the read operation on the selected analog pin.
71
- //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
72
158
uint32_t analogRead (uint32_t ulPin )
73
159
{
74
160
uint32_t value = 0 ;
75
161
#if defined(HAL_ADC_MODULE_ENABLED ) && !defined(HAL_ADC_MODULE_ONLY )
76
162
PinName p = analogInputToPinName (ulPin );
77
163
if (p != NC ) {
78
- value = adc_read_value (p );
79
- value = mapResolution (value , ADC_RESOLUTION , _readResolution );
164
+ value = adc_read_value (p , _internalReadResolution );
165
+ value = mapResolution (value , _internalReadResolution , _readResolution );
80
166
}
81
167
#else
82
168
UNUSED (ulPin );
@@ -115,7 +201,8 @@ void analogWrite(uint32_t ulPin, uint32_t ulValue)
115
201
if (is_pin_configured (p , g_anOutputPinConfigured ) == false) {
116
202
set_pin_configured (p , g_anOutputPinConfigured );
117
203
}
118
- pwm_start (p , _writeFreq , ulValue , _writeResolution );
204
+ ulValue = mapResolution (ulValue , _writeResolution , _internalWriteResolution );
205
+ pwm_start (p , _writeFreq , ulValue , _internalWriteResolution );
119
206
} else
120
207
#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */
121
208
{
0 commit comments