@@ -276,8 +276,8 @@ STATIC void pyb_timer_print(void (*print)(void *env, const char *fmt, ...), void
276
276
} else {
277
277
print (env , "Timer(%u, prescaler=%u, period=%u, mode=%s, div=%u)" ,
278
278
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 ) ,
281
281
self -> tim .Init .CounterMode == TIM_COUNTERMODE_UP ? "UP" :
282
282
self -> tim .Init .CounterMode == TIM_COUNTERMODE_DOWN ? "DOWN" : "CENTER" ,
283
283
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);
543
543
/// Keyword arguments for Timer.PWM modes:
544
544
///
545
545
/// - `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.
547
547
///
548
548
/// Keyword arguments for Timer.OC modes:
549
549
///
@@ -566,12 +566,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
566
566
/// ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=210000)
567
567
/// ch3 = timer.channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=420000)
568
568
STATIC 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 } },
575
575
};
576
576
#define PYB_TIMER_CHANNEL_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_channel_args)
577
577
@@ -667,9 +667,17 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
667
667
// absolute pulse width value given
668
668
oc_config .Pulse = vals [2 ].u_int ;
669
669
} else if (vals [3 ].u_obj != mp_const_none ) {
670
- // pulse width ratio given
670
+ // pulse width percent given
671
671
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
+ }
673
681
if (cmp < 0 ) {
674
682
cmp = 0 ;
675
683
} else if (cmp > period ) {
@@ -891,9 +899,6 @@ STATIC void pyb_timer_channel_print(void (*print)(void *env, const char *fmt, ..
891
899
/// pulse_width is the logical name to use when the channel is in PWM mode.
892
900
STATIC mp_obj_t pyb_timer_channel_capture_compare (mp_uint_t n_args , const mp_obj_t * args ) {
893
901
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
- }
897
902
if (n_args == 1 ) {
898
903
// get
899
904
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
910
915
/// a floating-point number between 0.0 and 1.0, and is relative to the period
911
916
/// of the timer associated with this channel. For example, a ratio of 0.5
912
917
/// 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 ) {
914
919
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
- }
918
920
uint32_t period = (__HAL_TIM_GetAutoreload (& self -> timer -> tim ) & TIMER_CNT_MASK (self -> timer )) + 1 ;
919
921
if (n_args == 1 ) {
920
922
// get
921
923
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
923
929
} else {
924
930
// 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
+ }
926
940
if (cmp < 0 ) {
927
941
cmp = 0 ;
928
942
} 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
932
946
return mp_const_none ;
933
947
}
934
948
}
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 );
936
950
937
951
/// \method callback(fun)
938
952
/// 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[] = {
976
990
// instance methods
977
991
{ MP_OBJ_NEW_QSTR (MP_QSTR_callback ), (mp_obj_t )& pyb_timer_channel_callback_obj },
978
992
{ 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 },
980
994
{ MP_OBJ_NEW_QSTR (MP_QSTR_capture ), (mp_obj_t )& pyb_timer_channel_capture_compare_obj },
981
995
{ MP_OBJ_NEW_QSTR (MP_QSTR_compare ), (mp_obj_t )& pyb_timer_channel_capture_compare_obj },
982
996
};
0 commit comments