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,11 @@ 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 ) {
168+ if (timer_self [ self -> timer_device -> device_id ] && timer_self [ self -> timer_device -> device_id ] != self ) {
166169 // TODO add multi-timer device support
167170 error_check (result == RT_EOK , "Only supports one timer device work" );
168171 } else {
169- timer_self = self ;
172+ timer_self [ self -> timer_device -> device_id ] = self ;
170173 }
171174 result = rt_device_set_rx_indicate (self -> timer_device , timer_event_handler );
172175 error_check (result == RT_EOK , "Timer set timout callback error" );
@@ -194,9 +197,47 @@ STATIC mp_obj_t machine_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_ma
194197}
195198STATIC MP_DEFINE_CONST_FUN_OBJ_KW (machine_timer_init_obj , 1 , machine_timer_init );
196199
200+
201+ STATIC mp_obj_t machine_timer_callback (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
202+ machine_timer_obj_t * self = (machine_timer_obj_t * )args [0 ];
203+ rt_bool_t result = RT_EOK ;
204+
205+ static const mp_arg_t allowed_args [] = {
206+ { MP_QSTR_callback , MP_ARG_OBJ , {.u_obj = mp_const_none } },
207+ };
208+
209+ mp_arg_val_t dargs [MP_ARRAY_SIZE (allowed_args )];
210+ mp_arg_parse_all (n_args - 1 , args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , dargs );
211+
212+ self -> timeout_cb = dargs [0 ].u_obj ;
213+
214+ if (n_args == 1 )
215+ {
216+ self -> timeout_cb = RT_NULL ;
217+ self -> timer_device -> rx_indicate = RT_NULL ;//注销回调函数
218+ }
219+ else if (n_args == 2 )
220+ {
221+ if (self -> timeout_cb != mp_const_none )
222+ {
223+ timer_self [self -> timer_device -> device_id ] = self ;
224+ result = rt_device_set_rx_indicate (self -> timer_device , timer_event_handler ); //注册回调函数
225+ error_check (result == RT_EOK , "Timer set timout callback error" );
226+ }
227+ else
228+ {
229+ self -> timeout_cb = RT_NULL ;
230+ self -> timer_device -> rx_indicate = RT_NULL ;//注销回调函数
231+ }
232+ }
233+ return mp_const_none ;
234+ }
235+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (machine_timer_callback_obj , 0 ,machine_timer_callback );
236+
197237STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table [] = {
198238 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& machine_timer_deinit_obj ) },
199239 { MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_timer_init_obj ) },
240+ { MP_ROM_QSTR (MP_QSTR_callback ), MP_ROM_PTR (& machine_timer_callback_obj ) },
200241 { MP_ROM_QSTR (MP_QSTR_ONE_SHOT ), MP_ROM_INT (RT_FALSE ) },
201242 { MP_ROM_QSTR (MP_QSTR_PERIODIC ), MP_ROM_INT (RT_TRUE ) },
202243};
0 commit comments