8000 esp32/machine_pwm: Clean up macro names and their use. · micropython/micropython@15e65b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15e65b7

Browse files
IhorNehrutsadpgeorge
authored andcommitted
esp32/machine_pwm: Clean up macro names and their use.
- Remove UI_RES_SHIFT macro. - Rename PWFREQ to PWM_FREQ. - Rename PWRES to PWM_RES_10_BIT. - Use UI_RES_16_BIT flag instead of HIGHEST_PWM_RES.
1 parent a5e64c2 commit 15e65b7

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

ports/esp32/machine_pwm.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ STATIC ledc_timer_config_t timers[PWM_TIMER_MAX];
7171
#define TIMER_IDX_TO_MODE(timer_idx) (timer_idx / LEDC_TIMER_MAX)
7272
#define TIMER_IDX_TO_TIMER(timer_idx) (timer_idx % LEDC_TIMER_MAX)
7373

74-
// Params for PW operation
74+
// Params for PWM operation
7575
// 5khz is default frequency
76-
#define PWFREQ (5000)
77-
78-
// 10-bit resolution (compatible with esp8266 PWM)
79-
#define PWRES (LEDC_TIMER_10_BIT)
76+
#define PWM_FREQ (5000)
77+
// default 10-bit resolution (compatible with esp8266 PWM)
78+
#define PWM_RES_10_BIT (LEDC_TIMER_10_BIT)
8079

8180
// Maximum duty value on 10-bit resolution
82-
#define MAX_DUTY_U10 ((1 << PWRES) - 1)
81+
#define MAX_DUTY_U10 ((1 << PWM_RES_10_BIT) - 1)
8382
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html#supported-range-of-frequency-and-duty-resolutions
8483
// duty() uses 10-bit resolution or less
8584
// duty_u16() and duty_ns() use 16-bit resolution or less
@@ -94,8 +93,6 @@ STATIC ledc_timer_config_t timers[PWM_TIMER_MAX];
9493
#define UI_RES_16_BIT (16)
9594
// Maximum duty value on highest user interface resolution
9695
#define UI_MAX_DUTY ((1 << UI_RES_16_BIT) - 1)
97-
// How much to shift from the HIGHEST_PWM_RES duty resolution to the user interface duty resolution UI_RES_16_BIT
98-
#define UI_RES_SHIFT (UI_RES_16_BIT - HIGHEST_PWM_RES) // 0 for ESP32, 2 for S2, S3, C3
9996

