Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
ba0383a8
Commit
ba0383a8
authored
Oct 04, 2014
by
Damien George
Browse files
stmhal, timer: Fix timer.chanel so mode can be a keyword.
parent
55f68b3c
Changes
1
Hide whitespace changes
Inline
Side-by-side
stmhal/timer.c
View file @
ba0383a8
...
...
@@ -690,22 +690,22 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
/// timer = pyb.Timer(2, freq=1000)
/// ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width=210000)
/// ch3 = timer.channel(3, pyb.Timer.PWM, pin=pyb.Pin.board.X3, pulse_width=420000)
STATIC
const
mp_arg_t
pyb_timer_channel_args
[]
=
{
{
MP_QSTR_callback
,
MP_ARG_KW_ONLY
|
MP_ARG_OBJ
,
{.
u_obj
=
mp_const_none
}
},
{
MP_QSTR_pin
,
MP_ARG_KW_ONLY
|
MP_ARG_OBJ
,
{.
u_obj
=
mp_const_none
}
},
{
MP_QSTR_pulse_width
,
MP_ARG_KW_ONLY
|
MP_ARG_INT
,
{.
u_int
=
0
}
},
{
MP_QSTR_pulse_width_percent
,
MP_ARG_KW_ONLY
|
MP_ARG_OBJ
,
{.
u_obj
=
mp_const_none
}
},
{
MP_QSTR_compare
,
MP_ARG_KW_ONLY
|
MP_ARG_INT
,
{.
u_int
=
0
}
},
{
MP_QSTR_polarity
,
MP_ARG_KW_ONLY
|
MP_ARG_INT
,
{.
u_int
=
0xffffffff
}
},
};
#define PYB_TIMER_CHANNEL_NUM_ARGS MP_ARRAY_SIZE(pyb_timer_channel_args)
STATIC
mp_obj_t
pyb_timer_channel
(
mp_uint_t
n_args
,
const
mp_obj_t
*
pos_args
,
mp_map_t
*
kw_args
)
{
static
const
mp_arg_t
allowed_args
[]
=
{
{
MP_QSTR_mode
,
MP_ARG_REQUIRED
|
MP_ARG_INT
,
{.
u_int
=
0
}
},
{
MP_QSTR_callback
,
MP_ARG_KW_ONLY
|
MP_ARG_OBJ
,
{.
u_obj
=
mp_const_none
}
},
{
MP_QSTR_pin
,
MP_ARG_KW_ONLY
|
MP_ARG_OBJ
,
{.
u_obj
=
mp_const_none
}
},
{
MP_QSTR_pulse_width
,
MP_ARG_KW_ONLY
|
MP_ARG_INT
,
{.
u_int
=
0
}
},
{
MP_QSTR_pulse_width_percent
,
MP_ARG_KW_ONLY
|
MP_ARG_OBJ
,
{.
u_obj
=
mp_const_none
}
},
{
MP_QSTR_compare
,
MP_ARG_KW_ONLY
|
MP_ARG_INT
,
{.
u_int
=
0
}
},
{
MP_QSTR_polarity
,
MP_ARG_KW_ONLY
|
MP_ARG_INT
,
{.
u_int
=
0xffffffff
}
},
};
STATIC
mp_obj_t
pyb_timer_channel
(
mp_uint_t
n_args
,
const
mp_obj_t
*
args
,
mp_map_t
*
kw_args
)
{
pyb_timer_obj_t
*
self
=
args
[
0
];
mp_int_t
channel
=
mp_obj_get_int
(
args
[
1
]);
pyb_timer_obj_t
*
self
=
pos_args
[
0
];
mp_int_t
channel
=
mp_obj_get_int
(
pos_args
[
1
]);
if
(
channel
<
1
||
channel
>
4
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
I
nvalid channel (%d)"
,
channel
));
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
i
nvalid channel (%d)"
,
channel
));
}
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
// If only the channel number is given return the previously allocated
// channel (or None if no previous channel).
if
(
n_args
==
2
)
{
if
(
n_args
==
2
&&
kw_args
->
used
==
0
)
{
if
(
chan
)
{
return
chan
;
}
...
...
@@ -744,18 +744,18 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
}
// Allocate and initialize a new channel
mp_arg_val_t
vals
[
PYB_TIMER_CHANNEL_NUM_ARGS
];
mp_arg_parse_all
(
n_args
-
3
,
args
+
3
,
kw_args
,
PYB_TIMER_CHANNEL_NUM_ARGS
,
pyb_timer_channel
_args
,
val
s
);
mp_arg_val_t
args
[
MP_ARRAY_SIZE
(
allowed_args
)
];
mp_arg_parse_all
(
n_args
-
2
,
pos_
args
+
2
,
kw_args
,
MP_ARRAY_SIZE
(
allowed_args
),
allowed
_args
,
arg
s
);
chan
=
m_new_obj
(
pyb_timer_channel_obj_t
);
memset
(
chan
,
0
,
sizeof
(
*
chan
));
chan
->
base
.
type
=
&
pyb_timer_channel_type
;
chan
->
timer
=
self
;
chan
->
channel
=
channel
;
chan
->
mode
=
mp_obj_get_int
(
args
[
2
])
;
chan
->
callback
=
val
s
[
0
].
u_obj
;
chan
->
mode
=
args
[
0
].
u_int
;
chan
->
callback
=
arg
s
[
1
].
u_obj
;
mp_obj_t
pin_obj
=
val
s
[
1
].
u_obj
;
mp_obj_t
pin_obj
=
arg
s
[
2
].
u_obj
;
if
(
pin_obj
!=
mp_const_none
)
{
if
(
!
MP_OBJ_IS_TYPE
(
pin_obj
,
&
pin_type
))
{
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
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
));
}
// pin.init(mode=AF_PP, af=idx)
const
mp_obj_t
args
[
6
]
=
{
const
mp_obj_t
args
2
[
6
]
=
{
(
mp_obj_t
)
&
pin_init_obj
,
pin_obj
,
MP_OBJ_NEW_QSTR
(
MP_QSTR_mode
),
MP_OBJ_NEW_SMALL_INT
(
GPIO_MODE_AF_PP
),
MP_OBJ_NEW_QSTR
(
MP_QSTR_af
),
MP_OBJ_NEW_SMALL_INT
(
af
->
idx
)
};
mp_call_method_n_kw
(
0
,
2
,
args
);
mp_call_method_n_kw
(
0
,
2
,
args
2
);
}
// 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
case
CHANNEL_MODE_PWM_INVERTED
:
{
TIM_OC_InitTypeDef
oc_config
;
oc_config
.
OCMode
=
channel_mode_info
[
chan
->
mode
].
oc_mode
;
if
(
val
s
[
3
].
u_obj
!=
mp_const_none
)
{
if
(
arg
s
[
4
].
u_obj
!=
mp_const_none
)
{
// pulse width percent given
uint32_t
period
=
compute_period
(
self
);
oc_config
.
Pulse
=
compute_pwm_value_from_percent
(
period
,
val
s
[
3
].
u_obj
);
oc_config
.
Pulse
=
compute_pwm_value_from_percent
(
period
,
arg
s
[
4
].
u_obj
);
}
else
{
// use absolute pulse width value (defaults to 0 if nothing given)
oc_config
.
Pulse
=
val
s
[
2
].
u_int
;
oc_config
.
Pulse
=
arg
s
[
3
].
u_int
;
}
oc_config
.
OCPolarity
=
TIM_OCPOLARITY_HIGH
;
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
case
CHANNEL_MODE_OC_FORCED_INACTIVE
:
{
TIM_OC_InitTypeDef
oc_config
;
oc_config
.
OCMode
=
channel_mode_info
[
chan
->
mode
].
oc_mode
;
oc_config
.
Pulse
=
val
s
[
4
].
u_int
;
oc_config
.
OCPolarity
=
val
s
[
5
].
u_int
;
oc_config
.
Pulse
=
arg
s
[
5
].
u_int
;
oc_config
.
OCPolarity
=
arg
s
[
6
].
u_int
;
if
(
oc_config
.
OCPolarity
==
0xffffffff
)
{
oc_config
.
OCPolarity
=
TIM_OCPOLARITY_HIGH
;
}
...
...
@@ -830,7 +830,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
oc_config
.
OCNIdleState
=
TIM_OCNIDLESTATE_SET
;
if
(
!
IS_TIM_OC_POLARITY
(
oc_config
.
OCPolarity
))
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
I
nvalid polarity (%d)"
,
oc_config
.
OCPolarity
));
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
i
nvalid polarity (%d)"
,
oc_config
.
OCPolarity
));
}
HAL_TIM_OC_ConfigChannel
(
&
self
->
tim
,
&
oc_config
,
TIMER_CHANNEL
(
chan
));
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
case
CHANNEL_MODE_IC
:
{
TIM_IC_InitTypeDef
ic_config
;
ic_config
.
ICPolarity
=
val
s
[
5
].
u_int
;
ic_config
.
ICPolarity
=
arg
s
[
6
].
u_int
;
if
(
ic_config
.
ICPolarity
==
0xffffffff
)
{
ic_config
.
ICPolarity
=
TIM_ICPOLARITY_RISING
;
}
...
...
@@ -853,7 +853,7 @@ STATIC mp_obj_t pyb_timer_channel(mp_uint_t n_args, const mp_obj_t *args, mp_map
ic_config
.
ICFilter
=
0
;
if
(
!
IS_TIM_IC_POLARITY
(
ic_config
.
ICPolarity
))
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
I
nvalid polarity (%d)"
,
ic_config
.
ICPolarity
));
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
i
nvalid polarity (%d)"
,
ic_config
.
ICPolarity
));
}
HAL_TIM_IC_ConfigChannel
(
&
self
->
tim
,
&
ic_config
,
TIMER_CHANNEL
(
chan
));
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
}
default:
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
I
nvalid mode (%d)"
,
chan
->
mode
));
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"
i
nvalid mode (%d)"
,
chan
->
mode
));
}
return
chan
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment