@@ -366,13 +366,20 @@ STATIC mp_obj_t pyb_timer_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_a
366366}
367367STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_timer_init_obj , 1 , pyb_timer_init );
368368
369+ STATIC mp_obj_t pyb_timer_callback (mp_obj_t self_in , mp_obj_t callback );
370+
369371/// \method deinit()
370372/// Deinitialises the timer.
371373///
372- /// *This function is not yet implemented.*
374+ /// Disables the callback (and the associated irq).
375+ /// Stops the timer, and disables the timer peripheral.
373376STATIC mp_obj_t pyb_timer_deinit (mp_obj_t self_in ) {
374- //pyb_timer_obj_t *self = self_in;
375- // TODO implement me
377+ pyb_timer_obj_t * self = self_in ;
378+
379+ // Disable the interrupt
380+ pyb_timer_callback (self_in , mp_const_none );
381+
382+ HAL_TIM_Base_DeInit (& self -> tim );
376383 return mp_const_none ;
377384}
378385STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_timer_deinit_obj , pyb_timer_deinit );
@@ -464,6 +471,16 @@ const mp_obj_type_t pyb_timer_type = {
464471 .locals_dict = (mp_obj_t )& pyb_timer_locals_dict ,
465472};
466473
474+ // Unregister all interrupt sources
475+ void timer_deinit (void ) {
476+ for (uint i = 0 ; i < PYB_TIMER_OBJ_ALL_NUM ; i ++ ) {
477+ pyb_timer_obj_t * tim = pyb_timer_obj_all [i ];
478+ if (tim != NULL ) {
479+ pyb_timer_deinit (tim );
480+ }
481+ }
482+ }
483+
467484void timer_irq_handler (uint tim_id ) {
468485 if (tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM ) {
469486 // get the timer object
0 commit comments