Reset timers separate from pwmio

This prevents timer leakage on builds without pwmio.

Fixes #5057
This commit is contained in:
Scott Shawcroft 2021-08-11 09:58:31 -07:00
parent 1e53cf40ad
commit 67c6932ca2
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
6 changed files with 133 additions and 41 deletions

View File

@ -322,6 +322,14 @@ SRC_C += \
reset.c \
timer_handler.c \
ifeq ($(CIRCUITPY_PWMIO),1)
SRC_C += shared_timers.c
endif
ifeq ($(CIRCUITPY_AUDIOIO),1)
SRC_C += shared_timers.c
endif
ifeq ($(CIRCUITPY_SDIOIO),1)
SRC_C += ports/atmel-samd/sd_mmc/sd_mmc.c
endif

View File

@ -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);
@ -97,32 +79,14 @@ void pwmout_reset(void) {
}
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) {
}
}
}

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"
@ -347,6 +348,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