Skip to content

Commit f186d16

Browse files
committed
esp32/machine_pwm.c: Fix freq.
Signed-off-by: Ihor Nehrutsa <[email protected]>
1 parent ad4e15e commit f186d16

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

ports/esp32/machine_pwm.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, " | %d at %s\n", __LINE__, __FILE__);
4444

45-
#define FADE 1
45+
#define FADE 0
4646

4747
// 10-bit user interface resolution compatible with esp8266 PWM.duty()
4848
#define UI_RES_10_BIT (10)
@@ -440,8 +440,17 @@ static bool is_free_channels(int mode, int pin) {
440440
return false;
441441
}
442442

443+
static bool is_free_timers(int mode, int32_t freq) {
444+
for (int timer = 0; timer < LEDC_TIMER_MAX; ++timer) {
445+
if ((timers[mode][timer].freq < 0) || (timers[mode][timer].freq == freq)) {
446+
return true;
447+
}
448+
}
449+
return false;
450+
}
451+
443452
// Find self channel or free channel
444-
static void find_channel(machine_pwm_obj_t *self, int *ret_mode, int *ret_channel) {
453+
static void find_channel(machine_pwm_obj_t *self, int *ret_mode, int *ret_channel, int32_t freq) {
445454
// Try to find self channel first
446455
for (int mode = 0; mode < LEDC_SPEED_MODE_MAX; ++mode) {
447456
#if SOC_LEDC_SUPPORT_HS_MODE
@@ -465,7 +474,7 @@ static void find_channel(machine_pwm_obj_t *self, int *ret_mode, int *ret_channe
465474
}
466475
#endif
467476
for (int channel = 0; channel < LEDC_CHANNEL_MAX; ++channel) {
468-
if (chans[mode][channel].pin < 0) {
477+
if ((chans[mode][channel].pin < 0) && is_free_timers(mode, freq)) {
469478
*ret_mode = mode;
470479
*ret_channel = channel;
471480
return;
@@ -617,7 +626,7 @@ static void mp_machine_pwm_init_helper(machine_pwm_obj_t *self,
617626
// Check the current mode and channel
618627
int mode = -1;
619628
int channel = -1;
620-
find_channel(self, &mode, &channel);
629+
find_channel(self, &mode, &channel, freq);
621630
if (channel < 0) {
622631
mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("out of %sPWM channels:%d"), self->light_sleep_enable ? "light sleep capable " : "", self->light_sleep_enable ? LEDC_CHANNEL_MAX : LEDC_SPEED_MODE_MAX *LEDC_CHANNEL_MAX);
623632
}

0 commit comments

Comments
 (0)