Skip to content

Commit 1d1d57c

Browse files
authored
Merge pull request #498 from Ragiton/teensy_6pwm_coasting_patch
Update teensy4_mcu.cpp _writeDutyCycle6PWM to handle phase_state to allow disabling the motor
2 parents 8f674b6 + a916bfa commit 1d1d57c

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Description
2+
3+
Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any architectures/sensors/motors that the PR is targeting if appicable.
4+
5+
The best approach would be to create an issue and reference it in this pull request.
6+
Example:
7+
8+
Fixes # (issue)
9+
10+
## Type of change
11+
12+
Please delete options that are not relevant.
13+
14+
- [ ] Bug fix (non-breaking change which fixes an issue)
15+
- [ ] New feature (non-breaking change which adds functionality)
16+
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
17+
- [ ] This change requires a documentation update
18+
19+
# How Has This Been Tested?
20+
21+
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
22+
23+
**Test Configuration/Setup**:
24+
* Hardware:
25+
* IDE: Arduino/Platformio
26+
* MCU package version (stm32duino/arduino-esp32/..)
27+
28+
# **IMPORTANT** when opening the pull request
29+
30+
* Your pull request MUST target the `dev` branch on this repository. By default you'll probably target `master`, make sure to change it.

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)