Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LEDC - Allow custom channel selection #9031

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions cores/esp32/esp32-hal-ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ static bool ledcDetachBus(void * bus){
return true;
}

bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel)
{
int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1);
if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH)
if (channel >= LEDC_CHANNELS || resolution > LEDC_MAX_BIT_WIDTH)
{
log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH);
log_e("Channel %u is not available! (maximum %u) or bit width too big (maximum %u)", channel, LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH);
return false;
}

Expand All @@ -71,7 +70,6 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
return false;
}

int channel = log2(free_channel & -free_channel);
uint8_t group=(channel/8), timer=((channel/2)%4);

ledc_timer_config_t ledc_timer = {
Expand Down Expand Up @@ -115,8 +113,22 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
return false;
}

log_i("LEDC attached to pin %u (channel %u, resolution %u)", pin, channel, resolution);
return true;
}

bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
{
uint8_t free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1);
if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH){
log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH);
return false;
}
int channel = log2(free_channel & -free_channel);

return ledcAttachChannel(pin, freq, resolution, channel);
}

bool ledcWrite(uint8_t pin, uint32_t duty)
{
ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/esp32-hal-ledc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct {

//channel 0-15 resolution 1-16bits freq limits depend on resolution
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution);
bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel);
bool ledcWrite(uint8_t pin, uint32_t duty);
uint32_t ledcWriteTone(uint8_t pin, uint32_t freq);
uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave);
Expand Down
20 changes: 20 additions & 0 deletions docs/source/api/ledc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ledcAttach
**********

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

.. code-block:: arduino

Expand All @@ -41,6 +42,25 @@ This function is used to setup LEDC pin with given frequency and resolution.
This function will return ``true`` if configuration is successful.
If ``false`` is returned, error occurs and LEDC channel was not configured.

ledcAttachChannel
*****************

This function is used to setup LEDC pin with given frequency, resolution and channel.

.. code-block:: arduino

bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel);

* ``pin`` select LEDC pin.
* ``freq`` select frequency of pwm.
* ``resolution`` select resolution for LEDC channel.
* ``channel`` select LEDC channel.

* range is 1-14 bits (1-20 bits for ESP32).

This function will return ``true`` if configuration is successful.
If ``false`` is returned, error occurs and LEDC channel was not configured.

ledcWrite
*********

Expand Down