@@ -218,6 +218,8 @@ void pwmout_write(pwmout_t* obj, float value) {
218
218
#ifdef FUMC_MTU2_PWM
219
219
/* PWM by MTU2 */
220
220
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 ;
221
223
222
224
if (value < 0.0f ) {
223
225
value = 0.0f ;
@@ -227,12 +229,35 @@ void pwmout_write(pwmout_t* obj, float value) {
227
229
// Do Nothing
228
230
}
229
231
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
+ }
230
238
231
239
// set channel match to percentage
232
240
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
+ }
234
250
} 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 ;
236
261
}
237
262
#endif
238
263
} else {
@@ -378,7 +403,6 @@ void pwmout_period_us(pwmout_t* obj, int us) {
378
403
MTU2TSTR &= ~tmp_tstr_st ;
379
404
wk_last_cycle = * p_mtu2_ctl -> period1 ;
380
405
* p_mtu2_ctl -> tcr = tmp_tcr_up | wk_cks ;
381
- * p_mtu2_ctl -> tior = 0x65 ;
382
406
// Set period
383
407
* p_mtu2_ctl -> period1 = (uint16_t )wk_cycle ;
384
408
if (p_mtu2_ctl -> period2 != NULL ) {
0 commit comments