@@ -112,6 +112,12 @@ STATIC const struct {
112112 { MP_QSTR_ENC_AB , TIM_ENCODERMODE_TI12 },
113113};
114114
115+ enum {
116+ BRK_OFF ,
117+ BRK_LOW ,
118+ BRK_HIGH ,
119+ };
120+
115121typedef struct _pyb_timer_channel_obj_t {
116122 mp_obj_base_t base ;
117123 struct _pyb_timer_obj_t * timer ;
@@ -462,14 +468,14 @@ STATIC mp_int_t compute_ticks_from_dtg(uint32_t dtg) {
462468 return 512 + ((dtg & 0x1F ) * 16 );
463469}
464470
465- STATIC void config_deadtime (pyb_timer_obj_t * self , mp_int_t ticks ) {
471+ STATIC void config_deadtime (pyb_timer_obj_t * self , mp_int_t ticks , mp_int_t brk ) {
466472 TIM_BreakDeadTimeConfigTypeDef deadTimeConfig ;
467473 deadTimeConfig .OffStateRunMode = TIM_OSSR_DISABLE ;
468474 deadTimeConfig .OffStateIDLEMode = TIM_OSSI_DISABLE ;
469475 deadTimeConfig .LockLevel = TIM_LOCKLEVEL_OFF ;
470476 deadTimeConfig .DeadTime = compute_dtg_from_ticks (ticks );
471- deadTimeConfig .BreakState = TIM_BREAK_DISABLE ;
472- deadTimeConfig .BreakPolarity = TIM_BREAKPOLARITY_LOW ;
477+ deadTimeConfig .BreakState = brk == BRK_OFF ? TIM_BREAK_DISABLE : TIM_BREAK_ENABLE ;
478+ deadTimeConfig .BreakPolarity = brk == BRK_LOW ? TIM_BREAKPOLARITY_LOW : TIM_BREAKPOLARITY_HIGH ;
473479 deadTimeConfig .AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE ;
474480 HAL_TIMEx_ConfigBreakDeadTime (& self -> tim , & deadTimeConfig );
475481}
@@ -512,6 +518,12 @@ STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
512518 {
513519 mp_printf (print , ", deadtime=%u" ,
514520 compute_ticks_from_dtg (self -> tim .Instance -> BDTR & TIM_BDTR_DTG ));
521+ if ((self -> tim .Instance -> BDTR & TIM_BDTR_BKE ) == TIM_BDTR_BKE ) {
522+ mp_printf (print , ", brk=%s" ,
523+ ((self -> tim .Instance -> BDTR & TIM_BDTR_BKP ) == TIM_BDTR_BKP ) ? "BRK_HIGH" : "BRK_LOW" );
524+ } else {
525+ mp_printf (print , ", brk=BRK_OFF" );
526+ }
515527 }
516528 mp_print_str (print , ")" );
517529 }
@@ -561,9 +573,15 @@ STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
561573/// measures ticks of `source_freq` divided by `div` clock ticks.
562574/// `deadtime` is only available on timers 1 and 8.
563575///
576+ /// - `brk` - specifies if the break mode is used to kill the output of
577+ /// the PWM when the BRK_IN input is asserted. The polarity set how the
578+ /// BRK_IN input is triggered. It can be set to `BRK_OFF`, `BRK_LOW`
579+ /// and `BRK_HIGH`.
580+ ///
581+ ///
564582/// You must either specify freq or both of period and prescaler.
565583STATIC mp_obj_t pyb_timer_init_helper (pyb_timer_obj_t * self , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
566- enum { ARG_freq , ARG_prescaler , ARG_period , ARG_tick_hz , ARG_mode , ARG_div , ARG_callback , ARG_deadtime };
584+ enum { ARG_freq , ARG_prescaler , ARG_period , ARG_tick_hz , ARG_mode , ARG_div , ARG_callback , ARG_deadtime , ARG_brk };
567585 static const mp_arg_t allowed_args [] = {
568586 { MP_QSTR_freq , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_PTR (& mp_const_none_obj )} },
569587 { MP_QSTR_prescaler , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0xffffffff } },
@@ -573,6 +591,7 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons
573591 { MP_QSTR_div , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 1 } },
574592 { MP_QSTR_callback , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_PTR (& mp_const_none_obj )} },
575593 { MP_QSTR_deadtime , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
594+ { MP_QSTR_brk , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = BRK_OFF } },
576595 };
577596
578597 // parse args
@@ -679,7 +698,8 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons
679698 #else
680699 if (0 ) {
681700 #endif
682- config_deadtime (self , args [ARG_deadtime ].u_int );
701+ config_deadtime (self , args [ARG_deadtime ].u_int , args [ARG_brk ].u_int );
702+
683703 }
684704
685705 // Enable ARPE so that the auto-reload register is buffered.
@@ -1307,6 +1327,9 @@ STATIC const mp_rom_map_elem_t pyb_timer_locals_dict_table[] = {
13071327 { MP_ROM_QSTR (MP_QSTR_RISING ), MP_ROM_INT (TIM_ICPOLARITY_RISING ) },
13081328 { MP_ROM_QSTR (MP_QSTR_FALLING ), MP_ROM_INT (TIM_ICPOLARITY_FALLING ) },
13091329 { MP_ROM_QSTR (MP_QSTR_BOTH ), MP_ROM_INT (TIM_ICPOLARITY_BOTHEDGE ) },
1330+ { MP_ROM_QSTR (MP_QSTR_BRK_OFF ), MP_ROM_INT (BRK_OFF ) },
1331+ { MP_ROM_QSTR (MP_QSTR_BRK_LOW ), MP_ROM_INT (BRK_LOW ) },
1332+ { MP_ROM_QSTR (MP_QSTR_BRK_HIGH ), MP_ROM_INT (BRK_HIGH ) },
13101333};
13111334STATIC MP_DEFINE_CONST_DICT (pyb_timer_locals_dict , pyb_timer_locals_dict_table );
13121335
0 commit comments