Skip to content

Commit

Permalink
fix(ledc): fix ledc_get_freq calculation err due to overflow
Browse files Browse the repository at this point in the history
Closes #14882
  • Loading branch information
songruo committed Dec 30, 2024
1 parent 67e7a93 commit 191c2ae
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components/esp_driver_ledc/src/ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num)
ledc_hal_get_clock_divider(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &clock_divider);
ledc_hal_get_duty_resolution(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &duty_resolution);
ledc_hal_get_clk_cfg(&(p_ledc_obj[speed_mode]->ledc_hal), timer_num, &clk_cfg);
uint32_t precision = (0x1 << duty_resolution);
uint64_t precision = (0x1 << duty_resolution);
uint32_t src_clk_freq = 0;
esp_clk_tree_src_get_freq_hz((soc_module_clk_t)clk_cfg, LEDC_CLK_SRC_FREQ_PRECISION, &src_clk_freq);
portEXIT_CRITICAL(&ledc_spinlock);
Expand Down
6 changes: 3 additions & 3 deletions docs/en/api-reference/peripherals/ledc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ The LEDC hardware provides the means to gradually transition from one duty cycle

On {IDF_TARGET_NAME}, the hardware additionally allows to perform up to {IDF_TARGET_LEDC_MAX_FADE_RANGE_NUM} consecutive linear fades without CPU intervention. This feature can be useful if you want to do a fade with gamma correction.

The luminance perceived by human eyes does not have a linear relationship with the PWM duty cycle. In order to make human feel the LED is dimming or lightening linearly, the change in duty cycle should be non-linear, which is the so-called gamma correction. The LED controller can simulate a gamma curve fading by piecewise linear approximation. :cpp:func:`ledc_fill_multi_fade_param_list` is a function that can help to construct the parameters for the piecewise linear fades. First, you need to allocate a memory block for saving the fade parameters, then by providing start/end PWM duty cycle values, gamma correction function, and the total number of desired linear segments to the helper function, it will fill the calculation results into the allocated space. You can also construct the array of :cpp:type:`ledc_fade_param_config_t` manually. Once the fade parameter structs are prepared, a consecutive fading can be configured by passing the pointer to the prepared :cpp:type:`ledc_fade_param_config_t` list and the total number of fade ranges to :cpp:func:`ledc_set_multi_fade`.
The luminance perceived by human eyes does not have a linear relationship with the PWM duty cycle. To make the LED appear to dim or brighten uniformly to the human eye, the change in duty cycle should be non-linear, which is the so-called gamma correction. The LED controller can simulate a gamma curve fading by piecewise linear approximation. :cpp:func:`ledc_fill_multi_fade_param_list` is a function that can help to construct the parameters for the piecewise linear fades. First, you need to allocate a memory block for saving the fade parameters, then by providing start/end PWM duty cycle values, gamma correction function, and the total number of desired linear segments to the helper function, it will fill the calculation results into the allocated space. You can also construct the array of :cpp:type:`ledc_fade_param_config_t` manually. Once the fade parameter structs are prepared, a consecutive fading can be configured by passing the pointer to the prepared :cpp:type:`ledc_fade_param_config_t` list and the total number of fade ranges to :cpp:func:`ledc_set_multi_fade`.

.. only:: esp32

Expand Down Expand Up @@ -300,14 +300,14 @@ The LEDC API provides several ways to change the PWM frequency "on the fly":
More Control Over PWM
"""""""""""""""""""""

There are several lower level timer-specific functions that can be used to change PWM settings:
There are several individual timer-specific functions that can be used to change PWM output:

* :cpp:func:`ledc_timer_set`
* :cpp:func:`ledc_timer_rst`
* :cpp:func:`ledc_timer_pause`
* :cpp:func:`ledc_timer_resume`

The first two functions are called "behind the scenes" by :cpp:func:`ledc_channel_config` to provide a startup of a timer after it is configured.
The first two functions are called "behind the scenes" by :cpp:func:`ledc_timer_config` to provide a startup of a timer after it is configured.


Use Interrupts
Expand Down
4 changes: 2 additions & 2 deletions docs/zh_CN/api-reference/peripherals/ledc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,14 @@ LED PWM 控制器 API 有多种方式即时改变 PWM 频率:
控制 PWM 的更多方式
"""""""""""""""""""""

有一些较底层的定时器特定函数可用于更改 PWM 设置
有一些较独立的定时器特定函数可用于更改 PWM 输出

* :cpp:func:`ledc_timer_set`
* :cpp:func:`ledc_timer_rst`
* :cpp:func:`ledc_timer_pause`
* :cpp:func:`ledc_timer_resume`

前两个功能可通过函数 :cpp:func:`ledc_channel_config` 在后台运行,在定时器配置后启动。
前两个功能可通过函数 :cpp:func:`ledc_timer_config` 在后台运行,在定时器配置后启动。


使用中断
Expand Down

0 comments on commit 191c2ae

Please sign in to comment.