|
|
@@ -44,8 +44,9 @@
|
|
|
typedef struct _machine_timer_obj_t {
|
|
|
mp_obj_base_t base;
|
|
|
rt_device_t timer_device;
|
|
|
+ char dev_name[RT_NAME_MAX];
|
|
|
mp_obj_t timeout_cb;
|
|
|
- uint8_t timerid;
|
|
|
+ int8_t timerid;
|
|
|
uint32_t timeout;
|
|
|
rt_bool_t is_repeat;
|
|
|
rt_bool_t is_init;
|
|
|
@@ -64,7 +65,11 @@ STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
|
|
|
|
|
|
mp_printf(print, "Timer(%p; ", self);
|
|
|
|
|
|
- mp_printf(print, "timerid=%d, ", self->timerid);
|
|
|
+ if (self->timerid >= 0) {
|
|
|
+ mp_printf(print, "timer_id=%d, ", self->timerid);
|
|
|
+ } else {
|
|
|
+ mp_printf(print, "timer_name=%s, ", self->dev_name);
|
|
|
+ }
|
|
|
mp_printf(print, "period=%d, ", self->timeout);
|
|
|
mp_printf(print, "auto_reload=%d)", self->is_repeat);
|
|
|
}
|
|
|
@@ -72,28 +77,39 @@ STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
|
|
|
STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
|
|
|
char timer_dev_name[RT_NAME_MAX] = {0};
|
|
|
- int device_id = 0;
|
|
|
|
|
|
// check arguments
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, true);
|
|
|
- device_id = mp_obj_get_int(args[0]);
|
|
|
+
|
|
|
+ // check input timer device name or ID
|
|
|
+ if (mp_obj_is_small_int(args[0])) {
|
|
|
+ int device_id = mp_obj_get_int(args[0]);
|
|
|
+ self->timerid = device_id;
|
|
|
+ self->timer_device->device_id = device_id;
|
|
|
+ rt_snprintf(timer_dev_name, sizeof(timer_dev_name), "timer%d", mp_obj_get_int(args[0]));
|
|
|
+ } else if (mp_obj_is_qstr(args[0])) {
|
|
|
+ static int device_id = 0;
|
|
|
+ self->timerid = -1;
|
|
|
+ self->timer_device->device_id = device_id++;
|
|
|
+ rt_strncpy(self->dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
|
|
|
+ rt_strncpy(timer_dev_name, self->dev_name, RT_NAME_MAX);
|
|
|
+ } else {
|
|
|
+ error_check(0, "Input ADC device name or ID error.");
|
|
|
+ }
|
|
|
|
|
|
// find timer device
|
|
|
- rt_snprintf(timer_dev_name, sizeof(timer_dev_name), "timer%d", device_id);
|
|
|
self->timer_device = rt_device_find(timer_dev_name);
|
|
|
if (self->timer_device == RT_NULL || self->timer_device->type != RT_Device_Class_Timer) {
|
|
|
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer(%s) don't exist", timer_dev_name));
|
|
|
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Timer(%s) don't exist", timer_dev_name));
|
|
|
}
|
|
|
|
|
|
// initialize timer device
|
|
|
self->base.type = &machine_timer_type;
|
|
|
- self->timerid = device_id;
|
|
|
self->timeout = 0;
|
|
|
self->timeout_cb = RT_NULL;
|
|
|
self->is_repeat = RT_TRUE;
|
|
|
self->is_init = RT_FALSE;
|
|
|
- self->timer_device->device_id = device_id-1;
|
|
|
-
|
|
|
+
|
|
|
// return constant object
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
}
|
|
|
@@ -162,7 +178,7 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
|
|
|
result = rt_device_open(self->timer_device, RT_DEVICE_OFLAG_RDWR);
|
|
|
error_check(result == RT_EOK, "Timer device open error");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (self->timeout_cb != RT_NULL) {
|
|
|
// set callback timer
|
|
|
if (timer_self[self->timer_device->device_id] && timer_self[self->timer_device->device_id] != self) {
|
|
|
@@ -174,7 +190,7 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
|
|
|
error_check(result == RT_EOK, "Timer set timout callback error");
|
|
|
}
|
|
|
|
|
|
- // set timer mode
|
|
|
+ // set timer mode
|
|
|
mode = self->is_repeat ? HWTIMER_MODE_PERIOD : HWTIMER_MODE_ONESHOT;
|
|
|
result = rt_device_control(self->timer_device, HWTIMER_CTRL_MODE_SET, &mode);
|
|
|
error_check(result == RT_EOK, "Timer set mode error");
|
|
|
@@ -191,7 +207,7 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
|
|
|
}
|
|
|
|
|
|
self->is_init = RT_TRUE;
|
|
|
-
|
|
|
+
|
|
|
return mp_const_none;
|
|
|
}
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
|
|
|
@@ -200,20 +216,20 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init)
|
|
|
STATIC mp_obj_t machine_timer_callback(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
machine_timer_obj_t *self = (machine_timer_obj_t *)args[0];
|
|
|
rt_bool_t result = RT_EOK;
|
|
|
-
|
|
|
+
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
{ MP_QSTR_callback, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
};
|
|
|
|
|
|
mp_arg_val_t dargs[MP_ARRAY_SIZE(allowed_args)];
|
|
|
mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, dargs);
|
|
|
-
|
|
|
+
|
|
|
self->timeout_cb = dargs[0].u_obj;
|
|
|
-
|
|
|
+
|
|
|
if(n_args == 1)
|
|
|
{
|
|
|
self->timeout_cb = RT_NULL;
|
|
|
- self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
|
|
|
+ self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
|
|
|
}
|
|
|
else if(n_args == 2)
|
|
|
{
|
|
|
@@ -226,7 +242,7 @@ STATIC mp_obj_t machine_timer_callback(mp_uint_t n_args, const mp_obj_t *args, m
|
|
|
else
|
|
|
{
|
|
|
self->timeout_cb = RT_NULL;
|
|
|
- self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
|
|
|
+ self->timer_device->rx_indicate = RT_NULL;//Log-off callback function
|
|
|
}
|
|
|
}
|
|
|
return mp_const_none;
|