10097
// If the PWM frequency is less than EMPIRIC_FREQ, then LEDC_REF_CLK_HZ(1 MHz) source is used, else LEDC_APB_CLK_HZ(80 MHz) source is used
10198
#define EMPIRIC_FREQ (10) // Hz
@@ -111,7 +108,7 @@ typedef struct _machine_pwm_obj_t {
111108
int mode;
112109
int channel;
113110
int timer;
114-
int duty_x; // PWRES if duty(), HIGHEST_PWM_RES if duty_u16(), -HIGHEST_PWM_RES if duty_ns()
111+
int duty_x; // PWM_RES_10_BIT if duty(), UI_RES_16_BIT if duty_u16(), -UI_RES_16_BIT if duty_ns()
115112
int duty_u10; // stored values from previous duty setters
116113
int duty_u16; // - / -
117114
int duty_ns; // - / -
@@ -264,11 +261,11 @@ STATIC void set_freq(machine_pwm_obj_t *self, unsigned int freq, ledc_timer_conf
264261

265262
// Save the same duty cycle when frequency is changed
266263
if (save_duty_resolution != timer->duty_resolution) {
267-
if (self->duty_x == HIGHEST_PWM_RES) {
264+
if (self->duty_x == UI_RES_16_BIT) {
268265
set_duty_u16(self, self->duty_u16);
269-
} else if (self->duty_x == PWRES) {
266+
} else if (self->duty_x == PWM_RES_10_BIT) {
270267
set_duty_u10(self, self->duty_u10);
271-
} else if (self->duty_x == -HIGHEST_PWM_RES) {
268+
} else if (self->duty_x == -UI_RES_16_BIT) {
272269
set_duty_ns(self, self->duty_ns);
273270
}
274271
}
@@ -296,7 +293,7 @@ STATIC int duty_to_ns(machine_pwm_obj_t *self, int duty) {
296293
#define get_duty_raw(self) ledc_get_duty(self->mode, self->channel)
297294

298295
STATIC uint32_t get_duty_u16(machine_pwm_obj_t *self) {
299-
return ledc_get_duty(self->mode, self->channel) << (HIGHEST_PWM_RES + UI_RES_SHIFT - timers[TIMER_IDX(self->mode, self->timer)].duty_resolution);
296+
return ledc_get_duty(self->mode, self->channel) << (UI_RES_16_BIT - timers[TIMER_IDX(self->mode, self->timer)].duty_resolution);
300297
}
301298

302299
STATIC uint32_t get_duty_u10(machine_pwm_obj_t *self) {
@@ -312,7 +309,7 @@ STATIC void set_duty_u16(machine_pwm_obj_t *self, int duty) {
312309
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("duty_u16 must be from 0 to %d"), UI_MAX_DUTY);
313310
}
314311
ledc_timer_config_t timer = timers[TIMER_IDX(self->mode, self->timer)];
315-
int channel_duty = duty >> (HIGHEST_PWM_RES + UI_RES_SHIFT - timer.duty_resolution);
312+
int channel_duty = duty >> (UI_RES_16_BIT - timer.duty_resolution);
316313
int max_duty = (1 << timer.duty_resolution) - 1;
317314
if (channel_duty < 0) {
318315
channel_duty = 0;
@@ -336,16 +333,16 @@ STATIC void set_duty_u16(machine_pwm_obj_t *self, int duty) {
336333
}
337334
*/
338335

339-
self->duty_x = HIGHEST_PWM_RES;
336+
self->duty_x = UI_RES_16_BIT;
340337
self->duty_u16 = duty;
341338
}
342339

343340
STATIC void set_duty_u10(machine_pwm_obj_t *self, int duty) {
344341
if ((duty < 0) || (duty > MAX_DUTY_U10)) {
345342
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("duty must be from 0 to %u"), MAX_DUTY_U10);
346343
}
347-
set_duty_u16(self, duty << (HIGHEST_PWM_RES + UI_RES_SHIFT - PWRES));
348-
self->duty_x = PWRES;
344+
set_duty_u16(self, duty << (UI_RES_16_BIT - LEDC_TIMER_10_BIT));
345+
self->duty_x = PWM_RES_10_BIT;
349346
self->duty_u10 = duty;
350347
}
351348

@@ -354,7 +351,7 @@ STATIC void set_duty_ns(machine_pwm_obj_t *self, int ns) {
354351
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("duty_ns must be from 0 to %d ns"), duty_to_ns(self, UI_MAX_DUTY));
355352
}
356353
set_duty_u16(self, ns_to_duty(self, ns));
357-
self->duty_x = -HIGHEST_PWM_RES;
354+
self->duty_x = -UI_RES_16_BIT;
358355
self->duty_ns = ns;
359356
}
360357

@@ -425,9 +422,9 @@ STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_p
425422
if (self->active) {
426423
mp_printf(print, ", freq=%u", ledc_get_freq(self->mode, self->timer));
427424

428-
if (self->duty_x == PWRES) {
425+
if (self->duty_x == PWM_RES_10_BIT) {
429426
mp_printf(print, ", duty=%d", get_duty_u10(self));
430-
} else if (self->duty_x == -HIGHEST_PWM_RES) {
427+
} else if (self->duty_x == -UI_RES_16_BIT) {
431428
mp_printf(print, ", duty_ns=%d", get_duty_ns(self));
432429
} else {
433430
mp_printf(print, ", duty_u16=%d", get_duty_u16(self));
@@ -479,7 +476,7 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self,
479476
freq = timers[chans[channel_idx].timer_idx].freq_hz;
480477
}
481478
if (freq <= 0) {
482-
freq = PWFREQ;
479+
freq = PWM_FREQ;
483480
}
484481
}
485482
if ((freq <= 0) || (freq > 40000000)) {
@@ -536,7 +533,7 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self,
536533
} else if (duty != -1) {
537534
set_duty_u10(self, duty);
538535
} else if (self->duty_x == 0) {
539-
set_duty_u10(self, (1 << PWRES) / 2); // 50%
536+
set_duty_u10(self, (1 << PWM_RES_10_BIT) / 2); // 50%
540537
}
541538
}
542539

0 commit comments

Comments
 (0)
0