diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index de0fac106eba2..92b9f1453a87c 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -309,12 +309,6 @@ $(BUILD)/lib/tlsf/tlsf.o: CFLAGS += -Wno-cast-align $(BUILD)/lib/tinyusb/src/portable/microchip/samd/dcd_samd.o: CFLAGS += -Wno-missing-prototypes -# 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_SAMD),1) SRC_C += bindings/samd/Clock.c bindings/samd/__init__.c endif diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.c b/ports/atmel-samd/common-hal/pulseio/PulseIn.c index f6e834d85b4da..8982fdf0f9899 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.c @@ -43,6 +43,7 @@ #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/pulseio/PulseIn.h" +#include "supervisor/samd_prevent_sleep.h" #include "supervisor/shared/tick.h" #include "supervisor/port.h" @@ -140,15 +141,6 @@ void pulsein_interrupt_handler(uint8_t channel) { common_hal_mcu_enable_interrupts(); } -void pulsein_reset() { - #ifdef SAMD21 - rtc_end_pulse(); - #endif - refcount = 0; - pulsein_tc_index = 0xff; - overflow_count = 0; -} - void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { if (!pin->has_extint) { @@ -241,9 +233,8 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, // Set config will enable the EIC. pulsein_set_config(self, true); #ifdef SAMD21 - rtc_start_pulse(); + samd_prevent_sleep(); #endif - } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { @@ -255,7 +246,7 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { return; } #ifdef SAMD21 - rtc_end_pulse(); + samd_allow_sleep(); #endif set_eic_handler(self->channel, EIC_HANDLER_NO_INTERRUPT); turn_off_eic_channel(self->channel); @@ -273,7 +264,7 @@ void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { uint32_t mask = 1 << self->channel; EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos; #ifdef SAMD21 - rtc_end_pulse(); + samd_allow_sleep(); #endif } @@ -303,7 +294,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos; #ifdef SAMD21 - rtc_start_pulse(); + samd_prevent_sleep(); #endif pulsein_set_config(self, true); } diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.h b/ports/atmel-samd/common-hal/pulseio/PulseIn.h index 81780aa130079..a168f033b64e7 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.h +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -46,14 +45,5 @@ typedef struct { volatile bool errored_too_fast; } pulseio_pulsein_obj_t; -void pulsein_reset(void); - void pulsein_interrupt_handler(uint8_t channel); void pulsein_timer_interrupt_handler(uint8_t index); -#ifdef SAMD21 -void rtc_start_pulse(void); -void rtc_end_pulse(void); -#endif - - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/atmel-samd/common-hal/pulseio/PulseOut.c b/ports/atmel-samd/common-hal/pulseio/PulseOut.c index 47d3d53d6ced8..488ef3f345d56 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseOut.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseOut.c @@ -36,6 +36,7 @@ #include "py/gc.h" #include "py/runtime.h" #include "shared-bindings/pulseio/PulseOut.h" +#include "supervisor/samd_prevent_sleep.h" #include "timer_handler.h" // This timer is shared amongst all PulseOut objects under the assumption that @@ -92,15 +93,6 @@ void pulseout_interrupt_handler(uint8_t index) { tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0; } -void pulseout_reset() { - refcount = 0; - pulseout_tc_index = 0xff; - active_pincfg = NULL; - #ifdef SAMD21 - rtc_end_pulse(); - #endif -} - void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, const mcu_pin_obj_t *pin, uint32_t frequency, @@ -168,9 +160,8 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, // Turn off the pinmux which should connect the port output. turn_off(self->pincfg); #ifdef SAMD21 - rtc_start_pulse(); + samd_prevent_sleep(); #endif - } bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) { @@ -194,7 +185,7 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) { self->pin = NO_PIN; common_hal_pwmio_pwmout_deinit(&self->pwmout); #ifdef SAMD21 - rtc_end_pulse(); + samd_allow_sleep(); #endif } diff --git a/ports/atmel-samd/common-hal/pulseio/PulseOut.h b/ports/atmel-samd/common-hal/pulseio/PulseOut.h index d4a767065e1b0..fcd9e07eefea5 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseOut.h +++ b/ports/atmel-samd/common-hal/pulseio/PulseOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -41,9 +40,3 @@ typedef struct { void pulseout_reset(void); void pulseout_interrupt_handler(uint8_t index); -#ifdef SAMD21 -void rtc_start_pulse(void); -void rtc_end_pulse(void); -#endif - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/atmel-samd/common-hal/pwmio/PWMOut.c b/ports/atmel-samd/common-hal/pwmio/PWMOut.c index 024b114611599..ffc5c32f9d15e 100644 --- a/ports/atmel-samd/common-hal/pwmio/PWMOut.c +++ b/ports/atmel-samd/common-hal/pwmio/PWMOut.c @@ -31,7 +31,6 @@ #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" @@ -61,25 +60,9 @@ uint8_t tcc_channels[5]; // Set by pwmout_reset() to {0xc0, 0xf0, 0xf8, 0xfc, void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { - timer_never_reset(self->timer->index, self->timer->is_tc); - never_reset_pin_number(self->pin->number); } -void pwmout_reset(void) { - // Reset all timers - for (int i = 0; i < TCC_INST_NUM; i++) { - target_tcc_frequencies[i] = 0; - tcc_refcount[i] = 0; - } - for (int i = 0; i < TCC_INST_NUM; i++) { - if (!timer_ok_to_reset(i, false)) { - continue; - } - tcc_channels[i] = 0xff << tcc_cc_num[i]; - } -} - static uint8_t tcc_channel(const pin_timer_t *t) { // For the SAMD51 this hardcodes the use of OTMX == 0x0, the output matrix mapping, which uses // SAMD21-style modulo mapping. @@ -97,7 +80,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, uint16_t duty, uint32_t frequency, bool variable_frequency) { - self->pin = pin; self->variable_frequency = variable_frequency; self->duty_cycle = duty; @@ -242,6 +224,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, } self->timer = timer; + self->pin = pin; gpio_set_pin_function(pin->number, GPIO_PIN_FUNCTION_E + mux_position); @@ -257,7 +240,6 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) { if (common_hal_pwmio_pwmout_deinited(self)) { return; } - timer_reset_ok(self->timer->index, self->timer->is_tc); const pin_timer_t *t = self->timer; if (t->is_tc) { Tc *tc = tc_insts[t->index]; diff --git a/ports/atmel-samd/common-hal/pwmio/PWMOut.h b/ports/atmel-samd/common-hal/pwmio/PWMOut.h index 1915991b545b4..b853b8a10c50a 100644 --- a/ports/atmel-samd/common-hal/pwmio/PWMOut.h +++ b/ports/atmel-samd/common-hal/pwmio/PWMOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -38,7 +37,3 @@ typedef struct { bool variable_frequency; uint16_t duty_cycle; } pwmio_pwmout_obj_t; - -void pwmout_reset(void); - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/atmel-samd/common-hal/rgbmatrix/RGBMatrix.c b/ports/atmel-samd/common-hal/rgbmatrix/RGBMatrix.c index eca3757f20385..558b165f2bb78 100644 --- a/ports/atmel-samd/common-hal/rgbmatrix/RGBMatrix.c +++ b/ports/atmel-samd/common-hal/rgbmatrix/RGBMatrix.c @@ -29,7 +29,6 @@ #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) { @@ -37,7 +36,6 @@ void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self) { if (timer_index == 0xff) { return NULL; } - timer_never_reset(timer_index, true); return tc_insts[timer_index]; } @@ -75,5 +73,4 @@ void common_hal_rgbmatrix_timer_free(void *ptr) { } tc_set_enable(ptr, false); tc_reset(ptr); - timer_reset_ok(timer_index, true); } diff --git a/ports/atmel-samd/shared_timers.c b/ports/atmel-samd/shared_timers.c deleted file mode 100644 index f07bd372da49b..0000000000000 --- a/ports/atmel-samd/shared_timers.c +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 - -#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) { - } - } -} diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index cd8f13de73b7c..6233fac53d695 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -30,6 +30,8 @@ #include "supervisor/board.h" #include "supervisor/port.h" +#include "supervisor/samd_prevent_sleep.h" + // ASF 4 #include "atmel_start_pins.h" #include "peripheral_clk_config.h" @@ -77,15 +79,6 @@ #include "common-hal/microcontroller/Pin.h" -#if CIRCUITPY_PULSEIO -#include "common-hal/pulseio/PulseIn.h" -#include "common-hal/pulseio/PulseOut.h" -#endif - -#if CIRCUITPY_PWMIO -#include "common-hal/pwmio/PWMOut.h" -#endif - #if CIRCUITPY_PS2IO #include "common-hal/ps2io/Ps2.h" #endif @@ -111,9 +104,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 "common-hal/pulseio/PulseIn.h" #include "supervisor/background_callback.h" #include "supervisor/shared/safe_mode.h" @@ -125,7 +116,7 @@ #if CIRCUITPY_PEW #include "common-hal/_pew/PewPew.h" #endif -static volatile bool sleep_ok = true; +static volatile size_t sleep_disable_count = 0; #ifdef SAMD21 static uint8_t _tick_event_channel = EVSYS_SYNCH_NUM; @@ -137,12 +128,16 @@ static bool tick_enabled(void) { // Sleeping requires a register write that can stall interrupt handling. Turning // off sleeps allows for more accurate interrupt timing. (Python still thinks // it is sleeping though.) -void rtc_start_pulse(void) { - sleep_ok = false; +void samd_prevent_sleep(void) { + sleep_disable_count++; } -void rtc_end_pulse(void) { - sleep_ok = true; +void samd_allow_sleep(void) { + if (sleep_disable_count == 0) { + // We should never reach this! + return; + } + sleep_disable_count--; } #endif // SAMD21 @@ -412,19 +407,6 @@ void reset_port(void) { eic_reset(); - #if CIRCUITPY_PULSEIO - pulsein_reset(); - pulseout_reset(); - #endif - - #if CIRCUITPY_PWMIO - pwmout_reset(); - #endif - - #if CIRCUITPY_PWMIO || CIRCUITPY_AUDIOIO || CIRCUITPY_FREQUENCYIO - reset_timers(); - #endif - #if CIRCUITPY_ANALOGIO analogin_reset(); analogout_reset(); @@ -675,7 +657,11 @@ void port_interrupt_after_ticks(uint32_t ticks) { return; } #ifdef SAMD21 - if (!sleep_ok) { + if (sleep_disable_count > 0) { + // "wake" immediately even if sleep_disable_count is set to 0 between + // now and when port_idle_until_interrupt is called. Otherwise we may + // sleep too long. + _woken_up = true; return; } #endif @@ -711,7 +697,7 @@ void port_idle_until_interrupt(void) { } #endif common_hal_mcu_disable_interrupts(); - if (!background_callback_pending() && sleep_ok && !_woken_up) { + if (!background_callback_pending() && sleep_disable_count == 0 && !_woken_up) { __DSB(); __WFI(); } diff --git a/ports/atmel-samd/shared_timers.h b/ports/atmel-samd/supervisor/samd_prevent_sleep.h similarity index 75% rename from ports/atmel-samd/shared_timers.h rename to ports/atmel-samd/supervisor/samd_prevent_sleep.h index 88edd3aa125e8..ea5581134cc2f 100644 --- a/ports/atmel-samd/shared_timers.h +++ b/ports/atmel-samd/supervisor/samd_prevent_sleep.h @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,14 +23,13 @@ * 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 +#pragma once #include -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 +// Sleeping causes interrupt delay to increase. These allow common-hal code to +// temporarily disable sleep as-needed. +#ifdef SAMD21 +void samd_prevent_sleep(void); +void samd_allow_sleep(void); +#endif diff --git a/ports/cxd56/common-hal/pulseio/PulseIn.h b/ports/cxd56/common-hal/pulseio/PulseIn.h index 70d1413b6364d..1604db257daa8 100644 --- a/ports/cxd56/common-hal/pulseio/PulseIn.h +++ b/ports/cxd56/common-hal/pulseio/PulseIn.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEIN_H -#define MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEIN_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -43,5 +42,3 @@ typedef struct { bool first_edge; bool paused; } pulseio_pulsein_obj_t; - -#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/cxd56/common-hal/pulseio/PulseOut.c b/ports/cxd56/common-hal/pulseio/PulseOut.c index 3f3e23fbb3a2d..0558659363f55 100644 --- a/ports/cxd56/common-hal/pulseio/PulseOut.c +++ b/ports/cxd56/common-hal/pulseio/PulseOut.c @@ -126,11 +126,3 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pu pulse_buffer = NULL; } - -void pulseout_reset(void) { - ioctl(pulse_fd, TCIOC_STOP, 0); - close(pulse_fd); - pulse_fd = -1; - - pulse_buffer = NULL; -} diff --git a/ports/cxd56/common-hal/pulseio/PulseOut.h b/ports/cxd56/common-hal/pulseio/PulseOut.h index 18106636c1eac..61d5f201a4e8d 100644 --- a/ports/cxd56/common-hal/pulseio/PulseOut.h +++ b/ports/cxd56/common-hal/pulseio/PulseOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEOUT_H -#define MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEOUT_H +#pragma once #include "py/obj.h" @@ -36,7 +35,3 @@ typedef struct { uint8_t pwm_num; pwmio_pwmout_obj_t pwmout; } pulseio_pulseout_obj_t; - -void pulseout_reset(void); - -#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/cxd56/common-hal/pwmio/PWMOut.c b/ports/cxd56/common-hal/pwmio/PWMOut.c index 10689dc55af43..79b974098f4d6 100644 --- a/ports/cxd56/common-hal/pwmio/PWMOut.c +++ b/ports/cxd56/common-hal/pwmio/PWMOut.c @@ -136,18 +136,6 @@ void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { pwmout_dev[self->number].reset = false; } -void pwmout_reset(void) { - for (int i = 0; i < MP_ARRAY_SIZE(pwmout_dev); i++) { - if (pwmout_dev[i].fd >= 0 && pwmout_dev[i].reset) { - ioctl(pwmout_dev[i].fd, PWMIOC_STOP, 0); - close(pwmout_dev[i].fd); - pwmout_dev[i].fd = -1; - - reset_pin_number(pwmout_dev[i].pin->number); - } - } -} - void pwmout_start(uint8_t pwm_num) { ioctl(pwmout_dev[pwm_num].fd, PWMIOC_START, 0); } diff --git a/ports/cxd56/common-hal/pwmio/PWMOut.h b/ports/cxd56/common-hal/pwmio/PWMOut.h index d9f3ea389d7df..f97058d472ceb 100644 --- a/ports/cxd56/common-hal/pwmio/PWMOut.h +++ b/ports/cxd56/common-hal/pwmio/PWMOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_CXD56_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include @@ -41,8 +40,5 @@ typedef struct { int8_t number; } pwmio_pwmout_obj_t; -void pwmout_reset(void); void pwmout_start(uint8_t pwm_num); void pwmout_stop(uint8_t pwm_num); - -#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c index 05f37cf8af51a..ecb16d77da91b 100644 --- a/ports/cxd56/supervisor/port.c +++ b/ports/cxd56/supervisor/port.c @@ -43,8 +43,6 @@ #include "common-hal/microcontroller/Pin.h" #include "common-hal/analogio/AnalogIn.h" -#include "common-hal/pulseio/PulseOut.h" -#include "common-hal/pwmio/PWMOut.h" #include "common-hal/busio/UART.h" #define SPRESENSE_MEM_ALIGN (32) @@ -82,12 +80,6 @@ void reset_port(void) { #if CIRCUITPY_ANALOGIO analogin_reset(); #endif - #if CIRCUITPY_PULSEIO - pulseout_reset(); - #endif - #if CIRCUITPY_PWMIO - pwmout_reset(); - #endif #if CIRCUITPY_BUSIO busio_uart_reset(); #endif diff --git a/ports/espressif/common-hal/pwmio/PWMOut.c b/ports/espressif/common-hal/pwmio/PWMOut.c index d5d28b9a9f446..ea9e5dd0c0f7b 100644 --- a/ports/espressif/common-hal/pwmio/PWMOut.c +++ b/ports/espressif/common-hal/pwmio/PWMOut.c @@ -36,8 +36,6 @@ STATIC uint32_t reserved_timer_freq[LEDC_TIMER_MAX]; STATIC bool varfreq_timers[LEDC_TIMER_MAX]; STATIC uint8_t reserved_channels[LEDC_CHANNEL_MAX] = { [0 ... LEDC_CHANNEL_MAX - 1] = INDEX_EMPTY}; -STATIC bool never_reset_tim[LEDC_TIMER_MAX]; -STATIC bool never_reset_chan[LEDC_CHANNEL_MAX]; STATIC uint32_t calculate_duty_cycle(uint32_t frequency) { uint32_t duty_bits = 0; @@ -54,22 +52,6 @@ STATIC uint32_t calculate_duty_cycle(uint32_t frequency) { return duty_bits; } -void pwmout_reset(void) { - for (size_t i = 0; i < LEDC_CHANNEL_MAX; i++) { - if (reserved_channels[i] != INDEX_EMPTY && !never_reset_chan[i]) { - ledc_stop(LEDC_LOW_SPEED_MODE, i, 0); - reserved_channels[i] = INDEX_EMPTY; - } - } - for (size_t i = 0; i < LEDC_TIMER_MAX; i++) { - if (reserved_timer_freq[i] && !never_reset_tim[i]) { - ledc_timer_rst(LEDC_LOW_SPEED_MODE, i); - reserved_timer_freq[i] = 0; - varfreq_timers[i] = false; - } - } -} - pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, const mcu_pin_obj_t *pin, uint16_t duty, @@ -159,9 +141,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, } void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { - never_reset_tim[self->tim_handle.timer_num] = true; - never_reset_chan[self->chan_handle.channel] = true; - never_reset_pin_number(self->pin->number); } @@ -178,29 +157,21 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) { ledc_stop(LEDC_LOW_SPEED_MODE, self->chan_handle.channel, 0); } reserved_channels[self->chan_handle.channel] = INDEX_EMPTY; - never_reset_chan[self->chan_handle.channel] = false; // Search if any other channel is using the timer bool taken = false; - bool other_never_reset = false; for (size_t i = 0; i < LEDC_CHANNEL_MAX; i++) { if (reserved_channels[i] == self->tim_handle.timer_num) { taken = true; - other_never_reset = never_reset_chan[i]; break; } } - // Clear the timer's never reset if the other channel isn't never reset. - if (!other_never_reset) { - never_reset_tim[self->tim_handle.timer_num] = false; - } // Variable frequency means there's only one channel on the timer if (!taken || self->variable_frequency) { ledc_timer_rst(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num); reserved_timer_freq[self->tim_handle.timer_num] = 0; // if timer isn't varfreq this will be off already varfreq_timers[self->tim_handle.timer_num] = false; - never_reset_tim[self->tim_handle.timer_num] = false; } common_hal_reset_pin(self->pin); self->deinited = true; diff --git a/ports/espressif/common-hal/pwmio/PWMOut.h b/ports/espressif/common-hal/pwmio/PWMOut.h index a7711ebf078c7..8967d8492f16c 100644 --- a/ports/espressif/common-hal/pwmio/PWMOut.h +++ b/ports/espressif/common-hal/pwmio/PWMOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" #include "components/driver/ledc/include/driver/ledc.h" @@ -39,7 +38,3 @@ typedef struct { bool variable_frequency : 1; bool deinited : 1; } pwmio_pwmout_obj_t; - -void pwmout_reset(void); - -#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/espressif/supervisor/port.c b/ports/espressif/supervisor/port.c index d122f604f34b0..9996f5b61bf64 100644 --- a/ports/espressif/supervisor/port.c +++ b/ports/espressif/supervisor/port.c @@ -49,8 +49,6 @@ #include "common-hal/busio/UART.h" #include "common-hal/dualbank/__init__.h" #include "common-hal/ps2io/Ps2.h" -#include "common-hal/pulseio/PulseIn.h" -#include "common-hal/pwmio/PWMOut.h" #include "common-hal/watchdog/WatchDogTimer.h" #include "common-hal/socketpool/Socket.h" #include "common-hal/wifi/__init__.h" @@ -383,10 +381,6 @@ void reset_port(void) { ps2_reset(); #endif - #if CIRCUITPY_PWMIO - pwmout_reset(); - #endif - #if CIRCUITPY_RTC rtc_reset(); #endif diff --git a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h index b9e4279927af0..462aa832a0b43 100644 --- a/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h +++ b/ports/mimxrt10xx/common-hal/audiopwmio/PWMAudioOut.h @@ -26,7 +26,10 @@ #pragma once -#if CIRCUITPY_AUDIOBUSIO_I2SOUT +#if !CIRCUITPY_AUDIOBUSIO_I2SOUT +#error "audiopwmio requires CIRCUITPY_AUDIOBUSIO_I2SOUT" +#endif + #include "supervisor/background_callback.h" #include "common-hal/microcontroller/Pin.h" @@ -37,5 +40,3 @@ typedef struct { i2s_t i2s; const mcu_pin_obj_t *left_channel, *right_channel; } audiopwmio_pwmaudioout_obj_t; - -#endif diff --git a/ports/mimxrt10xx/common-hal/pwmio/PWMOut.c b/ports/mimxrt10xx/common-hal/pwmio/PWMOut.c index e6b0f79422f90..c72e4fe7c7c94 100644 --- a/ports/mimxrt10xx/common-hal/pwmio/PWMOut.c +++ b/ports/mimxrt10xx/common-hal/pwmio/PWMOut.c @@ -39,8 +39,6 @@ static PWM_Type *const _flexpwms[] = PWM_BASE_PTRS; -// 4 bits for each submodule in each FlexPWM. -static uint16_t _pwm_never_reset[MP_ARRAY_SIZE(_flexpwms)]; // Bitmask of whether state machines are use for variable frequency. static uint8_t _pwm_variable_frequency[MP_ARRAY_SIZE(_flexpwms)]; // Configured frequency for each submodule. @@ -89,7 +87,6 @@ static uint16_t _outen_mask(pwm_submodule_t submodule, pwm_channels_t channel) { void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { common_hal_never_reset_pin(self->pin); - _pwm_never_reset[self->flexpwm_index] |= (1 << (self->pwm->submodule * 4 + self->pwm->channel)); } STATIC void _maybe_disable_clock(uint8_t instance) { @@ -98,33 +95,6 @@ STATIC void _maybe_disable_clock(uint8_t instance) { } } -void reset_all_flexpwm(void) { - for (size_t i = 1; i < MP_ARRAY_SIZE(_pwm_never_reset); i++) { - PWM_Type *flexpwm = _flexpwms[i]; - for (size_t submodule = 0; submodule < FSL_FEATURE_PWM_SUBMODULE_COUNT; submodule++) { - uint8_t sm_mask = 1 << submodule; - for (size_t channel = 0; channel < 3; channel++) { - uint16_t channel_mask = 0x1 << (submodule * 4 + channel); - if ((_pwm_never_reset[i] & channel_mask) != 0) { - continue; - } - - // Turn off the channel. - flexpwm->OUTEN &= ~_outen_mask(submodule, channel); - } - uint16_t submodule_mask = 0xf << (submodule * 4); - if ((_pwm_never_reset[i] & submodule_mask) != 0) { - // Leave the submodule on since a channel is marked for never_reset. - continue; - } - flexpwm->MCTRL &= ~(sm_mask << PWM_MCTRL_RUN_SHIFT); - _pwm_variable_frequency[i] &= ~sm_mask; - _pwm_sm_frequencies[i][submodule] = 0; - } - _maybe_disable_clock(i); - } -} - #define PWM_SRC_CLK_FREQ CLOCK_GetFreq(kCLOCK_IpgClk) static int calculate_pulse_count(uint32_t frequency, uint8_t *prescaler) { @@ -280,8 +250,6 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) { return; } - _pwm_never_reset[self->flexpwm_index] &= ~(1 << (self->pwm->submodule * 4 + self->pwm->channel)); - PWM_Type *flexpwm = self->pwm->pwm; pwm_submodule_t submodule = self->pwm->submodule; uint16_t sm_mask = 1 << submodule; diff --git a/ports/mimxrt10xx/common-hal/pwmio/PWMOut.h b/ports/mimxrt10xx/common-hal/pwmio/PWMOut.h index 6542d67e1f8e0..ec7cda52ad562 100644 --- a/ports/mimxrt10xx/common-hal/pwmio/PWMOut.h +++ b/ports/mimxrt10xx/common-hal/pwmio/PWMOut.h @@ -25,8 +25,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" #include "periph.h" @@ -42,7 +41,3 @@ typedef struct { uint16_t duty_cycle; uint16_t pulse_count; } pwmio_pwmout_obj_t; - -void reset_all_flexpwm(void); - -#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/mimxrt10xx/supervisor/port.c b/ports/mimxrt10xx/supervisor/port.c index 2bf01188a9b77..628a2fdb842e9 100644 --- a/ports/mimxrt10xx/supervisor/port.c +++ b/ports/mimxrt10xx/supervisor/port.c @@ -41,7 +41,6 @@ #endif #include "common-hal/microcontroller/Pin.h" -#include "common-hal/pwmio/PWMOut.h" #include "common-hal/rtc/RTC.h" #include "common-hal/busio/I2C.h" #include "common-hal/busio/SPI.h" @@ -464,12 +463,6 @@ void reset_port(void) { touchin_reset(); #endif -// eic_reset(); - - #if CIRCUITPY_PWMIO - reset_all_flexpwm(); - #endif - #if CIRCUITPY_RTC rtc_reset(); #endif diff --git a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c index 667632c644690..0730768df9d8d 100644 --- a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c +++ b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c @@ -83,15 +83,6 @@ STATIC void deactivate_audiopwmout_obj(audiopwmio_pwmaudioout_obj_t *self) { } } -void audiopwmout_reset() { - for (size_t i = 0; i < MP_ARRAY_SIZE(active_audio); i++) { - if (active_audio[i]) { - supervisor_disable_tick(); - } - active_audio[i] = NULL; - } -} - STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) { uint16_t *dev_buffer = self->buffers[buf]; uint8_t *buffer; diff --git a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.h b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.h index be147bb9b9c46..7af8f23ea0e59 100644 --- a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.h +++ b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_AUDIOPWM_AUDIOOUT_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_AUDIOPWM_AUDIOOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -53,8 +52,4 @@ typedef struct { bool single_buffer; } audiopwmio_pwmaudioout_obj_t; -void audiopwmout_reset(void); - void audiopwmout_background(void); - -#endif diff --git a/ports/nrf/common-hal/pulseio/PulseIn.c b/ports/nrf/common-hal/pulseio/PulseIn.c index eb548a8aa7274..97f887061df7e 100644 --- a/ports/nrf/common-hal/pulseio/PulseIn.c +++ b/ports/nrf/common-hal/pulseio/PulseIn.c @@ -111,15 +111,6 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action self->last_count = current_count; } -void pulsein_reset(void) { - if (timer != NULL) { - nrf_peripherals_free_timer(timer); - } - refcount = 0; - - memset(_objs, 0, sizeof(_objs)); -} - void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { int idx = _find_pulsein_obj(NULL); if (idx < 0) { diff --git a/ports/nrf/common-hal/pulseio/PulseIn.h b/ports/nrf/common-hal/pulseio/PulseIn.h index cdd0e669092a2..dc6677b0b4c0e 100644 --- a/ports/nrf/common-hal/pulseio/PulseIn.h +++ b/ports/nrf/common-hal/pulseio/PulseIn.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_PULSEIO_PULSEIN_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_PULSEIO_PULSEIN_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -47,7 +46,3 @@ typedef struct { volatile size_t last_overflow; volatile size_t last_count; } pulseio_pulsein_obj_t; - -void pulsein_reset(void); - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/nrf/common-hal/pulseio/PulseOut.c b/ports/nrf/common-hal/pulseio/PulseOut.c index e3ee9dbeb9de5..13432c1bb561f 100644 --- a/ports/nrf/common-hal/pulseio/PulseOut.c +++ b/ports/nrf/common-hal/pulseio/PulseOut.c @@ -96,13 +96,6 @@ static void pulseout_event_handler(nrf_timer_event_t event_type, void *p_context start_timer(); } -void pulseout_reset() { - if (timer != NULL) { - nrf_peripherals_free_timer(timer); - } - refcount = 0; -} - void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, const mcu_pin_obj_t *pin, uint32_t frequency, diff --git a/ports/nrf/common-hal/pulseio/PulseOut.h b/ports/nrf/common-hal/pulseio/PulseOut.h index 03560bceacdc3..46fd1fd1aab20 100644 --- a/ports/nrf/common-hal/pulseio/PulseOut.h +++ b/ports/nrf/common-hal/pulseio/PulseOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_PULSEIO_PULSEOUT_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_PULSEIO_PULSEOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" #include "common-hal/pwmio/PWMOut.h" @@ -36,7 +35,3 @@ typedef struct { mp_obj_base_t base; pwmio_pwmout_obj_t pwmout; } pulseio_pulseout_obj_t; - -void pulseout_reset(void); - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/nrf/common-hal/pwmio/PWMOut.c b/ports/nrf/common-hal/pwmio/PWMOut.c index b655417cb4e78..921bb68e55115 100644 --- a/ports/nrf/common-hal/pwmio/PWMOut.c +++ b/ports/nrf/common-hal/pwmio/PWMOut.c @@ -97,21 +97,6 @@ STATIC void reset_single_pwmout(uint8_t i) { } } -void pwmout_reset(void) { - for (size_t i = 0; i < MP_ARRAY_SIZE(pwms); i++) { - for (size_t c = 0; c < CHANNELS_PER_PWM; c++) { - if ((never_reset_pwm[i] & (1 << c)) != 0) { - continue; - } - pwms[i]->PSEL.OUT[c] = 0xFFFFFFFF; - } - if (never_reset_pwm[i] != 0) { - continue; - } - reset_single_pwmout(i); - } -} - // Find the smallest prescaler value that will allow the divisor to be in range. // This allows the most accuracy. STATIC bool convert_frequency(uint32_t frequency, uint16_t *countertop, nrf_pwm_clk_t *base_clock) { diff --git a/ports/nrf/common-hal/pwmio/PWMOut.h b/ports/nrf/common-hal/pwmio/PWMOut.h index 49e67c3b666cb..efc4a02b905db 100644 --- a/ports/nrf/common-hal/pwmio/PWMOut.h +++ b/ports/nrf/common-hal/pwmio/PWMOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include "nrfx_pwm.h" #include "py/obj.h" @@ -41,10 +40,7 @@ typedef struct { uint32_t frequency; } pwmio_pwmout_obj_t; -void pwmout_reset(void); NRF_PWM_Type *pwmout_allocate(uint16_t countertop, nrf_pwm_clk_t base_clock, bool variable_frequency, int8_t *channel_out, bool *pwm_already_in_use_out, IRQn_Type *irq); void pwmout_free_channel(NRF_PWM_Type *pwm, int8_t channel); - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index 602ed84faf566..44e435eba3bcf 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -50,9 +50,6 @@ #include "common-hal/busio/I2C.h" #include "common-hal/busio/SPI.h" #include "common-hal/busio/UART.h" -#include "common-hal/pulseio/PulseOut.h" -#include "common-hal/pulseio/PulseIn.h" -#include "common-hal/pwmio/PWMOut.h" #include "common-hal/rtc/RTC.h" #include "common-hal/neopixel_write/__init__.h" #include "common-hal/watchdog/WatchDogTimer.h" @@ -226,20 +223,6 @@ void reset_port(void) { i2s_reset(); #endif - #if CIRCUITPY_AUDIOPWMIO - audiopwmout_reset(); - #endif - - - #if CIRCUITPY_PULSEIO - pulseout_reset(); - pulsein_reset(); - #endif - - #if CIRCUITPY_PWMIO - pwmout_reset(); - #endif - #if CIRCUITPY_RTC rtc_reset(); #endif diff --git a/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c b/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c index 202e0c7e86053..8cbed46051626 100644 --- a/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c +++ b/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c @@ -101,12 +101,6 @@ static uint32_t limit_denominator(uint32_t max_denominator, uint32_t num_in, uin return bound1_num; } -void audiopwmout_reset() { - for (size_t i = 0; i < NUM_DMA_TIMERS; i++) { - dma_hw->timer[i] = 0; - } -} - // Caller validates that pins are free. void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *self, const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t quiescent_value) { diff --git a/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.h b/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.h index 4175bf63a2368..d86b20705bf98 100644 --- a/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.h +++ b/ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_AUDIOPWMIO_PWMAUDIOOUT_H -#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_AUDIOPWMIO_PWMAUDIOOUT_H +#pragma once #include "common-hal/pwmio/PWMOut.h" @@ -42,8 +41,4 @@ typedef struct { bool swap_channel; } audiopwmio_pwmaudioout_obj_t; -void audiopwmout_reset(void); - void audiopwmout_background(void); - -#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_AUDIOPWMIO_PWMAUDIOOUT_H diff --git a/ports/raspberrypi/common-hal/picodvi/Framebuffer.c b/ports/raspberrypi/common-hal/picodvi/Framebuffer.c index 4fdf871a902de..c166b7221e612 100644 --- a/ports/raspberrypi/common-hal/picodvi/Framebuffer.c +++ b/ports/raspberrypi/common-hal/picodvi/Framebuffer.c @@ -254,9 +254,6 @@ void common_hal_picodvi_framebuffer_construct(picodvi_framebuffer_obj_t *self, } self->pwm_slice = slice; - pwmout_never_reset(self->pwm_slice, 0); - pwmout_never_reset(self->pwm_slice, 1); - for (size_t i = 0; i < 4; i++) { never_reset_pin_number(self->pin_pair[i]); never_reset_pin_number(self->pin_pair[i] + 1); diff --git a/ports/raspberrypi/common-hal/pulseio/PulseIn.h b/ports/raspberrypi/common-hal/pulseio/PulseIn.h index 8694f75e6263e..9ad6a5d917aa5 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseIn.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseIn.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H +#pragma once #include "common-hal/microcontroller/Pin.h" #include "src/rp2_common/hardware_pio/include/hardware/pio.h" @@ -47,7 +46,4 @@ typedef struct { rp2pio_statemachine_obj_t state_machine; } pulseio_pulsein_obj_t; -void pulsein_reset(void); void common_hal_pulseio_pulsein_interrupt(void *); - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.c b/ports/raspberrypi/common-hal/pulseio/PulseOut.c index f0f3d36886270..758c814d242e0 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseOut.c +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.c @@ -70,9 +70,6 @@ int64_t pulseout_interrupt_handler(alarm_id_t id, void *user_data) { return 0; } -void pulseout_reset() { -} - void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, const mcu_pin_obj_t *pin, uint32_t frequency, diff --git a/ports/raspberrypi/common-hal/pulseio/PulseOut.h b/ports/raspberrypi/common-hal/pulseio/PulseOut.h index 3366946210bc9..329deb61e0c7d 100644 --- a/ports/raspberrypi/common-hal/pulseio/PulseOut.h +++ b/ports/raspberrypi/common-hal/pulseio/PulseOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" #include "common-hal/pwmio/PWMOut.h" @@ -46,7 +45,4 @@ typedef struct { volatile uint16_t pulse_index; } pulseio_pulseout_obj_t; -void pulseout_reset(void); int64_t pulseout_interrupt_handler(alarm_id_t id, void *user_data); - -#endif // MICROPY_INCLUDED_ATMEL SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/raspberrypi/common-hal/pwmio/PWMOut.c b/ports/raspberrypi/common-hal/pwmio/PWMOut.c index 603e8a1d11bf4..622e244c31261 100644 --- a/ports/raspberrypi/common-hal/pwmio/PWMOut.c +++ b/ports/raspberrypi/common-hal/pwmio/PWMOut.c @@ -83,37 +83,10 @@ void pwmio_release_slice_ab_channels(uint8_t slice) { channel_use &= ~channel_mask; } -void pwmout_never_reset(uint8_t slice, uint8_t ab_channel) { - never_reset_channel |= _mask(slice, ab_channel); -} - void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { - pwmout_never_reset(self->slice, self->ab_channel); - never_reset_pin_number(self->pin->number); } -void pwmout_reset(void) { - // Reset all slices - for (size_t slice = 0; slice < NUM_PWM_SLICES; slice++) { - bool reset = true; - for (size_t ab_channel = 0; ab_channel < AB_CHANNELS_PER_SLICE; ab_channel++) { - uint32_t channel_use_mask = _mask(slice, ab_channel); - if ((never_reset_channel & channel_use_mask) != 0) { - reset = false; - continue; - } - channel_use &= ~channel_use_mask; - } - if (!reset) { - continue; - } - pwm_set_enabled(slice, false); - target_slice_frequencies[slice] = 0; - slice_variable_frequency &= ~(1 << slice); - } -} - pwmout_result_t pwmout_allocate(uint8_t slice, uint8_t ab_channel, bool variable_frequency, uint32_t frequency) { uint32_t channel_use_mask = _mask(slice, ab_channel); diff --git a/ports/raspberrypi/common-hal/pwmio/PWMOut.h b/ports/raspberrypi/common-hal/pwmio/PWMOut.h index 0d179934d0a30..6203dca112283 100644 --- a/ports/raspberrypi/common-hal/pwmio/PWMOut.h +++ b/ports/raspberrypi/common-hal/pwmio/PWMOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_RASPBERRY_PI_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_RASPBERRY_PI_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -42,17 +41,12 @@ typedef struct { uint16_t top; } pwmio_pwmout_obj_t; -void pwmout_reset(void); // Private API for AudioPWMOut. void pwmio_pwmout_set_top(pwmio_pwmout_obj_t *self, uint16_t top); // Private APIs for RGBMatrix enum pwmout_result_t pwmout_allocate(uint8_t slice, uint8_t ab_channel, bool variable_frequency, uint32_t frequency); void pwmout_free(uint8_t slice, uint8_t ab_channel); -void pwmout_never_reset(uint8_t slice, uint8_t ab_channel); -void pwmout_reset_ok(uint8_t slice, uint8_t ab_channel); // Private API for countio to claim both ab_channels on a slice bool pwmio_claim_slice_ab_channels(uint8_t slice); void pwmio_release_slice_ab_channels(uint8_t slice); - -#endif // MICROPY_INCLUDED_RASPBERRY_PI_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/raspberrypi/common-hal/rgbmatrix/RGBMatrix.c b/ports/raspberrypi/common-hal/rgbmatrix/RGBMatrix.c index d580b4cc6abe1..f717849058bf9 100644 --- a/ports/raspberrypi/common-hal/rgbmatrix/RGBMatrix.c +++ b/ports/raspberrypi/common-hal/rgbmatrix/RGBMatrix.c @@ -43,7 +43,6 @@ void *common_hal_rgbmatrix_timer_allocate(rgbmatrix_rgbmatrix_obj_t *self) { if (result == PWMOUT_OK) { // return value must be nonzero (but slice and channel can both be // zero), so set bit 16... - pwmout_never_reset(slice, channel); return (void *)(intptr_t)(slice | (channel << 8) | 0x10000); } return NULL; diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index 9217eff0d1fd8..2938e7cb303c7 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -33,13 +33,15 @@ #include "bindings/rp2pio/StateMachine.h" #include "genhdr/mpversion.h" -#include "shared-bindings/audiopwmio/PWMAudioOut.h" #include "shared-bindings/busio/I2C.h" #include "shared-bindings/busio/SPI.h" #include "shared-bindings/countio/Counter.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/rtc/__init__.h" -#include "shared-bindings/pwmio/PWMOut.h" + +#if CIRCUITPY_AUDIOCORE +#include "audio_dma.h" +#endif #if CIRCUITPY_SSL #include "shared-module/ssl/__init__.h" @@ -184,10 +186,6 @@ void reset_port(void) { reset_countio(); #endif - #if CIRCUITPY_PWMIO - pwmout_reset(); - #endif - #if CIRCUITPY_RP2PIO reset_rp2pio_statemachine(); #endif @@ -196,9 +194,6 @@ void reset_port(void) { rtc_reset(); #endif - #if CIRCUITPY_AUDIOPWMIO - audiopwmout_reset(); - #endif #if CIRCUITPY_AUDIOCORE audio_dma_reset(); #endif diff --git a/ports/silabs/common-hal/pwmio/PWMOut.c b/ports/silabs/common-hal/pwmio/PWMOut.c index 13bef2fe4fee6..495199360f7a3 100644 --- a/ports/silabs/common-hal/pwmio/PWMOut.c +++ b/ports/silabs/common-hal/pwmio/PWMOut.c @@ -28,9 +28,6 @@ #include "shared-bindings/pwmio/PWMOut.h" #include "shared-bindings/microcontroller/Pin.h" -STATIC sl_pwm_instance_t pwm_handle[TIM_BANK_ARRAY_LEN]; -STATIC bool never_reset_tim[TIM_BANK_ARRAY_LEN]; - mcu_tim_pin_obj_t mcu_tim_list[TIM_BANK_ARRAY_LEN] = { TIM(TIMER0, 0, FN_TIMER0_CC0, NULL), TIM(TIMER1, 0, FN_TIMER1_CC0, NULL), @@ -39,20 +36,6 @@ mcu_tim_pin_obj_t mcu_tim_list[TIM_BANK_ARRAY_LEN] = { TIM(TIMER4, 0, FN_TIMER4_CC0, NULL), }; -// Reset all pwm channel -void pwmout_reset(void) { - uint8_t tim_index; - for (tim_index = 0; tim_index < TIM_BANK_ARRAY_LEN; tim_index++) { - mcu_tim_pin_obj_t *l_tim = &mcu_tim_list[tim_index]; - if (l_tim->pin != NULL) { - sl_pwm_deinit(&pwm_handle[tim_index]); - common_hal_reset_pin(l_tim->pin); - l_tim->pin = NULL; - } - } - -} - // Create a PWM object associated with the given pin pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, const mcu_pin_obj_t *pin, @@ -76,7 +59,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, || l_tim->pin == pin) { l_tim->pin = pin; self->tim = l_tim; - self->handle = &pwm_handle[tim_index]; break; } } @@ -89,18 +71,18 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, self->duty_cycle = duty; self->variable_frequency = variable_frequency; self->frequency = frequency; - self->handle->port = pin->port; - self->handle->pin = pin->number; - self->handle->timer = self->tim->timer; - self->handle->channel = self->tim->channel; + self->handle.port = pin->port; + self->handle.pin = pin->number; + self->handle.timer = self->tim->timer; + self->handle.channel = self->tim->channel; self->tim->pin = pin; - if (SL_STATUS_OK != sl_pwm_init(self->handle, &pwm_config)) { + if (SL_STATUS_OK != sl_pwm_init(&self->handle, &pwm_config)) { return PWMOUT_INITIALIZATION_ERROR; } - sl_pwm_start(self->handle); - sl_pwm_set_duty_cycle(self->handle, percent); + sl_pwm_start(&self->handle); + sl_pwm_set_duty_cycle(&self->handle, percent); common_hal_mcu_pin_claim(pin); return PWMOUT_OK; @@ -108,27 +90,11 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, // Mark pwm obj to never reset after reload void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { - - uint8_t tim_index; - for (tim_index = 0; tim_index < TIM_BANK_ARRAY_LEN; tim_index++) { - if (&mcu_tim_list[tim_index] == self->tim) { - never_reset_tim[tim_index] = true; - common_hal_never_reset_pin(self->tim->pin); - break; - } - } + common_hal_never_reset_pin(self->tim->pin); } // Pwm will be reset after reloading. void common_hal_pwmio_pwmout_reset_ok(pwmio_pwmout_obj_t *self) { - - uint8_t tim_index; - for (tim_index = 0; tim_index < TIM_BANK_ARRAY_LEN; tim_index++) { - if (&mcu_tim_list[tim_index] == self->tim) { - never_reset_tim[tim_index] = false; - break; - } - } } // Check pwm obj status, deinited or not @@ -136,9 +102,9 @@ bool common_hal_pwmio_pwmout_deinited(pwmio_pwmout_obj_t *self) { return self->tim == NULL; } -// Deint pwm obj +// Deinit pwm obj void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) { - sl_pwm_deinit(self->handle); + sl_pwm_deinit(&self->handle); common_hal_reset_pin(self->tim->pin); mcu_tim_pin_obj_t *l_tim = self->tim; l_tim->pin = NULL; @@ -148,7 +114,7 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) { void common_hal_pwmio_pwmout_set_duty_cycle(pwmio_pwmout_obj_t *self, uint16_t duty) { uint8_t percent = (duty * 100) / 65535; - sl_pwm_set_duty_cycle(self->handle, percent); + sl_pwm_set_duty_cycle(&self->handle, percent); self->duty_cycle = duty; } @@ -163,7 +129,7 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t *self, sl_pwm_config_t pwm_config; pwm_config.frequency = frequency; pwm_config.polarity = PWM_ACTIVE_LOW; - sl_pwm_init(self->handle, &pwm_config); + sl_pwm_init(&self->handle, &pwm_config); } // Get pwm frequency diff --git a/ports/silabs/common-hal/pwmio/PWMOut.h b/ports/silabs/common-hal/pwmio/PWMOut.h index 484b1d87b93eb..6037aa1154623 100644 --- a/ports/silabs/common-hal/pwmio/PWMOut.h +++ b/ports/silabs/common-hal/pwmio/PWMOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/Pin.h" @@ -35,13 +34,9 @@ typedef struct { mp_obj_base_t base; - sl_pwm_instance_t *handle; + sl_pwm_instance_t handle; mcu_tim_pin_obj_t *tim; bool variable_frequency : 1; uint16_t duty_cycle; uint32_t frequency; } pwmio_pwmout_obj_t; - -void pwmout_reset(void); - -#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/silabs/supervisor/port.c b/ports/silabs/supervisor/port.c index fe9c0cd464ed7..19a35da5a9fc2 100644 --- a/ports/silabs/supervisor/port.c +++ b/ports/silabs/supervisor/port.c @@ -32,24 +32,12 @@ #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/microcontroller/__init__.h" -#if CIRCUITPY_AUDIOPWMIO -#include "common-hal/audiopwmio/PWMAudioOut.h" -#endif #if CIRCUITPY_BUSIO #include "common-hal/busio/I2C.h" #include "common-hal/busio/SPI.h" #include "common-hal/busio/UART.h" #endif -#if CIRCUITPY_PULSEIO -#include "common-hal/pulseio/PulseOut.h" -#include "common-hal/pulseio/PulseIn.h" -#endif -#if CIRCUITPY_PWMIO -#include "common-hal/pwmio/PWMOut.h" -#endif -#if CIRCUITPY_PULSEIO || CIRCUITPY_PWMIO -#include "peripherals/timers.h" -#endif + #if CIRCUITPY_SDIOIO #include "common-hal/sdioio/SDCard.h" #endif @@ -177,10 +165,6 @@ void reset_port(void) { uart_reset(); #endif - #if CIRCUITPY_PWMIO - pwmout_reset(); - #endif - #if CIRCUITPY_ANALOGIO analogout_reset(); #endif diff --git a/ports/stm/common-hal/audiopwmio/PWMAudioOut.c b/ports/stm/common-hal/audiopwmio/PWMAudioOut.c index e96c2a99684b8..4fc4781c776f5 100644 --- a/ports/stm/common-hal/audiopwmio/PWMAudioOut.c +++ b/ports/stm/common-hal/audiopwmio/PWMAudioOut.c @@ -211,12 +211,6 @@ STATIC void pwmaudioout_event_handler(void) { } } -void audiopwmout_reset() { - if (active_audio) { - common_hal_audiopwmio_pwmaudioout_stop(active_audio); - } -} - // Caller validates that pins are free. void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *self, const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t quiescent_value) { diff --git a/ports/stm/common-hal/audiopwmio/PWMAudioOut.h b/ports/stm/common-hal/audiopwmio/PWMAudioOut.h index bff0bfc89fabf..2b0498f975d7c 100644 --- a/ports/stm/common-hal/audiopwmio/PWMAudioOut.h +++ b/ports/stm/common-hal/audiopwmio/PWMAudioOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM_COMMON_HAL_AUDIOPWM_AUDIOOUT_H -#define MICROPY_INCLUDED_STM_COMMON_HAL_AUDIOPWM_AUDIOOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -52,7 +51,3 @@ typedef struct { bool paused; bool loop; } audiopwmio_pwmaudioout_obj_t; - -void audiopwmout_reset(void); - -#endif diff --git a/ports/stm/common-hal/pulseio/PulseIn.c b/ports/stm/common-hal/pulseio/PulseIn.c index 0de1f3a7c0bbf..3f582fa252450 100644 --- a/ports/stm/common-hal/pulseio/PulseIn.c +++ b/ports/stm/common-hal/pulseio/PulseIn.c @@ -96,22 +96,6 @@ STATIC void pulsein_exti_event_handler(uint8_t num) { self->last_overflow = current_overflow; } -void pulsein_reset(void) { - // Disable all active interrupts and clear array - for (uint i = 0; i < STM32_GPIO_PORT_SIZE; i++) { - if (callback_obj_ref[i] != NULL) { - stm_peripherals_exti_disable(callback_obj_ref[i]->pin->number); - } - } - memset(callback_obj_ref, 0, sizeof(callback_obj_ref)); - - HAL_TIM_Base_DeInit(&tim_handle); - // tim_clock_disable() takes a bitmask of timers. - tim_clock_disable(1 << stm_peripherals_timer_get_index(tim_handle.Instance)); - memset(&tim_handle, 0, sizeof(tim_handle)); - refcount = 0; -} - void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { // STM32 has one shared EXTI for each pin number, 0-15 @@ -201,6 +185,7 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { refcount--; if (refcount == 0) { stm_peripherals_timer_free(tim_handle.Instance); + memset(&tim_handle, 0, sizeof(tim_handle)); } } diff --git a/ports/stm/common-hal/pulseio/PulseIn.h b/ports/stm/common-hal/pulseio/PulseIn.h index a9d925fa5b9d5..5c6933947fb49 100644 --- a/ports/stm/common-hal/pulseio/PulseIn.h +++ b/ports/stm/common-hal/pulseio/PulseIn.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_PULSEIO_PULSEIN_H -#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_PULSEIO_PULSEIN_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -47,7 +46,3 @@ typedef struct { volatile uint32_t last_overflow; volatile uint16_t last_count; } pulseio_pulsein_obj_t; - -void pulsein_reset(void); - -#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/stm/common-hal/pulseio/PulseOut.c b/ports/stm/common-hal/pulseio/PulseOut.c index 53e6c50e3b566..fbf58c3ad24cf 100644 --- a/ports/stm/common-hal/pulseio/PulseOut.c +++ b/ports/stm/common-hal/pulseio/PulseOut.c @@ -101,11 +101,6 @@ STATIC void pulseout_event_handler(void) { } } -void pulseout_reset() { - stm_peripherals_timer_free(tim_handle.Instance); - refcount = 0; -} - void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, const mcu_pin_obj_t *pin, uint32_t frequency, uint16_t duty_cycle) { pwmout_result_t result = common_hal_pwmio_pwmout_construct( diff --git a/ports/stm/common-hal/pulseio/PulseOut.h b/ports/stm/common-hal/pulseio/PulseOut.h index 3a0c460ff7379..ce5f62ceb4dd9 100644 --- a/ports/stm/common-hal/pulseio/PulseOut.h +++ b/ports/stm/common-hal/pulseio/PulseOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_PULSEIO_PULSEOUT_H -#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_PULSEIO_PULSEOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" #include "common-hal/pwmio/PWMOut.h" @@ -36,7 +35,3 @@ typedef struct { mp_obj_base_t base; pwmio_pwmout_obj_t pwmout; } pulseio_pulseout_obj_t; - -void pulseout_reset(void); - -#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/stm/common-hal/pwmio/PWMOut.c b/ports/stm/common-hal/pwmio/PWMOut.c index 8a687f30fb181..28ea61722b967 100644 --- a/ports/stm/common-hal/pwmio/PWMOut.c +++ b/ports/stm/common-hal/pwmio/PWMOut.c @@ -39,8 +39,6 @@ STATIC uint8_t tim_channels_taken[TIM_BANK_ARRAY_LEN]; // Initial frequency timer is set to. STATIC uint32_t tim_frequencies[TIM_BANK_ARRAY_LEN]; -STATIC uint8_t never_reset_tim[TIM_BANK_ARRAY_LEN]; -STATIC TIM_HandleTypeDef *active_handles[TIM_BANK_ARRAY_LEN]; STATIC uint32_t timer_get_internal_duty(uint16_t duty, uint32_t period) { // duty cycle is duty/0xFFFF fraction x (number of pulses per period) @@ -62,30 +60,6 @@ STATIC bool timer_get_optimal_divisors(uint32_t *period, uint32_t *prescaler, return *prescaler != 0; } -void pwmout_reset(void) { - for (int i = 0; i < TIM_BANK_ARRAY_LEN; i++) { - if (active_handles[i] == NULL) { - continue; - } - for (int c = 0; c < 8; c++) { - if ((never_reset_tim[i] & (1 << c)) != 0 || - (tim_channels_taken[i] & (1 << c)) == 0) { - continue; - } - HAL_TIM_PWM_Stop(active_handles[i], c); - } - // TODO: Actually shut down individual channels and PWM. - if (never_reset_tim[i] != 0) { - continue; - } - tim_channels_taken[i] = 0x00; - tim_frequencies[i] = 0; - stm_peripherals_timer_free(mcu_tim_banks[i]); - HAL_TIM_PWM_DeInit(active_handles[i]); - active_handles[i] = NULL; - } -} - pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, const mcu_pin_obj_t *pin, uint16_t duty, @@ -191,7 +165,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) { return PWMOUT_INITIALIZATION_ERROR; } - active_handles[tim_index] = &self->handle; } // Channel/PWM init @@ -215,13 +188,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t *self, } void common_hal_pwmio_pwmout_never_reset(pwmio_pwmout_obj_t *self) { - for (size_t i = 0; i < TIM_BANK_ARRAY_LEN; i++) { - if (mcu_tim_banks[i] == self->handle.Instance) { - never_reset_tim[i] = true; - common_hal_never_reset_pin(self->pin); - break; - } - } + common_hal_never_reset_pin(self->pin); } bool common_hal_pwmio_pwmout_deinited(pwmio_pwmout_obj_t *self) { @@ -241,13 +208,10 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t *self) { } common_hal_reset_pin(self->pin); - never_reset_tim[self->tim->tim_index] &= ~(1 << self->tim->channel_index); - // if reserved timer has no active channels, we can disable it if (tim_channels_taken[self->tim->tim_index] == 0) { tim_frequencies[self->tim->tim_index] = 0x00; HAL_TIM_PWM_DeInit(&self->handle); - active_handles[self->tim->tim_index] = NULL; stm_peripherals_timer_free(self->handle.Instance); } diff --git a/ports/stm/common-hal/pwmio/PWMOut.h b/ports/stm/common-hal/pwmio/PWMOut.h index 9a8b897c6ccd2..881fd40050035 100644 --- a/ports/stm/common-hal/pwmio/PWMOut.h +++ b/ports/stm/common-hal/pwmio/PWMOut.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_PWMIO_PWMOUT_H -#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_PWMIO_PWMOUT_H +#pragma once #include "common-hal/microcontroller/Pin.h" @@ -46,7 +45,3 @@ typedef struct { uint8_t channel; bool variable_frequency; } pwmio_pwmout_obj_t; - -void pwmout_reset(void); - -#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_PWMIO_PWMOUT_H diff --git a/ports/stm/peripherals/timers.c b/ports/stm/peripherals/timers.c index 085b44ccb26ca..b931f1b5a488f 100644 --- a/ports/stm/peripherals/timers.c +++ b/ports/stm/peripherals/timers.c @@ -216,18 +216,6 @@ size_t stm_peripherals_timer_get_irqnum(TIM_TypeDef *instance) { return irq_map[tim_id]; } -void timers_reset(void) { - uint16_t never_reset_mask = 0x00; - for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_tim_banks); i++) { - if (!stm_timer_never_reset[i]) { - stm_timer_reserved[i] = false; - } else { - never_reset_mask |= 1 << i; - } - } - tim_clock_disable(ALL_CLOCKS & ~(never_reset_mask)); -} - TIM_TypeDef *stm_peripherals_find_timer(void) { // Check for timers on pins outside the package size for (size_t i = 0; i < MP_ARRAY_SIZE(mcu_tim_banks); i++) { diff --git a/ports/stm/supervisor/port.c b/ports/stm/supervisor/port.c index d9683a034d919..fde75577397d7 100644 --- a/ports/stm/supervisor/port.c +++ b/ports/stm/supervisor/port.c @@ -33,21 +33,11 @@ #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/microcontroller/__init__.h" -#ifdef CIRCUITPY_AUDIOPWMIO -#include "common-hal/audiopwmio/PWMAudioOut.h" -#endif #if CIRCUITPY_BUSIO #include "common-hal/busio/I2C.h" #include "common-hal/busio/SPI.h" #include "common-hal/busio/UART.h" #endif -#if CIRCUITPY_PULSEIO -#include "common-hal/pulseio/PulseOut.h" -#include "common-hal/pulseio/PulseIn.h" -#endif -#if CIRCUITPY_PWMIO -#include "common-hal/pwmio/PWMOut.h" -#endif #if CIRCUITPY_PULSEIO || CIRCUITPY_PWMIO #include "peripherals/timers.h" #endif @@ -248,9 +238,6 @@ void reset_port(void) { rtc_reset(); #endif - #if CIRCUITPY_AUDIOPWMIO - audiopwmout_reset(); - #endif #if CIRCUITPY_BUSIO i2c_reset(); spi_reset(); @@ -259,17 +246,7 @@ void reset_port(void) { #if CIRCUITPY_SDIOIO sdioio_reset(); #endif - #if CIRCUITPY_PULSEIO || CIRCUITPY_PWMIO - timers_reset(); - #endif - #if CIRCUITPY_PULSEIO - pulseout_reset(); - pulsein_reset(); - #endif - #if CIRCUITPY_PWMIO - pwmout_reset(); - #endif - #if CIRCUITPY_PULSEIO || CIRCUITPY_ALARM + #if CIRCUITPY_ALARM exti_reset(); #endif } diff --git a/shared-bindings/pwmio/PWMOut.c b/shared-bindings/pwmio/PWMOut.c index 8ce58d5ddc160..734f3a2cf42ac 100644 --- a/shared-bindings/pwmio/PWMOut.c +++ b/shared-bindings/pwmio/PWMOut.c @@ -169,7 +169,8 @@ STATIC mp_obj_t pwmio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, bool variable_frequency = parsed_args[ARG_variable_frequency].u_bool; // create PWM object from the given pin - pwmio_pwmout_obj_t *self = mp_obj_malloc(pwmio_pwmout_obj_t, &pwmio_pwmout_type); + pwmio_pwmout_obj_t *self = m_new_obj_with_finaliser(pwmio_pwmout_obj_t); + self->base.type = &pwmio_pwmout_type; pwmout_result_t result = common_hal_pwmio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); common_hal_pwmio_pwmout_raise_error(result); @@ -280,6 +281,7 @@ MP_PROPERTY_GETSET(pwmio_pwmout_frequency_obj, STATIC const mp_rom_map_elem_t pwmio_pwmout_locals_dict_table[] = { // Methods { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pwmio_pwmout_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&pwmio_pwmout_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pwmio_pwmout___exit___obj) },