diff --git a/docs/conf_common.py b/docs/conf_common.py index b0afe34280..95e2db7512 100644 --- a/docs/conf_common.py +++ b/docs/conf_common.py @@ -100,7 +100,7 @@ RISCV_DOCS = [] # type: list[str] TWAI_DOCS = ['api-reference/peripherals/twai.rst'] -SIGMADELTA_DOCS = ['api-reference/peripherals/sigmadelta.rst'] +SDM_DOCS = ['api-reference/peripherals/sdm.rst'] I2S_DOCS = ['api-reference/peripherals/i2s.rst'] @@ -167,10 +167,10 @@ conditional_include_dict = {'SOC_BT_SUPPORTED':BT_DOCS, 'SOC_TEMP_SENSOR_SUPPORTED':TEMP_SENSOR_DOCS, 'SOC_TWAI_SUPPORTED':TWAI_DOCS, 'SOC_I2S_SUPPORTED':I2S_DOCS, - 'SOC_SIGMADELTA_SUPPORTED':SIGMADELTA_DOCS, 'SOC_RTC_MEM_SUPPORTED': RTC_MEM_DOCS, 'SOC_ADC_SUPPORTED':ADC_DOCS, 'SOC_ADC_DMA_SUPPORTED':ADC_DMA_DOCS, + 'SOC_SDM_SUPPORTED':SDM_DOCS, 'esp32':ESP32_DOCS, 'esp32s2':ESP32S2_DOCS, 'esp32s3':ESP32S3_DOCS, diff --git a/docs/doxygen/Doxyfile b/docs/doxygen/Doxyfile index 3e0dda2381..f67c53cb86 100644 --- a/docs/doxygen/Doxyfile +++ b/docs/doxygen/Doxyfile @@ -69,10 +69,10 @@ INPUT = \ $(PROJECT_PATH)/components/driver/include/driver/ledc.h \ $(PROJECT_PATH)/components/driver/include/driver/rtc_io.h \ $(PROJECT_PATH)/components/driver/include/driver/sdio_slave.h \ + $(PROJECT_PATH)/components/driver/include/driver/sdm.h \ $(PROJECT_PATH)/components/driver/include/driver/sdmmc_host.h \ $(PROJECT_PATH)/components/driver/include/driver/sdmmc_types.h \ $(PROJECT_PATH)/components/driver/include/driver/sdspi_host.h \ - $(PROJECT_PATH)/components/driver/include/driver/sigmadelta.h \ $(PROJECT_PATH)/components/driver/include/driver/spi_common.h \ $(PROJECT_PATH)/components/driver/include/driver/spi_master.h \ $(PROJECT_PATH)/components/driver/include/driver/spi_slave.h \ @@ -163,7 +163,7 @@ INPUT = \ $(PROJECT_PATH)/components/hal/include/hal/ledc_types.h \ $(PROJECT_PATH)/components/hal/include/hal/rtc_io_types.h \ $(PROJECT_PATH)/components/hal/include/hal/sdio_slave_types.h \ - $(PROJECT_PATH)/components/hal/include/hal/sigmadelta_types.h \ + $(PROJECT_PATH)/components/hal/include/hal/sdm_types.h \ $(PROJECT_PATH)/components/hal/include/hal/spi_flash_types.h \ $(PROJECT_PATH)/components/hal/include/hal/spi_types.h \ $(PROJECT_PATH)/components/hal/include/hal/timer_types.h \ diff --git a/docs/en/api-reference/peripherals/dac.rst b/docs/en/api-reference/peripherals/dac.rst index 73863b9639..e194f7405d 100644 --- a/docs/en/api-reference/peripherals/dac.rst +++ b/docs/en/api-reference/peripherals/dac.rst @@ -22,7 +22,7 @@ The DAC driver allows these channels to be set to arbitrary voltages. The DAC channels can also be driven with DMA-style written sample data by the digital controller, however the driver does not supported this yet. -For other analog output options, see the :doc:`Sigma-delta Modulation module ` and the :doc:`LED Control module `. Both these modules produce high frequency PWM output, which can be hardware low-pass filtered in order to generate a lower frequency analog output. +For other analog output options, see the :doc:`Sigma-delta Modulation module ` and the :doc:`LED Control module `. Both these modules produce high frequency PDM/PWM output, which can be hardware low-pass filtered in order to generate a lower frequency analog output. Application Example diff --git a/docs/en/api-reference/peripherals/index.rst b/docs/en/api-reference/peripherals/index.rst index 305cbb1d95..c5141011f0 100644 --- a/docs/en/api-reference/peripherals/index.rst +++ b/docs/en/api-reference/peripherals/index.rst @@ -27,7 +27,7 @@ Peripherals API :SOC_SDMMC_HOST_SUPPORTED: sdmmc_host sdspi_host :SOC_SDIO_SLAVE_SUPPORTED: sdio_slave - :SOC_SIGMADELTA_SUPPORTED: sigmadelta + :SOC_SDM_SUPPORTED: sdm spi_master spi_slave :esp32: secure_element diff --git a/docs/en/api-reference/peripherals/sdm.rst b/docs/en/api-reference/peripherals/sdm.rst new file mode 100644 index 0000000000..190d1dc89e --- /dev/null +++ b/docs/en/api-reference/peripherals/sdm.rst @@ -0,0 +1,132 @@ +Sigma-Delta Modulation (SDM) +============================ + +Introduction +------------ + +{IDF_TARGET_NAME} has a second-order sigma-delta modulator, which can generate independent PDM pulses to multiple channels. Please refer to the TRM to check how many hardware channels are available. [1]_ + +Typically, a Sigma-Delta modulated channel can be used in scenarios like: + +- LED dimming +- Simple DAC (8-bit), with the help of an active RC low-pass filter +- Class D amplifier, with the help of a half-bridge or full-bridge circuit plus an LC low-pass filter + +Functional Overview +------------------- + +The following sections of this document cover the typical steps to install and operate a SDM channel: + +- `Resource Allocation <#resource-allocation>`__ - covers which parameters should be set up to get a channel handle and how to recycle the resources when it finishes working. +- `Enable and Disable Channel <#enable-and-disable-channel>`__ - covers how to enable and disable the channel. +- `Set Equivalent Duty Cycle <#set-equivalent-duty-cycle>`__ - describes how to set the equivalent duty cycle of the PDM pulses. +- `Power Management <#power-management>`__ - describes how different source clock selections can affect power consumption. +- `IRAM Safe <#iram-safe>`__ - lists which functions are supposed to work even when the cache is disabled. +- `Thread Safety <#thread-safety>`__ - lists which APIs are guaranteed to be thread safe by the driver. +- `Kconfig Options <#kconfig-options>`__ - lists the supported Kconfig options that can be used to make a different effect on driver behavior. + +Resource Allocation +^^^^^^^^^^^^^^^^^^^ + +A SDM channel is represented by :cpp:type:`sdm_channel_handle_t`. Each channel is capable to output the binary, hardware generated signal with the sigma-delta modulation. The driver manages all available channels in a pool, so that users don't need to manually assign a fixed channel to a GPIO. + +To install a SDM channel, you should call :cpp:func:`sdm_new_channel` to get a channel handle. Channel specific configurations are passed in the :cpp:type:`sdm_config_t` structure: + +- :cpp:member:`sdm_config_t::gpio_num` sets the GPIO that the PDM pulses will output from +- :cpp:member:`sdm_config_t::clk_src` selects the source clock for the SDM module. Note that, all channels should select the same clock source. +- :cpp:member:`sdm_config_t::sample_rate_hz` sets the sample rate of the SDM module. +- :cpp:member:`sdm_config_t::invert_out` sets whether to invert the output signal. +- :cpp:member:`sdm_config_t::io_loop_back` is for debugging purposes only. It enables both the GPIO's input and output ability through the GPIO matrix peripheral. + +The function :cpp:func:`sdm_new_channel` can fail due to various errors such as insufficient memory, invalid arguments, etc. Specifically, when there are no more free channels (i.e. all hardware SDM channels have been used up), then :c:macro:`ESP_ERR_NOT_FOUND` will be returned. + +If a previously created SDM channel is no longer required, you should recycle it by calling :cpp:func:`sdm_del_channel`. It allows the underlying HW channel to be used for other purposes. Before deleting a SDM channel handle, you should disable it by :cpp:func:`sdm_channel_disable` in advance or make sure it has not enabled yet by :cpp:func:`sdm_channel_enable`. + +Creating a SDM Channel with Sample Rate of 1MHz +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: c + + sdm_channel_handle_t chan = NULL; + sdm_config_t config = { + .clk_src = SDM_CLK_SRC_DEFAULT, + .sample_rate_hz = 1 * 1000 * 1000, + .gpio_num = 0, + }; + ESP_ERROR_CHECK(sdm_new_channel(&config, &chan)); + +Enable and Disable Channel +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Before doing further IO control to the SDM channel, you should enable it first, by calling :cpp:func:`sdm_channel_enable`. Internally, this function will: + +* switch the channel state from **init** to **enable** +* acquire a proper power management lock is a specific clock source (e.g. APB clock) is selected. See also `Power management <#power-management>`__ for more information. + +On the contrary, calling :cpp:func:`sdm_channel_disable` will do the opposite, that is, put the channel back to the **init** state and release the power management lock. + +Set Equivalent Duty Cycle +^^^^^^^^^^^^^^^^^^^^^^^^^ + +For the output PDM signals, the duty cycle refers to the percentage of high level cycles to the whole statistical period. The average output voltage from the channel is calculated by ``Vout = VDD_IO / 256 * duty + VDD_IO / 2``. Thus the range of the ``duty`` input parameter of :cpp:func:`sdm_channel_set_duty` is from -128 to 127 (eight bit signed integer). For example,if zero value is set, then the output signal's duty will be about 50%. + +Power Management +^^^^^^^^^^^^^^^^ + +When power management is enabled (i.e. :ref:`CONFIG_PM_ENABLE` is on), the system will adjust the APB frequency before going into light sleep, thus potentially changing the sample rate of the sigma-delta modulator. + +However, the driver can prevent the system from changing APB frequency by acquiring a power management lock of type :cpp:enumerator:`ESP_PM_APB_FREQ_MAX`. Whenever the driver creates a SDM channel instance that has selected :cpp:enumerator:`SDM_CLK_SRC_APB` as its clock source, the driver will guarantee that the power management lock is acquired when enable the channel by :cpp:func:`sdm_channel_enable`. Likewise, the driver releases the lock when :cpp:func:`sdm_channel_disable` is called for that channel. + +IRAM Safe +^^^^^^^^^ + +There's a Kconfig option :ref:`CONFIG_SDM_CTRL_FUNC_IN_IRAM` that can put commonly used IO control functions into IRAM as well. So that these functions can also be executable when the cache is disabled. These IO control functions are listed as follows: + +- :cpp:func:`sdm_channel_set_duty` + +Thread Safety +^^^^^^^^^^^^^ + +The factory function :cpp:func:`sdm_new_channel` is guaranteed to be thread safe by the driver, which means, user can call it from different RTOS tasks without protection by extra locks. +The following functions are allowed to run under ISR context, the driver uses a critical section to prevent them being called concurrently in both task and ISR. + +- :cpp:func:`sdm_channel_set_duty` + +Other functions that take the :cpp:type:`sdm_channel_handle_t` as the first positional parameter, are not treated as thread safe. Which means the user should avoid calling them from multiple tasks. + +Kconfig Options +^^^^^^^^^^^^^^^ + +- :ref:`CONFIG_SDM_CTRL_FUNC_IN_IRAM` controls where to place the SDM channel control functions (IRAM or Flash), see `IRAM Safe <#iram-safe>`__ for more information. +- :ref:`CONFIG_SDM_ENABLE_DEBUG_LOG` is used to enabled the debug log output. Enable this option will increase the firmware binary size. + +Convert to analog signal (Optional) +----------------------------------- + +Typically, if the sigma-delta signal is connected to an LED, you don't have to add any filter between them (because our eyes are a low pass filter naturally). However, if you want to check the real voltage or watch the analog waveform, you need to design an analog low pass filter. Also, it is recommended to use an active filter instead of a passive filter to gain better isolation and not lose too much voltage. + +For example, you can take the following `Sallen-Key topology Low Pass Filter`_ as a reference. + +.. figure:: ../../../_static/typical_sallenkey_LP_filter.png + :align: center + :alt: Sallen-Key Low Pass Filter + :figclass: align-center + + Sallen-Key Low Pass Filter + + +Application Example +------------------- + +* LED driven by a GPIO that is modulated with Sigma-Delta: :example:`peripherals/sigma_delta`. + +API Reference +------------- + +.. include-build-file:: inc/sdm.inc +.. include-build-file:: inc/sdm_types.inc + +.. [1] + Different ESP chip series might have different numbers of SDM channels. Please refer to Chapter `GPIO and IOMUX <{IDF_TARGET_TRM_EN_URL}#iomuxgpio>`__ in {IDF_TARGET_NAME} Technical Reference Manual for more details. The driver won't forbid you from applying for more channels, but it will return error when all available hardware resources are used up. Please always check the return value when doing resource allocation (e.g. :cpp:func:`sdm_new_channel`). + +.. _Sallen-Key topology Low Pass Filter: https://en.wikipedia.org/wiki/Sallen%E2%80%93Key_topology diff --git a/docs/en/api-reference/peripherals/sigmadelta.rst b/docs/en/api-reference/peripherals/sigmadelta.rst deleted file mode 100644 index f3c45472ed..0000000000 --- a/docs/en/api-reference/peripherals/sigmadelta.rst +++ /dev/null @@ -1,53 +0,0 @@ -Sigma-delta Modulation -====================== - -{IDF_TARGET_SIGMA_DELTA_MODULATION_CHANNEL_NUM:default="8", esp32c3="4"} - -Introduction ------------- - -{IDF_TARGET_NAME} has a second-order sigma-delta modulation module. This driver configures the channels of the sigma-delta module. - -Functionality Overview ----------------------- - -There are {IDF_TARGET_SIGMA_DELTA_MODULATION_CHANNEL_NUM} independent sigma-delta modulation channels identified with :cpp:type:`sigmadelta_channel_t`. Each channel is capable to output the binary, hardware generated signal with the sigma-delta modulation. - -Selected channel should be set up by providing configuration parameters in :cpp:type:`sigmadelta_config_t` and then applying this configuration with :cpp:func:`sigmadelta_config`. - -Another option is to call individual functions, that will configure all required parameters one by one: - -* **Prescaler** of the sigma-delta generator - :cpp:func:`sigmadelta_set_prescale` -* **Duty** of the output signal - :cpp:func:`sigmadelta_set_duty` -* **GPIO pin** to output modulated signal - :cpp:func:`sigmadelta_set_pin` - -The range of the 'duty' input parameter of :cpp:func:`sigmadelta_set_duty` is from -128 to 127 (eight bit signed integer). If zero value is set, then the output signal's duty will be about 50%, see description of :cpp:func:`sigmadelta_set_duty`. - - -Convert to analog signal (Optional) ------------------------------------ - -Typically, if the sigma-delta signal is connected to an LED, you don't have to add any filter between them (because our eyes are a low pass filter naturally). However, if you want to check the real voltage or watch the analog waveform, you need to design an analog low pass filter. Also, it is recommended to use an active filter instead of a passive filter to gain better isolation and not lose too much voltage. - -For example, you can take the following `Sallen-Key topology Low Pass Filter`_ as a reference. - -.. figure:: ../../../_static/typical_sallenkey_LP_filter.png - :align: center - :alt: Sallen-Key Low Pass Filter - :figclass: align-center - - Sallen-Key Low Pass Filter - - -Application Example -------------------- - -Sigma-delta Modulation example: :example:`peripherals/sigmadelta`. - -API Reference -------------- - -.. include-build-file:: inc/sigmadelta.inc -.. include-build-file:: inc/sigmadelta_types.inc - -.. _Sallen-Key topology Low Pass Filter: https://en.wikipedia.org/wiki/Sallen%E2%80%93Key_topology diff --git a/docs/en/migration-guides/release-5.x/peripherals.rst b/docs/en/migration-guides/release-5.x/peripherals.rst index 82b448a901..dd3c47293a 100644 --- a/docs/en/migration-guides/release-5.x/peripherals.rst +++ b/docs/en/migration-guides/release-5.x/peripherals.rst @@ -33,10 +33,33 @@ GPIO The previous Kconfig option `RTCIO_SUPPORT_RTC_GPIO_DESC` has been removed, thus the ``rtc_gpio_desc`` array is unavailable. Please use ``rtc_io_desc`` array instead. +.. only:: SOC_SDM_SUPPORTED + + Sigma-Delta Modulator + --------------------- + + The Sigma-Delta Modulator driver has been redesigned into :doc:`SDM <../../api-reference/peripherals/sdm>`. The new driver implements a factory pattern, where the SDM channels are managed in a pool internally, thus you don't have to fix a SDM channel to a GPIO manually. All SDM channels can be allocated dynamically. Although it's recommended to use the new driver APIs, the legacy driver is still available in the previous include path ``driver/sigmadelta.h``. However, by default, including ``driver/sigmadelta.h`` will bring a build warning like ``The legacy sigma-delta driver is deprecated, please use driver/sdm.h``. The warning can be suppressed by Kconfig option :ref:`CONFIG_SDM_SUPPRESS_DEPRECATE_WARN`. + + The major breaking changes in concept and usage are listed as follows: + + Breaking Changes in Concepts + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + - SDM channel representation has changed from ``sigmadelta_channel_t`` to :cpp:type:`sdm_channel_handle_t`, which is an opaque pointer. + - SDM channel configurations are stored in :cpp:type:`sdm_config_t` now, instead the previous ``sigmadelta_config_t``. + - In the legacy driver, you don't have to set the clock source for SDM channel. But in the new driver, you need to set a proper one in the :cpp:member:`sdm_config_t::clk_src`. The available clock sources are listed in the :cpp:type:`soc_periph_sdm_clk_src_t`. + - In the legacy driver, you need to set a ``prescale`` for the channel, which will reflected into the frequency the modulator output a pulse. In the new driver, you should use :cpp:member:`sdm_config_t::sample_rate_hz`. + + Breaking Changes in Usage + ~~~~~~~~~~~~~~~~~~~~~~~~~ + + - Channel configuration was done by channel allocation, in :cpp:func:`sdm_new_channel`. In the new driver, only the ``duty`` can be changed at runtime, by :cpp:func:`sdm_channel_set_duty`. Other parameters like ``gpio number`` and ``prescale`` are only allowed to set during channel allocation. + - Before further channel operations, you should **enable** the channel in advance, by calling :cpp:func:`sdm_channel_enable`. This function will help to manage some system level services, like **Power Management**. + Timer Group Driver ------------------ -Timer Group driver has been redesigned into :doc:`GPTimer <../../api-reference/peripherals/gptimer>`, which aims to unify and simplify the usage of general purpose timer. Although it's recommended to use the the new driver APIs, the legacy driver is till available in the previous include path ``driver/timer.h``. However, by default, including ``driver/timer.h`` will bring a build warning like `legacy timer group driver is deprecated, please migrate to driver/gptimer.h`. The warning can be suppressed by the Kconfig option :ref:`CONFIG_GPTIMER_SUPPRESS_DEPRECATE_WARN`. +Timer Group driver has been redesigned into :doc:`GPTimer <../../api-reference/peripherals/gptimer>`, which aims to unify and simplify the usage of general purpose timer. Although it's recommended to use the the new driver APIs, the legacy driver is still available in the previous include path ``driver/timer.h``. However, by default, including ``driver/timer.h`` will bring a build warning like ``legacy timer group driver is deprecated, please migrate to driver/gptimer.h``. The warning can be suppressed by the Kconfig option :ref:`CONFIG_GPTIMER_SUPPRESS_DEPRECATE_WARN`. The major breaking changes in concept and usage are listed as follows: diff --git a/docs/page_redirects.txt b/docs/page_redirects.txt index c8c946b3e3..e0298de25a 100644 --- a/docs/page_redirects.txt +++ b/docs/page_redirects.txt @@ -65,6 +65,10 @@ api-guides/ulp-risc-v.rst api-reference/system/ulp-risc-v. api-reference/network/tcpip_adapter migration-guides/tcpip-adapter api-reference/system/system api-reference/system/misc_system_api +# Driver-NG refactor +api-reference/peripherals/timer api-reference/peripherals/gptimer +api-reference/peripherals/sigmadelta api-reference/peripherals/sdm + # The 'secure boot' guides are now 'secure boot v1' guides security/secure-boot security/secure-boot-v1 @@ -78,7 +82,7 @@ hw-reference/get-started-pico-kit hw-reference/esp32/get-started-p hw-reference/get-started-pico-kit-v3 hw-reference/esp32/get-started-pico-kit-v3 hw-reference/get-started-ethernet-kit-v1.0 hw-reference/esp32/get-started-ethernet-kit-v1.0 hw-reference/get-started-ethernet-kit hw-reference/esp32/get-started-ethernet-kit -hw-reference/esp32s2/usermacos-setup_mesh api-reference/network/esp-wifi-mesh +hw-reference/esp32s2/usermacos-setup_mesh api-reference/network/esp-wifi-mesh # Getting Started get-started/macos-setup get-started/linux-macos-setup diff --git a/docs/zh_CN/api-reference/peripherals/index.rst b/docs/zh_CN/api-reference/peripherals/index.rst index a02b00ca35..95040b7c8f 100644 --- a/docs/zh_CN/api-reference/peripherals/index.rst +++ b/docs/zh_CN/api-reference/peripherals/index.rst @@ -27,7 +27,7 @@ :SOC_SDMMC_HOST_SUPPORTED: sdmmc_host sdspi_host :SOC_SDIO_SLAVE_SUPPORTED: sdio_slave - :SOC_SIGMADELTA_SUPPORTED: sigmadelta + :SOC_SDM_SUPPORTED: sdm spi_master spi_slave :esp32: secure_element diff --git a/docs/zh_CN/api-reference/peripherals/sdm.rst b/docs/zh_CN/api-reference/peripherals/sdm.rst new file mode 100644 index 0000000000..9dd4ae148f --- /dev/null +++ b/docs/zh_CN/api-reference/peripherals/sdm.rst @@ -0,0 +1 @@ +.. include:: ../../../en/api-reference/peripherals/sdm.rst \ No newline at end of file diff --git a/docs/zh_CN/api-reference/peripherals/sigmadelta.rst b/docs/zh_CN/api-reference/peripherals/sigmadelta.rst deleted file mode 100644 index 8a26b7a8d8..0000000000 --- a/docs/zh_CN/api-reference/peripherals/sigmadelta.rst +++ /dev/null @@ -1 +0,0 @@ -.. include:: ../../../en/api-reference/peripherals/sigmadelta.rst \ No newline at end of file