@@ -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.
120100void 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);
0 commit comments