Skip to content

Commit 200fe29

Browse files
committed
feat(ledc): Allow custom channel selection
1 parent e9ee9c1 commit 200fe29

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cores/esp32/esp32-hal-ledc.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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, int 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 = {
@@ -117,6 +115,19 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
117115

118116
return true;
119117
}
118+
119+
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
120+
{
121+
int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1);
122+
if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH){
123+
log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH);
124+
return false;
125+
}
126+
int channel = log2(free_channel & -free_channel);
127+
128+
return ledcAttachChannel(pin, freq, resolution, channel);
129+
}
130+
120131
bool ledcWrite(uint8_t pin, uint32_t duty)
121132
{
122133
ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);

cores/esp32/esp32-hal-ledc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef struct {
4545

4646
//channel 0-15 resolution 1-16bits freq limits depend on resolution
4747
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution);
48+
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int channel);
4849
bool ledcWrite(uint8_t pin, uint32_t duty);
4950
uint32_t ledcWriteTone(uint8_t pin, uint32_t freq);
5051
uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave);

0 commit comments

Comments
 (0)