Skip to content

Commit 9c8d263

Browse files
committed
Remove noise when duty is 0% and 100%
1 parent 09df3eb commit 9c8d263

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

targets/TARGET_RENESAS/TARGET_RZ_A1XX/pwmout_api.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ void pwmout_write(pwmout_t* obj, float value) {
218218
#ifdef FUMC_MTU2_PWM
219219
/* PWM by MTU2 */
220220
st_mtu2_ctrl_t * p_mtu2_ctl = &mtu2_ctl[(int)(obj->pwm - MTU2_PWM_OFFSET)];
221+
uint32_t wk_pulse;
222+
uint8_t tmp_tstr_st;
221223

222224
if (value < 0.0f) {
223225
value = 0.0f;
@@ -227,12 +229,35 @@ void pwmout_write(pwmout_t* obj, float value) {
227229
// Do Nothing
228230
}
229231
wk_cycle = (uint32_t)*p_mtu2_ctl->period1;
232+
wk_pulse = (uint32_t)*p_mtu2_ctl->pulse1;
233+
if ((obj->ch == 4) || (obj->ch == 3)) {
234+
tmp_tstr_st = (1 << (obj->ch + 3));
235+
} else {
236+
tmp_tstr_st = (1 << obj->ch);
237+
}
230238

231239
// set channel match to percentage
232240
if (value == 1.0f) {
233-
*p_mtu2_ctl->pulse1 = (uint16_t)(wk_cycle - 1);
241+
if (wk_pulse != wk_cycle) {
242+
MTU2TSTR &= ~tmp_tstr_st;
243+
*p_mtu2_ctl->tior = 0x66;
244+
}
245+
} else if (value == 0.0f) {
246+
if (wk_pulse != 0) {
247+
MTU2TSTR &= ~tmp_tstr_st;
248+
*p_mtu2_ctl->tior = 0x11;
249+
}
234250
} else {
235-
*p_mtu2_ctl->pulse1 = (uint16_t)((float)wk_cycle * value);
251+
if ((wk_pulse == wk_cycle) || (wk_pulse == 0)) {
252+
MTU2TSTR &= ~tmp_tstr_st;
253+
*p_mtu2_ctl->tior = 0x65;
254+
}
255+
}
256+
*p_mtu2_ctl->pulse1 = (uint16_t)((float)wk_cycle * value);
257+
258+
// Counter Restart
259+
if ((MTU2TSTR & tmp_tstr_st) == 0) {
260+
MTU2TSTR |= tmp_tstr_st;
236261
}
237262
#endif
238263
} else {
@@ -378,7 +403,6 @@ void pwmout_period_us(pwmout_t* obj, int us) {
378403
MTU2TSTR &= ~tmp_tstr_st;
379404
wk_last_cycle = *p_mtu2_ctl->period1;
380405
*p_mtu2_ctl->tcr = tmp_tcr_up | wk_cks;
381-
*p_mtu2_ctl->tior = 0x65;
382406
// Set period
383407
*p_mtu2_ctl->period1 = (uint16_t)wk_cycle;
384408
if (p_mtu2_ctl->period2 != NULL) {

0 commit comments

Comments
 (0)