Merge pull request #5127 from tannewt/fix_samd_timer_leak

Reset timers separate from pwmio
This commit is contained in:
microDev 2021-08-14 11:26:18 +05:30 committed by GitHub
commit 82a952b6aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 135 additions and 49 deletions

View File

@ -322,6 +322,12 @@ SRC_C += \
reset.c \
timer_handler.c \
# This is an OR because it filters to any 1s and then checks to see if it is not
# empty.
ifneq (,$(filter 1,$(CIRCUITPY_PWMIO) $(CIRCUITPY_AUDIOIO) $(CIRCUITPY_RGBMATRIX)))
SRC_C += shared_timers.c
endif
ifeq ($(CIRCUITPY_SDIOIO),1)
SRC_C += ports/atmel-samd/sd_mmc/sd_mmc.c
endif

View File

@ -31,15 +31,15 @@
#include "common-hal/pwmio/PWMOut.h"
#include "shared-bindings/pwmio/PWMOut.h"
#include "shared-bindings/microcontroller/Processor.h"
#include "shared_timers.h"
#include "timer_handler.h"
#include "atmel_start_pins.h"
#include "hal/utils/include/utils_repeat_macro.h"
#include "samd/pins.h"
#include "samd/timers.h"
#include "supervisor/shared/translate.h"
#include "samd/pins.h"
#undef ENABLE
#define _TCC_SIZE(unused, n) TCC##n##_SIZE,
@ -60,24 +60,6 @@ uint8_t tcc_channels[3]; // Set by pwmout_reset() to {0xf0, 0xfc, 0xfc} initia
uint8_t tcc_channels[5]; // Set by pwmout_reset() to {0xc0, 0xf0, 0xf8, 0xfc, 0xfc} initially.
#endif
static uint8_t never_reset_tc_or_tcc[TC_INST_NUM + TCC_INST_NUM];
STATIC void timer_refcount(int index, bool is_tc, int increment) {
if (is_tc) {
never_reset_tc_or_tcc[index] += increment;
} else {
never_reset_tc_or_tcc[TC_INST_NUM + index] += increment;
}
}
void timer_never_reset(int index, bool is_tc) {
timer_refcount(index, is_tc, 1);
}
void timer_reset_ok(int index, bool is_tc) {
timer_refcount(index, is_tc, -1);
}
void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) {
timer_never_reset(self->timer->index, self->timer->is_tc);
@ -95,34 +77,11 @@ void pwmout_reset(void) {
target_tcc_frequencies[i] = 0;
tcc_refcount[i] = 0;
}
Tcc *tccs[TCC_INST_NUM] = TCC_INSTS;
for (int i = 0; i < TCC_INST_NUM; i++) {
if (never_reset_tc_or_tcc[TC_INST_NUM + i] > 0) {
if (!timer_ok_to_reset(i, false)) {
continue;
}
// Disable the module before resetting it.
if (tccs[i]->CTRLA.bit.ENABLE == 1) {
tccs[i]->CTRLA.bit.ENABLE = 0;
while (tccs[i]->SYNCBUSY.bit.ENABLE == 1) {
}
}
uint8_t mask = 0xff;
for (uint8_t j = 0; j < tcc_cc_num[i]; j++) {
mask <<= 1;
}
tcc_channels[i] = mask;
tccs[i]->CTRLA.bit.SWRST = 1;
while (tccs[i]->CTRLA.bit.SWRST == 1) {
}
}
Tc *tcs[TC_INST_NUM] = TC_INSTS;
for (int i = 0; i < TC_INST_NUM; i++) {
if (never_reset_tc_or_tcc[i] > 0) {
continue;
}
tcs[i]->COUNT16.CTRLA.bit.SWRST = 1;
while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) {
}
tcc_channels[i] = 0xff << tcc_cc_num[i];
}
}

View File

@ -29,6 +29,7 @@
#include "common-hal/rgbmatrix/RGBMatrix.h"
#include "samd/timers.h"
#include "shared_timers.h"
#include "timer_handler.h"
void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self) {

View File

@ -0,0 +1,84 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include "samd/timers.h"
#include "shared_timers.h"
static uint8_t never_reset_tc_or_tcc[TC_INST_NUM + TCC_INST_NUM];
static void timer_refcount(int index, bool is_tc, int increment) {
if (is_tc) {
never_reset_tc_or_tcc[index] += increment;
} else {
never_reset_tc_or_tcc[TC_INST_NUM + index] += increment;
}
}
void timer_never_reset(int index, bool is_tc) {
timer_refcount(index, is_tc, 1);
}
void timer_reset_ok(int index, bool is_tc) {
timer_refcount(index, is_tc, -1);
}
bool timer_ok_to_reset(int index, bool is_tc) {
if (is_tc) {
return never_reset_tc_or_tcc[index] == 0;
}
return never_reset_tc_or_tcc[TC_INST_NUM + index] == 0;
}
void reset_timers(void) {
// Reset all timers
Tcc *tccs[TCC_INST_NUM] = TCC_INSTS;
for (int i = 0; i < TCC_INST_NUM; i++) {
if (!timer_ok_to_reset(i, false)) {
continue;
}
// Disable the module before resetting it.
if (tccs[i]->CTRLA.bit.ENABLE == 1) {
tccs[i]->CTRLA.bit.ENABLE = 0;
while (tccs[i]->SYNCBUSY.bit.ENABLE == 1) {
}
}
tccs[i]->CTRLA.bit.SWRST = 1;
while (tccs[i]->CTRLA.bit.SWRST == 1) {
}
}
Tc *tcs[TC_INST_NUM] = TC_INSTS;
for (int i = 0; i < TC_INST_NUM; i++) {
if (!timer_ok_to_reset(i, true)) {
continue;
}
tcs[i]->COUNT16.CTRLA.bit.SWRST = 1;
while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) {
}
}
}

View File

@ -0,0 +1,36 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_TIMERS_H
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_TIMERS_H
#include <stdbool.h>
void timer_never_reset(int index, bool is_tc);
void timer_reset_ok(int index, bool is_tc);
bool timer_ok_to_reset(int index, bool is_tc);
void reset_timers(void);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_TIMERS_H

View File

@ -77,6 +77,7 @@
#include "samd/dma.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/rtc/__init__.h"
#include "shared_timers.h"
#include "reset.h"
#include "supervisor/shared/safe_mode.h"
@ -362,6 +363,9 @@ void reset_port(void) {
#if CIRCUITPY_PWMIO
pwmout_reset();
#endif
#if CIRCUITPY_PWMIO || CIRCUITPY_AUDIOIO
reset_timers();
#endif
#if CIRCUITPY_ANALOGIO
analogin_reset();

View File

@ -36,8 +36,4 @@
void set_timer_handler(bool is_tc, uint8_t index, uint8_t timer_handler);
void shared_timer_handler(bool is_tc, uint8_t index);
// implementation of these functions is in PWMOut.c
void timer_never_reset(int index, bool is_tc);
void timer_reset_ok(int index, bool is_tc);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_TIMER_HANDLER_H