42
42
43
43
#define debug_printf (...) // mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, " | %d at %s\n", __LINE__, __FILE__);
44
44
45
- #define FADE 1
45
+ #define FADE 0
46
46
47
47
// 10-bit user interface resolution compatible with esp8266 PWM.duty()
48
48
#define UI_RES_10_BIT (10)
@@ -440,8 +440,17 @@ static bool is_free_channels(int mode, int pin) {
440
440
return false;
441
441
}
442
442
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
+
443
452
// 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 ) {
445
454
// Try to find self channel first
446
455
for (int mode = 0 ; mode < LEDC_SPEED_MODE_MAX ; ++ mode ) {
447
456
#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
465
474
}
466
475
#endif
467
476
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 ) ) {
469
478
* ret_mode = mode ;
470
479
* ret_channel = channel ;
471
480
return ;
@@ -617,7 +626,7 @@ static void mp_machine_pwm_init_helper(machine_pwm_obj_t *self,
617
626
// Check the current mode and channel
618
627
int mode = -1 ;
619
628
int channel = -1 ;
620
- find_channel (self , & mode , & channel );
629
+ find_channel (self , & mode , & channel , freq );
621
630
if (channel < 0 ) {
622
631
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 );
623
632
}
0 commit comments