Skip to content

Commit

Permalink
LEDC - Allow custom channel selection (#9031)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
P-R-O-C-H-Y authored Jan 8, 2024
1 parent 53aa8c8 commit 8a1e463
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
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

0 comments on commit 8a1e463

Please sign in to comment.