@@ -315,8 +315,9 @@ STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
315315/// Initialise the timer. Initialisation must give the desired mode
316316/// and an optional timer width
317317///
318+ /// tim.init(mode=Timer.ONE_SHOT, width=32) # one shot mode
318319/// tim.init(mode=Timer.PERIODIC) # configure in free running periodic mode
319- /// tim.init(mode=Timer.ONE_SHOT, width=16) # one shot mode splitted into two 16-bit independent timers
320+ /// split into two 16-bit independent timers
320321///
321322/// Keyword arguments:
322323///
@@ -326,7 +327,7 @@ STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
326327STATIC mp_obj_t pyb_timer_init_helper (pyb_timer_obj_t * tim , mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
327328 static const mp_arg_t allowed_args [] = {
328329 { MP_QSTR_mode , MP_ARG_REQUIRED | MP_ARG_INT , },
329- { MP_QSTR_width , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 32 } },
330+ { MP_QSTR_width , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 16 } },
330331 };
331332
332333 // parse args
@@ -405,7 +406,7 @@ STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) {
405406}
406407STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_timer_deinit_obj , pyb_timer_deinit );
407408
408- /// \method channel(channel, *, freq, polarity, duty_cycle)
409+ /// \method channel(channel, *, freq, period, polarity, duty_cycle)
409410/// Initialise the timer channel. Initialization requires at least a frequency param. With no
410411/// extra params given besides the channel id, the channel is returned with the previous configuration
411412/// os 'None', if it hasn't been initialized before.
@@ -735,7 +736,7 @@ STATIC mp_obj_t pyb_timer_channel_duty_cycle(mp_uint_t n_args, const mp_obj_t *a
735736}
736737STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pyb_timer_channel_duty_cycle_obj , 1 , 3 , pyb_timer_channel_duty_cycle );
737738
738- /// \method callback(handler, value, priority )
739+ /// \method callback(handler, priority, value )
739740/// create a callback object associated with the timer channel
740741STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
741742 mp_arg_val_t args [mpcallback_INIT_NUM_ARGS ];
@@ -753,10 +754,21 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
753754 goto invalid_args ;
754755 }
755756
757+ uint32_t _config = (ch -> channel == TIMER_B ) ? ((ch -> timer -> config & TIMER_B ) >> 8 ) : (ch -> timer -> config & TIMER_A );
758+
759+ // validate and set the value if we are in edge count mode
760+ if (_config == TIMER_CFG_A_CAP_COUNT ) {
761+ uint32_t c_value = args [3 ].u_int ;
762+ if (!c_value || c_value > 0xFFFF ) {
763+ // zero or exceeds the maximum value of a 16-bit timer
764+ goto invalid_args ;
765+ }
766+ MAP_TimerMatchSet (ch -> timer -> timer , ch -> channel , c_value );
767+ }
768+
756769 // disable the callback first
757770 pyb_timer_channel_callback_disable (ch );
758771
759- uint32_t _config = (ch -> channel == TIMER_B ) ? ((ch -> timer -> config & TIMER_B ) >> 8 ) : (ch -> timer -> config & TIMER_A );
760772 uint8_t shift = (ch -> channel == TIMER_B ) ? 8 : 0 ;
761773 switch (_config ) {
762774 case TIMER_CFG_A_ONE_SHOT :
@@ -778,13 +790,9 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
778790 default :
779791 break ;
780792 }
793+ // special case for a 32-bit timer
781794 if (ch -> channel == (TIMER_A | TIMER_B )) {
782- // again a special case for the pwm match interrupt
783- if (_config == TIMER_CFG_A_PWM ) {
784- ch -> timer -> intflags |= TIMER_TIMB_MATCH ;
785- } else {
786- ch -> timer -> intflags |= (ch -> timer -> intflags << 8 );
787- }
795+ ch -> timer -> intflags |= (ch -> timer -> intflags << 8 );
788796 }
789797
790798 void (* pfnHandler )(void );
@@ -835,6 +843,12 @@ STATIC mp_obj_t pyb_timer_channel_callback (mp_uint_t n_args, const mp_obj_t *po
835843 // create the callback
836844 _callback = mpcallback_new (ch , args [1 ].u_obj , & pyb_timer_channel_cb_methods );
837845
846+ // reload the timer
847+ uint32_t period_c ;
848+ uint32_t match ;
849+ compute_prescaler_period_and_match_value (ch , & period_c , & match );
850+ MAP_TimerLoadSet (ch -> timer -> timer , ch -> channel , period_c );
851+
838852 // enable the callback before returning
839853 pyb_timer_channel_callback_enable (ch );
840854 }
0 commit comments