Skip to content

Commit 82a952b

Browse files
authored
Merge pull request #5127 from tannewt/fix_samd_timer_leak
Reset timers separate from pwmio
2 parents ffb9ecf + 3ffab36 commit 82a952b

File tree

7 files changed

+135
-49
lines changed

7 files changed

+135
-49
lines changed

ports/atmel-samd/Makefile

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

325+
# This is an OR because it filters to any 1s and then checks to see if it is not
326+
# empty.
327+
ifneq (,$(filter 1,$(CIRCUITPY_PWMIO) $(CIRCUITPY_AUDIOIO) $(CIRCUITPY_RGBMATRIX)))
328+
SRC_C += shared_timers.c
329+
endif
330+
325331
ifeq ($(CIRCUITPY_SDIOIO),1)
326332
SRC_C += ports/atmel-samd/sd_mmc/sd_mmc.c
327333
endif

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

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
#include "common-hal/pwmio/PWMOut.h"
3232
#include "shared-bindings/pwmio/PWMOut.h"
3333
#include "shared-bindings/microcontroller/Processor.h"
34+
#include "shared_timers.h"
3435
#include "timer_handler.h"
3536

3637
#include "atmel_start_pins.h"
3738
#include "hal/utils/include/utils_repeat_macro.h"
39+
#include "samd/pins.h"
3840
#include "samd/timers.h"
3941
#include "supervisor/shared/translate.h"
4042

41-
#include "samd/pins.h"
42-
4343
#undef ENABLE
4444

4545
#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
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);
@@ -95,34 +77,11 @@ void pwmout_reset(void) {
9577
target_tcc_frequencies[i] = 0;
9678
tcc_refcount[i] = 0;
9779
}
98-
Tcc *tccs[TCC_INST_NUM] = TCC_INSTS;
9980
for (int i = 0; i < TCC_INST_NUM; i++) {
100-
if (never_reset_tc_or_tcc[TC_INST_NUM + i] > 0) {
101-
continue;
102-
}
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-
}
109-
uint8_t mask = 0xff;
110-
for (uint8_t j = 0; j < tcc_cc_num[i]; j++) {
111-
mask <<= 1;
112-
}
113-
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) {
81+
if (!timer_ok_to_reset(i, false)) {
12182
continue;
12283
}
123-
tcs[i]->COUNT16.CTRLA.bit.SWRST = 1;
124-
while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) {
125-
}
84+
tcc_channels[i] = 0xff << tcc_cc_num[i];
12685
}
12786
}
12887

ports/atmel-samd/common-hal/rgbmatrix/RGBMatrix.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "common-hal/rgbmatrix/RGBMatrix.h"
3030

3131
#include "samd/timers.h"
32+
#include "shared_timers.h"
3233
#include "timer_handler.h"
3334

3435
void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self) {

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"
@@ -362,6 +363,9 @@ void reset_port(void) {
362363
#if CIRCUITPY_PWMIO
363364
pwmout_reset();
364365
#endif
366+
#if CIRCUITPY_PWMIO || CIRCUITPY_AUDIOIO
367+
reset_timers();
368+
#endif
365369

366370
#if CIRCUITPY_ANALOGIO
367371
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)