From 22b7cd3d519255f9d7c72c5265d3da0a68de638c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 2 May 2018 15:15:25 -0700 Subject: [PATCH] Fix 8 bit recordings on CPX. The DMA trigger source was incorrect when using serializer 1 on the SAMD21. Playback register was incorrect for 8 bit as well. Now fixed. --- ports/atmel-samd/audio_dma.c | 9 +++++++-- ports/atmel-samd/common-hal/audiobusio/PDMIn.c | 7 ++++++- ports/atmel-samd/shared_dma.c | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ports/atmel-samd/audio_dma.c b/ports/atmel-samd/audio_dma.c index 227a2e1881..b468acf51e 100644 --- a/ports/atmel-samd/audio_dma.c +++ b/ports/atmel-samd/audio_dma.c @@ -274,11 +274,16 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t* dma, MP_STATE_PORT(playing_audio)[dma->dma_channel] = dma->sample; } - dma->beat_size = 1; - dma->bytes_per_sample = 1; + if (audiosample_bits_per_sample(sample) == 16) { dma->beat_size = 2; dma->bytes_per_sample = 2; + } else { + dma->beat_size = 1; + dma->bytes_per_sample = 1; + if (single_channel) { + output_register_address += 1; + } } // Transfer both channels at once. if (!single_channel && audiosample_channel_count(sample) == 2) { diff --git a/ports/atmel-samd/common-hal/audiobusio/PDMIn.c b/ports/atmel-samd/common-hal/audiobusio/PDMIn.c index 7788335d1c..8cf86421c9 100644 --- a/ports/atmel-samd/common-hal/audiobusio/PDMIn.c +++ b/ports/atmel-samd/common-hal/audiobusio/PDMIn.c @@ -371,7 +371,12 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se setup_dma(self, output_buffer_length, dma_descriptor(dma_channel), &second_descriptor, words_per_buffer, words_per_sample, first_buffer, second_buffer); - dma_configure(dma_channel, I2S_DMAC_ID_RX_0, true); + uint8_t trigger_source = I2S_DMAC_ID_RX_0; + #ifdef SAMD21 + trigger_source += self->serializer; + #endif + + dma_configure(dma_channel, trigger_source, true); init_event_channel_interrupt(event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel); dma_enable_channel(dma_channel); diff --git a/ports/atmel-samd/shared_dma.c b/ports/atmel-samd/shared_dma.c index 88c89abeeb..a7c27a46eb 100644 --- a/ports/atmel-samd/shared_dma.c +++ b/ports/atmel-samd/shared_dma.c @@ -99,6 +99,8 @@ void dma_enable_channel(uint8_t channel_number) { common_hal_mcu_disable_interrupts(); /** Select the DMA channel and clear software trigger */ DMAC->CHID.reg = DMAC_CHID_ID(channel_number); + // Clear any previous interrupts. + DMAC->CHINTFLAG.reg = DMAC_CHINTFLAG_MASK; DMAC->CHCTRLA.bit.ENABLE = true; common_hal_mcu_enable_interrupts(); #endif @@ -106,6 +108,8 @@ void dma_enable_channel(uint8_t channel_number) { #ifdef SAMD51 DmacChannel* channel = &DMAC->Channel[channel_number]; channel->CHCTRLA.bit.ENABLE = true; + // Clear any previous interrupts. + channel->CHINTFLAG.reg = DMAC_CHINTFLAG_MASK; #endif }