57
57
#ifdef SAMD21
58
58
static void ramp_value (uint16_t start , uint16_t end ) {
59
59
start = DAC -> DATA .reg ;
60
- int32_t diff = (int32_t ) end - start ;
60
+ int32_t diff = (int32_t )end - start ;
61
61
int32_t step = 49 ;
62
62
int32_t steps = diff / step ;
63
63
if (diff < 0 ) {
@@ -76,7 +76,7 @@ static void ramp_value(uint16_t start, uint16_t end) {
76
76
77
77
#ifdef SAM_D5X_E5X
78
78
static void ramp_value (uint16_t start , uint16_t end ) {
79
- int32_t diff = (int32_t ) end - start ;
79
+ int32_t diff = (int32_t )end - start ;
80
80
int32_t step = 49 ;
81
81
int32_t steps = diff / step ;
82
82
if (diff < 0 ) {
@@ -102,10 +102,12 @@ void audioout_reset(void) {
102
102
return ;
103
103
#endif
104
104
#ifdef SAMD21
105
- while (DAC -> STATUS .reg & DAC_STATUS_SYNCBUSY ) {}
105
+ while (DAC -> STATUS .reg & DAC_STATUS_SYNCBUSY ) {
106
+ }
106
107
#endif
107
108
#ifdef SAM_D5X_E5X
108
- while (DAC -> SYNCBUSY .reg & DAC_SYNCBUSY_SWRST ) {}
109
+ while (DAC -> SYNCBUSY .reg & DAC_SYNCBUSY_SWRST ) {
110
+ }
109
111
#endif
110
112
if (DAC -> CTRLA .bit .ENABLE ) {
111
113
ramp_value (0x8000 , 0 );
@@ -116,8 +118,8 @@ void audioout_reset(void) {
116
118
}
117
119
118
120
// Caller validates that pins are free.
119
- void common_hal_audioio_audioout_construct (audioio_audioout_obj_t * self ,
120
- const mcu_pin_obj_t * left_channel , const mcu_pin_obj_t * right_channel , uint16_t quiescent_value ) {
121
+ void common_hal_audioio_audioout_construct (audioio_audioout_obj_t * self ,
122
+ const mcu_pin_obj_t * left_channel , const mcu_pin_obj_t * right_channel , uint16_t quiescent_value ) {
121
123
#ifdef SAM_D5X_E5X
122
124
bool dac_clock_enabled = hri_mclk_get_APBDMASK_DAC_bit (MCLK );
123
125
#endif
@@ -174,7 +176,8 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t* self,
174
176
_gclk_enable_channel (DAC_GCLK_ID , CONF_GCLK_DAC_SRC );
175
177
176
178
DAC -> CTRLA .bit .SWRST = 1 ;
177
- while (DAC -> CTRLA .bit .SWRST == 1 ) {}
179
+ while (DAC -> CTRLA .bit .SWRST == 1 ) {
180
+ }
178
181
// Make sure there are no outstanding access errors. (Reading DATA can cause this.)
179
182
#ifdef SAM_D5X_E5X
180
183
PAC -> INTFLAGD .reg = PAC_INTFLAGD_DAC ;
@@ -191,37 +194,41 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t* self,
191
194
DAC -> EVCTRL .reg |= DAC_EVCTRL_STARTEI ;
192
195
// We disable the voltage pump because we always run at 3.3v.
193
196
DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_AVCC |
194
- DAC_CTRLB_LEFTADJ |
195
- DAC_CTRLB_EOEN |
196
- DAC_CTRLB_VPD ;
197
+ DAC_CTRLB_LEFTADJ |
198
+ DAC_CTRLB_EOEN |
199
+ DAC_CTRLB_VPD ;
197
200
#endif
198
201
#ifdef SAM_D5X_E5X
199
202
DAC -> EVCTRL .reg |= DAC_EVCTRL_STARTEI0 ;
200
203
DAC -> DACCTRL [0 ].reg = DAC_DACCTRL_CCTRL_CC12M |
201
- DAC_DACCTRL_ENABLE |
202
- DAC_DACCTRL_LEFTADJ ;
204
+ DAC_DACCTRL_ENABLE |
205
+ DAC_DACCTRL_LEFTADJ ;
203
206
DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_VREFPU ;
204
207
#endif
205
208
}
206
209
#ifdef SAM_D5X_E5X
207
210
if (channel1_enabled ) {
208
211
DAC -> EVCTRL .reg |= DAC_EVCTRL_STARTEI1 ;
209
212
DAC -> DACCTRL [1 ].reg = DAC_DACCTRL_CCTRL_CC12M |
210
- DAC_DACCTRL_ENABLE |
211
- DAC_DACCTRL_LEFTADJ ;
213
+ DAC_DACCTRL_ENABLE |
214
+ DAC_DACCTRL_LEFTADJ ;
212
215
DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_VREFPU ;
213
216
}
214
217
#endif
215
218
216
219
// Re-enable the DAC
217
220
DAC -> CTRLA .bit .ENABLE = 1 ;
218
221
#ifdef SAMD21
219
- while (DAC -> STATUS .bit .SYNCBUSY == 1 ) {}
222
+ while (DAC -> STATUS .bit .SYNCBUSY == 1 ) {
223
+ }
220
224
#endif
221
225
#ifdef SAM_D5X_E5X
222
- while (DAC -> SYNCBUSY .bit .ENABLE == 1 ) {}
223
- while (channel0_enabled && DAC -> STATUS .bit .READY0 == 0 ) {}
224
- while (channel1_enabled && DAC -> STATUS .bit .READY1 == 0 ) {}
226
+ while (DAC -> SYNCBUSY .bit .ENABLE == 1 ) {
227
+ }
228
+ while (channel0_enabled && DAC -> STATUS .bit .READY0 == 0 ) {
229
+ }
230
+ while (channel1_enabled && DAC -> STATUS .bit .READY1 == 0 ) {
231
+ }
225
232
#endif
226
233
227
234
// Use a timer to coordinate when DAC conversions occur.
@@ -302,11 +309,11 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t* self,
302
309
// Leave the DMA setup to playback.
303
310
}
304
311
305
- bool common_hal_audioio_audioout_deinited (audioio_audioout_obj_t * self ) {
312
+ bool common_hal_audioio_audioout_deinited (audioio_audioout_obj_t * self ) {
306
313
return self -> left_channel == NULL ;
307
314
}
308
315
309
- void common_hal_audioio_audioout_deinit (audioio_audioout_obj_t * self ) {
316
+ void common_hal_audioio_audioout_deinit (audioio_audioout_obj_t * self ) {
310
317
if (common_hal_audioio_audioout_deinited (self )) {
311
318
return ;
312
319
}
@@ -320,10 +327,12 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self) {
320
327
321
328
DAC -> CTRLA .bit .ENABLE = 0 ;
322
329
#ifdef SAMD21
323
- while (DAC -> STATUS .bit .SYNCBUSY == 1 ) {}
330
+ while (DAC -> STATUS .bit .SYNCBUSY == 1 ) {
331
+ }
324
332
#endif
325
333
#ifdef SAM_D5X_E5X
326
- while (DAC -> SYNCBUSY .bit .ENABLE == 1 ) {}
334
+ while (DAC -> SYNCBUSY .bit .ENABLE == 1 ) {
335
+ }
327
336
#endif
328
337
329
338
disable_event_channel (self -> tc_to_dac_event_channel );
@@ -338,7 +347,7 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self) {
338
347
#endif
339
348
}
340
349
341
- static void set_timer_frequency (Tc * timer , uint32_t frequency ) {
350
+ static void set_timer_frequency (Tc * timer , uint32_t frequency ) {
342
351
uint32_t system_clock = 48000000 ;
343
352
uint32_t new_top ;
344
353
uint8_t new_divisor ;
@@ -359,8 +368,8 @@ static void set_timer_frequency(Tc* timer, uint32_t frequency) {
359
368
tc_wait_for_sync (timer );
360
369
}
361
370
362
- void common_hal_audioio_audioout_play (audioio_audioout_obj_t * self ,
363
- mp_obj_t sample , bool loop ) {
371
+ void common_hal_audioio_audioout_play (audioio_audioout_obj_t * self ,
372
+ mp_obj_t sample , bool loop ) {
364
373
if (common_hal_audioio_audioout_get_playing (self )) {
365
374
common_hal_audioio_audioout_stop (self );
366
375
}
@@ -377,40 +386,40 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t* self,
377
386
}
378
387
#ifdef SAMD21
379
388
result = audio_dma_setup_playback (& self -> left_dma , sample , loop , true, 0 ,
380
- false /* output unsigned */ ,
381
- (uint32_t ) & DAC -> DATABUF .reg ,
382
- DAC_DMAC_ID_EMPTY );
389
+ false /* output unsigned */ ,
390
+ (uint32_t )& DAC -> DATABUF .reg ,
391
+ DAC_DMAC_ID_EMPTY );
383
392
#endif
384
393
385
394
#ifdef SAM_D5X_E5X
386
- uint32_t left_channel_reg = (uint32_t ) & DAC -> DATABUF [0 ].reg ;
395
+ uint32_t left_channel_reg = (uint32_t )& DAC -> DATABUF [0 ].reg ;
387
396
uint8_t tc_trig_id = TC0_DMAC_ID_OVF + 3 * self -> tc_index ;
388
397
uint8_t left_channel_trigger = tc_trig_id ;
389
398
uint32_t right_channel_reg = 0 ;
390
399
uint8_t right_channel_trigger = tc_trig_id ;
391
400
if (self -> left_channel == & pin_PA05 ) {
392
- left_channel_reg = (uint32_t ) & DAC -> DATABUF [1 ].reg ;
401
+ left_channel_reg = (uint32_t )& DAC -> DATABUF [1 ].reg ;
393
402
} else if (self -> right_channel == & pin_PA05 ) {
394
- right_channel_reg = (uint32_t ) & DAC -> DATABUF [1 ].reg ;
403
+ right_channel_reg = (uint32_t )& DAC -> DATABUF [1 ].reg ;
395
404
}
396
405
if (self -> right_channel == & pin_PA02 ) {
397
- right_channel_reg = (uint32_t ) & DAC -> DATABUF [0 ].reg ;
406
+ right_channel_reg = (uint32_t )& DAC -> DATABUF [0 ].reg ;
398
407
}
399
- if (right_channel_reg == left_channel_reg + 2 && audiosample_bits_per_sample (sample ) == 16 ) {
408
+ if (right_channel_reg == left_channel_reg + 2 && audiosample_bits_per_sample (sample ) == 16 ) {
400
409
result = audio_dma_setup_playback (& self -> left_dma , sample , loop , false, 0 ,
401
- false /* output unsigned */ ,
402
- left_channel_reg ,
403
- left_channel_trigger );
410
+ false /* output unsigned */ ,
411
+ left_channel_reg ,
412
+ left_channel_trigger );
404
413
} else {
405
414
result = audio_dma_setup_playback (& self -> left_dma , sample , loop , true, 0 ,
406
- false /* output unsigned */ ,
407
- left_channel_reg ,
408
- left_channel_trigger );
415
+ false /* output unsigned */ ,
416
+ left_channel_reg ,
417
+ left_channel_trigger );
409
418
if (right_channel_reg != 0 && result == AUDIO_DMA_OK ) {
410
419
result = audio_dma_setup_playback (& self -> right_dma , sample , loop , true, 1 ,
411
- false /* output unsigned */ ,
412
- right_channel_reg ,
413
- right_channel_trigger );
420
+ false /* output unsigned */ ,
421
+ right_channel_reg ,
422
+ right_channel_trigger );
414
423
}
415
424
}
416
425
#endif
@@ -425,21 +434,22 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t* self,
425
434
mp_raise_RuntimeError (translate ("Unable to allocate buffers for signed conversion" ));
426
435
}
427
436
}
428
- Tc * timer = tc_insts [self -> tc_index ];
437
+ Tc * timer = tc_insts [self -> tc_index ];
429
438
set_timer_frequency (timer , audiosample_sample_rate (sample ));
430
439
timer -> COUNT16 .CTRLBSET .reg = TC_CTRLBSET_CMD_RETRIGGER ;
431
- while (timer -> COUNT16 .STATUS .bit .STOP == 1 ) {}
440
+ while (timer -> COUNT16 .STATUS .bit .STOP == 1 ) {
441
+ }
432
442
self -> playing = true;
433
443
}
434
444
435
- void common_hal_audioio_audioout_pause (audioio_audioout_obj_t * self ) {
445
+ void common_hal_audioio_audioout_pause (audioio_audioout_obj_t * self ) {
436
446
audio_dma_pause (& self -> left_dma );
437
447
#ifdef SAM_D5X_E5X
438
448
audio_dma_pause (& self -> right_dma );
439
449
#endif
440
450
}
441
451
442
- void common_hal_audioio_audioout_resume (audioio_audioout_obj_t * self ) {
452
+ void common_hal_audioio_audioout_resume (audioio_audioout_obj_t * self ) {
443
453
// Clear any overrun/underrun errors
444
454
#ifdef SAMD21
445
455
DAC -> INTFLAG .reg = DAC_INTFLAG_UNDERRUN ;
@@ -454,12 +464,12 @@ void common_hal_audioio_audioout_resume(audioio_audioout_obj_t* self) {
454
464
#endif
455
465
}
456
466
457
- bool common_hal_audioio_audioout_get_paused (audioio_audioout_obj_t * self ) {
467
+ bool common_hal_audioio_audioout_get_paused (audioio_audioout_obj_t * self ) {
458
468
return audio_dma_get_paused (& self -> left_dma );
459
469
}
460
470
461
- void common_hal_audioio_audioout_stop (audioio_audioout_obj_t * self ) {
462
- Tc * timer = tc_insts [self -> tc_index ];
471
+ void common_hal_audioio_audioout_stop (audioio_audioout_obj_t * self ) {
472
+ Tc * timer = tc_insts [self -> tc_index ];
463
473
timer -> COUNT16 .CTRLBSET .reg = TC_CTRLBSET_CMD_STOP ;
464
474
audio_dma_stop (& self -> left_dma );
465
475
#ifdef SAM_D5X_E5X
@@ -470,7 +480,7 @@ void common_hal_audioio_audioout_stop(audioio_audioout_obj_t* self) {
470
480
ramp_value (self -> quiescent_value , self -> quiescent_value );
471
481
}
472
482
473
- bool common_hal_audioio_audioout_get_playing (audioio_audioout_obj_t * self ) {
483
+ bool common_hal_audioio_audioout_get_playing (audioio_audioout_obj_t * self ) {
474
484
bool now_playing = audio_dma_get_playing (& self -> left_dma );
475
485
if (self -> playing && !now_playing ) {
476
486
common_hal_audioio_audioout_stop (self );
0 commit comments