@@ -349,7 +349,7 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *tim, mp_uint_t n_args, co
349349
350350 timer_init (tim );
351351 // register it with the sleep module
352- pybsleep_add ((const mp_obj_t )tim , (WakeUpCB_t )timer_channel_init );
352+ pybsleep_add ((const mp_obj_t )tim , (WakeUpCB_t )timer_init );
353353
354354 return mp_const_none ;
355355
@@ -431,7 +431,6 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
431431 if (!tim -> config ) {
432432 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_request_not_possible ));
433433 }
434-
435434 if (channel_n != TIMER_A && channel_n != TIMER_B && channel_n != (TIMER_A | TIMER_B )) {
436435 // invalid channel
437436 goto error ;
@@ -463,10 +462,6 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
463462 if ((tim -> config & TIMER_A ) == TIMER_CFG_A_PWM && args [1 ].u_int == (PYBTIMER_POLARITY_POS | PYBTIMER_POLARITY_NEG )) {
464463 goto error ;
465464 }
466- // check the range of the duty cycle
467- if (args [2 ].u_int < 0 || args [2 ].u_int > 100 ) {
468- goto error ;
469- }
470465
471466 // allocate a new timer channel
472467 pyb_timer_channel_obj_t * ch = m_new_obj (pyb_timer_channel_obj_t );
@@ -477,10 +472,13 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp
477472 // get the frequency the polarity and the duty cycle
478473 ch -> frequency = args [0 ].u_int ;
479474 ch -> polarity = args [1 ].u_int ;
480- ch -> duty_cycle = args [2 ].u_int ;
475+ ch -> duty_cycle = MIN ( 100 , MAX ( 0 , args [2 ].u_int )) ;
481476
482477 timer_channel_init (ch );
483478
479+ // register it with the sleep module
480+ pybsleep_add ((const mp_obj_t )ch , (WakeUpCB_t )timer_channel_init );
481+
484482 // add the timer to the list
485483 pyb_timer_channel_add (ch );
486484
@@ -687,7 +685,7 @@ STATIC mp_obj_t pyb_timer_channel_duty_cycle(mp_uint_t n_args, const mp_obj_t *a
687685 // calculate the period, the prescaler and the match value
688686 uint32_t period ;
689687 uint32_t match ;
690- ch -> duty_cycle = mp_obj_get_int (args [1 ]);
688+ ch -> duty_cycle = MIN ( 100 , MAX ( 0 , mp_obj_get_int (args [1 ])) );
691689 compute_prescaler_period_and_match_value (ch , & period , & match );
692690 if (n_args == 3 ) {
693691 // set the new polarity if requested
@@ -742,7 +740,7 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
742740 break ;
743741 }
744742 if (ch -> channel == (TIMER_A | TIMER_B )) {
745- // again a special case for the match interrupt
743+ // again a special case for the pwm match interrupt
746744 if (_config == TIMER_CFG_A_PWM ) {
747745 ch -> timer -> intflags |= TIMER_TIMB_MATCH ;
748746 } else {
@@ -798,11 +796,17 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
798796 // create the callback
799797 _callback = mpcallback_new (ch , args [1 ].u_obj , & pyb_timer_channel_cb_methods );
800798
799+ // get the value if given
800+ uint32_t c_value = MAX (0 , args [3 ].u_int );
801+ ch -> duty_cycle = MIN (100 , c_value );
802+
801803 // reload the timer
802804 uint32_t period ;
803805 uint32_t match ;
804806 compute_prescaler_period_and_match_value (ch , & period , & match );
805807 MAP_TimerLoadSet (ch -> timer -> timer , ch -> channel , period );
808+ // set the appropiate match value
809+ MAP_TimerMatchSet (ch -> timer -> timer , ch -> channel , (_config == TIMER_CFG_A_PWM ) ? match : c_value );
806810
807811 // enable the callback before returning
808812 pyb_timer_channel_callback_enable (ch );
0 commit comments