4444typedef struct _machine_timer_obj_t {
4545 mp_obj_base_t base ;
4646 rt_device_t timer_device ;
47+ char dev_name [RT_NAME_MAX ];
4748 mp_obj_t timeout_cb ;
48- uint8_t timerid ;
49+ int8_t timerid ;
4950 uint32_t timeout ;
5051 rt_bool_t is_repeat ;
5152 rt_bool_t is_init ;
@@ -64,36 +65,51 @@ STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
6465
6566 mp_printf (print , "Timer(%p; " , self );
6667
67- mp_printf (print , "timerid=%d, " , self -> timerid );
68+ if (self -> timerid >= 0 ) {
69+ mp_printf (print , "timer_id=%d, " , self -> timerid );
70+ } else {
71+ mp_printf (print , "timer_name=%s, " , self -> dev_name );
72+ }
6873 mp_printf (print , "period=%d, " , self -> timeout );
6974 mp_printf (print , "auto_reload=%d)" , self -> is_repeat );
7075}
7176
7277STATIC 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 ) {
7378 machine_timer_obj_t * self = m_new_obj (machine_timer_obj_t );
7479 char timer_dev_name [RT_NAME_MAX ] = {0 };
75- int device_id = 0 ;
7680
7781 // check arguments
7882 mp_arg_check_num (n_args , n_kw , 1 , 1 , true);
79- device_id = mp_obj_get_int (args [0 ]);
83+
84+ // check input timer device name or ID
85+ if (mp_obj_is_small_int (args [0 ])) {
86+ int device_id = mp_obj_get_int (args [0 ]);
87+ self -> timerid = device_id ;
88+ self -> timer_device -> device_id = device_id ;
89+ rt_snprintf (timer_dev_name , sizeof (timer_dev_name ), "timer%d" , mp_obj_get_int (args [0 ]));
90+ } else if (mp_obj_is_qstr (args [0 ])) {
91+ static int device_id = 0 ;
92+ self -> timerid = -1 ;
93+ self -> timer_device -> device_id = device_id ++ ;
94+ rt_strncpy (self -> dev_name , mp_obj_str_get_str (args [0 ]), RT_NAME_MAX );
95+ rt_strncpy (timer_dev_name , self -> dev_name , RT_NAME_MAX );
96+ } else {
97+ error_check (0 , "Input ADC device name or ID error." );
98+ }
8099
81100 // find timer device
82- rt_snprintf (timer_dev_name , sizeof (timer_dev_name ), "timer%d" , device_id );
83101 self -> timer_device = rt_device_find (timer_dev_name );
84102 if (self -> timer_device == RT_NULL || self -> timer_device -> type != RT_Device_Class_Timer ) {
85- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "Timer(%s) don't exist" , timer_dev_name ));
103+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "Timer(%s) don't exist" , timer_dev_name ));
86104 }
87105
88106 // initialize timer device
89107 self -> base .type = & machine_timer_type ;
90- self -> timerid = device_id ;
91108 self -> timeout = 0 ;
92109 self -> timeout_cb = RT_NULL ;
93110 self -> is_repeat = RT_TRUE ;
94111 self -> is_init = RT_FALSE ;
95- self -> timer_device -> device_id = device_id - 1 ;
96-
112+
97113 // return constant object
98114 return MP_OBJ_FROM_PTR (self );
99115}
@@ -162,7 +178,7 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
162178 result = rt_device_open (self -> timer_device , RT_DEVICE_OFLAG_RDWR );
163179 error_check (result == RT_EOK , "Timer device open error" );
164180 }
165-
181+
166182 if (self -> timeout_cb != RT_NULL ) {
167183 // set callback timer
168184 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
174190 error_check (result == RT_EOK , "Timer set timout callback error" );
175191 }
176192
177- // set timer mode
193+ // set timer mode
178194 mode = self -> is_repeat ? HWTIMER_MODE_PERIOD : HWTIMER_MODE_ONESHOT ;
179195 result = rt_device_control (self -> timer_device , HWTIMER_CTRL_MODE_SET , & mode );
180196 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
191207 }
192208
193209 self -> is_init = RT_TRUE ;
194-
210+
195211 return mp_const_none ;
196212}
197213STATIC 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)
200216STATIC mp_obj_t machine_timer_callback (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
201217 machine_timer_obj_t * self = (machine_timer_obj_t * )args [0 ];
202218 rt_bool_t result = RT_EOK ;
203-
219+
204220 static const mp_arg_t allowed_args [] = {
205221 { MP_QSTR_callback , MP_ARG_OBJ , {.u_obj = mp_const_none } },
206222 };
207223
208224 mp_arg_val_t dargs [MP_ARRAY_SIZE (allowed_args )];
209225 mp_arg_parse_all (n_args - 1 , args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , dargs );
210-
226+
211227 self -> timeout_cb = dargs [0 ].u_obj ;
212-
228+
213229 if (n_args == 1 )
214230 {
215231 self -> timeout_cb = RT_NULL ;
216- self -> timer_device -> rx_indicate = RT_NULL ;//Log-off callback function
232+ self -> timer_device -> rx_indicate = RT_NULL ;//Log-off callback function
217233 }
218234 else if (n_args == 2 )
219235 {
@@ -226,7 +242,7 @@ STATIC mp_obj_t machine_timer_callback(mp_uint_t n_args, const mp_obj_t *args, m
226242 else
227243 {
228244 self -> timeout_cb = RT_NULL ;
229- self -> timer_device -> rx_indicate = RT_NULL ;//Log-off callback function
245+ self -> timer_device -> rx_indicate = RT_NULL ;//Log-off callback function
230246 }
231247 }
232248 return mp_const_none ;
0 commit comments