@@ -60,7 +60,20 @@ class BldcDriver {
6060 /* *
6161 * @brief Disable the BldcDriver and destroy it.
6262 */
63- ~BldcDriver () { disable (); }
63+ ~BldcDriver () {
64+ disable ();
65+ for (auto &gen : generators_) {
66+ mcpwm_del_generator (gen);
67+ }
68+ for (auto &comp : comparators_) {
69+ mcpwm_del_comparator (comp);
70+ }
71+ mcpwm_del_fault (fault_handle_);
72+ for (auto &oper : operators_) {
73+ mcpwm_del_operator (oper);
74+ }
75+ mcpwm_del_timer (timer_);
76+ }
6477
6578 /* *
6679 * @brief Enable the driver, set the enable pin high (if we were provided an
@@ -220,9 +233,9 @@ class BldcDriver {
220233protected:
221234 void init (const Config &config) {
222235 configure_enable_gpio ();
223- configure_fault_gpio ();
224236 configure_timer ();
225237 configure_operators ();
238+ configure_fault ();
226239 configure_comparators ();
227240 configure_generators ();
228241 enable ();
@@ -241,20 +254,6 @@ class BldcDriver {
241254 gpio_set_level ((gpio_num_t )gpio_en_, 0 );
242255 }
243256
244- void configure_fault_gpio () {
245- if (gpio_fault_ < 0 ) {
246- return ;
247- }
248- logger_.info (" Configure fault pin" );
249- gpio_config_t drv_fault_config;
250- memset (&drv_fault_config, 0 , sizeof (drv_fault_config));
251- drv_fault_config.pin_bit_mask = 1ULL << gpio_fault_;
252- drv_fault_config.mode = GPIO_MODE_INPUT;
253- drv_fault_config.pull_up_en = GPIO_PULLUP_DISABLE;
254- drv_fault_config.pull_down_en = GPIO_PULLDOWN_ENABLE;
255- ESP_ERROR_CHECK (gpio_config (&drv_fault_config));
256- }
257-
258257 void configure_timer () {
259258 logger_.info (" Create MCPWM timer" );
260259 mcpwm_timer_config_t timer_config;
@@ -281,6 +280,40 @@ class BldcDriver {
281280 }
282281 }
283282
283+ void configure_fault () {
284+ if (gpio_fault_ < 0 ) {
285+ return ;
286+ }
287+ logger_.info (" Create fault detector" );
288+ mcpwm_gpio_fault_config_t gpio_fault_config{};
289+ memset (&gpio_fault_config, 0 , sizeof (gpio_fault_config));
290+ gpio_fault_config.gpio_num = (gpio_num_t )gpio_fault_;
291+ gpio_fault_config.group_id = 0 ;
292+ gpio_fault_config.flags .active_level = 1 ; // high level means fault, refer to TMC6300 datasheet
293+ gpio_fault_config.flags .pull_down = true ; // internally pull down
294+ gpio_fault_config.flags .io_loop_back = true ; // enable loop back to GPIO input
295+ ESP_ERROR_CHECK (mcpwm_new_gpio_fault (&gpio_fault_config, &fault_handle_));
296+
297+ logger_.info (" Set brake mode on the fault event" );
298+ mcpwm_brake_config_t brake_config{};
299+ memset (&brake_config, 0 , sizeof (brake_config));
300+ brake_config.brake_mode = MCPWM_OPER_BRAKE_MODE_CBC;
301+ brake_config.fault = fault_handle_;
302+ brake_config.flags .cbc_recover_on_tez = true ;
303+ for (int i = 0 ; i < 3 ; i++) {
304+ ESP_ERROR_CHECK (mcpwm_operator_set_brake_on_fault (operators_[i], &brake_config));
305+ }
306+
307+ logger_.info (" Configure fault pin" );
308+ gpio_config_t drv_fault_config;
309+ memset (&drv_fault_config, 0 , sizeof (drv_fault_config));
310+ drv_fault_config.pin_bit_mask = 1ULL << gpio_fault_;
311+ drv_fault_config.mode = GPIO_MODE_INPUT;
312+ drv_fault_config.pull_up_en = GPIO_PULLUP_DISABLE;
313+ drv_fault_config.pull_down_en = GPIO_PULLDOWN_ENABLE;
314+ ESP_ERROR_CHECK (gpio_config (&drv_fault_config));
315+ }
316+
284317 void configure_comparators () {
285318 logger_.info (" Create comparators" );
286319 mcpwm_comparator_config_t compare_config;
@@ -301,6 +334,10 @@ class BldcDriver {
301334 ESP_ERROR_CHECK (mcpwm_new_generator (operators_[i / 2 ], &gen_config, &generators_[i]));
302335 }
303336
337+ // set high and low generators to output the same waveform using the
338+ // comparator. we will use the dead time module to add edge delay, also make
339+ // gen_high and gen_low complementary
340+
304341 // A high / low
305342 configure_generator_action (generators_[0 ], generators_[1 ], comparators_[0 ]);
306343 configure_generator_deadtime (generators_[0 ], generators_[1 ]);
@@ -315,16 +352,31 @@ class BldcDriver {
315352 void configure_generator_action (mcpwm_gen_handle_t &gen_high, mcpwm_gen_handle_t &gen_low,
316353 mcpwm_cmpr_handle_t comp) {
317354 logger_.info (" Setup generator action" );
318- ESP_ERROR_CHECK (mcpwm_generator_set_actions_on_compare_event (
355+
356+ // set high/low generators to output low when the timer is counting up, and
357+ // high when the timer is counting down.
358+ ESP_ERROR_CHECK (mcpwm_generator_set_action_on_compare_event (
319359 gen_high,
320- MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW),
321- MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH),
322- MCPWM_GEN_COMPARE_EVENT_ACTION_END ()));
323- ESP_ERROR_CHECK (mcpwm_generator_set_actions_on_compare_event (
360+ MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW)));
361+ ESP_ERROR_CHECK (mcpwm_generator_set_action_on_compare_event (
362+ gen_high,
363+ MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH)));
364+
365+ ESP_ERROR_CHECK (mcpwm_generator_set_action_on_compare_event (
366+ gen_low,
367+ MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW)));
368+ ESP_ERROR_CHECK (mcpwm_generator_set_action_on_compare_event (
324369 gen_low,
325- MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_UP, comp, MCPWM_GEN_ACTION_LOW),
326- MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH),
327- MCPWM_GEN_COMPARE_EVENT_ACTION_END ()));
370+ MCPWM_GEN_COMPARE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_DOWN, comp, MCPWM_GEN_ACTION_HIGH)));
371+
372+ // set the brake event to stop the motor (set low) for both the high and low generators
373+ ESP_ERROR_CHECK (mcpwm_generator_set_action_on_brake_event (
374+ gen_high, MCPWM_GEN_BRAKE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_UP, MCPWM_OPER_BRAKE_MODE_CBC,
375+ MCPWM_GEN_ACTION_LOW)));
376+
377+ ESP_ERROR_CHECK (mcpwm_generator_set_action_on_brake_event (
378+ gen_low, MCPWM_GEN_BRAKE_EVENT_ACTION (MCPWM_TIMER_DIRECTION_UP, MCPWM_OPER_BRAKE_MODE_CBC,
379+ MCPWM_GEN_ACTION_LOW)));
328380 }
329381
330382 void configure_generator_deadtime (mcpwm_gen_handle_t &gen_high, mcpwm_gen_handle_t &gen_low) {
@@ -350,6 +402,7 @@ class BldcDriver {
350402 gpio_num_t gpio_cl_;
351403 int gpio_en_;
352404 int gpio_fault_;
405+ mcpwm_fault_handle_t fault_handle_;
353406 std::atomic<float > power_supply_voltage_;
354407 std::atomic<float > limit_voltage_;
355408 float dead_zone_;
0 commit comments