From bf5c09aacde48e0a04d066094efb5fe4b53996e1 Mon Sep 17 00:00:00 2001 From: Anne Brondijk <73667900+abrondijk@users.noreply.github.com> Date: Fri, 14 Jul 2023 21:44:03 +0200 Subject: [PATCH 1/2] feat(mcpwm): Allow for pull up/down to be configurable on generators --- components/driver/include/driver/mcpwm_gen.h | 2 ++ components/driver/mcpwm/mcpwm_gen.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/components/driver/include/driver/mcpwm_gen.h b/components/driver/include/driver/mcpwm_gen.h index 154e721250..f070146dd6 100644 --- a/components/driver/include/driver/mcpwm_gen.h +++ b/components/driver/include/driver/mcpwm_gen.h @@ -24,6 +24,8 @@ typedef struct { struct { uint32_t invert_pwm: 1; /*!< Whether to invert the PWM signal (done by GPIO matrix) */ uint32_t io_loop_back: 1; /*!< For debug/test, the signal output from the GPIO will be fed to the input path as well */ + uint32_t pull_up: 1; /*!< Whether to pull up internally */ + uint32_t pull_down: 1; /*!< Whether to pull down internally */ } flags; /*!< Extra configuration flags for generator */ } mcpwm_generator_config_t; diff --git a/components/driver/mcpwm/mcpwm_gen.c b/components/driver/mcpwm/mcpwm_gen.c index e6ef87841b..104f462aeb 100644 --- a/components/driver/mcpwm/mcpwm_gen.c +++ b/components/driver/mcpwm/mcpwm_gen.c @@ -88,8 +88,8 @@ esp_err_t mcpwm_new_generator(mcpwm_oper_handle_t oper, const mcpwm_generator_co .intr_type = GPIO_INTR_DISABLE, .mode = GPIO_MODE_OUTPUT | (config->flags.io_loop_back ? GPIO_MODE_INPUT : 0), // also enable the input path if `io_loop_back` is enabled .pin_bit_mask = (1ULL << config->gen_gpio_num), - .pull_down_en = false, - .pull_up_en = true, + .pull_down_en = config->flags.pull_down, + .pull_up_en = config->flags.pull_up, }; ESP_GOTO_ON_ERROR(gpio_config(&gpio_conf), err, TAG, "config gen GPIO failed"); esp_rom_gpio_connect_out_signal(config->gen_gpio_num, From a87a04992e472dd1939df06287ee0e6b84356a41 Mon Sep 17 00:00:00 2001 From: morris Date: Wed, 19 Jul 2023 11:27:11 +0800 Subject: [PATCH 2/2] feat(mcpwm): support open drain output for generator IO Closes https://github.com/espressif/esp-idf/issues/11877 --- components/driver/include/driver/mcpwm_gen.h | 1 + components/driver/mcpwm/mcpwm_gen.c | 3 ++- components/driver/rmt/rmt_tx.c | 2 +- docs/en/api-reference/peripherals/mcpwm.rst | 4 +++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/components/driver/include/driver/mcpwm_gen.h b/components/driver/include/driver/mcpwm_gen.h index f070146dd6..91d46ce702 100644 --- a/components/driver/include/driver/mcpwm_gen.h +++ b/components/driver/include/driver/mcpwm_gen.h @@ -24,6 +24,7 @@ typedef struct { struct { uint32_t invert_pwm: 1; /*!< Whether to invert the PWM signal (done by GPIO matrix) */ uint32_t io_loop_back: 1; /*!< For debug/test, the signal output from the GPIO will be fed to the input path as well */ + uint32_t io_od_mode: 1; /*!< Configure the GPIO as open-drain mode */ uint32_t pull_up: 1; /*!< Whether to pull up internally */ uint32_t pull_down: 1; /*!< Whether to pull down internally */ } flags; /*!< Extra configuration flags for generator */ diff --git a/components/driver/mcpwm/mcpwm_gen.c b/components/driver/mcpwm/mcpwm_gen.c index 104f462aeb..8b5355a2f8 100644 --- a/components/driver/mcpwm/mcpwm_gen.c +++ b/components/driver/mcpwm/mcpwm_gen.c @@ -86,7 +86,8 @@ esp_err_t mcpwm_new_generator(mcpwm_oper_handle_t oper, const mcpwm_generator_co // GPIO configuration gpio_config_t gpio_conf = { .intr_type = GPIO_INTR_DISABLE, - .mode = GPIO_MODE_OUTPUT | (config->flags.io_loop_back ? GPIO_MODE_INPUT : 0), // also enable the input path if `io_loop_back` is enabled + // also enable the input path if `io_loop_back` is enabled + .mode = (config->flags.io_od_mode ? GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT) | (config->flags.io_loop_back ? GPIO_MODE_INPUT : 0), .pin_bit_mask = (1ULL << config->gen_gpio_num), .pull_down_en = config->flags.pull_down, .pull_up_en = config->flags.pull_up, diff --git a/components/driver/rmt/rmt_tx.c b/components/driver/rmt/rmt_tx.c index f4325b041e..0557733d09 100644 --- a/components/driver/rmt/rmt_tx.c +++ b/components/driver/rmt/rmt_tx.c @@ -278,7 +278,7 @@ esp_err_t rmt_new_tx_channel(const rmt_tx_channel_config_t *config, rmt_channel_ tx_channel->base.gpio_num = config->gpio_num; gpio_config_t gpio_conf = { .intr_type = GPIO_INTR_DISABLE, - // also enable the input path is `io_loop_back` is on, this is useful for bi-directional buses + // also enable the input path if `io_loop_back` is on, this is useful for bi-directional buses .mode = (config->flags.io_od_mode ? GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT) | (config->flags.io_loop_back ? GPIO_MODE_INPUT : 0), .pull_down_en = false, .pull_up_en = true, diff --git a/docs/en/api-reference/peripherals/mcpwm.rst b/docs/en/api-reference/peripherals/mcpwm.rst index 4fa3ff5a67..8f27558def 100644 --- a/docs/en/api-reference/peripherals/mcpwm.rst +++ b/docs/en/api-reference/peripherals/mcpwm.rst @@ -107,7 +107,9 @@ You can allocate a MCPWM generator object by calling :cpp:func:`mcpwm_new_genera - :cpp:member:`mcpwm_generator_config_t::gen_gpio_num` sets the GPIO number used by the generator. - :cpp:member:`mcpwm_generator_config_t::invert_pwm` sets whether to invert the PWM signal. -- :cpp:member:`mcpwm_generator_config_t::io_loop_back` sets whether to enable the loop back mode. It is for debugging purposes only. It enables both the GPIO's input and output ability through the GPIO matrix peripheral. +- :cpp:member:`mcpwm_generator_config_t::io_loop_back` sets whether to enable the Loop-back mode. It is for debugging purposes only. It enables both the GPIO's input and output ability through the GPIO matrix peripheral. +- :cpp:member:`mcpwm_generator_config_t::io_od_mode` configures the PWM GPIO as open-drain output. +- :cpp:member:`mcpwm_generator_config_t::pull_up` and :cpp:member:`mcpwm_generator_config_t::pull_down` controls whether to enable the internal pull-up and pull-down resistors accordingly. The :cpp:func:`mcpwm_new_generator` will return a pointer to the allocated generator object if the allocation succeeds. Otherwise, it will return error code. Specifically, when there are no more free generators in the MCPWM operator, this function will return :c:macro:`ESP_ERR_NOT_FOUND` error. [1]_