Skip to content

Commit 132d88a

Browse files
committed
esp32: Implement review comments.
1 parent 28030a8 commit 132d88a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

ports/esp32/machine_pwm.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ 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)
76+
#define PWM_FREQ (5000)
7777

7878
// 10-bit resolution (compatible with esp8266 PWM)
79-
#define PWRES (LEDC_TIMER_10_BIT)
79+
#define PWM_RES_10_BIT (LEDC_TIMER_10_BIT)
8080

8181
// Maximum duty value on 10-bit resolution
82-
#define MAX_DUTY_U10 ((1 << PWRES) - 1)
82+
#define MAX_DUTY_U10 ((1 << PWM_RES_10_BIT) - 1)
8383
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html#supported-range-of-frequency-and-duty-resolutions
8484
// duty() uses 10-bit resolution or less
8585
// duty_u16() and duty_ns() use 16-bit resolution or less
@@ -111,7 +111,7 @@ typedef struct _machine_pwm_obj_t {
111111
int mode;
112112
int channel;
113113
int timer;
114-
int duty_x; // PWRES if duty(), HIGHEST_PWM_RES if duty_u16(), -HIGHEST_PWM_RES if duty_ns()
114+
int duty_x; // PWM_RES_10_BIT if duty(), HIGHEST_PWM_RES if duty_u16(), -HIGHEST_PWM_RES if duty_ns()
115115
int duty_u10; // stored values from previous duty setters
116116
int duty_u16; // - / -
117117
int duty_ns; // - / -
@@ -264,7 +264,7 @@ STATIC void set_freq(machine_pwm_obj_t *self, unsigned int freq, ledc_timer_conf
264264
// Save the same duty cycle when frequency is changed
265265
if (self->duty_x == HIGHEST_PWM_RES) {
266266
set_duty_u16(self, self->duty_u16);
267-
} else if (self->duty_x == PWRES) {
267+
} else if (self->duty_x == PWM_RES_10_BIT) {
268268
set_duty_u10(self, self->duty_u10);
269269
} else if (self->duty_x == -HIGHEST_PWM_RES) {
270270
set_duty_ns(self, self->duty_ns);
@@ -352,8 +352,8 @@ STATIC void set_duty_u10(machine_pwm_obj_t *self, int duty) {
352352
if ((duty < 0) || (duty > MAX_DUTY_U10)) {
353353
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("duty must be from 0 to %u"), MAX_DUTY_U10);
354354
}
355-
set_duty_u16(self, duty << (HIGHEST_PWM_RES + UI_RES_SHIFT - PWRES));
356-
self->duty_x = PWRES;
355+
set_duty_u16(self, duty << (UI_RES_16_BIT - PWM_RES_10_BIT));
356+
self->duty_x = PWM_RES_10_BIT;
357357
self->duty_u10 = duty;
358358
}
359359

@@ -433,7 +433,7 @@ STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_p
433433
if (self->active) {
434434
mp_printf(print, ", freq=%u", ledc_get_freq(self->mode, self->timer));
435435

436-
if (self->duty_x == PWRES) {
436+
if (self->duty_x == PWM_RES_10_BIT) {
437437
mp_printf(print, ", duty=%d", get_duty_u10(self));
438438
} else if (self->duty_x == -HIGHEST_PWM_RES) {
439439
mp_printf(print, ", duty_ns=%d", get_duty_ns(self));
@@ -487,7 +487,7 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self,
487487
freq = timers[chans[channel_idx].timer_idx].freq_hz;
488488
}
489489
if (freq <= 0) {
490-
freq = PWFREQ;
490+
freq = PWM_FREQ;
491491
}
492492
}
493493
if ((freq <= 0) || (freq > 40000000)) {
@@ -544,7 +544,7 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self,
544544
} else if (duty != -1) {
545545
set_duty_u10(self, duty);
546546
} else if (self->duty_x == 0) {
547-
set_duty_u10(self, (1 << PWRES) / 2); // 50%
547+
set_duty_u10(self, (1 << PWM_RES_10_BIT) / 2); // 50%
548548
}
549549
}
550550

0 commit comments

Comments
 (0)