Skip to content

Commit

Permalink
Fix issue: Timer PWM mode edge case issue fixed
Browse files Browse the repository at this point in the history
when timer period end the tmr->count is reset to 0x00000001
and the timer resumes counting. On edge case, when pwm==0 code stuck in while loop
Replace while loop with a smart logic that check current status of pwm
and reset counter/flags then start a new one

Co-authored-by: Jake Carter <[email protected]>
Signed-off-by: Sadik Ozer <[email protected]>
  • Loading branch information
ozersa and Jake-Carter committed Dec 12, 2023
1 parent 93236f9 commit 7e0a1d0
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Libraries/PeriphDrivers/Source/TMR/tmr_revb.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,29 @@ int MXC_TMR_RevB_SetPWM(mxc_tmr_revb_regs_t *tmr, uint32_t pwm)
return E_BAD_PARAM;
}

while (tmr->cnt >= pwm) {}
bool timera_is_running = tmr->ctrl0 & MXC_F_TMR_CTRL0_EN_A;
bool timerb_is_running = tmr->ctrl0 & MXC_F_TMR_CTRL0_EN_B;

if (timera_is_running || timerb_is_running) {
MXC_TMR_RevB_ClearFlags(tmr); // Clear flags so we can catch the next one
while (!MXC_TMR_RevB_GetFlags(tmr)) {} // Wait for next PWM transition
MXC_TMR_RevB_Stop(tmr); // Pause timer
MXC_TMR_RevB_SetCount(tmr, 0); // Reset the count
MXC_TMR_RevB_ClearFlags(
tmr); // Clear flags since app code wants the new PWM transitions set by this function
}

tmr->pwm = pwm;
while (!(tmr->intfl & MXC_F_TMR_REVB_INTFL_WRDONE_A)) {}

if (timera_is_running) {
tmr->ctrl0 |= MXC_F_TMR_REVB_CTRL0_EN_A; // Unpause A
}

if (timerb_is_running) {
tmr->ctrl0 |= MXC_F_TMR_REVB_CTRL0_EN_B; // Unpause B
}

return E_NO_ERROR;
}

Expand Down

0 comments on commit 7e0a1d0

Please sign in to comment.