mcpwm_timer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "driver/mcpwm_types.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @brief Group of supported MCPWM timer event callbacks
  16. * @note The callbacks are all running under ISR environment
  17. */
  18. typedef struct {
  19. mcpwm_timer_event_cb_t on_full; /*!< callback function when MCPWM timer counts to peak value */
  20. mcpwm_timer_event_cb_t on_empty; /*!< callback function when MCPWM timer counts to zero */
  21. mcpwm_timer_event_cb_t on_stop; /*!< callback function when MCPWM timer stops */
  22. } mcpwm_timer_event_callbacks_t;
  23. /**
  24. * @brief MCPWM timer configuration
  25. */
  26. typedef struct {
  27. int group_id; /*!< Specify from which group to allocate the MCPWM timer */
  28. mcpwm_timer_clock_source_t clk_src; /*!< MCPWM timer clock source */
  29. uint32_t resolution_hz; /*!< Counter resolution in Hz
  30. The step size of each count tick equals to (1 / resolution_hz) seconds */
  31. mcpwm_timer_count_mode_t count_mode; /*!< Count mode */
  32. uint32_t period_ticks; /*!< Number of count ticks within a period */
  33. int intr_priority; /*!< MCPWM timer interrupt priority,
  34. if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
  35. struct {
  36. uint32_t update_period_on_empty: 1; /*!< Whether to update period when timer counts to zero */
  37. uint32_t update_period_on_sync: 1; /*!< Whether to update period on sync event */
  38. } flags; /*!< Extra configuration flags for timer */
  39. } mcpwm_timer_config_t;
  40. /**
  41. * @brief Create MCPWM timer
  42. *
  43. * @param[in] config MCPWM timer configuration
  44. * @param[out] ret_timer Returned MCPWM timer handle
  45. * @return
  46. * - ESP_OK: Create MCPWM timer successfully
  47. * - ESP_ERR_INVALID_ARG: Create MCPWM timer failed because of invalid argument
  48. * - ESP_ERR_NO_MEM: Create MCPWM timer failed because out of memory
  49. * - ESP_ERR_NOT_FOUND: Create MCPWM timer failed because all hardware timers are used up and no more free one
  50. * - ESP_FAIL: Create MCPWM timer failed because of other error
  51. */
  52. esp_err_t mcpwm_new_timer(const mcpwm_timer_config_t *config, mcpwm_timer_handle_t *ret_timer);
  53. /**
  54. * @brief Delete MCPWM timer
  55. *
  56. * @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
  57. * @return
  58. * - ESP_OK: Delete MCPWM timer successfully
  59. * - ESP_ERR_INVALID_ARG: Delete MCPWM timer failed because of invalid argument
  60. * - ESP_ERR_INVALID_STATE: Delete MCPWM timer failed because timer is not in init state
  61. * - ESP_FAIL: Delete MCPWM timer failed because of other error
  62. */
  63. esp_err_t mcpwm_del_timer(mcpwm_timer_handle_t timer);
  64. /**
  65. * @brief Set a new period for MCPWM timer
  66. *
  67. * @note If `mcpwm_timer_config_t::update_period_on_empty` and `mcpwm_timer_config_t::update_period_on_sync` are not set,
  68. * the new period will take effect immediately.
  69. * Otherwise, the new period will take effect when timer counts to zero or on sync event.
  70. * @note You may need to use `mcpwm_comparator_set_compare_value` to set a new compare value for MCPWM comparator
  71. * in order to keep the same PWM duty cycle.
  72. *
  73. * @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer`
  74. * @param[in] period_ticks New period in count ticks
  75. * @return
  76. * - ESP_OK: Set new period for MCPWM timer successfully
  77. * - ESP_ERR_INVALID_ARG: Set new period for MCPWM timer failed because of invalid argument
  78. * - ESP_FAIL: Set new period for MCPWM timer failed because of other error
  79. */
  80. esp_err_t mcpwm_timer_set_period(mcpwm_timer_handle_t timer, uint32_t period_ticks);
  81. /**
  82. * @brief Enable MCPWM timer
  83. *
  84. * @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
  85. * @return
  86. * - ESP_OK: Enable MCPWM timer successfully
  87. * - ESP_ERR_INVALID_ARG: Enable MCPWM timer failed because of invalid argument
  88. * - ESP_ERR_INVALID_STATE: Enable MCPWM timer failed because timer is enabled already
  89. * - ESP_FAIL: Enable MCPWM timer failed because of other error
  90. */
  91. esp_err_t mcpwm_timer_enable(mcpwm_timer_handle_t timer);
  92. /**
  93. * @brief Disable MCPWM timer
  94. *
  95. * @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
  96. * @return
  97. * - ESP_OK: Disable MCPWM timer successfully
  98. * - ESP_ERR_INVALID_ARG: Disable MCPWM timer failed because of invalid argument
  99. * - ESP_ERR_INVALID_STATE: Disable MCPWM timer failed because timer is disabled already
  100. * - ESP_FAIL: Disable MCPWM timer failed because of other error
  101. */
  102. esp_err_t mcpwm_timer_disable(mcpwm_timer_handle_t timer);
  103. /**
  104. * @brief Send specific start/stop commands to MCPWM timer
  105. *
  106. * @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
  107. * @param[in] command Supported command list for MCPWM timer
  108. * @return
  109. * - ESP_OK: Start or stop MCPWM timer successfully
  110. * - ESP_ERR_INVALID_ARG: Start or stop MCPWM timer failed because of invalid argument
  111. * - ESP_ERR_INVALID_STATE: Start or stop MCPWM timer failed because timer is not enabled
  112. * - ESP_FAIL: Start or stop MCPWM timer failed because of other error
  113. */
  114. esp_err_t mcpwm_timer_start_stop(mcpwm_timer_handle_t timer, mcpwm_timer_start_stop_cmd_t command);
  115. /**
  116. * @brief Set event callbacks for MCPWM timer
  117. *
  118. * @note The first call to this function needs to be before the call to `mcpwm_timer_enable`
  119. * @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
  120. *
  121. * @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
  122. * @param[in] cbs Group of callback functions
  123. * @param[in] user_data User data, which will be passed to callback functions directly
  124. * @return
  125. * - ESP_OK: Set event callbacks successfully
  126. * - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
  127. * - ESP_ERR_INVALID_STATE: Set event callbacks failed because timer is not in init state
  128. * - ESP_FAIL: Set event callbacks failed because of other error
  129. */
  130. esp_err_t mcpwm_timer_register_event_callbacks(mcpwm_timer_handle_t timer, const mcpwm_timer_event_callbacks_t *cbs, void *user_data);
  131. /**
  132. * @brief MCPWM Timer sync phase configuration
  133. */
  134. typedef struct {
  135. mcpwm_sync_handle_t sync_src; /*!< The sync event source. Set to NULL will disable the timer being synced by others */
  136. uint32_t count_value; /*!< The count value that should lock to upon sync event */
  137. mcpwm_timer_direction_t direction; /*!< The count direction that should lock to upon sync event */
  138. } mcpwm_timer_sync_phase_config_t;
  139. /**
  140. * @brief Set sync phase for MCPWM timer
  141. *
  142. * @param[in] timer MCPWM timer handle, allocated by `mcpwm_new_timer()`
  143. * @param[in] config MCPWM timer sync phase configuration
  144. * @return
  145. * - ESP_OK: Set sync phase for MCPWM timer successfully
  146. * - ESP_ERR_INVALID_ARG: Set sync phase for MCPWM timer failed because of invalid argument
  147. * - ESP_FAIL: Set sync phase for MCPWM timer failed because of other error
  148. */
  149. esp_err_t mcpwm_timer_set_phase_on_sync(mcpwm_timer_handle_t timer, const mcpwm_timer_sync_phase_config_t *config);
  150. #ifdef __cplusplus
  151. }
  152. #endif