1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 #include "soc/soc_caps.h"
9 #include "hal/mcpwm_hal.h"
10 #include "hal/mcpwm_ll.h"
11 
mcpwm_hal_init(mcpwm_hal_context_t * hal,const mcpwm_hal_init_config_t * init_config)12 void mcpwm_hal_init(mcpwm_hal_context_t *hal, const mcpwm_hal_init_config_t *init_config)
13 {
14     hal->dev = MCPWM_LL_GET_HW(init_config->group_id);
15     mcpwm_ll_group_enable_shadow_mode(hal->dev);
16     mcpwm_ll_group_flush_shadow(hal->dev);
17 }
18 
mcpwm_hal_deinit(mcpwm_hal_context_t * hal)19 void mcpwm_hal_deinit(mcpwm_hal_context_t *hal)
20 {
21     hal->dev = NULL;
22 }
23 
mcpwm_hal_timer_reset(mcpwm_hal_context_t * hal,int timer_id)24 void mcpwm_hal_timer_reset(mcpwm_hal_context_t *hal, int timer_id)
25 {
26     mcpwm_ll_timer_set_count_mode(hal->dev, timer_id, MCPWM_TIMER_COUNT_MODE_PAUSE);
27     mcpwm_ll_timer_update_period_at_once(hal->dev, timer_id);
28     // disable sync input and output by default
29     mcpwm_ll_timer_disable_sync_out(hal->dev, timer_id);
30     mcpwm_ll_timer_enable_sync_input(hal->dev, timer_id, false);
31     mcpwm_ll_timer_clear_sync_input(hal->dev, timer_id);
32 }
33 
mcpwm_hal_operator_reset(mcpwm_hal_context_t * hal,int oper_id)34 void mcpwm_hal_operator_reset(mcpwm_hal_context_t *hal, int oper_id)
35 {
36     // allow to update action, compare, and dead time configuration
37     mcpwm_ll_operator_stop_update_action(hal->dev, oper_id, false);
38     mcpwm_ll_operator_update_action_at_once(hal->dev, oper_id);
39     mcpwm_ll_deadtime_stop_update_delay(hal->dev, oper_id, false);
40     mcpwm_ll_deadtime_update_delay_at_once(hal->dev, oper_id);
41     for (int i = 0; i < SOC_MCPWM_COMPARATORS_PER_OPERATOR; i++) {
42         mcpwm_ll_operator_stop_update_compare(hal->dev, oper_id, i, false);
43         mcpwm_ll_operator_update_compare_at_once(hal->dev, oper_id, i);
44     }
45     mcpwm_ll_brake_enable_cbc_refresh_on_tez(hal->dev, oper_id, false);
46     mcpwm_ll_fault_enable_cbc_refresh_on_tep(hal->dev, oper_id, false);
47     mcpwm_ll_brake_enable_soft_cbc(hal->dev, oper_id, false);
48     mcpwm_ll_brake_enable_soft_ost(hal->dev, oper_id, false);
49 }
50 
mcpwm_hal_generator_reset(mcpwm_hal_context_t * hal,int oper_id,int gen_id)51 void mcpwm_hal_generator_reset(mcpwm_hal_context_t *hal, int oper_id, int gen_id)
52 {
53     mcpwm_ll_generator_reset_actions(hal->dev, oper_id, gen_id);
54     mcpwm_ll_gen_disable_continue_force_action(hal->dev, oper_id, gen_id);
55     mcpwm_ll_gen_disable_noncontinue_force_action(hal->dev, oper_id, gen_id);
56 }
57