From 0d81c133b31774058408a5f1d1a058d9734a9def Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Mon, 30 Jun 2014 07:55:54 -0700 Subject: [PATCH 1/2] Add timer_deinit and call it just before doing a soft-restart This fixes #733. --- stmhal/main.c | 2 ++ stmhal/timer.c | 23 ++++++++++++++++++++--- stmhal/timer.h | 2 ++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/stmhal/main.c b/stmhal/main.c index 9f48fbfd65..947c25b41e 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -549,6 +549,8 @@ soft_reset: printf("PYB: sync filesystems\n"); storage_flush(); + timer_deinit(); + printf("PYB: soft reboot\n"); first_soft_reset = false; diff --git a/stmhal/timer.c b/stmhal/timer.c index ec0c4dec4c..8634c46bf2 100644 --- a/stmhal/timer.c +++ b/stmhal/timer.c @@ -366,13 +366,20 @@ STATIC mp_obj_t pyb_timer_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_a } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_init_obj, 1, pyb_timer_init); +STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback); + /// \method deinit() /// Deinitialises the timer. /// -/// *This function is not yet implemented.* +/// Disables the callback (and the associated irq). +/// Stops the timer, and disables the timer peripheral. STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) { - //pyb_timer_obj_t *self = self_in; - // TODO implement me + pyb_timer_obj_t *self = self_in; + + // Disable the interrupt + pyb_timer_callback(self_in, mp_const_none); + + HAL_TIM_Base_DeInit(&self->tim); return mp_const_none; } STATIC 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 = { .locals_dict = (mp_obj_t)&pyb_timer_locals_dict, }; +// Unregister all interrupt sources +void timer_deinit(void) { + for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) { + pyb_timer_obj_t *tim = pyb_timer_obj_all[i]; + if (tim != NULL) { + pyb_timer_deinit(tim); + } + } +} + void timer_irq_handler(uint tim_id) { if (tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) { // get the timer object diff --git a/stmhal/timer.h b/stmhal/timer.h index 9b19d1cd12..2a892db285 100644 --- a/stmhal/timer.h +++ b/stmhal/timer.h @@ -40,4 +40,6 @@ void timer_tim3_init(void); void timer_tim5_init(void); void timer_tim6_init(uint freq); +void timer_deinit(void); + void timer_irq_handler(uint tim_id); From e70b5dbe589ea1c085247ca21c7b4d0efd972c27 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 2 Jul 2014 14:09:44 +0100 Subject: [PATCH 2/2] stmhal: Some reordering of code/functions. --- stmhal/main.c | 3 +-- stmhal/timer.c | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/stmhal/main.c b/stmhal/main.c index c674574575..ae4ff5141c 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -548,9 +548,8 @@ soft_reset: printf("PYB: sync filesystems\n"); storage_flush(); - timer_deinit(); - printf("PYB: soft reboot\n"); + timer_deinit(); first_soft_reset = false; goto soft_reset; diff --git a/stmhal/timer.c b/stmhal/timer.c index 8634c46bf2..73842e4b1d 100644 --- a/stmhal/timer.c +++ b/stmhal/timer.c @@ -107,6 +107,9 @@ static uint32_t tim3_counter = 0; STATIC pyb_timer_obj_t *pyb_timer_obj_all[14]; #define PYB_TIMER_OBJ_ALL_NUM MP_ARRAY_SIZE(pyb_timer_obj_all) +STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in); +STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback); + void timer_init0(void) { tim3_counter = 0; for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) { @@ -114,6 +117,16 @@ void timer_init0(void) { } } +// unregister all interrupt sources +void timer_deinit(void) { + for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) { + pyb_timer_obj_t *tim = pyb_timer_obj_all[i]; + if (tim != NULL) { + pyb_timer_deinit(tim); + } + } +} + // TIM3 is set-up for the USB CDC interface void timer_tim3_init(void) { // set up the timer for USBD CDC @@ -366,8 +379,6 @@ STATIC mp_obj_t pyb_timer_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_a } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_init_obj, 1, pyb_timer_init); -STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback); - /// \method deinit() /// Deinitialises the timer. /// @@ -471,16 +482,6 @@ const mp_obj_type_t pyb_timer_type = { .locals_dict = (mp_obj_t)&pyb_timer_locals_dict, }; -// Unregister all interrupt sources -void timer_deinit(void) { - for (uint i = 0; i < PYB_TIMER_OBJ_ALL_NUM; i++) { - pyb_timer_obj_t *tim = pyb_timer_obj_all[i]; - if (tim != NULL) { - pyb_timer_deinit(tim); - } - } -} - void timer_irq_handler(uint tim_id) { if (tim_id - 1 < PYB_TIMER_OBJ_ALL_NUM) { // get the timer object