@@ -96,26 +96,6 @@ static void ramp_value(uint16_t start, uint16_t end) {
96
96
}
97
97
#endif
98
98
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
-
119
99
// Caller validates that pins are free.
120
100
void common_hal_audioio_audioout_construct (audioio_audioout_obj_t * self ,
121
101
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,
231
211
}
232
212
#endif
233
213
214
+
234
215
// 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 ) {
245
218
common_hal_audioio_audioout_deinit (self );
246
219
mp_raise_RuntimeError (MP_ERROR_TEXT ("All timers in use" ));
247
220
return ;
248
221
}
249
222
self -> tc_index = tc_index ;
223
+ Tc * t = tc_insts [tc_index ];
250
224
251
225
// Use the 48MHz clocks on both the SAMD21 and 51 because we will be going much slower.
252
226
uint8_t tc_gclk = 0 ;
@@ -322,10 +296,6 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t *self) {
322
296
common_hal_audioio_audioout_stop (self );
323
297
}
324
298
325
- // Ramp the DAC down.
326
- ramp_value (self -> quiescent_value , 0 );
327
-
328
- DAC -> CTRLA .bit .ENABLE = 0 ;
329
299
#ifdef SAMD21
330
300
while (DAC -> STATUS .bit .SYNCBUSY == 1 ) {
331
301
}
@@ -335,6 +305,15 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t *self) {
335
305
}
336
306
#endif
337
307
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
+
338
317
disable_event_channel (self -> tc_to_dac_event_channel );
339
318
340
319
tc_set_enable (tc_insts [self -> tc_index ], false);
0 commit comments