Skip to content

Commit a916bfa

Browse files
authored
Update teensy4_mcu.cpp _writeDutyCycle6PWM to handle phase_state
Handles phase_state so that PWM pins will be disabled when motor.disable() is called. This enables coasting the motor on drivers where the enable pin does not fully disable the driver and all 6 PWM pins must be disabled.
1 parent 395b6cd commit a916bfa

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/drivers/hardware_specific/teensy/teensy4_mcu.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,16 +526,59 @@ void* _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, cons
526526
}
527527

528528

529+
// Helper function to enable or disable pwm pins based on phase state (allows for disabling the motor)
530+
static inline void _teensy4_apply_phase_state(
531+
IMXRT_FLEXPWM_t *flexpwm, int submodule, PhaseState state
532+
)
533+
{
534+
uint16_t submodule_mask = 1u << submodule;
535+
uint16_t enable_a = FLEXPWM_OUTEN_PWMA_EN( submodule_mask );
536+
uint16_t enable_b = FLEXPWM_OUTEN_PWMB_EN( submodule_mask );
537+
538+
switch ( state )
539+
{
540+
case PhaseState::PHASE_OFF:
541+
flexpwm->OUTEN &= ~( enable_a | enable_b );
542+
break;
543+
case PhaseState::PHASE_HI:
544+
flexpwm->OUTEN |= enable_a;
545+
flexpwm->OUTEN &= ~enable_b;
546+
break;
547+
case PhaseState::PHASE_LO:
548+
flexpwm->OUTEN &= ~enable_a;
549+
flexpwm->OUTEN |= enable_b;
550+
break;
551+
case PhaseState::PHASE_ON:
552+
default:
553+
flexpwm->OUTEN |= ( enable_a | enable_b );
554+
break;
555+
}
556+
}
529557

530558
// function setting the pwm duty cycle to the hardware
531559
// - Stepper motor - 6PWM setting
532560
// - hardware specific
533561
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void* params){
534-
Teensy4DriverParams* p = (Teensy4DriverParams*)((TeensyDriverParams*)params)->additional_params;
535-
_UNUSED(phase_state);
536-
write_pwm_pair (p->flextimers[0], p->submodules[0], dc_a);
537-
write_pwm_pair (p->flextimers[1], p->submodules[1], dc_b);
538-
write_pwm_pair (p->flextimers[2], p->submodules[2], dc_c);
562+
Teensy4DriverParams *p = (Teensy4DriverParams *)( (TeensyDriverParams *)params )->additional_params;
563+
564+
PhaseState phase_a = phase_state ? phase_state[ 0 ] : PhaseState::PHASE_ON;
565+
PhaseState phase_b = phase_state ? phase_state[ 1 ] : PhaseState::PHASE_ON;
566+
PhaseState phase_c = phase_state ? phase_state[ 2 ] : PhaseState::PHASE_ON;
567+
568+
if ( PhaseState::PHASE_OFF == phase_a )
569+
dc_a = 0.0f;
570+
if ( PhaseState::PHASE_OFF == phase_b )
571+
dc_b = 0.0f;
572+
if ( PhaseState::PHASE_OFF == phase_c )
573+
dc_c = 0.0f;
574+
575+
_teensy4_apply_phase_state( p->flextimers[ 0 ], p->submodules[ 0 ], phase_a );
576+
_teensy4_apply_phase_state( p->flextimers[ 1 ], p->submodules[ 1 ], phase_b );
577+
_teensy4_apply_phase_state( p->flextimers[ 2 ], p->submodules[ 2 ], phase_c );
578+
579+
write_pwm_pair( p->flextimers[ 0 ], p->submodules[ 0 ], dc_a );
580+
write_pwm_pair( p->flextimers[ 1 ], p->submodules[ 1 ], dc_b );
581+
write_pwm_pair( p->flextimers[ 2 ], p->submodules[ 2 ], dc_c );
539582
}
540583

541584
void write_pwm_on_pin(IMXRT_FLEXPWM_t *p, unsigned int submodule, uint8_t channel, float duty)

0 commit comments

Comments
 (0)