Skip to content

Reset timers separate from pwmio #5127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ports/atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 4 additions & 45 deletions ports/atmel-samd/common-hal/pwmio/PWMOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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) {
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) {
if (!timer_ok_to_reset(i, false)) {
continue;
}
tcs[i]->COUNT16.CTRLA.bit.SWRST = 1;
while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) {
}
tcc_channels[i] = 0xff << tcc_cc_num[i];
}
}

Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/common-hal/rgbmatrix/RGBMatrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
84 changes: 84 additions & 0 deletions ports/atmel-samd/shared_timers.c
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}
36 changes: 36 additions & 0 deletions ports/atmel-samd/shared_timers.h
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions ports/atmel-samd/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 0 additions & 4 deletions ports/atmel-samd/timer_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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