mcpwm_hal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*******************************************************************************
  15. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for MCPWM (common part)
  20. /*
  21. * MCPWM HAL usages:
  22. *
  23. * Initialization:
  24. * 1. Fill the parameters in `mcpwm_hal_context_t`.
  25. * 2. Call `mcpwm_hal_init` to initialize the context.
  26. * 3. Call `mcpwm_hal_hw_init` to initialize the hardware.
  27. *
  28. * Basic PWM:
  29. * 1. Update parameters for the timers, comparators and generators.
  30. * 2. Call `mcpwm_hal_timer_update_basic` to update the timer used.
  31. * 3. Call `mcpwm_hal_operator_update_basic` to update all the parameters of a operator.
  32. *
  33. * Alternatively, if only the comparator is updated (duty rate), call
  34. * `mcpwm_hal_operator_update_comparator` to update the comparator parameters; if only the
  35. * generator is updated (output style), call `mcpwm_hal_operator_update_generator` to update the
  36. * generator parameters.
  37. *
  38. * 4. At any time, call `mcpwm_hal_timer_start` to start the timer (so that PWM output will toggle
  39. * according to settings), or call `mcpwm_hal_timer_stop` to stop the timer (so that the PWM output
  40. * will be kept as called).
  41. *
  42. * Timer settings:
  43. * - Sync: Call `mcpwm_hal_timer_enable_sync` to enable the sync for the timer, and call
  44. * `mcpwm_hal_timer_disable_sync` to disable it.
  45. *
  46. * Operator settings:
  47. * - Carrier: Call `mcpwm_hal_operator_enable_carrier` to enable carrier for an operator, and call
  48. * `mcpwm_hal_operator_disable_carrier` to disable it.
  49. *
  50. * - Deadzone: Call `mcpwm_hal_operator_update_deadzone` to update settings of deadzone for an operator.
  51. *
  52. * Fault handling settings:
  53. * 1. Call `mcpwm_hal_fault_init` to initialize an fault signal to be detected.
  54. * 2. Call `mcpwm_hal_operator_update_fault` to update the behavior of an operator when fault is
  55. * detected.
  56. * 3. If the operator selects oneshot mode to handle the fault event, call
  57. * `mcpwm_hal_fault_oneshot_clear` to clear that fault event after the fault is handled properly.
  58. * 4. Call `mcpwm_hal_fault_disable` to deinitialize the fault signal when it's no longer used.
  59. *
  60. * Capture:
  61. * 1. Call `mcpwm_hal_capture_enable` to enable the capture for one capture signal.
  62. * 2. Call `mcpwm_hal_capture_get_result` to get the last captured result.
  63. * 3. Call `mcpwm_hal_capture_disable` to disable the capture for a signal.
  64. */
  65. #pragma once
  66. #include <esp_err.h>
  67. #include "hal/mcpwm_ll.h"
  68. #define MCPWM_BASE_CLK (2 * APB_CLK_FREQ) //2*APB_CLK_FREQ 160Mhz
  69. /// Configuration of HAL that used only once.
  70. typedef struct {
  71. int host_id; ///< Which MCPWM peripheral to use, 0-1.
  72. } mcpwm_hal_init_config_t;
  73. /// Configuration of each generator (output of operator)
  74. typedef struct {
  75. mcpwm_duty_type_t duty_type; ///< How the generator output
  76. int comparator; ///< for mode `MCPWM_DUTY_MODE_*`, which comparator it refers to.
  77. } mcpwm_hal_generator_config_t;
  78. /// Configuration of each operator
  79. typedef struct {
  80. mcpwm_hal_generator_config_t gen[SOC_MCPWM_GENERATOR_NUM]; ///< Configuration of the generators
  81. float duty[SOC_MCPWM_COMPARATOR_NUM]; ///< Duty rate for each comparator, 10 means 10%.
  82. int timer; ///< The timer this operator is using
  83. } mcpwm_hal_operator_config_t;
  84. /// Configuration of each timer
  85. typedef struct {
  86. uint32_t timer_prescale; ///< The prescale from the MCPWM main clock to the timer clock, TIMER_FREQ=(MCPWM_FREQ/(timer_prescale+1))
  87. uint32_t freq; ///< Frequency desired, will be updated to actual value after the `mcpwm_hal_timer_update_freq` is called.
  88. mcpwm_counter_type_t count_mode; ///< Counting mode
  89. } mcpwm_hal_timer_config_t;
  90. typedef struct {
  91. mcpwm_dev_t *dev; ///< Beginning address of the MCPWM peripheral registers. Call `mcpwm_hal_init` to initialize it.
  92. uint32_t prescale; ///< Prescale from the 160M clock to MCPWM main clock.
  93. mcpwm_hal_timer_config_t timer[SOC_MCPWM_TIMER_NUM]; ///< Configuration of the timers
  94. mcpwm_hal_operator_config_t op[SOC_MCPWM_OP_NUM]; ///< Configuration of the operators
  95. } mcpwm_hal_context_t;
  96. /// Configuration of the carrier
  97. typedef struct {
  98. bool inverted; ///< Whether to invert the output
  99. uint8_t duty; ///< Duty of the carrier, 0-7. Duty rate = duty/8.
  100. uint8_t oneshot_pulse_width; ///< oneshot pulse width, in carrier periods. 0 to disable. 0-15.
  101. uint32_t period; ///< Prescale from the MCPWM main clock to the carrier clock. CARRIER_FREQ=(MCPWM_FREQ/(period+1)/8.)
  102. } mcpwm_hal_carrier_conf_t;
  103. /// Configuration of the deadzone
  104. typedef struct {
  105. mcpwm_deadtime_type_t mode; ///< Deadzone mode, `MCPWM_DEADTIME_BYPASS` to disable.
  106. uint32_t fed; ///< Delay on falling edge. By MCPWM main clock.
  107. uint32_t red; ///< Delay on rising edge. By MCPWM main clock.
  108. } mcpwm_hal_deadzone_conf_t;
  109. /// Configuration of the fault handling for each operator
  110. typedef struct {
  111. uint32_t cbc_enabled_mask; ///< Whether the cycle-by-cycle fault handling is enabled on each fault signal. BIT(n) stands for signal n.
  112. uint32_t ost_enabled_mask; ///< Whether the oneshot fault handling is enabled on each on each fault signal. BIT(n) stands for signal n.
  113. mcpwm_output_action_t action_on_fault[SOC_MCPWM_GENERATOR_NUM]; ///< Action to perform on each generator when any one of the fault signal triggers.
  114. } mcpwm_hal_fault_conf_t;
  115. /// Configuration of the synchronization of each clock
  116. typedef struct {
  117. mcpwm_sync_signal_t sync_sig; ///< Sync signal to use
  118. uint32_t reload_permillage; ///< Reload permillage when the sync is triggered. 100 means the timer will be reload to (period * 100)/1000=10% period value.
  119. } mcpwm_hal_sync_config_t;
  120. /// Configuration of the capture feature on each capture signal
  121. typedef struct {
  122. mcpwm_capture_on_edge_t cap_edge; ///< Whether the edges is captured, bitwise.
  123. uint32_t prescale; ///< Prescale of the input signal.
  124. } mcpwm_hal_capture_config_t;
  125. /**
  126. * @brief Initialize the internal state of the HAL. Call after settings are set and before other functions are called.
  127. *
  128. * @note Since There are several individual parts (timers + operators, captures), this funciton is
  129. * allowed to called several times.
  130. *
  131. * @param hal Context of the HAL layer.
  132. * @param init_config Configuration for the HAL to be used only once.
  133. */
  134. void mcpwm_hal_init(mcpwm_hal_context_t *hal, const mcpwm_hal_init_config_t *init_config);
  135. /**
  136. * @brief Initialize the hardware, call after `mcpwm_hal_init` and before other functions.
  137. *
  138. * @param hal Context of the HAL layer.
  139. */
  140. void mcpwm_hal_hw_init(mcpwm_hal_context_t *hal);
  141. /**
  142. * @brief Start a timer
  143. *
  144. * @param hal Context of the HAL layer.
  145. * @param timer Timer to start, 0-2.
  146. */
  147. void mcpwm_hal_timer_start(mcpwm_hal_context_t *hal, int timer);
  148. /**
  149. * @brief Stop a timer.
  150. *
  151. * @param hal Context of the HAL layer.
  152. * @param timer Timer to stop, 0-2.
  153. */
  154. void mcpwm_hal_timer_stop(mcpwm_hal_context_t *hal, int timer);
  155. /**
  156. * @brief Update the basic parameters of a timer.
  157. *
  158. * @note This will influence the duty rate and count mode of each operator relies on this timer.
  159. * Call `mcpwm_hal_operator_update_basic` for each of the operator that relies on this timer after
  160. * to update the duty rate and generator output.
  161. *
  162. * @param hal Context of the HAL layer.
  163. * @param timer Timer to update, 0-2.
  164. */
  165. void mcpwm_hal_timer_update_basic(mcpwm_hal_context_t *hal, int timer);
  166. /**
  167. * @brief Start the synchronization for a timer.
  168. *
  169. * @param hal Context of the HAL layer.
  170. * @param timer Timer to enable, 0-2.
  171. * @param sync_conf Configuration of the sync operation.
  172. */
  173. void mcpwm_hal_timer_enable_sync(mcpwm_hal_context_t *hal, int timer, const mcpwm_hal_sync_config_t *sync_conf);
  174. /**
  175. * @brief Stop the synchronization for a timer.
  176. *
  177. * @param hal Context of the HAL layer.
  178. * @param timer Timer to disable sync, 0-2.
  179. */
  180. void mcpwm_hal_timer_disable_sync(mcpwm_hal_context_t *hal, int timer);
  181. /**
  182. * @brief Update the basic settings (duty, output mode) for an operator.
  183. *
  184. * Will call `mcpwm_hal_operator_update_comparator` and `mcpwm_hal_operator_update_generator`
  185. * recursively to update each of their duty and output mode.
  186. *
  187. * @param hal Context of the HAL layer.
  188. * @param op Operator to update, 0-2.
  189. */
  190. void mcpwm_hal_operator_update_basic(mcpwm_hal_context_t *hal, int op);
  191. /**
  192. * @brief Update a comparator (duty) for an operator.
  193. *
  194. * @param hal Context of the HAL layer.
  195. * @param op Operator to update, 0-2.
  196. * @param cmp Comparator to update, 0-1.
  197. */
  198. void mcpwm_hal_operator_update_comparator(mcpwm_hal_context_t *hal, int op, int cmp);
  199. /**
  200. * @brief Update a generator (output mode) for an operator.
  201. *
  202. * @param hal Context of the HAL layer.
  203. * @param op Operator to update, 0-2.
  204. * @param cmp Comparator to update, 0-1.
  205. */
  206. void mcpwm_hal_operator_update_generator(mcpwm_hal_context_t *hal, int op, int gen_num);
  207. /**
  208. * @brief Enable the carrier for an operator.
  209. *
  210. * @param hal Context of the HAL layer.
  211. * @param op Operator to enable carrier, 0-2.
  212. * @param carrier_conf Configuration of the carrier.
  213. */
  214. void mcpwm_hal_operator_enable_carrier(mcpwm_hal_context_t *hal, int op, const mcpwm_hal_carrier_conf_t *carrier_conf);
  215. /**
  216. * @brief Disable the carrier for an operator.
  217. *
  218. * @param hal Context of the HAL layer.
  219. * @param op Operator to disable carrier, 0-2.
  220. */
  221. void mcpwm_hal_operator_disable_carrier(mcpwm_hal_context_t *hal, int op);
  222. /**
  223. * @brief Update the deadzone for an operator.
  224. *
  225. * @param hal Context of the HAL layer.
  226. * @param op Operator to update the deadzone, 0-2.
  227. * @param deadzone Configuration of the deadzone. Set member `mode` to `MCPWM_DEADTIME_BYPASS` will bypass the deadzone.
  228. */
  229. void mcpwm_hal_operator_update_deadzone(mcpwm_hal_context_t *hal, int op, const mcpwm_hal_deadzone_conf_t *deadzone);
  230. /**
  231. * @brief Enable one of the fault signal.
  232. *
  233. * @param hal Context of the HAL layer.
  234. * @param fault_sig The signal to enable, 0-2.
  235. * @param level The active level for the fault signal, true for high and false for low.
  236. */
  237. void mcpwm_hal_fault_init(mcpwm_hal_context_t *hal, int fault_sig, bool level);
  238. /**
  239. * @brief Configure how the operator behave to the fault signals.
  240. *
  241. * Call after the fault signal is enabled by `mcpwm_hal_fault_init`.
  242. *
  243. * @param hal Context of the HAL layer.
  244. * @param op Operator to configure, 0-2.
  245. * @param fault_conf Configuration of the behavior of the operator when fault. Clear member `cbc_enabled_mask` and `ost_enabled_mask` will disable the fault detection of this operator.
  246. */
  247. void mcpwm_hal_operator_update_fault(mcpwm_hal_context_t *hal, int op, const mcpwm_hal_fault_conf_t *fault_conf);
  248. /**
  249. * @brief Clear the oneshot fault status for an operator.
  250. *
  251. * @param hal Context of the HAL layer.
  252. * @param op The operator to clear oneshot fault status, 0-2.
  253. */
  254. void mcpwm_hal_fault_oneshot_clear(mcpwm_hal_context_t *hal, int op);
  255. /**
  256. * @brief Disable one of the fault signal.
  257. *
  258. * @param hal Context of the HAL layer.
  259. * @param fault_sig The fault signal to disable, 0-2.
  260. */
  261. void mcpwm_hal_fault_disable(mcpwm_hal_context_t *hal, int fault_sig);
  262. /**
  263. * @brief Enable one of the capture signal.
  264. *
  265. * @param hal Context of the HAL layer.
  266. * @param cap_sig Capture signal to enable, 0-2.
  267. * @param conf Configuration on how to capture the signal.
  268. */
  269. void mcpwm_hal_capture_enable(mcpwm_hal_context_t *hal, int cap_sig, const mcpwm_hal_capture_config_t *conf);
  270. /**
  271. * @brief Get the capture result.
  272. *
  273. * @note The output value will always be updated with the register value, no matter event triggered or not.
  274. *
  275. * @param hal Context of the HAL layer.
  276. * @param cap_sig Signal to get capture result, 0-2.
  277. * @param out_count Output of the captured counter.
  278. * @param out_edge Output of the captured edge.
  279. * @return
  280. * - ESP_OK: if a signal is captured
  281. * - ESP_ERR_NOT_FOUND: if no capture event happened.
  282. */
  283. esp_err_t mcpwm_hal_capture_get_result(mcpwm_hal_context_t *hal, int cap_sig, uint32_t *out_count,
  284. mcpwm_capture_on_edge_t *out_edge);
  285. /**
  286. * @brief Disable one of the capture signal.
  287. *
  288. * @param hal Context of the HAL layer.
  289. * @param cap_sig The signal to capture, 0-2.
  290. */
  291. void mcpwm_hal_capture_disable(mcpwm_hal_context_t *hal, int cap_sig);