Skip to content

Commit ed79211

Browse files
tannewtnitz
authored andcommitted
Reset timers separate from pwmio
This prevents timer leakage on builds without pwmio. Fixes adafruit#5057
1 parent 0632a1e commit ed79211

File tree

6 files changed

+133
-41
lines changed

6 files changed

+133
-41
lines changed

ports/atmel-samd/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ SRC_C += \
322322
reset.c \
323323
timer_handler.c \
324324

325+
ifeq ($(CIRCUITPY_PWMIO),1)
326+
SRC_C += shared_timers.c
327+
endif
328+
329+
ifeq ($(CIRCUITPY_AUDIOIO),1)
330+
SRC_C += shared_timers.c
331+
endif
332+
325333
ifeq ($(CIRCUITPY_SDIOIO),1)
326334
SRC_C += ports/atmel-samd/sd_mmc/sd_mmc.c
327335
endif

ports/atmel-samd/common-hal/pwmio/PWMOut.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,6 @@ uint8_t tcc_channels[3]; // Set by pwmout_reset() to {0xf0, 0xfc, 0xfc} initia
6060
uint8_t tcc_channels[5]; // Set by pwmout_reset() to {0xc0, 0xf0, 0xf8, 0xfc, 0xfc} initially.
6161
#endif
6262

63-
static uint8_t never_reset_tc_or_tcc[TC_INST_NUM + TCC_INST_NUM];
64-
65-
STATIC void timer_refcount(int index, bool is_tc, int increment) {
66-
if (is_tc) {
67-
never_reset_tc_or_tcc[index] += increment;
68-
} else {
69-
never_reset_tc_or_tcc[TC_INST_NUM + index] += increment;
70-
}
71-
}
72-
73-
void timer_never_reset(int index, bool is_tc) {
74-
timer_refcount(index, is_tc, 1);
75-
}
76-
77-
void timer_reset_ok(int index, bool is_tc) {
78-
timer_refcount(index, is_tc, -1);
79-
}
80-
8163

8264
void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) {
8365
timer_never_reset(self->timer->index, self->timer->is_tc);
@@ -97,32 +79,14 @@ void pwmout_reset(void) {
9779
}
9880
Tcc *tccs[TCC_INST_NUM] = TCC_INSTS;
9981
for (int i = 0; i < TCC_INST_NUM; i++) {
100-
if (never_reset_tc_or_tcc[TC_INST_NUM + i] > 0) {
82+
if (!timer_ok_to_reset(i, false)) {
10183
continue;
10284
}
103-
// Disable the module before resetting it.
104-
if (tccs[i]->CTRLA.bit.ENABLE == 1) {
105-
tccs[i]->CTRLA.bit.ENABLE = 0;
106-
while (tccs[i]->SYNCBUSY.bit.ENABLE == 1) {
107-
}
108-
}
10985
uint8_t mask = 0xff;
11086
for (uint8_t j = 0; j < tcc_cc_num[i]; j++) {
11187
mask <<= 1;
11288
}
11389
tcc_channels[i] = mask;
114-
tccs[i]->CTRLA.bit.SWRST = 1;
115-
while (tccs[i]->CTRLA.bit.SWRST == 1) {
116-
}
117-
}
118-
Tc *tcs[TC_INST_NUM] = TC_INSTS;
119-
for (int i = 0; i < TC_INST_NUM; i++) {
120-
if (never_reset_tc_or_tcc[i] > 0) {
121-
continue;
122-
}
123-
tcs[i]->COUNT16.CTRLA.bit.SWRST = 1;
124-
while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) {
125-
}
12690
}
12791
}
12892

ports/atmel-samd/shared_timers.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
#include "samd/timers.h"
30+
31+
#include "shared_timers.h"
32+
33+
static uint8_t never_reset_tc_or_tcc[TC_INST_NUM + TCC_INST_NUM];
34+
35+
static void timer_refcount(int index, bool is_tc, int increment) {
36+
if (is_tc) {
37+
never_reset_tc_or_tcc[index] += increment;
38+
} else {
39+
never_reset_tc_or_tcc[TC_INST_NUM + index] += increment;
40+
}
41+
}
42+
43+
void timer_never_reset(int index, bool is_tc) {
44+
timer_refcount(index, is_tc, 1);
45+
}
46+
47+
void timer_reset_ok(int index, bool is_tc) {
48+
timer_refcount(index, is_tc, -1);
49+
}
50+
51+
bool timer_ok_to_reset(int index, bool is_tc) {
52+
if (is_tc) {
53+
return never_reset_tc_or_tcc[index] == 0;
54+
}
55+
return never_reset_tc_or_tcc[TC_INST_NUM + index] == 0;
56+
}
57+
58+
void reset_timers(void) {
59+
// Reset all timers
60+
Tcc *tccs[TCC_INST_NUM] = TCC_INSTS;
61+
for (int i = 0; i < TCC_INST_NUM; i++) {
62+
if (!timer_ok_to_reset(i, false)) {
63+
continue;
64+
}
65+
// Disable the module before resetting it.
66+
if (tccs[i]->CTRLA.bit.ENABLE == 1) {
67+
tccs[i]->CTRLA.bit.ENABLE = 0;
68+
while (tccs[i]->SYNCBUSY.bit.ENABLE == 1) {
69+
}
70+
}
71+
tccs[i]->CTRLA.bit.SWRST = 1;
72+
while (tccs[i]->CTRLA.bit.SWRST == 1) {
73+
}
74+
}
75+
Tc *tcs[TC_INST_NUM] = TC_INSTS;
76+
for (int i = 0; i < TC_INST_NUM; i++) {
77+
if (!timer_ok_to_reset(i, true)) {
78+
continue;
79+
}
80+
tcs[i]->COUNT16.CTRLA.bit.SWRST = 1;
81+
while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) {
82+
}
83+
}
84+
}

ports/atmel-samd/shared_timers.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_TIMERS_H
27+
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_TIMERS_H
28+
29+
#include <stdbool.h>
30+
31+
void timer_never_reset(int index, bool is_tc);
32+
void timer_reset_ok(int index, bool is_tc);
33+
bool timer_ok_to_reset(int index, bool is_tc);
34+
void reset_timers(void);
35+
36+
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_TIMERS_H

ports/atmel-samd/supervisor/port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include "samd/dma.h"
7878
#include "shared-bindings/microcontroller/__init__.h"
7979
#include "shared-bindings/rtc/__init__.h"
80+
#include "shared_timers.h"
8081
#include "reset.h"
8182

8283
#include "supervisor/shared/safe_mode.h"
@@ -347,6 +348,9 @@ void reset_port(void) {
347348
#if CIRCUITPY_PWMIO
348349
pwmout_reset();
349350
#endif
351+
#if CIRCUITPY_PWMIO || CIRCUITPY_AUDIOIO
352+
reset_timers();
353+
#endif
350354

351355
#if CIRCUITPY_ANALOGIO
352356
analogin_reset();

ports/atmel-samd/timer_handler.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,4 @@
3636
void set_timer_handler(bool is_tc, uint8_t index, uint8_t timer_handler);
3737
void shared_timer_handler(bool is_tc, uint8_t index);
3838

39-
// implementation of these functions is in PWMOut.c
40-
void timer_never_reset(int index, bool is_tc);
41-
void timer_reset_ok(int index, bool is_tc);
42-
4339
#endif // MICROPY_INCLUDED_ATMEL_SAMD_TIMER_HANDLER_H

0 commit comments

Comments
 (0)