Skip to content

Commit ba0383a

Browse files
committed
stmhal, timer: Fix timer.chanel so mode can be a keyword.
1 parent 55f68b3 commit ba0383a

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

stmhal/timer.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -690,22 +690,22 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
690690
/// timer = pyb.Timer(2, freq=1000)
691691
/// ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=210000)
692692
/// ch3 = timer.channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=420000)
693-
STATIC const mp_arg_t pyb_timer_channel_args[] = {
694-
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
695-
{ MP_QSTR_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
696-
{ MP_QSTR_pulse_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
697-
{ MP_QSTR_pulse_width_percent, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
698-
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
699-
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
700-
};
701-
#define PYB_TIMER_CHANNEL_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_channel_args)
693+
STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
694+
static const mp_arg_t allowed_args[] = {
695+
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
696+
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
697+
{ MP_QSTR_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
698+
{ MP_QSTR_pulse_width, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
699+
{ MP_QSTR_pulse_width_percent, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
700+
{ MP_QSTR_compare, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
701+
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
702+
};
702703

703-
STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
704-
pyb_timer_obj_t *self = args[0];
705-
mp_int_t channel = mp_obj_get_int(args[1]);
704+
pyb_timer_obj_t *self = pos_args[0];
705+
mp_int_t channel = mp_obj_get_int(pos_args[1]);
706706

707707
if (channel < 1 || channel > 4) {
708-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid channel (%d)", channel));
708+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid channel (%d)", channel));
709709
}
710710

711711
pyb_timer_channel_obj_t *chan = self->channel;
@@ -721,7 +721,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
721721

722722
// If only the channel number is given return the previously allocated
723723
// channel (or None if no previous channel).
724-
if (n_args == 2) {
724+
if (n_args == 2 && kw_args->used == 0) {
725725
if (chan) {
726726
return chan;
727727
}
@@ -744,18 +744,18 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
744744
}
745745

746746
// Allocate and initialize a new channel
747-
mp_arg_val_t vals[PYB_TIMER_CHANNEL_NUM_ARGS];
748-
mp_arg_parse_all(n_args - 3, args + 3, kw_args, PYB_TIMER_CHANNEL_NUM_ARGS, pyb_timer_channel_args, vals);
747+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
748+
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
749749

750750
chan = m_new_obj(pyb_timer_channel_obj_t);
751751
memset(chan, 0, sizeof(*chan));
752752
chan->base.type = &pyb_timer_channel_type;
753753
chan->timer = self;
754754
chan->channel = channel;
755-
chan->mode = mp_obj_get_int(args[2]);
756-
chan->callback = vals[0].u_obj;
755+
chan->mode = args[0].u_int;
756+
chan->callback = args[1].u_obj;
757757

758-
mp_obj_t pin_obj = vals[1].u_obj;
758+
mp_obj_t pin_obj = args[2].u_obj;
759759
if (pin_obj != mp_const_none) {
760760
if (!MP_OBJ_IS_TYPE(pin_obj, &pin_type)) {
761761
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "pin argument needs to be be a Pin type"));
@@ -766,13 +766,13 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
766766
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %s doesn't have an af for TIM%d", qstr_str(pin->name), self->tim_id));
767767
}
768768
// pin.init(mode=AF_PP, af=idx)
769-
const mp_obj_t args[6] = {
769+
const mp_obj_t args2[6] = {
770770
(mp_obj_t)&pin_init_obj,
771771
pin_obj,
772772
MP_OBJ_NEW_QSTR(MP_QSTR_mode), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_AF_PP),
773773
MP_OBJ_NEW_QSTR(MP_QSTR_af), MP_OBJ_NEW_SMALL_INT(af->idx)
774774
};
775-
mp_call_method_n_kw(0, 2, args);
775+
mp_call_method_n_kw(0, 2, args2);
776776
}
777777

778778
// Link the channel to the timer before we turn the channel on.
@@ -788,13 +788,13 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
788788
case CHANNEL_MODE_PWM_INVERTED: {
789789
TIM_OC_InitTypeDef oc_config;
790790
oc_config.OCMode = channel_mode_info[chan->mode].oc_mode;
791-
if (vals[3].u_obj != mp_const_none) {
791+
if (args[4].u_obj != mp_const_none) {
792792
// pulse width percent given
793793
uint32_t period = compute_period(self);
794-
oc_config.Pulse = compute_pwm_value_from_percent(period, vals[3].u_obj);
794+
oc_config.Pulse = compute_pwm_value_from_percent(period, args[4].u_obj);
795795
} else {
796796
// use absolute pulse width value (defaults to 0 if nothing given)
797-
oc_config.Pulse = vals[2].u_int;
797+
oc_config.Pulse = args[3].u_int;
798798
}
799799
oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;
800800
oc_config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
@@ -819,8 +819,8 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
819819
case CHANNEL_MODE_OC_FORCED_INACTIVE: {
820820
TIM_OC_InitTypeDef oc_config;
821821
oc_config.OCMode = channel_mode_info[chan->mode].oc_mode;
822-
oc_config.Pulse = vals[4].u_int;
823-
oc_config.OCPolarity = vals[5].u_int;
822+
oc_config.Pulse = args[5].u_int;
823+
oc_config.OCPolarity = args[6].u_int;
824824
if (oc_config.OCPolarity == 0xffffffff) {
825825
oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;
826826
}
@@ -830,7 +830,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
830830
oc_config.OCNIdleState = TIM_OCNIDLESTATE_SET;
831831

832832
if (!IS_TIM_OC_POLARITY(oc_config.OCPolarity)) {
833-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid polarity (%d)", oc_config.OCPolarity));
833+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid polarity (%d)", oc_config.OCPolarity));
834834
}
835835
HAL_TIM_OC_ConfigChannel(&self->tim, &oc_config, TIMER_CHANNEL(chan));
836836
if (chan->callback == mp_const_none) {
@@ -844,7 +844,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
844844
case CHANNEL_MODE_IC: {
845845
TIM_IC_InitTypeDef ic_config;
846846

847-
ic_config.ICPolarity = vals[5].u_int;
847+
ic_config.ICPolarity = args[6].u_int;
848848
if (ic_config.ICPolarity == 0xffffffff) {
849849
ic_config.ICPolarity = TIM_ICPOLARITY_RISING;
850850
}
@@ -853,7 +853,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
853853
ic_config.ICFilter = 0;
854854

855855
if (!IS_TIM_IC_POLARITY(ic_config.ICPolarity)) {
856-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid polarity (%d)", ic_config.ICPolarity));
856+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid polarity (%d)", ic_config.ICPolarity));
857857
}
858858
HAL_TIM_IC_ConfigChannel(&self->tim, &ic_config, TIMER_CHANNEL(chan));
859859
if (chan->callback == mp_const_none) {
@@ -865,7 +865,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
865865
}
866866

867867
default:
868-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Invalid mode (%d)", chan->mode));
868+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid mode (%d)", chan->mode));
869869
}
870870

871871
return chan;

0 commit comments

Comments
 (0)