Skip to content

Commit 9ca0b9f

Browse files
committed
chore(analog): cleanup befor HALv2
- remove unused analogOutputInit function - reorder functions per features Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent d35320e commit 9ca0b9f

3 files changed

Lines changed: 425 additions & 434 deletions

File tree

‎cores/arduino/wiring.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ extern void analogWriteResolution(int res);
5454
* \param freq
5555
*/
5656
extern void analogWriteFrequency(uint32_t freq);
57-
extern void analogOutputInit(void) ;
5857

5958
/* wiring_constants */
6059
#define DEFAULT 1

‎cores/arduino/wiring_analog.c‎

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,24 @@
2323
extern "C" {
2424
#endif
2525

26+
/* DAC/PWM */
2627
#if (defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)) ||\
2728
(defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY))
2829
//This is the list of the IOs configured
2930
uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0};
3031
#endif
32+
#define MAX_PWM_RESOLUTION 16
33+
static int _writeResolution = PWM_RESOLUTION;
34+
static int _internalWriteResolution =
35+
#if PWM_RESOLUTION > MAX_PWM_RESOLUTION
36+
MAX_PWM_RESOLUTION
37+
#else
38+
PWM_RESOLUTION
39+
#endif
40+
;
41+
static uint32_t _writeFreq = PWM_FREQUENCY;
3142

43+
/* ADC */
3244
#if defined(ADC_RESOLUTION_16B) || defined(ADC_DS_DATA_WIDTH_16_BIT)
3345
#define MAX_ADC_RESOLUTION 16
3446
#elif defined(ADC_DS_DATA_WIDTH_15_BIT)
@@ -40,7 +52,6 @@ uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0};
4052
#else
4153
#define MAX_ADC_RESOLUTION 12
4254
#endif
43-
#define MAX_PWM_RESOLUTION 16
4455

4556
static int _readResolution = ADC_RESOLUTION;
4657
static int _internalReadResolution =
@@ -71,17 +82,22 @@ static int _internalReadResolution =
7182
#endif /* ADC_RESOLUTION > MAX_ADC_RESOLUTION */
7283
;
7384

74-
static int _writeResolution = PWM_RESOLUTION;
75-
static int _internalWriteResolution =
76-
#if PWM_RESOLUTION > MAX_PWM_RESOLUTION
77-
MAX_PWM_RESOLUTION
78-
#else
79-
PWM_RESOLUTION
80-
#endif
81-
;
82-
83-
static uint32_t _writeFreq = PWM_FREQUENCY;
85+
/* Common utility function to map a value from one resolution to another */
86+
static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to)
87+
{
88+
if (from != to) {
89+
if (from > to) {
90+
value = (value < (uint32_t)(1 << (from - to))) ? 0 : ((value + 1) >> (from - to)) - 1;
91+
} else {
92+
if (value != 0) {
93+
value = ((value + 1) << (to - from)) - 1;
94+
}
95+
}
96+
}
97+
return value;
98+
}
8499

100+
/* ADC */
85101
void analogReadResolution(int res)
86102
{
87103
if ((res > 0) && (res <= 32)) {
@@ -144,39 +160,6 @@ void analogReadResolution(int res)
144160
}
145161
}
146162

147-
void analogWriteResolution(int res)
148-
{
149-
if ((res > 0) && (res <= 32)) {
150-
_writeResolution = res;
151-
if (_writeResolution > MAX_PWM_RESOLUTION) {
152-
_internalWriteResolution = MAX_PWM_RESOLUTION;
153-
} else {
154-
_internalWriteResolution = _writeResolution;
155-
}
156-
} else {
157-
Error_Handler();
158-
}
159-
}
160-
161-
void analogWriteFrequency(uint32_t freq)
162-
{
163-
_writeFreq = freq;
164-
}
165-
166-
static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to)
167-
{
168-
if (from != to) {
169-
if (from > to) {
170-
value = (value < (uint32_t)(1 << (from - to))) ? 0 : ((value + 1) >> (from - to)) - 1;
171-
} else {
172-
if (value != 0) {
173-
value = ((value + 1) << (to - from)) - 1;
174-
}
175-
}
176-
}
177-
return value;
178-
}
179-
180163
void analogReference(uint8_t mode)
181164
{
182165
(void)mode;
@@ -199,15 +182,28 @@ int analogRead(pin_size_t pinNumber)
199182
return value;
200183
}
201184

185+
/* DAC/PWM */
186+
void analogWriteResolution(int res)
187+
{
188+
if ((res > 0) && (res <= 32)) {
189+
_writeResolution = res;
190+
if (_writeResolution > MAX_PWM_RESOLUTION) {
191+
_internalWriteResolution = MAX_PWM_RESOLUTION;
192+
} else {
193+
_internalWriteResolution = _writeResolution;
194+
}
195+
} else {
196+
Error_Handler();
197+
}
198+
}
202199

203-
void analogOutputInit(void)
200+
void analogWriteFrequency(uint32_t freq)
204201
{
202+
_writeFreq = freq;
205203
}
206204

207-
// Right now, PWM output only works on the pins with
208-
// hardware support. These are defined in the appropriate
209-
// variant.cpp file. For the rest of the pins, we default
210-
// to digital output.
205+
// Output only works on the pins with Hardware Timer (PWM) or DAC support.
206+
// For the other pins, default to digital output.
211207
void analogWrite(pin_size_t pinNumber, int value)
212208
{
213209
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)

0 commit comments

Comments
 (0)