@@ -56,12 +56,11 @@ static bool ledcDetachBus(void * bus){
5656 return true;
5757}
5858
59- bool ledcAttach (uint8_t pin , uint32_t freq , uint8_t resolution )
59+ bool ledcAttachChannel (uint8_t pin , uint32_t freq , uint8_t resolution , uint8_t channel )
6060{
61- int free_channel = ~ledc_handle .used_channels & (ledc_handle .used_channels + 1 );
62- if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH )
61+ if (channel >= LEDC_CHANNELS || resolution > LEDC_MAX_BIT_WIDTH )
6362 {
64- log_e ("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)" , LEDC_CHANNELS , LEDC_MAX_BIT_WIDTH );
63+ log_e ("Channel %u is not available! (maximum %u) or bit width too big (maximum %u)" , channel , LEDC_CHANNELS , LEDC_MAX_BIT_WIDTH );
6564 return false;
6665 }
6766
@@ -71,7 +70,6 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
7170 return false;
7271 }
7372
74- int channel = log2 (free_channel & - free_channel );
7573 uint8_t group = (channel /8 ), timer = ((channel /2 )%4 );
7674
7775 ledc_timer_config_t ledc_timer = {
@@ -115,8 +113,22 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
115113 return false;
116114 }
117115
116+ log_i ("LEDC attached to pin %u (channel %u, resolution %u)" , pin , channel , resolution );
118117 return true;
119118}
119+
120+ bool ledcAttach (uint8_t pin , uint32_t freq , uint8_t resolution )
121+ {
122+ uint8_t free_channel = ~ledc_handle .used_channels & (ledc_handle .used_channels + 1 );
123+ if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH ){
124+ log_e ("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)" , LEDC_CHANNELS , LEDC_MAX_BIT_WIDTH );
125+ return false;
126+ }
127+ int channel = log2 (free_channel & - free_channel );
128+
129+ return ledcAttachChannel (pin , freq , resolution , channel );
130+ }
131+
120132bool ledcWrite (uint8_t pin , uint32_t duty )
121133{
122134 ledc_channel_handle_t * bus = (ledc_channel_handle_t * )perimanGetPinBus (pin , ESP32_BUS_TYPE_LEDC );
0 commit comments