Skip to content

Commit 8a1e463

Browse files
authored
LEDC - Allow custom channel selection (#9031)
* feat(ledc): Allow custom channel selection * fix(ledc): Fix check of maximum channel * docs(ledc): Add ledcAttachChannel to docs * feat(ledc): Change channel to uint8_t + add log message
1 parent 53aa8c8 commit 8a1e463

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Diff for: cores/esp32/esp32-hal-ledc.c

+17-5
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, 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+
120132
bool ledcWrite(uint8_t pin, uint32_t duty)
121133
{
122134
ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);

Diff for: cores/esp32/esp32-hal-ledc.h

+1
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, uint8_t 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);

Diff for: docs/source/api/ledc.rst

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ledcAttach
2727
**********
2828

2929
This function is used to setup LEDC pin with given frequency and resolution.
30+
LEDC channel will be selected automatically.
3031

3132
.. code-block:: arduino
3233
@@ -41,6 +42,25 @@ This function is used to setup LEDC pin with given frequency and resolution.
4142
This function will return ``true`` if configuration is successful.
4243
If ``false`` is returned, error occurs and LEDC channel was not configured.
4344

45+
ledcAttachChannel
46+
*****************
47+
48+
This function is used to setup LEDC pin with given frequency, resolution and channel.
49+
50+
.. code-block:: arduino
51+
52+
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel);
53+
54+
* ``pin`` select LEDC pin.
55+
* ``freq`` select frequency of pwm.
56+
* ``resolution`` select resolution for LEDC channel.
57+
* ``channel`` select LEDC channel.
58+
59+
* range is 1-14 bits (1-20 bits for ESP32).
60+
61+
This function will return ``true`` if configuration is successful.
62+
If ``false`` is returned, error occurs and LEDC channel was not configured.
63+
4464
ledcWrite
4565
*********
4666

0 commit comments

Comments
 (0)