ledc.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. LED Control (LEDC)
  2. ==================
  3. {IDF_TARGET_LEDC_CHAN_NUM:default="8", esp32="16", esp32s2="8", esp32c3="6", esp32s3="8", esp32c2="6", esp32h2="6"}
  4. :link_to_translation:`zh_CN:[中文]`
  5. Introduction
  6. ------------
  7. The LED control (LEDC) peripheral is primarily designed to control the intensity of LEDs, although it can also be used to generate PWM signals for other purposes.
  8. It has {IDF_TARGET_LEDC_CHAN_NUM} channels which can generate independent waveforms that can be used, for example, to drive RGB LED devices.
  9. .. only:: esp32
  10. LEDC channels are divided into two groups of 8 channels each. One group of LEDC channels operates in high speed mode. This mode is implemented in hardware and offers automatic and glitch-free changing of the PWM duty cycle. The other group of channels operate in low speed mode, the PWM duty cycle must be changed by the driver in software. Each group of channels is also able to use different clock sources.
  11. The PWM controller can automatically increase or decrease the duty cycle gradually, allowing for fades without any processor interference.
  12. Functionality Overview
  13. ----------------------
  14. .. only:: esp32
  15. Setting up a channel of the LEDC in either :ref:`high or low speed mode <ledc-api-high_low_speed_mode>` is done in three steps:
  16. .. only:: not esp32
  17. Setting up a channel of the LEDC is done in three steps. Note that unlike ESP32, {IDF_TARGET_NAME} only supports configuring channels in "low speed" mode.
  18. 1. :ref:`ledc-api-configure-timer` by specifying the PWM signal's frequency and duty cycle resolution.
  19. 2. :ref:`ledc-api-configure-channel` by associating it with the timer and GPIO to output the PWM signal.
  20. 3. :ref:`ledc-api-change-pwm-signal` that drives the output in order to change LED's intensity. This can be done under the full control of software or with hardware fading functions.
  21. As an optional step, it is also possible to set up an interrupt on fade end.
  22. .. figure:: ../../../_static/ledc-api-settings.jpg
  23. :align: center
  24. :alt: Key Settings of LED PWM Controller's API
  25. :figclass: align-center
  26. Key Settings of LED PWM Controller's API
  27. .. _ledc-api-configure-timer:
  28. Timer Configuration
  29. ^^^^^^^^^^^^^^^^^^^
  30. Setting the timer is done by calling the function :cpp:func:`ledc_timer_config` and passing the data structure :cpp:type:`ledc_timer_config_t` that contains the following configuration settings:
  31. .. list::
  32. :esp32: - Speed mode :cpp:type:`ledc_mode_t`
  33. :not esp32: - Speed mode (value must be ``LEDC_LOW_SPEED_MODE``)
  34. - Timer number :cpp:type:`ledc_timer_t`
  35. - PWM signal frequency
  36. - Resolution of PWM duty
  37. - Source clock :cpp:type:`ledc_clk_cfg_t`
  38. The frequency and the duty resolution are interdependent. The higher the PWM frequency, the lower the duty resolution which is available, and vice versa. This relationship might be important if you are planning to use this API for purposes other than changing the intensity of LEDs. For more details, see Section :ref:`ledc-api-supported-range-frequency-duty-resolution`.
  39. The source clock can also limit the PWM frequency. The higher the source clock frequency, the higher the maximum PWM frequency can be configured.
  40. .. only:: esp32
  41. .. list-table:: Characteristics of {IDF_TARGET_NAME} LEDC source clocks
  42. :widths: 5 5 5 20
  43. :header-rows: 1
  44. * - Clock name
  45. - Clock freq
  46. - Speed mode
  47. - Clock capabilities
  48. * - APB_CLK
  49. - 80 MHz
  50. - High / Low
  51. - /
  52. * - REF_TICK
  53. - 1 MHz
  54. - High / Low
  55. - Dynamic Frequency Scaling compatible
  56. * - RTC8M_CLK
  57. - ~8 MHz
  58. - Low
  59. - Dynamic Frequency Scaling compatible, Light sleep compatible
  60. .. only:: esp32s2
  61. .. list-table:: Characteristics of {IDF_TARGET_NAME} LEDC source clocks
  62. :widths: 15 15 30
  63. :header-rows: 1
  64. * - Clock name
  65. - Clock freq
  66. - Clock capabilities
  67. * - APB_CLK
  68. - 80 MHz
  69. - /
  70. * - REF_TICK
  71. - 1 MHz
  72. - Dynamic Frequency Scaling compatible
  73. * - RTC8M_CLK
  74. - ~8 MHz
  75. - Dynamic Frequency Scaling compatible, Light sleep compatible
  76. * - XTAL_CLK
  77. - 40 MHz
  78. - Dynamic Frequency Scaling compatible
  79. .. only:: esp32s3 or esp32c3
  80. .. list-table:: Characteristics of {IDF_TARGET_NAME} LEDC source clocks
  81. :widths: 15 15 30
  82. :header-rows: 1
  83. * - Clock name
  84. - Clock freq
  85. - Clock capabilities
  86. * - APB_CLK
  87. - 80 MHz
  88. - /
  89. * - RTC20M_CLK
  90. - ~20 MHz
  91. - Dynamic Frequency Scaling compatible, Light sleep compatible
  92. * - XTAL_CLK
  93. - 40 MHz
  94. - Dynamic Frequency Scaling compatible
  95. .. only:: esp32c2
  96. .. list-table:: Characteristics of {IDF_TARGET_NAME} LEDC source clocks
  97. :widths: 15 15 30
  98. :header-rows: 1
  99. * - Clock name
  100. - Clock freq
  101. - Clock capabilities
  102. * - PLL_60M_CLK
  103. - 60 MHz
  104. - /
  105. * - RTC20M_CLK
  106. - ~20 MHz
  107. - Dynamic Frequency Scaling compatible, Light sleep compatible
  108. * - XTAL_CLK
  109. - 40 MHz
  110. - Dynamic Frequency Scaling compatible
  111. .. only:: esp32h2
  112. .. list-table:: Characteristics of {IDF_TARGET_NAME} LEDC source clocks
  113. :widths: 15 15 30
  114. :header-rows: 1
  115. * - Clock name
  116. - Clock freq
  117. - Clock capabilities
  118. * - APB_CLK
  119. - 96 MHz
  120. - /
  121. * - RTC8M_CLK
  122. - ~8 MHz
  123. - Dynamic Frequency Scaling compatible, Light sleep compatible
  124. * - XTAL_CLK
  125. - 32 MHz
  126. - Dynamic Frequency Scaling compatible
  127. .. note::
  128. .. only:: not esp32h2
  129. 1. On {IDF_TARGET_NAME}, if RTCxM_CLK is chosen as the LEDC clock source, an internal calibration will be performed to get the exact frequency of the clock. This ensures the accuracy of output PWM signal frequency.
  130. .. only:: esp32h2
  131. 1. On {IDF_TARGET_NAME}, if RTC8M_CLK is chosen as the LEDC clock source, you may see the frequency of output PWM signal is not very accurate. This is because no internal calibration is performed to get the exact frequency of the clock due to hardware limitation, a theoretic frequency value is used.
  132. .. only:: not SOC_LEDC_HAS_TIMER_SPECIFIC_MUX
  133. 2. For {IDF_TARGET_NAME}, all timers share one clock source. In other words, it is impossible to use different clock sources for different timers.
  134. .. _ledc-api-configure-channel:
  135. Channel Configuration
  136. ^^^^^^^^^^^^^^^^^^^^^
  137. When the timer is set up, configure the desired channel (one out of :cpp:type:`ledc_channel_t`). This is done by calling the function :cpp:func:`ledc_channel_config`.
  138. Similar to the timer configuration, the channel setup function should be passed a structure :cpp:type:`ledc_channel_config_t` that contains the channel's configuration parameters.
  139. At this point, the channel should start operating and generating the PWM signal on the selected GPIO, as configured in :cpp:type:`ledc_channel_config_t`, with the frequency specified in the timer settings and the given duty cycle. The channel operation (signal generation) can be suspended at any time by calling the function :cpp:func:`ledc_stop`.
  140. .. _ledc-api-change-pwm-signal:
  141. Change PWM Signal
  142. ^^^^^^^^^^^^^^^^^
  143. Once the channel starts operating and generating the PWM signal with the constant duty cycle and frequency, there are a couple of ways to change this signal. When driving LEDs, primarily the duty cycle is changed to vary the light intensity.
  144. The following two sections describe how to change the duty cycle using software and hardware fading. If required, the signal's frequency can also be changed; it is covered in Section :ref:`ledc-api-change-pwm-frequency`.
  145. .. only:: not esp32
  146. .. note::
  147. All the timers and channels in the {IDF_TARGET_NAME}'s LED PWM Controller only support low speed mode. Any change of PWM settings must be explicitly triggered by software (see below).
  148. Change PWM Duty Cycle Using Software
  149. """"""""""""""""""""""""""""""""""""
  150. To set the duty cycle, use the dedicated function :cpp:func:`ledc_set_duty`. After that, call :cpp:func:`ledc_update_duty` to activate the changes. To check the currently set value, use the corresponding ``_get_`` function :cpp:func:`ledc_get_duty`.
  151. Another way to set the duty cycle, as well as some other channel parameters, is by calling :cpp:func:`ledc_channel_config` covered in Section :ref:`ledc-api-configure-channel`.
  152. The range of the duty cycle values passed to functions depends on selected ``duty_resolution`` and should be from ``0`` to ``(2 ** duty_resolution) - 1``. For example, if the selected duty resolution is 10, then the duty cycle values can range from 0 to 1023. This provides the resolution of ~0.1%.
  153. Change PWM Duty Cycle using Hardware
  154. """"""""""""""""""""""""""""""""""""
  155. The LEDC hardware provides the means to gradually transition from one duty cycle value to another. To use this functionality, enable fading with :cpp:func:`ledc_fade_func_install` and then configure it by calling one of the available fading functions:
  156. * :cpp:func:`ledc_set_fade_with_time`
  157. * :cpp:func:`ledc_set_fade_with_step`
  158. * :cpp:func:`ledc_set_fade`
  159. .. only:: esp32
  160. Start fading with :cpp:func:`ledc_fade_start`. A fade can be operated in blocking or non-blocking mode, please check :cpp:enum:`ledc_fade_mode_t` for the difference between the two available fade modes. Note that with either fade mode, the next fade or fixed-duty update will not take effect until the last fade finishes. Due to hardware limitations, there is no way to stop a fade before it reaches its target duty.
  161. .. only:: not esp32
  162. Start fading with :cpp:func:`ledc_fade_start`. A fade can be operated in blocking or non-blocking mode, please check :cpp:enum:`ledc_fade_mode_t` for the difference between the two available fade modes. Note that with either fade mode, the next fade or fixed-duty update will not take effect until the last fade finishes or is stopped. :cpp:func:`ledc_fade_stop` has to be called to stop a fade that is in progress.
  163. To get a notification about the completion of a fade operation, a fade end callback function can be registered for each channel by calling :cpp:func:`ledc_cb_register` after the fade service being installed.
  164. If not required anymore, fading and an associated interrupt can be disabled with :cpp:func:`ledc_fade_func_uninstall`.
  165. .. _ledc-api-change-pwm-frequency:
  166. Change PWM Frequency
  167. """"""""""""""""""""
  168. The LEDC API provides several ways to change the PWM frequency "on the fly":
  169. * Set the frequency by calling :cpp:func:`ledc_set_freq`. There is a corresponding function :cpp:func:`ledc_get_freq` to check the current frequency.
  170. * Change the frequency and the duty resolution by calling :cpp:func:`ledc_bind_channel_timer` to bind some other timer to the channel.
  171. * Change the channel's timer by calling :cpp:func:`ledc_channel_config`.
  172. More Control Over PWM
  173. """""""""""""""""""""
  174. There are several lower level timer-specific functions that can be used to change PWM settings:
  175. * :cpp:func:`ledc_timer_set`
  176. * :cpp:func:`ledc_timer_rst`
  177. * :cpp:func:`ledc_timer_pause`
  178. * :cpp:func:`ledc_timer_resume`
  179. 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.
  180. Use Interrupts
  181. ^^^^^^^^^^^^^^
  182. When configuring an LEDC channel, one of the parameters selected within :cpp:type:`ledc_channel_config_t` is :cpp:type:`ledc_intr_type_t` which triggers an interrupt on fade completion.
  183. For registration of a handler to address this interrupt, call :cpp:func:`ledc_isr_register`.
  184. .. only:: esp32
  185. .. _ledc-api-high_low_speed_mode:
  186. LEDC High and Low Speed Mode
  187. ----------------------------
  188. High speed mode enables a glitch-free changeover of timer settings. This means that if the timer settings are modified, the changes will be applied automatically on the next overflow interrupt of the timer. In contrast, when updating the low-speed timer, the change of settings should be explicitly triggered by software. The LEDC driver handles it in the background, e.g., when :cpp:func:`ledc_timer_config` or :cpp:func:`ledc_timer_set` is called.
  189. For additional details regarding speed modes, see *{IDF_TARGET_NAME} Technical Reference Manual* > *LED PWM Controller (LEDC)* [`PDF <{IDF_TARGET_TRM_EN_URL}#ledpwm>`__].
  190. .. _ledc-api-supported-range-frequency-duty-resolution:
  191. .. only:: not esp32
  192. .. _ledc-api-supported-range-frequency-duty-resolution:
  193. Supported Range of Frequency and Duty Resolutions
  194. -------------------------------------------------
  195. The LED PWM Controller is designed primarily to drive LEDs. It provides a large flexibility of PWM duty cycle settings. For instance, the PWM frequency of 5 kHz can have the maximum duty resolution of 13 bits. This means that the duty can be set anywhere from 0 to 100% with a resolution of ~0.012% (2 ** 13 = 8192 discrete levels of the LED intensity). Note, however, that these parameters depend on the clock signal clocking the LED PWM Controller timer which in turn clocks the channel (see :ref:`timer configuration<ledc-api-configure-timer>` and the *{IDF_TARGET_NAME} Technical Reference Manual* > *LED PWM Controller (LEDC)* [`PDF <{IDF_TARGET_TRM_EN_URL}#ledpwm>`__]).
  196. The LEDC can be used for generating signals at much higher frequencies that are sufficient enough to clock other devices, e.g., a digital camera module. In this case, the maximum available frequency is 40 MHz with duty resolution of 1 bit. This means that the duty cycle is fixed at 50% and cannot be adjusted.
  197. The LEDC API is designed to report an error when trying to set a frequency and a duty resolution that exceed the range of LEDC's hardware. For example, an attempt to set the frequency to 20 MHz and the duty resolution to 3 bits will result in the following error reported on a serial monitor:
  198. .. highlight:: none
  199. ::
  200. E (196) ledc: requested frequency and duty resolution cannot be achieved, try reducing freq_hz or duty_resolution. div_param=128
  201. In such a situation, either the duty resolution or the frequency must be reduced. For example, setting the duty resolution to 2 will resolve this issue and will make it possible to set the duty cycle at 25% steps, i.e., at 25%, 50% or 75%.
  202. The LEDC driver will also capture and report attempts to configure frequency / duty resolution combinations that are below the supported minimum, e.g.:
  203. ::
  204. E (196) ledc: requested frequency and duty resolution cannot be achieved, try increasing freq_hz or duty_resolution. div_param=128000000
  205. The duty resolution is normally set using :cpp:type:`ledc_timer_bit_t`. This enumeration covers the range from 10 to 15 bits. If a smaller duty resolution is required (from 10 down to 1), enter the equivalent numeric values directly.
  206. Application Example
  207. -------------------
  208. The LEDC change duty cycle and fading control example: :example:`peripherals/ledc/ledc_fade`.
  209. The LEDC basic example: :example:`peripherals/ledc/ledc_basic`.
  210. API Reference
  211. -------------
  212. .. include-build-file:: inc/ledc.inc
  213. .. include-build-file:: inc/ledc_types.inc