feat(mcpwm): refactor mcpwm driver into a component

This commit is contained in:
morris
2023-10-30 10:59:17 +08:00
parent 92c4714128
commit eb5183f503
59 changed files with 167 additions and 143 deletions

View File

@@ -0,0 +1,245 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MCPWM capture timer configuration structure
*/
typedef struct {
int group_id; /*!< Specify from which group to allocate the capture timer */
mcpwm_capture_clock_source_t clk_src; /*!< MCPWM capture timer clock source */
uint32_t resolution_hz; /*!< Resolution of capture timer */
} mcpwm_capture_timer_config_t;
/**
* @brief Create MCPWM capture timer
*
* @param[in] config MCPWM capture timer configuration
* @param[out] ret_cap_timer Returned MCPWM capture timer handle
* @return
* - ESP_OK: Create MCPWM capture timer successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM capture timer failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM capture timer failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM capture timer failed because can't find free resource
* - ESP_FAIL: Create MCPWM capture timer failed because of other error
*/
esp_err_t mcpwm_new_capture_timer(const mcpwm_capture_timer_config_t *config, mcpwm_cap_timer_handle_t *ret_cap_timer);
/**
* @brief Delete MCPWM capture timer
*
* @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()`
* @return
* - ESP_OK: Delete MCPWM capture timer successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM capture timer failed because of invalid argument
* - ESP_FAIL: Delete MCPWM capture timer failed because of other error
*/
esp_err_t mcpwm_del_capture_timer(mcpwm_cap_timer_handle_t cap_timer);
/**
* @brief Enable MCPWM capture timer
*
* @param[in] cap_timer MCPWM capture timer handle, allocated by `mcpwm_new_capture_timer()`
* @return
* - ESP_OK: Enable MCPWM capture timer successfully
* - ESP_ERR_INVALID_ARG: Enable MCPWM capture timer failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Enable MCPWM capture timer failed because timer is enabled already
* - ESP_FAIL: Enable MCPWM capture timer failed because of other error
*/
esp_err_t mcpwm_capture_timer_enable(mcpwm_cap_timer_handle_t cap_timer);
/**
* @brief Disable MCPWM capture timer
*
* @param[in] cap_timer MCPWM capture timer handle, allocated by `mcpwm_new_capture_timer()`
* @return
* - ESP_OK: Disable MCPWM capture timer successfully
* - ESP_ERR_INVALID_ARG: Disable MCPWM capture timer failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Disable MCPWM capture timer failed because timer is disabled already
* - ESP_FAIL: Disable MCPWM capture timer failed because of other error
*/
esp_err_t mcpwm_capture_timer_disable(mcpwm_cap_timer_handle_t cap_timer);
/**
* @brief Start MCPWM capture timer
*
* @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()`
* @return
* - ESP_OK: Start MCPWM capture timer successfully
* - ESP_ERR_INVALID_ARG: Start MCPWM capture timer failed because of invalid argument
* - ESP_FAIL: Start MCPWM capture timer failed because of other error
*/
esp_err_t mcpwm_capture_timer_start(mcpwm_cap_timer_handle_t cap_timer);
/**
* @brief Start MCPWM capture timer
*
* @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()`
* @return
* - ESP_OK: Stop MCPWM capture timer successfully
* - ESP_ERR_INVALID_ARG: Stop MCPWM capture timer failed because of invalid argument
* - ESP_FAIL: Stop MCPWM capture timer failed because of other error
*/
esp_err_t mcpwm_capture_timer_stop(mcpwm_cap_timer_handle_t cap_timer);
/**
* @brief Get MCPWM capture timer resolution, in Hz
*
* @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()`
* @param[out] out_resolution Returned capture timer resolution, in Hz
* @return
* - ESP_OK: Get capture timer resolution successfully
* - ESP_ERR_INVALID_ARG: Get capture timer resolution failed because of invalid argument
* - ESP_FAIL: Get capture timer resolution failed because of other error
*/
esp_err_t mcpwm_capture_timer_get_resolution(mcpwm_cap_timer_handle_t cap_timer, uint32_t *out_resolution);
/**
* @brief MCPWM Capture timer sync phase configuration
*/
typedef struct {
mcpwm_sync_handle_t sync_src; /*!< The sync event source */
uint32_t count_value; /*!< The count value that should lock to upon sync event */
mcpwm_timer_direction_t direction; /*!< The count direction that should lock to upon sync event */
} mcpwm_capture_timer_sync_phase_config_t;
/**
* @brief Set sync phase for MCPWM capture timer
*
* @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()`
* @param[in] config MCPWM capture timer sync phase configuration
* @return
* - ESP_OK: Set sync phase for MCPWM capture timer successfully
* - ESP_ERR_INVALID_ARG: Set sync phase for MCPWM capture timer failed because of invalid argument
* - ESP_FAIL: Set sync phase for MCPWM capture timer failed because of other error
*/
esp_err_t mcpwm_capture_timer_set_phase_on_sync(mcpwm_cap_timer_handle_t cap_timer, const mcpwm_capture_timer_sync_phase_config_t *config);
/**
* @brief MCPWM capture channel configuration structure
*/
typedef struct {
int gpio_num; /*!< GPIO used capturing input signal */
int intr_priority; /*!< MCPWM capture interrupt priority,
if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
uint32_t prescale; /*!< Prescale of input signal, effective frequency = cap_input_clk/prescale */
struct {
uint32_t pos_edge: 1; /*!< Whether to capture on positive edge */
uint32_t neg_edge: 1; /*!< Whether to capture on negative edge */
uint32_t pull_up: 1; /*!< Whether to pull up internally */
uint32_t pull_down: 1; /*!< Whether to pull down internally */
uint32_t invert_cap_signal: 1; /*!< Invert the input capture signal */
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 keep_io_conf_at_exit: 1; /*!< For debug/test, whether to keep the GPIO configuration when capture channel is deleted.
By default, driver will reset the GPIO pin at exit. */
} flags; /*!< Extra configuration flags for capture channel */
} mcpwm_capture_channel_config_t;
/**
* @brief Create MCPWM capture channel
*
* @note The created capture channel won't be enabled until calling `mcpwm_capture_channel_enable`
*
* @param[in] cap_timer MCPWM capture timer, allocated by `mcpwm_new_capture_timer()`, will be connected to the new capture channel
* @param[in] config MCPWM capture channel configuration
* @param[out] ret_cap_channel Returned MCPWM capture channel
* @return
* - ESP_OK: Create MCPWM capture channel successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM capture channel failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM capture channel failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM capture channel failed because can't find free resource
* - ESP_FAIL: Create MCPWM capture channel failed because of other error
*/
esp_err_t mcpwm_new_capture_channel(mcpwm_cap_timer_handle_t cap_timer, const mcpwm_capture_channel_config_t *config, mcpwm_cap_channel_handle_t *ret_cap_channel);
/**
* @brief Delete MCPWM capture channel
*
* @param[in] cap_channel MCPWM capture channel handle, allocated by `mcpwm_new_capture_channel()`
* @return
* - ESP_OK: Delete MCPWM capture channel successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM capture channel failed because of invalid argument
* - ESP_FAIL: Delete MCPWM capture channel failed because of other error
*/
esp_err_t mcpwm_del_capture_channel(mcpwm_cap_channel_handle_t cap_channel);
/**
* @brief Enable MCPWM capture channel
*
* @note This function will transit the channel state from init to enable.
* @note This function will enable the interrupt service, if it's lazy installed in `mcpwm_capture_channel_register_event_callbacks()`.
*
* @param[in] cap_channel MCPWM capture channel handle, allocated by `mcpwm_new_capture_channel()`
* @return
* - ESP_OK: Enable MCPWM capture channel successfully
* - ESP_ERR_INVALID_ARG: Enable MCPWM capture channel failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Enable MCPWM capture channel failed because the channel is already enabled
* - ESP_FAIL: Enable MCPWM capture channel failed because of other error
*/
esp_err_t mcpwm_capture_channel_enable(mcpwm_cap_channel_handle_t cap_channel);
/**
* @brief Disable MCPWM capture channel
*
* @param[in] cap_channel MCPWM capture channel handle, allocated by `mcpwm_new_capture_channel()`
* @return
* - ESP_OK: Disable MCPWM capture channel successfully
* - ESP_ERR_INVALID_ARG: Disable MCPWM capture channel failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Disable MCPWM capture channel failed because the channel is not enabled yet
* - ESP_FAIL: Disable MCPWM capture channel failed because of other error
*/
esp_err_t mcpwm_capture_channel_disable(mcpwm_cap_channel_handle_t cap_channel);
/**
* @brief Group of supported MCPWM capture event callbacks
* @note The callbacks are all running under ISR environment
*/
typedef struct {
mcpwm_capture_event_cb_t on_cap; /*!< Callback function that would be invoked when capture event occurred */
} mcpwm_capture_event_callbacks_t;
/**
* @brief Set event callbacks for MCPWM capture channel
*
* @note The first call to this function needs to be before the call to `mcpwm_capture_channel_enable`
* @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
*
* @param[in] cap_channel MCPWM capture channel handle, allocated by `mcpwm_new_capture_channel()`
* @param[in] cbs Group of callback functions
* @param[in] user_data User data, which will be passed to callback functions directly
* @return
* - ESP_OK: Set event callbacks successfully
* - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Set event callbacks failed because the channel is not in init state
* - ESP_FAIL: Set event callbacks failed because of other error
*/
esp_err_t mcpwm_capture_channel_register_event_callbacks(mcpwm_cap_channel_handle_t cap_channel, const mcpwm_capture_event_callbacks_t *cbs, void *user_data);
/**
* @brief Trigger a catch by software
*
* @param[in] cap_channel MCPWM capture channel handle, allocated by `mcpwm_new_capture_channel()`
* @return
* - ESP_OK: Trigger software catch successfully
* - ESP_ERR_INVALID_ARG: Trigger software catch failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Trigger software catch failed because the channel is not enabled yet
* - ESP_FAIL: Trigger software catch failed because of other error
*/
esp_err_t mcpwm_capture_channel_trigger_soft_catch(mcpwm_cap_channel_handle_t cap_channel);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,118 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MCPWM comparator configuration
*/
typedef struct {
int intr_priority; /*!< MCPWM comparator interrupt priority,
if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
struct {
uint32_t update_cmp_on_tez: 1; /*!< Whether to update compare value when timer count equals to zero (tez) */
uint32_t update_cmp_on_tep: 1; /*!< Whether to update compare value when timer count equals to peak (tep) */
uint32_t update_cmp_on_sync: 1; /*!< Whether to update compare value on sync event */
} flags; /*!< Extra configuration flags for comparator */
} mcpwm_comparator_config_t;
/**
* @brief Create MCPWM comparator
*
* @param[in] oper MCPWM operator, allocated by `mcpwm_new_operator()`, the new comparator will be allocated from this operator
* @param[in] config MCPWM comparator configuration
* @param[out] ret_cmpr Returned MCPWM comparator
* @return
* - ESP_OK: Create MCPWM comparator successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM comparator failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM comparator failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM comparator failed because can't find free resource
* - ESP_FAIL: Create MCPWM comparator failed because of other error
*/
esp_err_t mcpwm_new_comparator(mcpwm_oper_handle_t oper, const mcpwm_comparator_config_t *config, mcpwm_cmpr_handle_t *ret_cmpr);
/**
* @brief Delete MCPWM comparator
*
* @param[in] cmpr MCPWM comparator handle, allocated by `mcpwm_new_comparator()`
* @return
* - ESP_OK: Delete MCPWM comparator successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM comparator failed because of invalid argument
* - ESP_FAIL: Delete MCPWM comparator failed because of other error
*/
esp_err_t mcpwm_del_comparator(mcpwm_cmpr_handle_t cmpr);
#if SOC_MCPWM_SUPPORT_EVENT_COMPARATOR
/**
* @brief MCPWM event comparator configuration
*/
typedef struct {
} mcpwm_event_comparator_config_t;
/**
* @brief Create MCPWM event comparator
*
* @param[in] oper MCPWM operator, allocated by `mcpwm_new_operator()`, the new event comparator will be allocated from this operator
* @param[in] config MCPWM comparator configuration
* @param[out] ret_cmpr Returned MCPWM event comparator
* @return
* - ESP_OK: Create MCPWM event comparator successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM event comparator failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM event comparator failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM event comparator failed because can't find free resource
* - ESP_FAIL: Create MCPWM event comparator failed because of other error
*/
esp_err_t mcpwm_new_event_comparator(mcpwm_oper_handle_t oper, const mcpwm_event_comparator_config_t *config, mcpwm_cmpr_handle_t *ret_cmpr);
#endif // SOC_MCPWM_SUPPORT_EVENT_COMPARATOR
/**
* @brief Group of supported MCPWM compare event callbacks
* @note The callbacks are all running under ISR environment
*/
typedef struct {
mcpwm_compare_event_cb_t on_reach; /*!< ISR callback function which would be invoked when counter reaches compare value */
} mcpwm_comparator_event_callbacks_t;
/**
* @brief Set event callbacks for MCPWM comparator
*
* @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
*
* @param[in] cmpr MCPWM comparator handle, allocated by `mcpwm_new_comparator()`
* @param[in] cbs Group of callback functions
* @param[in] user_data User data, which will be passed to callback functions directly
* @return
* - ESP_OK: Set event callbacks successfully
* - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
* - ESP_FAIL: Set event callbacks failed because of other error
*/
esp_err_t mcpwm_comparator_register_event_callbacks(mcpwm_cmpr_handle_t cmpr, const mcpwm_comparator_event_callbacks_t *cbs, void *user_data);
/**
* @brief Set MCPWM comparator's compare value
*
* @param[in] cmpr MCPWM comparator handle, allocated by `mcpwm_new_comparator()`
* @param[in] cmp_ticks The new compare value
* @return
* - ESP_OK: Set MCPWM compare value successfully
* - ESP_ERR_INVALID_ARG: Set MCPWM compare value failed because of invalid argument (e.g. the cmp_ticks is out of range)
* - ESP_ERR_INVALID_STATE: Set MCPWM compare value failed because the operator doesn't have a timer connected
* - ESP_FAIL: Set MCPWM compare value failed because of other error
*/
esp_err_t mcpwm_comparator_set_compare_value(mcpwm_cmpr_handle_t cmpr, uint32_t cmp_ticks);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,43 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "esp_etm.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MCPWM event comparator ETM event configuration
*/
typedef struct {
mcpwm_comparator_etm_event_type_t event_type; /*!< MCPWM comparator ETM event type */
} mcpwm_cmpr_etm_event_config_t;
/**
* @brief Get the ETM event for MCPWM comparator
*
* @note The created ETM event object can be deleted later by calling `esp_etm_del_event`
*
* @param[in] cmpr MCPWM comparator, allocated by `mcpwm_new_comparator()` or `mcpwm_new_event_comparator()`
* @param[in] config MCPWM ETM comparator event configuration
* @param[out] out_event Returned ETM event handle
* @return
* - ESP_OK: Get ETM event successfully
* - ESP_ERR_INVALID_ARG: Get ETM event failed because of invalid argument
* - ESP_FAIL: Get ETM event failed because of other error
*/
esp_err_t mcpwm_comparator_new_etm_event(mcpwm_cmpr_handle_t cmpr, const mcpwm_cmpr_etm_event_config_t *config, esp_etm_event_handle_t *out_event);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,116 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MCPWM GPIO fault configuration structure
*/
typedef struct {
int group_id; /*!< In which MCPWM group that the GPIO fault belongs to */
int intr_priority; /*!< MCPWM GPIO fault interrupt priority,
if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
int gpio_num; /*!< GPIO used by the fault signal */
struct {
uint32_t active_level: 1; /*!< On which level the fault signal is treated as active */
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 GPIO fault */
} mcpwm_gpio_fault_config_t;
/**
* @brief Create MCPWM GPIO fault
*
* @param[in] config MCPWM GPIO fault configuration
* @param[out] ret_fault Returned GPIO fault handle
* @return
* - ESP_OK: Create MCPWM GPIO fault successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM GPIO fault failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM GPIO fault failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM GPIO fault failed because can't find free resource
* - ESP_FAIL: Create MCPWM GPIO fault failed because of other error
*/
esp_err_t mcpwm_new_gpio_fault(const mcpwm_gpio_fault_config_t *config, mcpwm_fault_handle_t *ret_fault);
/**
* @brief MCPWM software fault configuration structure
*/
typedef struct {
} mcpwm_soft_fault_config_t;
/**
* @brief Create MCPWM software fault
*
* @param[in] config MCPWM software fault configuration
* @param[out] ret_fault Returned software fault handle
* @return
* - ESP_OK: Create MCPWM software fault successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM software fault failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM software fault failed because out of memory
* - ESP_FAIL: Create MCPWM software fault failed because of other error
*/
esp_err_t mcpwm_new_soft_fault(const mcpwm_soft_fault_config_t *config, mcpwm_fault_handle_t *ret_fault);
/**
* @brief Delete MCPWM fault
*
* @param[in] fault MCPWM fault handle allocated by `mcpwm_new_gpio_fault()` or `mcpwm_new_soft_fault()`
* @return
* - ESP_OK: Delete MCPWM fault successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM fault failed because of invalid argument
* - ESP_FAIL: Delete MCPWM fault failed because of other error
*/
esp_err_t mcpwm_del_fault(mcpwm_fault_handle_t fault);
/**
* @brief Activate the software fault, trigger the fault event for once
*
* @param[in] fault MCPWM soft fault, allocated by `mcpwm_new_soft_fault()`
* @return
* - ESP_OK: Trigger MCPWM software fault event successfully
* - ESP_ERR_INVALID_ARG: Trigger MCPWM software fault event failed because of invalid argument
* - ESP_FAIL: Trigger MCPWM software fault event failed because of other error
*/
esp_err_t mcpwm_soft_fault_activate(mcpwm_fault_handle_t fault);
/**
* @brief Group of supported MCPWM fault event callbacks
* @note The callbacks are all running under ISR environment
*/
typedef struct {
mcpwm_fault_event_cb_t on_fault_enter; /*!< ISR callback function that would be invoked when fault signal becomes active */
mcpwm_fault_event_cb_t on_fault_exit; /*!< ISR callback function that would be invoked when fault signal becomes inactive */
} mcpwm_fault_event_callbacks_t;
/**
* @brief Set event callbacks for MCPWM fault
*
* @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
*
* @param[in] fault MCPWM GPIO fault handle, allocated by `mcpwm_new_gpio_fault()`
* @param[in] cbs Group of callback functions
* @param[in] user_data User data, which will be passed to callback functions directly
* @return
* - ESP_OK: Set event callbacks successfully
* - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
* - ESP_FAIL: Set event callbacks failed because of other error
*/
esp_err_t mcpwm_fault_register_event_callbacks(mcpwm_fault_handle_t fault, const mcpwm_fault_event_callbacks_t *cbs, void *user_data);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,295 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MCPWM generator configuration
*/
typedef struct {
int gen_gpio_num; /*!< The GPIO number used to output the PWM signal */
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 */
} mcpwm_generator_config_t;
/**
* @brief Allocate MCPWM generator from given operator
*
* @param[in] oper MCPWM operator, allocated by `mcpwm_new_operator()`
* @param[in] config MCPWM generator configuration
* @param[out] ret_gen Returned MCPWM generator
* @return
* - ESP_OK: Create MCPWM generator successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM generator failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM generator failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM generator failed because can't find free resource
* - ESP_FAIL: Create MCPWM generator failed because of other error
*/
esp_err_t mcpwm_new_generator(mcpwm_oper_handle_t oper, const mcpwm_generator_config_t *config, mcpwm_gen_handle_t *ret_gen);
/**
* @brief Delete MCPWM generator
*
* @param[in] gen MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @return
* - ESP_OK: Delete MCPWM generator successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM generator failed because of invalid argument
* - ESP_FAIL: Delete MCPWM generator failed because of other error
*/
esp_err_t mcpwm_del_generator(mcpwm_gen_handle_t gen);
/**
* @brief Set force level for MCPWM generator
*
* @note The force level will be applied to the generator immediately, regardless any other events that would change the generator's behaviour.
* @note If the `hold_on` is true, the force level will retain forever, until user removes the force level by setting the force level to `-1`.
* @note If the `hold_on` is false, the force level can be overridden by the next event action.
* @note The force level set by this function can be inverted by GPIO matrix or dead-time module. So the level set here doesn't equal to the final output level.
*
* @param[in] gen MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] level GPIO level to be applied to MCPWM generator, specially, -1 means to remove the force level
* @param[in] hold_on Whether the forced PWM level should retain (i.e. will remain unchanged until manually remove the force level)
* @return
* - ESP_OK: Set force level for MCPWM generator successfully
* - ESP_ERR_INVALID_ARG: Set force level for MCPWM generator failed because of invalid argument
* - ESP_FAIL: Set force level for MCPWM generator failed because of other error
*/
esp_err_t mcpwm_generator_set_force_level(mcpwm_gen_handle_t gen, int level, bool hold_on);
/**
* @brief Generator action on specific timer event
*/
typedef struct {
mcpwm_timer_direction_t direction; /*!< Timer direction */
mcpwm_timer_event_t event; /*!< Timer event */
mcpwm_generator_action_t action; /*!< Generator action should perform */
} mcpwm_gen_timer_event_action_t;
/**
* @brief Help macros to construct a mcpwm_gen_timer_event_action_t entry
*/
#define MCPWM_GEN_TIMER_EVENT_ACTION(dir, ev, act) \
(mcpwm_gen_timer_event_action_t) { .direction = dir, .event = ev, .action = act }
#define MCPWM_GEN_TIMER_EVENT_ACTION_END() \
(mcpwm_gen_timer_event_action_t) { .event = MCPWM_TIMER_EVENT_INVALID }
/**
* @brief Set generator action on MCPWM timer event
*
* @param[in] gen MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM timer event action, can be constructed by `MCPWM_GEN_TIMER_EVENT_ACTION` helper macro
* @return
* - ESP_OK: Set generator action successfully
* - ESP_ERR_INVALID_ARG: Set generator action failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Set generator action failed because of timer is not connected to operator
* - ESP_FAIL: Set generator action failed because of other error
*/
esp_err_t mcpwm_generator_set_action_on_timer_event(mcpwm_gen_handle_t gen, mcpwm_gen_timer_event_action_t ev_act);
/**
* @brief Set generator actions on multiple MCPWM timer events
*
* @note This is an aggregation version of `mcpwm_generator_set_action_on_timer_event`, which allows user to set multiple actions in one call.
*
* @param[in] gen MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM timer event action list, must be terminated by `MCPWM_GEN_TIMER_EVENT_ACTION_END()`
* @return
* - ESP_OK: Set generator actions successfully
* - ESP_ERR_INVALID_ARG: Set generator actions failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Set generator actions failed because of timer is not connected to operator
* - ESP_FAIL: Set generator actions failed because of other error
*/
esp_err_t mcpwm_generator_set_actions_on_timer_event(mcpwm_gen_handle_t gen, mcpwm_gen_timer_event_action_t ev_act, ...);
/**
* @brief Generator action on specific comparator event
*/
typedef struct {
mcpwm_timer_direction_t direction; /*!< Timer direction */
mcpwm_cmpr_handle_t comparator; /*!< Comparator handle */
mcpwm_generator_action_t action; /*!< Generator action should perform */
} mcpwm_gen_compare_event_action_t;
/**
* @brief Help macros to construct a mcpwm_gen_compare_event_action_t entry
*/
#define MCPWM_GEN_COMPARE_EVENT_ACTION(dir, cmp, act) \
(mcpwm_gen_compare_event_action_t) { .direction = dir, .comparator = cmp, .action = act }
#define MCPWM_GEN_COMPARE_EVENT_ACTION_END() \
(mcpwm_gen_compare_event_action_t) { .comparator = NULL }
/**
* @brief Set generator action on MCPWM compare event
*
* @param[in] generator MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM compare event action, can be constructed by `MCPWM_GEN_COMPARE_EVENT_ACTION` helper macro
* @return
* - ESP_OK: Set generator action successfully
* - ESP_ERR_INVALID_ARG: Set generator action failed because of invalid argument
* - ESP_FAIL: Set generator action failed because of other error
*/
esp_err_t mcpwm_generator_set_action_on_compare_event(mcpwm_gen_handle_t generator, mcpwm_gen_compare_event_action_t ev_act);
/**
* @brief Set generator actions on multiple MCPWM compare events
*
* @note This is an aggregation version of `mcpwm_generator_set_action_on_compare_event`, which allows user to set multiple actions in one call.
*
* @param[in] generator MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM compare event action list, must be terminated by `MCPWM_GEN_COMPARE_EVENT_ACTION_END()`
* @return
* - ESP_OK: Set generator actions successfully
* - ESP_ERR_INVALID_ARG: Set generator actions failed because of invalid argument
* - ESP_FAIL: Set generator actions failed because of other error
*/
esp_err_t mcpwm_generator_set_actions_on_compare_event(mcpwm_gen_handle_t generator, mcpwm_gen_compare_event_action_t ev_act, ...);
/**
* @brief Generator action on specific brake event
*/
typedef struct {
mcpwm_timer_direction_t direction; /*!< Timer direction */
mcpwm_operator_brake_mode_t brake_mode; /*!< Brake mode */
mcpwm_generator_action_t action; /*!< Generator action should perform */
} mcpwm_gen_brake_event_action_t;
/**
* @brief Help macros to construct a mcpwm_gen_brake_event_action_t entry
*/
#define MCPWM_GEN_BRAKE_EVENT_ACTION(dir, mode, act) \
(mcpwm_gen_brake_event_action_t) { .direction = dir, .brake_mode = mode, .action = act }
#define MCPWM_GEN_BRAKE_EVENT_ACTION_END() \
(mcpwm_gen_brake_event_action_t) { .brake_mode = MCPWM_OPER_BRAKE_MODE_INVALID }
/**
* @brief Set generator action on MCPWM brake event
*
* @param[in] generator MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM brake event action, can be constructed by `MCPWM_GEN_BRAKE_EVENT_ACTION` helper macro
* @return
* - ESP_OK: Set generator action successfully
* - ESP_ERR_INVALID_ARG: Set generator action failed because of invalid argument
* - ESP_FAIL: Set generator action failed because of other error
*/
esp_err_t mcpwm_generator_set_action_on_brake_event(mcpwm_gen_handle_t generator, mcpwm_gen_brake_event_action_t ev_act);
/**
* @brief Set generator actions on multiple MCPWM brake events
*
* @note This is an aggregation version of `mcpwm_generator_set_action_on_brake_event`, which allows user to set multiple actions in one call.
*
* @param[in] generator MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM brake event action list, must be terminated by `MCPWM_GEN_BRAKE_EVENT_ACTION_END()`
* @return
* - ESP_OK: Set generator actions successfully
* - ESP_ERR_INVALID_ARG: Set generator actions failed because of invalid argument
* - ESP_FAIL: Set generator actions failed because of other error
*/
esp_err_t mcpwm_generator_set_actions_on_brake_event(mcpwm_gen_handle_t generator, mcpwm_gen_brake_event_action_t ev_act, ...);
/**
* @brief Generator action on specific fault event
*/
typedef struct {
mcpwm_timer_direction_t direction; /*!< Timer direction */
mcpwm_fault_handle_t fault; /*!< Which fault as the trigger. Only support GPIO fault */
mcpwm_generator_action_t action; /*!< Generator action should perform */
} mcpwm_gen_fault_event_action_t;
/**
* @brief Help macros to construct a mcpwm_gen_fault_event_action_t entry
*/
#define MCPWM_GEN_FAULT_EVENT_ACTION(dir, flt, act) \
(mcpwm_gen_fault_event_action_t) { .direction = dir, .fault = flt, .action = act }
/**
* @brief Set generator action on MCPWM Fault event
*
* @param[in] generator MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM trigger event action, can be constructed by `MCPWM_GEN_FAULT_EVENT_ACTION` helper macro
* @return
* - ESP_OK: Set generator action successfully
* - ESP_ERR_INVALID_ARG: Set generator action failed because of invalid argument
* - ESP_FAIL: Set generator action failed because of other error
*/
esp_err_t mcpwm_generator_set_action_on_fault_event(mcpwm_gen_handle_t generator, mcpwm_gen_fault_event_action_t ev_act);
/**
* @brief Generator action on specific sync event
*/
typedef struct {
mcpwm_timer_direction_t direction; /*!< Timer direction */
mcpwm_sync_handle_t sync; /*!< Which sync as the trigger*/
mcpwm_generator_action_t action; /*!< Generator action should perform */
} mcpwm_gen_sync_event_action_t;
/**
* @brief Help macros to construct a mcpwm_gen_sync_event_action_t entry
*/
#define MCPWM_GEN_SYNC_EVENT_ACTION(dir, syn, act) \
(mcpwm_gen_sync_event_action_t) { .direction = dir, .sync = syn, .action = act }
/**
* @brief Set generator action on MCPWM Sync event
*
* @note The trigger only support one sync action, regardless of the kinds. Should not call this function more than once.
*
* @param[in] generator MCPWM generator handle, allocated by `mcpwm_new_generator()`
* @param[in] ev_act MCPWM trigger event action, can be constructed by `MCPWM_GEN_SYNC_EVENT_ACTION` helper macro
* @return
* - ESP_OK: Set generator action successfully
* - ESP_ERR_INVALID_ARG: Set generator action failed because of invalid argument
* - ESP_FAIL: Set generator action failed because of other error
*/
esp_err_t mcpwm_generator_set_action_on_sync_event(mcpwm_gen_handle_t generator, mcpwm_gen_sync_event_action_t ev_act);
/**
* @brief MCPWM dead time configuration structure
*/
typedef struct {
uint32_t posedge_delay_ticks; /*!< delay time applied to rising edge, 0 means no rising delay time */
uint32_t negedge_delay_ticks; /*!< delay time applied to falling edge, 0 means no falling delay time */
struct {
uint32_t invert_output: 1; /*!< Invert the signal after applied the dead time */
} flags; /*!< Extra flags for dead time configuration */
} mcpwm_dead_time_config_t;
/**
* @brief Set dead time for MCPWM generator
*
* @note Due to a hardware limitation, you can't set rising edge delay for both MCPWM generator 0 and 1 at the same time,
* otherwise, there will be a conflict inside the dead time module. The same goes for the falling edge setting.
* But you can set both the rising edge and falling edge delay for the same MCPWM generator.
*
* @param[in] in_generator MCPWM generator, before adding the dead time
* @param[in] out_generator MCPWM generator, after adding the dead time
* @param[in] config MCPWM dead time configuration
* @return
* - ESP_OK: Set dead time for MCPWM generator successfully
* - ESP_ERR_INVALID_ARG: Set dead time for MCPWM generator failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Set dead time for MCPWM generator failed because of invalid state (e.g. delay module is already in use by other generator)
* - ESP_FAIL: Set dead time for MCPWM generator failed because of other error
*/
esp_err_t mcpwm_generator_set_dead_time(mcpwm_gen_handle_t in_generator, mcpwm_gen_handle_t out_generator, const mcpwm_dead_time_config_t *config);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,164 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MCPWM operator configuration
*/
typedef struct {
int group_id; /*!< Specify from which group to allocate the MCPWM operator */
int intr_priority; /*!< MCPWM operator interrupt priority,
if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
struct {
uint32_t update_gen_action_on_tez: 1; /*!< Whether to update generator action when timer counts to zero */
uint32_t update_gen_action_on_tep: 1; /*!< Whether to update generator action when timer counts to peak */
uint32_t update_gen_action_on_sync: 1; /*!< Whether to update generator action on sync event */
uint32_t update_dead_time_on_tez: 1; /*!< Whether to update dead time when timer counts to zero */
uint32_t update_dead_time_on_tep: 1; /*!< Whether to update dead time when timer counts to peak */
uint32_t update_dead_time_on_sync: 1; /*!< Whether to update dead time on sync event */
} flags; /*!< Extra configuration flags for operator */
} mcpwm_operator_config_t;
/**
* @brief Create MCPWM operator
*
* @param[in] config MCPWM operator configuration
* @param[out] ret_oper Returned MCPWM operator handle
* @return
* - ESP_OK: Create MCPWM operator successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM operator failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM operator failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM operator failed because can't find free resource
* - ESP_FAIL: Create MCPWM operator failed because of other error
*/
esp_err_t mcpwm_new_operator(const mcpwm_operator_config_t *config, mcpwm_oper_handle_t *ret_oper);
/**
* @brief Delete MCPWM operator
*
* @param[in] oper MCPWM operator, allocated by `mcpwm_new_operator()`
* @return
* - ESP_OK: Delete MCPWM operator successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM operator failed because of invalid argument
* - ESP_FAIL: Delete MCPWM operator failed because of other error
*/
esp_err_t mcpwm_del_operator(mcpwm_oper_handle_t oper);
/**
* @brief Connect MCPWM operator and timer, so that the operator can be driven by the timer
*
* @param[in] oper MCPWM operator handle, allocated by `mcpwm_new_operator()`
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @return
* - ESP_OK: Connect MCPWM operator and timer successfully
* - ESP_ERR_INVALID_ARG: Connect MCPWM operator and timer failed because of invalid argument
* - ESP_FAIL: Connect MCPWM operator and timer failed because of other error
*/
esp_err_t mcpwm_operator_connect_timer(mcpwm_oper_handle_t oper, mcpwm_timer_handle_t timer);
/**
* @brief MCPWM brake configuration structure
*/
typedef struct {
mcpwm_fault_handle_t fault; /*!< Which fault causes the operator to brake */
mcpwm_operator_brake_mode_t brake_mode; /*!< Brake mode */
struct {
uint32_t cbc_recover_on_tez: 1; /*!< Recovery CBC brake state on tez event */
uint32_t cbc_recover_on_tep: 1; /*!< Recovery CBC brake state on tep event */
} flags; /*!< Extra flags for brake configuration */
} mcpwm_brake_config_t;
/**
* @brief Set brake method for MCPWM operator
*
* @param[in] oper MCPWM operator, allocated by `mcpwm_new_operator()`
* @param[in] config MCPWM brake configuration
* @return
* - ESP_OK: Set trip for operator successfully
* - ESP_ERR_INVALID_ARG: Set trip for operator failed because of invalid argument
* - ESP_FAIL: Set trip for operator failed because of other error
*/
esp_err_t mcpwm_operator_set_brake_on_fault(mcpwm_oper_handle_t oper, const mcpwm_brake_config_t *config);
/**
* @brief Try to make the operator recover from fault
*
* @note To recover from fault or escape from trip, you make sure the fault signal has dissappeared already.
* Otherwise the recovery can't succeed.
*
* @param[in] oper MCPWM operator, allocated by `mcpwm_new_operator()`
* @param[in] fault MCPWM fault handle
* @return
* - ESP_OK: Recover from fault successfully
* - ESP_ERR_INVALID_ARG: Recover from fault failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Recover from fault failed because the fault source is still active
* - ESP_FAIL: Recover from fault failed because of other error
*/
esp_err_t mcpwm_operator_recover_from_fault(mcpwm_oper_handle_t oper, mcpwm_fault_handle_t fault);
/**
* @brief Group of supported MCPWM operator event callbacks
* @note The callbacks are all running under ISR environment
*/
typedef struct {
mcpwm_brake_event_cb_t on_brake_cbc; /*!< callback function when mcpwm operator brakes in CBC */
mcpwm_brake_event_cb_t on_brake_ost; /*!< callback function when mcpwm operator brakes in OST */
} mcpwm_operator_event_callbacks_t;
/**
* @brief Set event callbacks for MCPWM operator
*
* @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
*
* @param[in] oper MCPWM operator handle, allocated by `mcpwm_new_operator()`
* @param[in] cbs Group of callback functions
* @param[in] user_data User data, which will be passed to callback functions directly
* @return
* - ESP_OK: Set event callbacks successfully
* - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
* - ESP_FAIL: Set event callbacks failed because of other error
*/
esp_err_t mcpwm_operator_register_event_callbacks(mcpwm_oper_handle_t oper, const mcpwm_operator_event_callbacks_t *cbs, void *user_data);
/**
* @brief MCPWM carrier configuration structure
*/
typedef struct {
mcpwm_carrier_clock_source_t clk_src; /*!< MCPWM carrier clock source */
uint32_t frequency_hz; /*!< Carrier frequency in Hz */
uint32_t first_pulse_duration_us; /*!< The duration of the first PWM pulse, in us */
float duty_cycle; /*!< Carrier duty cycle */
struct {
uint32_t invert_before_modulate: 1; /*!< Invert the raw signal */
uint32_t invert_after_modulate: 1; /*!< Invert the modulated signal */
} flags; /*!< Extra flags for carrier configuration */
} mcpwm_carrier_config_t;
/**
* @brief Apply carrier feature for MCPWM operator
*
* @param[in] oper MCPWM operator, allocated by `mcpwm_new_operator()`
* @param[in] config MCPWM carrier specific configuration
* @return
* - ESP_OK: Set carrier for operator successfully
* - ESP_ERR_INVALID_ARG: Set carrier for operator failed because of invalid argument
* - ESP_FAIL: Set carrier for operator failed because of other error
*/
esp_err_t mcpwm_operator_apply_carrier(mcpwm_oper_handle_t oper, const mcpwm_carrier_config_t *config);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @brief MCPWM peripheral contains many submodules, whose drivers are scattered in different header files.
* This header file serves as a prelude, contains every thing that is needed to work with the MCPWM peripheral.
*/
#pragma once
#include "driver/mcpwm_timer.h"
#include "driver/mcpwm_oper.h"
#include "driver/mcpwm_cmpr.h"
#include "driver/mcpwm_gen.h"
#include "driver/mcpwm_fault.h"
#include "driver/mcpwm_sync.h"
#include "driver/mcpwm_cap.h"
#include "driver/mcpwm_etm.h"

