3939#include <drivers/hwtimer.h>
4040#include "machine_timer.h"
4141
42+ #define MAX_TIMER 17
43+
4244typedef struct _machine_timer_obj_t {
4345 mp_obj_base_t base ;
4446 rt_device_t timer_device ;
@@ -90,12 +92,13 @@ STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args,
9092 self -> timeout_cb = RT_NULL ;
9193 self -> is_repeat = RT_TRUE ;
9294 self -> is_init = RT_FALSE ;
95+ self -> timer_device -> device_id = device_id - 1 ;
9396
9497 // return constant object
9598 return MP_OBJ_FROM_PTR (self );
9699}
97100
98- static machine_timer_obj_t * timer_self = RT_NULL ;
101+ static machine_timer_obj_t * timer_self [ MAX_TIMER ] = { RT_NULL } ;
99102
100103STATIC mp_obj_t machine_timer_deinit (mp_obj_t self_in ) {
101104 machine_timer_obj_t * self = self_in ;
@@ -105,15 +108,15 @@ STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
105108 result = rt_device_close (self -> timer_device );
106109 error_check (result == RT_EOK , "Timer device close error" );
107110 self -> is_init = RT_FALSE ;
108- timer_self = RT_NULL ;
111+ timer_self [ self -> timer_device -> device_id ] = RT_NULL ;
109112 }
110113
111114 return mp_const_none ;
112115}
113116STATIC MP_DEFINE_CONST_FUN_OBJ_1 (machine_timer_deinit_obj , machine_timer_deinit );
114117
115118STATIC rt_err_t timer_event_handler (rt_device_t dev , rt_size_t size ) {
116- machine_timer_obj_t * self = timer_self ;
119+ machine_timer_obj_t * self = timer_self [ dev -> device_id ] ;
117120
118121 mp_sched_schedule (self -> timeout_cb , MP_OBJ_FROM_PTR (self ));
119122 return RT_EOK ;
@@ -162,11 +165,10 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
162165
163166 if (self -> timeout_cb != RT_NULL ) {
164167 // set callback timer
165- if (timer_self && timer_self != self ) {
166- // TODO add multi-timer device support
167- error_check (result == RT_EOK , "Only supports one timer device work" );
168+ if (timer_self [self -> timer_device -> device_id ] && timer_self [self -> timer_device -> device_id ] != self ) {
169+ error_check (result == RT_EOK , "Timer device callback function already exists" );
168170 } else {
169- timer_self = self ;
171+ timer_self [ self -> timer_device -> device_id ] = self ;
170172 }
171173 result = rt_device_set_rx_indicate (self -> timer_device , timer_event_handler );
172174 error_check (result == RT_EOK , "Timer set timout callback error" );
@@ -194,9 +196,47 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
194196}
195197STATIC MP_DEFINE_CONST_FUN_OBJ_KW (machine_timer_init_obj , 1 , machine_timer_init );
196198
199+
200+ STATIC mp_obj_t machine_timer_callback (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
201+ machine_timer_obj_t * self = (machine_timer_obj_t * )args [0 ];
202+ rt_bool_t result = RT_EOK ;
203+
204+ static const mp_arg_t allowed_args [] = {
205+ { MP_QSTR_callback , MP_ARG_OBJ , {.u_obj = mp_const_none } },
206+ };
207+
208+ mp_arg_val_t dargs [MP_ARRAY_SIZE (allowed_args )];
209+ mp_arg_parse_all (n_args - 1 , args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , dargs );
210+
211+ self -> timeout_cb = dargs [0 ].u_obj ;
212+
213+ if (n_args == 1 )
214+ {
215+ self -> timeout_cb = RT_NULL ;
216+ self -> timer_device -> rx_indicate = RT_NULL ;//Log-off callback function
217+ }
218+ else if (n_args == 2 )
219+ {
220+ if (self -> timeout_cb != mp_const_none )
221+ {
222+ timer_self [self -> timer_device -> device_id ] = self ;
223+ result = rt_device_set_rx_indicate (self -> timer_device , timer_event_handler ); //set callback timer
224+ error_check (result == RT_EOK , "Timer set timout callback error" );
225+ }
226+ else
227+ {
228+ self -> timeout_cb = RT_NULL ;
229+ self -> timer_device -> rx_indicate = RT_NULL ;//Log-off callback function
230+ }
231+ }
232+ return mp_const_none ;
233+ }
234+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (machine_timer_callback_obj , 0 ,machine_timer_callback );
235+
197236STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table [] = {
198237 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& machine_timer_deinit_obj ) },
199238 { MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_timer_init_obj ) },
239+ { MP_ROM_QSTR (MP_QSTR_callback ), MP_ROM_PTR (& machine_timer_callback_obj ) },
200240 { MP_ROM_QSTR (MP_QSTR_ONE_SHOT ), MP_ROM_INT (RT_FALSE ) },
201241 { MP_ROM_QSTR (MP_QSTR_PERIODIC ), MP_ROM_INT (RT_TRUE ) },
202242};
0 commit comments