Skip to content

Switch to finaliser for PWMOut and audiopwmio #8966

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
Feb 22, 2024
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: 0 additions & 6 deletions ports/atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 5 additions & 14 deletions ports/atmel-samd/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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
}

Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 1 addition & 11 deletions ports/atmel-samd/common-hal/pulseio/PulseIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
15 changes: 3 additions & 12 deletions ports/atmel-samd/common-hal/pulseio/PulseOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down
9 changes: 1 addition & 8 deletions ports/atmel-samd/common-hal/pulseio/PulseOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
20 changes: 1 addition & 19 deletions ports/atmel-samd/common-hal/pwmio/PWMOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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;

Expand Down Expand Up @@ -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);

Expand All @@ -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];
Expand Down
7 changes: 1 addition & 6 deletions ports/atmel-samd/common-hal/pwmio/PWMOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
3 changes: 0 additions & 3 deletions ports/atmel-samd/common-hal/rgbmatrix/RGBMatrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
#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) {
uint8_t timer_index = find_free_timer();
if (timer_index == 0xff) {
return NULL;
}
timer_never_reset(timer_index, true);
return tc_insts[timer_index];
}

Expand Down Expand Up @@ -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);
}
84 changes: 0 additions & 84 deletions ports/atmel-samd/shared_timers.c

This file was deleted.

Loading