|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "esp_hal_bldc_6pwm.h" |
| 8 | +#include "esp_log.h" |
| 9 | +#include "esp_check.h" |
| 10 | + |
| 11 | +#define _PWM_FREQUENCY 20000 /*!< default frequency of MCPWM */ |
| 12 | +#define _PWM_FREQUENCY_MAX 50000 /*!< Max frequency of MCPWM */ |
| 13 | +#define _PWM_TIMEBASE_RESOLUTION_HZ (10 * 1000 * 1000) /*!< Resolution of MCPWM */ |
| 14 | + |
| 15 | +static const char* TAG = "esp_hal_bldc_6pwm"; |
| 16 | + |
| 17 | +BLDCDriver6PWM::BLDCDriver6PWM(int phA_h, int phA_l, int phB_h, int phB_l, int phC_h, int phC_l, int en, int mcpwm_group_id) |
| 18 | +{ |
| 19 | + pwmA_h = phA_h; |
| 20 | + pwmB_h = phB_h; |
| 21 | + pwmC_h = phC_h; |
| 22 | + pwmA_l = phA_l; |
| 23 | + pwmB_l = phB_l; |
| 24 | + pwmC_l = phC_l; |
| 25 | + |
| 26 | + // enable_pin pin |
| 27 | + enable_pin = en; |
| 28 | + |
| 29 | + // default power-supply value |
| 30 | + voltage_power_supply = DEF_POWER_SUPPLY; |
| 31 | + voltage_limit = NOT_SET; |
| 32 | + pwm_frequency = NOT_SET; |
| 33 | + |
| 34 | + // dead zone initial - 2% |
| 35 | + dead_zone = 0.02f; |
| 36 | + |
| 37 | + this->mcpwm_group_id = mcpwm_group_id; |
| 38 | +} |
| 39 | + |
| 40 | +void BLDCDriver6PWM::enable() |
| 41 | +{ |
| 42 | + // enable_pin the driver - if enable_pin pin available |
| 43 | + if (_isset(enable_pin)) { |
| 44 | + digitalWrite(enable_pin, enable_active_high); |
| 45 | + } |
| 46 | + // set phase state enabled |
| 47 | + setPhaseState(PhaseState::PHASE_ON, PhaseState::PHASE_ON, PhaseState::PHASE_ON); |
| 48 | + // set zero to PWM |
| 49 | + setPwm(0, 0, 0); |
| 50 | +} |
| 51 | + |
| 52 | +void BLDCDriver6PWM::disable() |
| 53 | +{ |
| 54 | + // set phase state to disabled |
| 55 | + setPhaseState(PhaseState::PHASE_OFF, PhaseState::PHASE_OFF, PhaseState::PHASE_OFF); |
| 56 | + // set zero to PWM |
| 57 | + setPwm(0, 0, 0); |
| 58 | + // disable the driver - if enable_pin pin available |
| 59 | + if (_isset(enable_pin)) { |
| 60 | + digitalWrite(enable_pin, !enable_active_high); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +int BLDCDriver6PWM::init() |
| 65 | +{ |
| 66 | + // PWM pins |
| 67 | + pinMode(pwmA_h, OUTPUT); |
| 68 | + pinMode(pwmB_h, OUTPUT); |
| 69 | + pinMode(pwmC_h, OUTPUT); |
| 70 | + pinMode(pwmA_l, OUTPUT); |
| 71 | + pinMode(pwmB_l, OUTPUT); |
| 72 | + pinMode(pwmC_l, OUTPUT); |
| 73 | + if (_isset(enable_pin)) { |
| 74 | + pinMode(enable_pin, OUTPUT); |
| 75 | + } |
| 76 | + |
| 77 | + // sanity check for the voltage limit configuration |
| 78 | + if (!_isset(voltage_limit) || voltage_limit > voltage_power_supply) { |
| 79 | + voltage_limit = voltage_power_supply; |
| 80 | + } |
| 81 | + |
| 82 | + // set phase state to disabled |
| 83 | + phase_state[0] = PhaseState::PHASE_OFF; |
| 84 | + phase_state[1] = PhaseState::PHASE_OFF; |
| 85 | + phase_state[2] = PhaseState::PHASE_OFF; |
| 86 | + |
| 87 | + // set zero to PWM |
| 88 | + dc_a = dc_b = dc_c = 0; |
| 89 | + |
| 90 | + // configure 6pwm |
| 91 | + // hardware specific function - depending on driver and mcu |
| 92 | + if (mcpwm_group_id < 0 || mcpwm_group_id >= SOC_MCPWM_GROUPS) { |
| 93 | + ESP_LOGE(TAG, "MCPWM group id: %d is not available\n", mcpwm_group_id); |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + // Init mcpwm driver |
| 98 | + mcpwm_timer_config_t timer_config = { |
| 99 | + .group_id = mcpwm_group_id, |
| 100 | + .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, |
| 101 | + .resolution_hz = _PWM_TIMEBASE_RESOLUTION_HZ, |
| 102 | + .count_mode = MCPWM_TIMER_COUNT_MODE_UP_DOWN, |
| 103 | + .period_ticks = (uint32_t)(1 * _PWM_TIMEBASE_RESOLUTION_HZ / _PWM_FREQUENCY), |
| 104 | + }; |
| 105 | + ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer)); |
| 106 | + |
| 107 | + mcpwm_operator_config_t operator_config = { |
| 108 | + .group_id = mcpwm_group_id, |
| 109 | + }; |
| 110 | + |
| 111 | + for (int i = 0; i < 3; i++) { |
| 112 | + ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config, &oper[i])); |
| 113 | + ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper[i], timer)); |
| 114 | + } |
| 115 | + |
| 116 | + mcpwm_comparator_config_t comparator_config = {0}; |
| 117 | + comparator_config.flags.update_cmp_on_tez = true; |
| 118 | + comparator_config.flags.update_cmp_on_tep = false; |
| 119 | + comparator_config.flags.update_cmp_on_sync = false; |
| 120 | + |
| 121 | + for (int i = 0; i < 3; i++) { |
| 122 | + ESP_ERROR_CHECK(mcpwm_new_comparator(oper[i], &comparator_config, &comparator[i])); |
| 123 | + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator[i], (0))); |
| 124 | + } |
| 125 | + |
| 126 | + mcpwm_generator_config_t generator_config = {}; |
| 127 | + int gen_gpios[3][2] = { |
| 128 | + {pwmA_h, pwmA_l}, |
| 129 | + {pwmB_h, pwmB_l}, |
| 130 | + {pwmC_h, pwmC_l}, |
| 131 | + }; |
| 132 | + |
| 133 | + for (int i = 0; i < 3; i++) { |
| 134 | + for (int j = 0; j < 2; j++) { |
| 135 | + generator_config.gen_gpio_num = gen_gpios[i][j]; |
| 136 | + ESP_ERROR_CHECK(mcpwm_new_generator(oper[i], &generator_config, &generator[i][j])); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + for (int i = 0; i < 3; i++) { |
| 141 | + ESP_ERROR_CHECK(mcpwm_generator_set_actions_on_compare_event(generator[i][0], |
| 142 | + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator[i], MCPWM_GEN_ACTION_LOW), |
| 143 | + MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comparator[i], MCPWM_GEN_ACTION_HIGH), |
| 144 | + MCPWM_GEN_COMPARE_EVENT_ACTION_END())); |
| 145 | + } |
| 146 | + |
| 147 | + mcpwm_dead_time_config_t dt_config = { |
| 148 | + .posedge_delay_ticks = (uint32_t)(dead_zone * timer_config.period_ticks), |
| 149 | + }; |
| 150 | + |
| 151 | + mcpwm_dead_time_config_t inv_dt_config = { |
| 152 | + .negedge_delay_ticks = (uint32_t)(dead_zone * timer_config.period_ticks), |
| 153 | + }; |
| 154 | + inv_dt_config.flags.invert_output = true; |
| 155 | + |
| 156 | + for (int i = 0; i < 3; i++) { |
| 157 | + ESP_ERROR_CHECK(mcpwm_generator_set_dead_time(generator[i][0], generator[i][0], &dt_config)); |
| 158 | + ESP_ERROR_CHECK(mcpwm_generator_set_dead_time(generator[i][0], generator[i][1], &inv_dt_config)); |
| 159 | + } |
| 160 | + |
| 161 | + // enable mcpwm |
| 162 | + ESP_ERROR_CHECK(mcpwm_timer_enable(timer)); |
| 163 | + ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP)); |
| 164 | + |
| 165 | + // save timer period |
| 166 | + mcpwm_period = timer_config.period_ticks / 2; |
| 167 | + initialized = 1; |
| 168 | + |
| 169 | + return 1; |
| 170 | +} |
| 171 | + |
| 172 | +int BLDCDriver6PWM::deinit() |
| 173 | +{ |
| 174 | + if (initialized) { |
| 175 | + ESP_ERROR_CHECK(mcpwm_timer_disable(timer)); |
| 176 | + |
| 177 | + for (int i = 0; i < 3; i++) { |
| 178 | + ESP_ERROR_CHECK(mcpwm_del_generator(generator[i][0])); |
| 179 | + ESP_ERROR_CHECK(mcpwm_del_generator(generator[i][1])); |
| 180 | + ESP_ERROR_CHECK(mcpwm_del_comparator(comparator[i])); |
| 181 | + ESP_ERROR_CHECK(mcpwm_del_operator(oper[i])); |
| 182 | + } |
| 183 | + ESP_ERROR_CHECK(mcpwm_del_timer(timer)); |
| 184 | + initialized = 0; |
| 185 | + return 1; |
| 186 | + } |
| 187 | + |
| 188 | + return 0; |
| 189 | +} |
| 190 | + |
| 191 | +void BLDCDriver6PWM::setPwm(float Ua, float Ub, float Uc) |
| 192 | +{ |
| 193 | + // limit the voltage in driver |
| 194 | + Ua = _constrain(Ua, 0, voltage_limit); |
| 195 | + Ub = _constrain(Ub, 0, voltage_limit); |
| 196 | + Uc = _constrain(Uc, 0, voltage_limit); |
| 197 | + // calculate duty cycle |
| 198 | + // limited in [0,1] |
| 199 | + dc_a = _constrain(Ua / voltage_power_supply, 0.0f, 1.0f); |
| 200 | + dc_b = _constrain(Ub / voltage_power_supply, 0.0f, 1.0f); |
| 201 | + dc_c = _constrain(Uc / voltage_power_supply, 0.0f, 1.0f); |
| 202 | + // hardware specific writing |
| 203 | + // hardware specific function - depending on driver and mcu |
| 204 | + halPwmWrite(); |
| 205 | +} |
| 206 | + |
| 207 | +void BLDCDriver6PWM::setPhaseState(PhaseState sa, PhaseState sb, PhaseState sc) |
| 208 | +{ |
| 209 | + phase_state[0] = sa; |
| 210 | + phase_state[1] = sb; |
| 211 | + phase_state[2] = sc; |
| 212 | +} |
| 213 | + |
| 214 | +void BLDCDriver6PWM::halPwmWrite() |
| 215 | +{ |
| 216 | + if (!initialized) { |
| 217 | + return; |
| 218 | + } |
| 219 | + |
| 220 | + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator[0], (uint32_t)((mcpwm_period * dc_a)))); |
| 221 | + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator[1], (uint32_t)((mcpwm_period * dc_b)))); |
| 222 | + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator[2], (uint32_t)((mcpwm_period * dc_c)))); |
| 223 | +} |
0 commit comments