@@ -276,8 +276,8 @@ STATIC void pyb_timer_print(void (*print)(void *env, const char *fmt, ...), void
276276 } else {
277277 print (env , "Timer(%u, prescaler=%u, period=%u, mode=%s, div=%u)" ,
278278 self -> tim_id ,
279- self -> tim .Init . Prescaler ,
280- self -> tim . Init . Period ,
279+ self -> tim .Instance -> PSC & 0xffff ,
280+ __HAL_TIM_GetAutoreload ( & self -> tim ) & TIMER_CNT_MASK ( self ) ,
281281 self -> tim .Init .CounterMode == TIM_COUNTERMODE_UP ? "UP" :
282282 self -> tim .Init .CounterMode == TIM_COUNTERMODE_DOWN ? "DOWN" : "CENTER" ,
283283 self -> tim .Init .ClockDivision == TIM_CLOCKDIVISION_DIV4 ? 4 :
@@ -543,7 +543,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
543543/// Keyword arguments for Timer.PWM modes:
544544///
545545/// - `pulse_width` - determines the initial pulse width value to use.
546- /// - `pulse_width_ratio ` - determines the initial pulse width ratio to use.
546+ /// - `pulse_width_percent ` - determines the initial pulse width percentage to use.
547547///
548548/// Keyword arguments for Timer.OC modes:
549549///
@@ -566,12 +566,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
566566/// ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=210000)
567567/// ch3 = timer.channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=420000)
568568STATIC const mp_arg_t pyb_timer_channel_args [] = {
569- { MP_QSTR_callback , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
570- { MP_QSTR_pin , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
571- { MP_QSTR_pulse_width , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0xffffffff } },
572- { MP_QSTR_pulse_width_ratio , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
573- { MP_QSTR_compare , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
574- { MP_QSTR_polarity , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0xffffffff } },
569+ { MP_QSTR_callback , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
570+ { MP_QSTR_pin , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
571+ { MP_QSTR_pulse_width , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0xffffffff } },
572+ { MP_QSTR_pulse_width_percent , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
573+ { MP_QSTR_compare , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
574+ { MP_QSTR_polarity , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0xffffffff } },
575575};
576576#define PYB_TIMER_CHANNEL_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_channel_args)
577577
@@ -667,9 +667,17 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
667667 // absolute pulse width value given
668668 oc_config .Pulse = vals [2 ].u_int ;
669669 } else if (vals [3 ].u_obj != mp_const_none ) {
670- // pulse width ratio given
670+ // pulse width percent given
671671 uint32_t period = (__HAL_TIM_GetAutoreload (& self -> tim ) & TIMER_CNT_MASK (self )) + 1 ;
672- uint32_t cmp = mp_obj_get_float (vals [3 ].u_obj ) * period ;
672+ uint32_t cmp ;
673+ #if MICROPY_PY_BUILTINS_FLOAT
674+ if (MP_OBJ_IS_TYPE (vals [3 ].u_obj , & mp_type_float )) {
675+ cmp = mp_obj_get_float (vals [3 ].u_obj ) * period / 100.0 ;
676+ } else
677+ #endif
678+ {
679+ cmp = mp_obj_get_int (vals [3 ].u_obj ) * period / 100 ;
680+ }
673681 if (cmp < 0 ) {
674682 cmp = 0 ;
675683 } else if (cmp > period ) {
@@ -891,9 +899,6 @@ STATIC void pyb_timer_channel_print(void (*print)(void *env, const char *fmt, ..
891899/// pulse_width is the logical name to use when the channel is in PWM mode.
892900STATIC mp_obj_t pyb_timer_channel_capture_compare (mp_uint_t n_args , const mp_obj_t * args ) {
893901 pyb_timer_channel_obj_t * self = args [0 ];
894- if (self -> channel == 0 ) {
895- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "Timer %d no channel specified" , self -> timer -> tim_id ));
896- }
897902 if (n_args == 1 ) {
898903 // get
899904 return mp_obj_new_int (__HAL_TIM_GetCompare (& self -> timer -> tim , TIMER_CHANNEL (self )) & TIMER_CNT_MASK (self -> timer ));
@@ -910,19 +915,28 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_channel_capture_compare_obj
910915/// a floating-point number between 0.0 and 1.0, and is relative to the period
911916/// of the timer associated with this channel. For example, a ratio of 0.5
912917/// would be a 50% duty cycle.
913- STATIC mp_obj_t pyb_timer_channel_pulse_width_ratio (mp_uint_t n_args , const mp_obj_t * args ) {
918+ STATIC mp_obj_t pyb_timer_channel_pulse_width_percent (mp_uint_t n_args , const mp_obj_t * args ) {
914919 pyb_timer_channel_obj_t * self = args [0 ];
915- if (self -> channel == 0 ) {
916- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "Timer %d no channel specified" , self -> timer -> tim_id ));
917- }
918920 uint32_t period = (__HAL_TIM_GetAutoreload (& self -> timer -> tim ) & TIMER_CNT_MASK (self -> timer )) + 1 ;
919921 if (n_args == 1 ) {
920922 // get
921923 uint32_t cmp = __HAL_TIM_GetCompare (& self -> timer -> tim , TIMER_CHANNEL (self )) & TIMER_CNT_MASK (self -> timer );
922- return mp_obj_new_float ((float )cmp / (float )period );
924+ #if MICROPY_PY_BUILTINS_FLOAT
925+ return mp_obj_new_float ((float )cmp * 100.0 / (float )period );
926+ #else
927+ return mp_obj_new_int (cmp * 100 / period );
928+ #endif
923929 } else {
924930 // set
925- uint32_t cmp = mp_obj_get_float (args [1 ]) * period ;
931+ uint32_t cmp ;
932+ #if MICROPY_PY_BUILTINS_FLOAT
933+ if (MP_OBJ_IS_TYPE (args [1 ], & mp_type_float )) {
934+ cmp = mp_obj_get_float (args [1 ]) * period / 100.0 ;
935+ } else
936+ #endif
937+ {
938+ cmp = mp_obj_get_int (args [1 ]) * period / 100 ;
939+ }
926940 if (cmp < 0 ) {
927941 cmp = 0 ;
928942 } else if (cmp > period ) {
@@ -932,7 +946,7 @@ STATIC mp_obj_t pyb_timer_channel_pulse_width_ratio(mp_uint_t n_args, const mp_o
932946 return mp_const_none ;
933947 }
934948}
935- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pyb_timer_channel_pulse_width_ratio_obj , 1 , 2 , pyb_timer_channel_pulse_width_ratio );
949+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pyb_timer_channel_pulse_width_percent_obj , 1 , 2 , pyb_timer_channel_pulse_width_percent );
936950
937951/// \method callback(fun)
938952/// Set the function to be called when the timer channel triggers.
@@ -976,7 +990,7 @@ STATIC const mp_map_elem_t pyb_timer_channel_locals_dict_table[] = {
976990 // instance methods
977991 { MP_OBJ_NEW_QSTR (MP_QSTR_callback ), (mp_obj_t )& pyb_timer_channel_callback_obj },
978992 { MP_OBJ_NEW_QSTR (MP_QSTR_pulse_width ), (mp_obj_t )& pyb_timer_channel_capture_compare_obj },
979- { MP_OBJ_NEW_QSTR (MP_QSTR_pulse_width_ratio ), (mp_obj_t )& pyb_timer_channel_pulse_width_ratio_obj },
993+ { MP_OBJ_NEW_QSTR (MP_QSTR_pulse_width_percent ), (mp_obj_t )& pyb_timer_channel_pulse_width_percent_obj },
980994 { MP_OBJ_NEW_QSTR (MP_QSTR_capture ), (mp_obj_t )& pyb_timer_channel_capture_compare_obj },
981995 { MP_OBJ_NEW_QSTR (MP_QSTR_compare ), (mp_obj_t )& pyb_timer_channel_capture_compare_obj },
982996};
0 commit comments