View File

@@ -0,0 +1,114 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief MCPWM timer sync source configuration
*/
typedef struct {
mcpwm_timer_event_t timer_event; /*!< Timer event, upon which MCPWM timer will generate the sync signal */
struct {
uint32_t propagate_input_sync: 1; /*!< The input sync signal would be routed to its sync output */
} flags; /*!< Extra configuration flags for timer sync source */
} mcpwm_timer_sync_src_config_t;
/**
* @brief Create MCPWM timer sync source
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @param[in] config MCPWM timer sync source configuration
* @param[out] ret_sync Returned MCPWM sync handle
* @return
* - ESP_OK: Create MCPWM timer sync source successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM timer sync source failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM timer sync source failed because out of memory
* - ESP_ERR_INVALID_STATE: Create MCPWM timer sync source failed because the timer has created a sync source before
* - ESP_FAIL: Create MCPWM timer sync source failed because of other error
*/
esp_err_t mcpwm_new_timer_sync_src(mcpwm_timer_handle_t timer, const mcpwm_timer_sync_src_config_t *config, mcpwm_sync_handle_t *ret_sync);
/**
* @brief MCPWM GPIO sync source configuration
*/
typedef struct {
int group_id; /*!< MCPWM group ID */
int gpio_num; /*!< GPIO used by sync source */
struct {
uint32_t active_neg: 1; /*!< Whether the sync signal is active on negedge, by default, the sync signal's posedge is treated as active */
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 GPIO sync source */
} mcpwm_gpio_sync_src_config_t;
/**
* @brief Create MCPWM GPIO sync source
*
* @param[in] config MCPWM GPIO sync source configuration
* @param[out] ret_sync Returned MCPWM GPIO sync handle
* @return
* - ESP_OK: Create MCPWM GPIO sync source successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM GPIO sync source failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM GPIO sync source failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM GPIO sync source failed because can't find free resource
* - ESP_FAIL: Create MCPWM GPIO sync source failed because of other error
*/
esp_err_t mcpwm_new_gpio_sync_src(const mcpwm_gpio_sync_src_config_t *config, mcpwm_sync_handle_t *ret_sync);
/**
* @brief MCPWM software sync configuration structure
*/
typedef struct {
} mcpwm_soft_sync_config_t;
/**
* @brief Create MCPWM software sync source
*
* @param[in] config MCPWM software sync source configuration
* @param[out] ret_sync Returned software sync handle
* @return
* - ESP_OK: Create MCPWM software sync successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM software sync failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM software sync failed because out of memory
* - ESP_FAIL: Create MCPWM software sync failed because of other error
*/
esp_err_t mcpwm_new_soft_sync_src(const mcpwm_soft_sync_config_t *config, mcpwm_sync_handle_t *ret_sync);
/**
* @brief Delete MCPWM sync source
*
* @param[in] sync MCPWM sync handle, allocated by `mcpwm_new_timer_sync_src()` or `mcpwm_new_gpio_sync_src()` or `mcpwm_new_soft_sync_src()`
* @return
* - ESP_OK: Delete MCPWM sync source successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM sync source failed because of invalid argument
* - ESP_FAIL: Delete MCPWM sync source failed because of other error
*/
esp_err_t mcpwm_del_sync_src(mcpwm_sync_handle_t sync);
/**
* @brief Activate the software sync, trigger the sync event for once
*
* @param[in] sync MCPWM soft sync handle, allocated by `mcpwm_new_soft_sync_src()`
* @return
* - ESP_OK: Trigger MCPWM software sync event successfully
* - ESP_ERR_INVALID_ARG: Trigger MCPWM software sync event failed because of invalid argument
* - ESP_FAIL: Trigger MCPWM software sync event failed because of other error
*/
esp_err_t mcpwm_soft_sync_activate(mcpwm_sync_handle_t sync);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,167 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Group of supported MCPWM timer event callbacks
* @note The callbacks are all running under ISR environment
*/
typedef struct {
mcpwm_timer_event_cb_t on_full; /*!< callback function when MCPWM timer counts to peak value */
mcpwm_timer_event_cb_t on_empty; /*!< callback function when MCPWM timer counts to zero */
mcpwm_timer_event_cb_t on_stop; /*!< callback function when MCPWM timer stops */
} mcpwm_timer_event_callbacks_t;
/**
* @brief MCPWM timer configuration
*/
typedef struct {
int group_id; /*!< Specify from which group to allocate the MCPWM timer */
mcpwm_timer_clock_source_t clk_src; /*!< MCPWM timer clock source */
uint32_t resolution_hz; /*!< Counter resolution in Hz
The step size of each count tick equals to (1 / resolution_hz) seconds */
mcpwm_timer_count_mode_t count_mode; /*!< Count mode */
uint32_t period_ticks; /*!< Number of count ticks within a period */
int intr_priority; /*!< MCPWM timer interrupt priority,
if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
struct {
uint32_t update_period_on_empty: 1; /*!< Whether to update period when timer counts to zero */
uint32_t update_period_on_sync: 1; /*!< Whether to update period on sync event */
} flags; /*!< Extra configuration flags for timer */
} mcpwm_timer_config_t;
/**
* @brief Create MCPWM timer
*
* @param[in] config MCPWM timer configuration
* @param[out] ret_timer Returned MCPWM timer handle
* @return
* - ESP_OK: Create MCPWM timer successfully
* - ESP_ERR_INVALID_ARG: Create MCPWM timer failed because of invalid argument
* - ESP_ERR_NO_MEM: Create MCPWM timer failed because out of memory
* - ESP_ERR_NOT_FOUND: Create MCPWM timer failed because all hardware timers are used up and no more free one
* - ESP_FAIL: Create MCPWM timer failed because of other error
*/
esp_err_t mcpwm_new_timer(const mcpwm_timer_config_t *config, mcpwm_timer_handle_t *ret_timer);
/**
* @brief Delete MCPWM timer
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @return
* - ESP_OK: Delete MCPWM timer successfully
* - ESP_ERR_INVALID_ARG: Delete MCPWM timer failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Delete MCPWM timer failed because timer is not in init state
* - ESP_FAIL: Delete MCPWM timer failed because of other error
*/
esp_err_t mcpwm_del_timer(mcpwm_timer_handle_t timer);
/**
* @brief Set a new period for MCPWM timer
*
* @note If `mcpwm_timer_config_t::update_period_on_empty` and `mcpwm_timer_config_t::update_period_on_sync` are not set,
* the new period will take effect immediately.
* Otherwise, the new period will take effect when timer counts to zero or on sync event.
* @note You may need to use `mcpwm_comparator_set_compare_value` to set a new compare value for MCPWM comparator
* in order to keep the same PWM duty cycle.
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer`
* @param[in] period_ticks New period in count ticks
* @return
* - ESP_OK: Set new period for MCPWM timer successfully
* - ESP_ERR_INVALID_ARG: Set new period for MCPWM timer failed because of invalid argument
* - ESP_FAIL: Set new period for MCPWM timer failed because of other error
*/
esp_err_t mcpwm_timer_set_period(mcpwm_timer_handle_t timer, uint32_t period_ticks);
/**
* @brief Enable MCPWM timer
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @return
* - ESP_OK: Enable MCPWM timer successfully
* - ESP_ERR_INVALID_ARG: Enable MCPWM timer failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Enable MCPWM timer failed because timer is enabled already
* - ESP_FAIL: Enable MCPWM timer failed because of other error
*/
esp_err_t mcpwm_timer_enable(mcpwm_timer_handle_t timer);
/**
* @brief Disable MCPWM timer
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @return
* - ESP_OK: Disable MCPWM timer successfully
* - ESP_ERR_INVALID_ARG: Disable MCPWM timer failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Disable MCPWM timer failed because timer is disabled already
* - ESP_FAIL: Disable MCPWM timer failed because of other error
*/
esp_err_t mcpwm_timer_disable(mcpwm_timer_handle_t timer);
/**
* @brief Send specific start/stop commands to MCPWM timer
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @param[in] command Supported command list for MCPWM timer
* @return
* - ESP_OK: Start or stop MCPWM timer successfully
* - ESP_ERR_INVALID_ARG: Start or stop MCPWM timer failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Start or stop MCPWM timer failed because timer is not enabled
* - ESP_FAIL: Start or stop MCPWM timer failed because of other error
*/
esp_err_t mcpwm_timer_start_stop(mcpwm_timer_handle_t timer, mcpwm_timer_start_stop_cmd_t command);
/**
* @brief Set event callbacks for MCPWM timer
*
* @note The first call to this function needs to be before the call to `mcpwm_timer_enable`
* @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @param[in] cbs Group of callback functions
* @param[in] user_data User data, which will be passed to callback functions directly
* @return
* - ESP_OK: Set event callbacks successfully
* - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
* - ESP_ERR_INVALID_STATE: Set event callbacks failed because timer is not in init state
* - ESP_FAIL: Set event callbacks failed because of other error
*/
esp_err_t mcpwm_timer_register_event_callbacks(mcpwm_timer_handle_t timer, const mcpwm_timer_event_callbacks_t *cbs, void *user_data);
/**
* @brief MCPWM Timer sync phase configuration
*/
typedef struct {
mcpwm_sync_handle_t sync_src; /*!< The sync event source. Set to NULL will disable the timer being synced by others */
uint32_t count_value; /*!< The count value that should lock to upon sync event */
mcpwm_timer_direction_t direction; /*!< The count direction that should lock to upon sync event */
} mcpwm_timer_sync_phase_config_t;
/**
* @brief Set sync phase for MCPWM timer
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @param[in] config MCPWM timer sync phase configuration
* @return
* - ESP_OK: Set sync phase for MCPWM timer successfully
* - ESP_ERR_INVALID_ARG: Set sync phase for MCPWM timer failed because of invalid argument
* - ESP_FAIL: Set sync phase for MCPWM timer failed because of other error
*/
esp_err_t mcpwm_timer_set_phase_on_sync(mcpwm_timer_handle_t timer, const mcpwm_timer_sync_phase_config_t *config);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,145 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "hal/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Type of MCPWM timer handle
*/
typedef struct mcpwm_timer_t *mcpwm_timer_handle_t;
/**
* @brief Type of MCPWM operator handle
*/
typedef struct mcpwm_oper_t *mcpwm_oper_handle_t;
/**
* @brief Type of MCPWM comparator handle
*/
typedef struct mcpwm_cmpr_t *mcpwm_cmpr_handle_t;
/**
* @brief Type of MCPWM generator handle
*/
typedef struct mcpwm_gen_t *mcpwm_gen_handle_t;
/**
* @brief Type of MCPWM fault handle
*/
typedef struct mcpwm_fault_t *mcpwm_fault_handle_t;
/**
* @brief Type of MCPWM sync handle
*/
typedef struct mcpwm_sync_t *mcpwm_sync_handle_t;
/**
* @brief Type of MCPWM capture timer handle
*/
typedef struct mcpwm_cap_timer_t *mcpwm_cap_timer_handle_t;
/**
* @brief Type of MCPWM capture channel handle
*/
typedef struct mcpwm_cap_channel_t *mcpwm_cap_channel_handle_t;
/**
* @brief MCPWM timer event data
*/
typedef struct {
uint32_t count_value; /*!< MCPWM timer count value */
mcpwm_timer_direction_t direction; /*!< MCPWM timer count direction */
} mcpwm_timer_event_data_t;
/**
* @brief MCPWM timer event callback function
*
* @param[in] timer MCPWM timer handle
* @param[in] edata MCPWM timer event data, fed by driver
* @param[in] user_ctx User data, set in `mcpwm_timer_register_event_callbacks()`
* @return Whether a high priority task has been waken up by this function
*/
typedef bool (*mcpwm_timer_event_cb_t)(mcpwm_timer_handle_t timer, const mcpwm_timer_event_data_t *edata, void *user_ctx);
/**
* @brief MCPWM brake event data
*/
typedef struct {
} mcpwm_brake_event_data_t;
/**
* @brief MCPWM operator brake event callback function
*
* @param[in] oper MCPWM operator handle
* @param[in] edata MCPWM brake event data, fed by driver
* @param[in] user_ctx User data, set in `mcpwm_operator_register_event_callbacks()`
* @return Whether a high priority task has been waken up by this function
*/
typedef bool (*mcpwm_brake_event_cb_t)(mcpwm_oper_handle_t oper, const mcpwm_brake_event_data_t *edata, void *user_ctx);
/**
* @brief MCPWM fault event data
*/
typedef struct {
} mcpwm_fault_event_data_t;
/**
* @brief MCPWM fault event callback function
*
* @param fault MCPWM fault handle
* @param edata MCPWM fault event data, fed by driver
* @param user_ctx User data, set in `mcpwm_fault_register_event_callbacks()`
* @return whether a task switch is needed after the callback returns
*/
typedef bool (*mcpwm_fault_event_cb_t)(mcpwm_fault_handle_t fault, const mcpwm_fault_event_data_t *edata, void *user_ctx);
/**
* @brief MCPWM compare event data
*/
typedef struct {
uint32_t compare_ticks; /*!< Compare value */
mcpwm_timer_direction_t direction; /*!< Count direction */
} mcpwm_compare_event_data_t;
/**
* @brief MCPWM comparator event callback function
*
* @param comparator MCPWM comparator handle
* @param edata MCPWM comparator event data, fed by driver
* @param user_ctx User data, set in `mcpwm_comparator_register_event_callbacks()`
* @return Whether a high priority task has been waken up by this function
*/
typedef bool (*mcpwm_compare_event_cb_t)(mcpwm_cmpr_handle_t comparator, const mcpwm_compare_event_data_t *edata, void *user_ctx);
/**
* @brief MCPWM capture event data
*/
typedef struct {
uint32_t cap_value; /*!< Captured value */
mcpwm_capture_edge_t cap_edge; /*!< Capture edge */
} mcpwm_capture_event_data_t;
/**
* @brief MCPWM capture event callback function
*
* @param cap_channel MCPWM capture channel handle
* @param edata MCPWM capture event data, fed by driver
* @param user_ctx User data, set in `mcpwm_capture_channel_register_event_callbacks()`
* @return Whether a high priority task has been waken up by this function
*/
typedef bool (*mcpwm_capture_event_cb_t)(mcpwm_cap_channel_handle_t cap_channel, const mcpwm_capture_event_data_t *edata, void *user_ctx);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,36 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
// DO NOT USE THESE APIS IN YOUR APPLICATIONS
// The following APIs are for internal use, public to other IDF components, but not for users' applications.
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "driver/mcpwm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Get MCPWM timer phase
*
* @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
* @param[out] count_value Returned MCPWM timer phase
* @param[out] direction Returned MCPWM timer counting direction
* @return
* - ESP_OK: Get MCPWM timer status successfully
* - ESP_ERR_INVALID_ARG: Get MCPWM timer status failed because of invalid argument
* - ESP_FAIL: Get MCPWM timer status failed because of other error
*/
esp_err_t mcpwm_timer_get_phase(mcpwm_timer_handle_t timer, uint32_t *count_value, mcpwm_timer_direction_t *direction);
#ifdef __cplusplus
}
#endif