From 138189bad130569ac9a9b3051543b804db54ec6f Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Wed, 22 Jul 2020 13:58:57 -0400 Subject: [PATCH] Free timers when modules de-init --- ports/stm/.gitignore | 1 - ports/stm/common-hal/microcontroller/Pin.c | 4 ++++ ports/stm/common-hal/microcontroller/Pin.h | 1 + ports/stm/common-hal/pulseio/PWMOut.c | 2 +- ports/stm/common-hal/pulseio/PulseIn.c | 3 +-- ports/stm/common-hal/pulseio/PulseOut.c | 2 +- ports/stm/common-hal/rgbmatrix/RGBMatrix.c | 11 +++++++---- ports/stm/peripherals/timers.c | 1 + ports/stm/peripherals/timers.h | 8 -------- 9 files changed, 16 insertions(+), 17 deletions(-) diff --git a/ports/stm/.gitignore b/ports/stm/.gitignore index e46c927eec..5d645392ca 100644 --- a/ports/stm/.gitignore +++ b/ports/stm/.gitignore @@ -5,6 +5,5 @@ build-*/ # Reference files ##################### ref/ -_working* .gdb_history diff --git a/ports/stm/common-hal/microcontroller/Pin.c b/ports/stm/common-hal/microcontroller/Pin.c index d919b07ea7..00763208d9 100644 --- a/ports/stm/common-hal/microcontroller/Pin.c +++ b/ports/stm/common-hal/microcontroller/Pin.c @@ -116,6 +116,10 @@ bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) { return !(claimed_pins[pin_port] & 1<tim->tim_index - 1]) { tim_frequencies[self->tim->tim_index - 1] = 0x00; - tim_clock_disable(1 << (self->tim->tim_index - 1)); + stm_peripherals_timer_free(self->handle.Instance); } } diff --git a/ports/stm/common-hal/pulseio/PulseIn.c b/ports/stm/common-hal/pulseio/PulseIn.c index db01968c4d..811fc8c492 100644 --- a/ports/stm/common-hal/pulseio/PulseIn.c +++ b/ports/stm/common-hal/pulseio/PulseIn.c @@ -186,14 +186,13 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { return; } //Remove pulsein slot from shared array - HAL_NVIC_DisableIRQ(self->irq); _objs[self->pin->number] = NULL; reset_pin_number(self->pin->port, self->pin->number); self->pin = NULL; refcount--; if (refcount == 0) { - tim_clock_disable(1<< stm_peripherals_timer_get_index(tim_handle.Instance)); + stm_peripherals_timer_free(tim_handle.Instance); } } diff --git a/ports/stm/common-hal/pulseio/PulseOut.c b/ports/stm/common-hal/pulseio/PulseOut.c index 865b41dcf5..bf578bed22 100644 --- a/ports/stm/common-hal/pulseio/PulseOut.c +++ b/ports/stm/common-hal/pulseio/PulseOut.c @@ -152,7 +152,7 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { refcount--; if (refcount == 0) { - tim_clock_disable(1<< stm_peripherals_timer_get_index(tim_handle.Instance)); + stm_peripherals_timer_free(tim_handle.Instance); } } diff --git a/ports/stm/common-hal/rgbmatrix/RGBMatrix.c b/ports/stm/common-hal/rgbmatrix/RGBMatrix.c index 312f153580..3b42631adc 100644 --- a/ports/stm/common-hal/rgbmatrix/RGBMatrix.c +++ b/ports/stm/common-hal/rgbmatrix/RGBMatrix.c @@ -34,20 +34,23 @@ extern void _PM_IRQ_HANDLER(void); void *common_hal_rgbmatrix_timer_allocate() { - return stm_peripherals_find_timer(); + TIM_TypeDef * timer = stm_peripherals_find_timer(); + stm_peripherals_timer_reserve(timer); + return timer; } void common_hal_rgbmatrix_timer_enable(void* ptr) { - HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); + TIM_TypeDef *tim = (TIM_TypeDef*)ptr; + HAL_NVIC_EnableIRQ(stm_peripherals_timer_get_irqnum(tim)); } void common_hal_rgbmatrix_timer_disable(void* ptr) { TIM_TypeDef *tim = (TIM_TypeDef*)ptr; tim->DIER &= ~TIM_DIER_UIE; - HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn); } void common_hal_rgbmatrix_timer_free(void* ptr) { + TIM_TypeDef *tim = (TIM_TypeDef*)ptr; + stm_peripherals_timer_free(tim); common_hal_rgbmatrix_timer_disable(ptr); - // TODO(jepler) properly handle resource allocation including never-reset } diff --git a/ports/stm/peripherals/timers.c b/ports/stm/peripherals/timers.c index 7950d8a57b..baa81d2b5f 100644 --- a/ports/stm/peripherals/timers.c +++ b/ports/stm/peripherals/timers.c @@ -233,6 +233,7 @@ void stm_peripherals_timer_set_callback(void(*callback)(void), TIM_TypeDef * tim void stm_peripherals_timer_free(TIM_TypeDef * instance) { size_t tim_idx = stm_peripherals_timer_get_index(instance); + HAL_NVIC_DisableIRQ(irq_map[tim_idx]); stm_timer_callback[tim_idx] = NULL; tim_clock_disable(1 << tim_idx); stm_timer_reserved[tim_idx] = false; diff --git a/ports/stm/peripherals/timers.h b/ports/stm/peripherals/timers.h index 02e4e304e3..c4b6c63673 100644 --- a/ports/stm/peripherals/timers.h +++ b/ports/stm/peripherals/timers.h @@ -24,14 +24,6 @@ * THE SOFTWARE. */ -// typedef struct { -// TIM_TypeDef * timer; -// bool reserved; -// bool never_reset; -// void (*stm_timer_callback)(void); -// size_t irq; -// } stm_timer_t; - #include #include "py/mphal.h" #include "peripherals/periph.h"