8000 switch atmel-samd AudioOut to use finalizer · brushmate/circuitpython@84e15d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 84e15d7

Browse files
committed
switch atmel-samd AudioOut to use finalizer
1 parent 050caf7 commit 84e15d7

File tree

6 files changed

+18
-44
lines changed

6 files changed

+18
-44
lines changed

ports/atmel-samd/common-hal/analogio/AnalogOut.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
155155
}
156156

157157
void analogout_reset(void) {
158-
// audioout_reset also resets the DAC, and does a smooth ramp down to avoid clicks
159-
// if it was enabled, so do that instead if AudioOut is enabled.
160-
#if CIRCUITPY_AUDIOIO
161-
audioout_reset();
162-
#elif HAVE_ANALOGOUT
158+
#if HAVE_ANALOGOUT
163159
#ifdef SAMD21
164160
while (DAC->STATUS.reg & DAC_STATUS_SYNCBUSY) {
165161
}

ports/atmel-samd/common-hal/audioio/AudioOut.c

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,6 @@ static void ramp_value(uint16_t start, uint16_t end) {
9696
}
9797
#endif
9898

99-
void audioout_reset(void) {
100-
#if defined(SAMD21) && !defined(PIN_PA02)
101-
return;
102-
#endif
103-
#ifdef SAMD21
104-
while (DAC->STATUS.reg & DAC_STATUS_SYNCBUSY) {
105-
}
106-
#endif
107-
#ifdef SAM_D5X_E5X
108-
while (DAC->SYNCBUSY.reg & DAC_SYNCBUSY_SWRST) {
109-
}
110-
#endif
111-
if (DAC->CTRLA.bit.ENABLE) {
112-
ramp_value(0x8000, 0);
113-
}
114-
DAC->CTRLA.reg |= DAC_CTRLA_SWRST;
115-
116-
// TODO(tannewt): Turn off the DAC clocks to save power.
117-
}
118-
11999
// Caller validates that pins are free.
120100
void common_hal_audioio_audioout_construct(audioio_audioout_obj_t *self,
121101
const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t quiescent_value) {
@@ -231,22 +211,16 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t *self,
231211
}
232212
#endif
233213

214+
234215
// Use a timer to coordinate when DAC conversions occur.
235-
Tc *t = NULL;
236-
uint8_t tc_index = TC_INST_NUM;
237-
for (uint8_t i = TC_INST_NUM; i > 0; i--) {
238-
if (tc_insts[i - 1]->COUNT16.CTRLA.bit.ENABLE == 0) {
239-
t = tc_insts[i - 1];
240-
tc_index = i - 1;
241-
break;
242-
}
243-
}
244-
if (t == NULL) {
216+
uint8_t tc_index = find_free_timer();
217+
if (tc_index == 0xFF) {
245218
common_hal_audioio_audioout_deinit(self);
246219
mp_raise_RuntimeError(MP_ERROR_TEXT("All timers in use"));
247220
return;
248221
}
249222
self->tc_index = tc_index;
223+
Tc *t = tc_insts[tc_index];
250224

251225
// Use the 48MHz clocks on both the SAMD21 and 51 because we will be going much slower.
252226
uint8_t tc_gclk = 0;
@@ -322,10 +296,6 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t *self) {
322296
common_hal_audioio_audioout_stop(self);
323297
}
324298

325-
// Ramp the DAC down.
326-
ramp_value(self->quiescent_value, 0);
327-
328-
DAC->CTRLA.bit.ENABLE = 0;
329299
#ifdef SAMD21
330300
while (DAC->STATUS.bit.SYNCBUSY == 1) {
331301
}
@@ -335,6 +305,15 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t *self) {
335305
}
336306
#endif
337307

308+
// Ramp the DAC down.
309+
ramp_value(self->quiescent_value, 0);
310+
311+
DAC->CTRLA.reg |= DAC_CTRLA_SWRST;
312+
313+
// TODO(tannewt): Turn off the DAC clocks to save power.
314+
315+
DAC->CTRLA.bit.ENABLE = 0;
316+
338317
disable_event_channel(self->tc_to_dac_event_channel);
339318

340319
tc_set_enable(tc_insts[self->tc_index], false);

ports/atmel-samd/common-hal/audioio/AudioOut.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ typedef struct {
4747
uint16_t quiescent_value;
4848
} audioio_audioout_obj_t;
4949

50-
void audioout_reset(void);
51-
5250
void audioout_background(void);
5351

5452
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOIO_AUDIOOUT_H

ports/atmel-samd/supervisor/port.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ void reset_port(void) {
386386

387387
#if CIRCUITPY_AUDIOIO
388388
audio_dma_reset();
389-
audioout_reset();
390389
#endif
391390

392391
#if CIRCUITPY_AUDIOBUSIO

ports/mimxrt10xx/supervisor/port.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ void reset_port(void) {
453453

454454
#if CIRCUITPY_AUDIOIO
455455
audio_dma_reset();
456-
audioout_reset();
457456
#endif
457+
458458
#if CIRCUITPY_AUDIOBUSIO
459459
i2s_reset();
460460
#endif

shared-bindings/audioio/AudioOut.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ STATIC mp_obj_t audioio_audioout_make_new(const mp_obj_type_t *type, size_t n_ar
110110
validate_obj_is_free_pin_or_none(args[ARG_right_channel].u_obj, MP_QSTR_right_channel);
111111

112112
// create AudioOut object from the given pin
113-
audioio_audioout_obj_t *self = mp_obj_malloc(audioio_audioout_obj_t, &audioio_audioout_type);
113+
audioio_audioout_obj_t *self = m_new_obj_with_finaliser(audioio_audioout_obj_t);
114+
self->base.type = &audioio_audioout_type;
114115
common_hal_audioio_audioout_construct(self, left_channel_pin, right_channel_pin, args[ARG_quiescent_value].u_int);
115116

116117
return MP_OBJ_FROM_PTR(self);
@@ -244,6 +245,7 @@ MP_PROPERTY_GETTER(audioio_audioout_paused_obj,
244245

245246
STATIC const mp_rom_map_elem_t audioio_audioout_locals_dict_table[] = {
246247
// Methods
248+
{ MP_ROM_QSTR(MP_QSTR___del__), 49F2 MP_ROM_PTR(&audioio_audioout_deinit_obj) },
247249
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audioio_audioout_deinit_obj) },
248250
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
249251
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audioio_audioout___exit___obj) },

0 commit comments

Comments
 (0)
0