Skip to content

Commit f9e4b99

Browse files
committed
WIP debugging prints mingled with code to maybe fix adafruit#1908
1 parent 95d2694 commit f9e4b99

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

ports/atmel-samd/audio_dma.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,41 @@ static audio_dma_t* audio_dma_state[AUDIO_DMA_CHANNEL_COUNT];
3939

4040
// This cannot be in audio_dma_state because it's volatile.
4141
static volatile bool audio_dma_pending[AUDIO_DMA_CHANNEL_COUNT];
42+
static bool audio_dma_shadow_enable[AUDIO_DMA_CHANNEL_COUNT];
4243

4344
uint8_t find_free_audio_dma_channel(void) {
4445
uint8_t channel;
4546
for (channel = 0; channel < AUDIO_DMA_CHANNEL_COUNT; channel++) {
46-
if (!dma_channel_enabled(channel)) {
47+
if (!dma_channel_enabled(channel) && !audio_dma_shadow_enable[channel]) {
48+
mp_printf(&mp_plat_print, "find_free_audio_dma_channel() -> %d\n", channel);
4749
return channel;
4850
}
4951
}
50-
return channel;
52+
return AUDIO_DMA_CHANNEL_COUNT;
53+
}
54+
55+
void audio_dma_enable_channel(uint8_t channel) {
56+
mp_printf(&mp_plat_print, "audio_dma_enable_channel(%d)\n", channel);
57+
if(channel >= AUDIO_DMA_CHANNEL_COUNT)
58+
mp_raise_msg_varg(&mp_type_MemoryError, translate("Internal error: enable invalid channel %d"), channel);
59+
bool real_enable = dma_channel_enabled(channel);
60+
if(audio_dma_shadow_enable[channel] != real_enable)
61+
mp_printf(&mp_plat_print, "audio_dma_shadow mismatch: channel %d: %d vs %d\n", channel, audio_dma_shadow_enable[channel], real_enable);
62+
dma_enable_channel(channel);
63+
audio_dma_shadow_enable[channel] = true;
64+
if(!dma_channel_enabled(channel))
65+
mp_raise_msg_varg(&mp_type_MemoryError, translate("Internal error: channel %d not active after enable"), channel);
66+
}
67+
68+
void audio_dma_disable_channel(uint8_t channel) {
69+
mp_printf(&mp_plat_print, "audio_dma_disable_channel(%d)\n", channel);
70+
if(channel >= AUDIO_DMA_CHANNEL_COUNT)
71+
mp_raise_msg_varg(&mp_type_MemoryError, translate("Internal error: disable invalid channel %d"), channel);
72+
bool real_enable = dma_channel_enabled(channel);
73+
if(audio_dma_shadow_enable[channel] != real_enable)
74+
mp_printf(&mp_plat_print, "audio_dma_shadow mismatch: channel %d: %d vs %d\n", channel, audio_dma_shadow_enable[channel], real_enable);
75+
audio_dma_shadow_enable[channel] = false;
76+
dma_disable_channel(channel);
5177
}
5278

5379
void audio_dma_convert_signed(audio_dma_t* dma, uint8_t* buffer, uint32_t buffer_length,
@@ -106,6 +132,7 @@ void audio_dma_load_next_block(audio_dma_t* dma) {
106132
dma->first_descriptor_free = !dma->first_descriptor_free;
107133

108134
if (get_buffer_result == GET_BUFFER_ERROR) {
135+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
109136
audio_dma_stop(dma);
110137
return;
111138
}
@@ -153,6 +180,7 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t* dma,
153180
bool output_signed,
154181
uint32_t output_register_address,
155182
uint8_t dma_trigger_source) {
183+
mp_printf(&mp_plat_print, "%s:%d:\n", __FILE__, __LINE__);
156184
uint8_t dma_channel = find_free_audio_dma_channel();
157185
if (dma_channel >= AUDIO_DMA_CHANNEL_COUNT) {
158186
return AUDIO_DMA_DMA_BUSY;
@@ -252,16 +280,19 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t* dma,
252280
}
253281

254282
dma_configure(dma_channel, dma_trigger_source, true);
255-
dma_enable_channel(dma_channel);
283+
audio_dma_enable_channel(dma_channel);
256284

257285
return AUDIO_DMA_OK;
258286
}
259287

260288
void audio_dma_stop(audio_dma_t* dma) {
261-
dma_disable_channel(dma->dma_channel);
262-
disable_event_channel(dma->event_channel);
289+
mp_printf(&mp_plat_print, "%s:%d: audio_dma_stop(%d)\n", __FILE__, __LINE__, dma->dma_channel);
290+
if(dma->dma_channel < AUDIO_DMA_CHANNEL_COUNT)
291+
{
292+
audio_dma_disable_channel(dma->dma_channel);
293+
disable_event_channel(dma->event_channel);
294+
}
263295
MP_STATE_PORT(playing_audio)[dma->dma_channel] = NULL;
264-
265296
dma->dma_channel = AUDIO_DMA_CHANNEL_COUNT;
266297
}
267298

@@ -290,7 +321,8 @@ void audio_dma_reset(void) {
290321
for (uint8_t i = 0; i < AUDIO_DMA_CHANNEL_COUNT; i++) {
291322
audio_dma_state[i] = NULL;
292323
audio_dma_pending[i] = false;
293-
dma_disable_channel(i);
324+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
325+
audio_dma_disable_channel(i);
294326
dma_descriptor(i)->BTCTRL.bit.VALID = false;
295327
MP_STATE_PORT(playing_audio)[i] = NULL;
296328
}
@@ -302,6 +334,7 @@ bool audio_dma_get_playing(audio_dma_t* dma) {
302334
}
303335
uint32_t status = dma_transfer_status(dma->dma_channel);
304336
if ((status & DMAC_CHINTFLAG_TCMPL) != 0 || (status & DMAC_CHINTFLAG_TERR) != 0) {
337+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
305338
audio_dma_stop(dma);
306339
}
307340

ports/atmel-samd/audio_dma.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ void audio_dma_pause(audio_dma_t* dma);
8989
void audio_dma_resume(audio_dma_t* dma);
9090
bool audio_dma_get_paused(audio_dma_t* dma);
9191

92+
void audio_dma_enable_channel(uint8_t);
93+
void audio_dma_disable_channel(uint8_t);
94+
9295
void audio_dma_background(void);
9396

9497
#endif // MICROPY_INCLUDED_ATMEL_SAMD_AUDIO_DMA_H

ports/atmel-samd/common-hal/audiobusio/PDMIn.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,11 @@ static uint16_t filter_sample(uint32_t pdm_samples[4]) {
355355
// output_buffer_length is the number of slots, not the number of bytes.
356356
uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self,
357357
uint16_t* output_buffer, uint32_t output_buffer_length) {
358+
mp_printf(&mp_plat_print, "%s:%d:\n", __FILE__, __LINE__);
358359
uint8_t dma_channel = find_free_audio_dma_channel();
360+
if (dma_channel >= AUDIO_DMA_CHANNEL_COUNT) {
361+
mp_raise_RuntimeError(translate("No DMA channel found"));
362+
}
359363
uint8_t event_channel = find_sync_event_channel();
360364
if (event_channel >= EVSYS_SYNCH_NUM) {
361365
mp_raise_RuntimeError(translate("All sync event channels in use"));
@@ -385,7 +389,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
385389
init_event_channel_interrupt(event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel);
386390
// Turn on serializer now to get it in sync with DMA.
387391
i2s_set_serializer_enable(self->serializer, true);
388-
dma_enable_channel(dma_channel);
392+
audio_dma_enable_channel(dma_channel);
389393

390394
// Record
391395
uint32_t buffers_processed = 0;
@@ -466,7 +470,8 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
466470
}
467471

468472
disable_event_channel(event_channel);
469-
dma_disable_channel(dma_channel);
473+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
474+
audio_dma_disable_channel(dma_channel);
470475
// Turn off serializer, but leave clock on, to avoid mic startup delay.
471476
i2s_set_serializer_enable(self->serializer, false);
472477

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,10 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t* self,
411411
}
412412
#endif
413413
if (result != AUDIO_DMA_OK) {
414+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
414415
audio_dma_stop(&self->left_dma);
415416
#ifdef SAMD51
417+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
416418
audio_dma_stop(&self->right_dma);
417419
#endif
418420
if (result == AUDIO_DMA_DMA_BUSY) {
@@ -457,8 +459,10 @@ bool common_hal_audioio_audioout_get_paused(audioio_audioout_obj_t* self) {
457459
void common_hal_audioio_audioout_stop(audioio_audioout_obj_t* self) {
458460
Tc* timer = tc_insts[self->tc_index];
459461
timer->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP;
462+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
460463
audio_dma_stop(&self->left_dma);
461464
#ifdef SAMD51
465+
mp_printf(&mp_plat_print, "%s:%d\n", __FILE__, __LINE__);
462466
audio_dma_stop(&self->right_dma);
463467
#endif
464468
// Ramp the DAC to default. The start is ignored when the current value can be readback.

0 commit comments

Comments
 (0)