fix SAMD21 PDMIn DMA event use

This commit is contained in:
Dan Halbert 2021-10-15 09:39:13 -04:00
parent ec335c1210
commit 79ee78e690
5 changed files with 58 additions and 22 deletions

View File

@ -396,7 +396,7 @@ STATIC void dma_callback_fun(void *arg) {
} }
} }
void audio_evsys_handler(void) { void audio_dma_evsys_handler(void) {
for (uint8_t i = 0; i < AUDIO_DMA_CHANNEL_COUNT; i++) { for (uint8_t i = 0; i < AUDIO_DMA_CHANNEL_COUNT; i++) {
audio_dma_t *dma = audio_dma_state[i]; audio_dma_t *dma = audio_dma_state[i];
if (dma == NULL) { if (dma == NULL) {

View File

@ -101,6 +101,6 @@ void audio_dma_background(void);
uint8_t find_sync_event_channel_raise(void); uint8_t find_sync_event_channel_raise(void);
void audio_evsys_handler(void); void audio_dma_evsys_handler(void);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_AUDIO_DMA_H #endif // MICROPY_INCLUDED_ATMEL_SAMD_AUDIO_DMA_H

View File

@ -35,6 +35,7 @@
#include "common-hal/audiobusio/PDMIn.h" #include "common-hal/audiobusio/PDMIn.h"
#include "shared-bindings/analogio/AnalogOut.h" #include "shared-bindings/analogio/AnalogOut.h"
#include "shared-bindings/audiobusio/PDMIn.h" #include "shared-bindings/audiobusio/PDMIn.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "supervisor/shared/translate.h" #include "supervisor/shared/translate.h"
@ -64,7 +65,20 @@
#define SERCTRL(name) I2S_RXCTRL_ ## name #define SERCTRL(name) I2S_RXCTRL_ ## name
#endif #endif
// Set by interrupt handler when DMA block has finished transferring.
static bool pdmin_dma_block_done;
// Event channel used to trigger interrupt. Set to invalid value EVSYS_SYNCH_NUM when not in use.
static uint8_t pdmin_event_channel;
void pdmin_evsys_handler(void) {
if (pdmin_event_channel < EVSYS_SYNCH_NUM && event_interrupt_active(pdmin_event_channel)) {
pdmin_dma_block_done = true;
}
}
void pdmin_reset(void) { void pdmin_reset(void) {
pdmin_event_channel = EVSYS_SYNCH_NUM;
while (I2S->SYNCBUSY.reg & I2S_SYNCBUSY_ENABLE) {} while (I2S->SYNCBUSY.reg & I2S_SYNCBUSY_ENABLE) {}
I2S->INTENCLR.reg = I2S_INTENCLR_MASK; I2S->INTENCLR.reg = I2S_INTENCLR_MASK;
I2S->INTFLAG.reg = I2S_INTFLAG_MASK; I2S->INTFLAG.reg = I2S_INTFLAG_MASK;
@ -368,7 +382,8 @@ static uint16_t filter_sample(uint32_t pdm_samples[4]) {
uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self, uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self,
uint16_t* output_buffer, uint32_t output_buffer_length) { uint16_t* output_buffer, uint32_t output_buffer_length) {
uint8_t dma_channel = dma_allocate_channel(); uint8_t dma_channel = dma_allocate_channel();
uint8_t event_channel = find_sync_event_channel_raise(); pdmin_event_channel = find_sync_event_channel_raise();
pdmin_dma_block_done = false;
// We allocate two buffers on the stack to use for double buffering. // We allocate two buffers on the stack to use for double buffering.
const uint8_t samples_per_buffer = SAMPLES_PER_BUFFER; const uint8_t samples_per_buffer = SAMPLES_PER_BUFFER;
@ -391,7 +406,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
#endif #endif
dma_configure(dma_channel, trigger_source, true); dma_configure(dma_channel, trigger_source, true);
init_event_channel_interrupt(event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel); init_event_channel_interrupt(pdmin_event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel);
// Turn on serializer now to get it in sync with DMA. // Turn on serializer now to get it in sync with DMA.
i2s_set_serializer_enable(self->serializer, true); i2s_set_serializer_enable(self->serializer, true);
audio_dma_enable_channel(dma_channel); audio_dma_enable_channel(dma_channel);
@ -402,23 +417,12 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
uint32_t remaining_samples_needed = output_buffer_length; uint32_t remaining_samples_needed = output_buffer_length;
while (values_output < output_buffer_length) { while (values_output < output_buffer_length) {
if (event_interrupt_overflow(event_channel)) { while (!pdmin_dma_block_done) {
// Looks like we aren't keeping up. We shouldn't skip a buffer so stop early.
break;
}
// Wait for the next buffer to fill
uint32_t wait_counts = 0;
#ifdef SAMD21
#define MAX_WAIT_COUNTS 1000
#endif
#ifdef SAM_D5X_E5X
#define MAX_WAIT_COUNTS 6000
#endif
// If wait_counts exceeds the max count, buffer has probably stopped filling;
// DMA may have missed an I2S trigger event.
while (!event_interrupt_active(event_channel) && ++wait_counts < MAX_WAIT_COUNTS) {
RUN_BACKGROUND_TASKS; RUN_BACKGROUND_TASKS;
} }
common_hal_mcu_disable_interrupts();
pdmin_dma_block_done = false;
common_hal_mcu_enable_interrupts();
// The mic is running all the time, so we don't need to wait the usual 10msec or 100msec // The mic is running all the time, so we don't need to wait the usual 10msec or 100msec
// for it to start up. // for it to start up.
@ -430,6 +434,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
buffer = second_buffer; buffer = second_buffer;
descriptor = &second_descriptor; descriptor = &second_descriptor;
} }
// Decimate and filter the buffer that was just filled. // Decimate and filter the buffer that was just filled.
uint32_t samples_gathered = descriptor->BTCNT.reg / words_per_sample; uint32_t samples_gathered = descriptor->BTCNT.reg / words_per_sample;
// Don't run off the end of output buffer. Process only as many as needed. // Don't run off the end of output buffer. Process only as many as needed.
@ -472,7 +477,8 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
} }
} }
disable_event_channel(event_channel); disable_event_channel(pdmin_event_channel);
pdmin_event_channel = EVSYS_SYNCH_NUM; // Invalid event_channel.
dma_free_channel(dma_channel); dma_free_channel(dma_channel);
// Turn off serializer, but leave clock on, to avoid mic startup delay. // Turn off serializer, but leave clock on, to avoid mic startup delay.
i2s_set_serializer_enable(self->serializer, false); i2s_set_serializer_enable(self->serializer, false);
@ -481,5 +487,4 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
} }
void common_hal_audiobusio_pdmin_record_to_file(audiobusio_pdmin_obj_t* self, uint8_t* buffer, uint32_t length) { void common_hal_audiobusio_pdmin_record_to_file(audiobusio_pdmin_obj_t* self, uint8_t* buffer, uint32_t length) {
} }

View File

@ -46,6 +46,8 @@ typedef struct {
void pdmin_reset(void); void pdmin_reset(void);
void pdmin_evsys_handler(void);
void pdmin_background(void); void pdmin_background(void);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOBUSIO_AUDIOOUT_H #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOBUSIO_AUDIOOUT_H

View File

@ -53,18 +53,42 @@
#error Unknown chip family #error Unknown chip family
#endif #endif
#if CIRCUITPY_ANALOGIO
#include "common-hal/analogio/AnalogIn.h" #include "common-hal/analogio/AnalogIn.h"
#include "common-hal/analogio/AnalogOut.h" #include "common-hal/analogio/AnalogOut.h"
#endif
#if CIRCUITPY_AUDIOBUSIO
#include "common-hal/audiobusio/PDMIn.h" #include "common-hal/audiobusio/PDMIn.h"
#include "common-hal/audiobusio/I2SOut.h" #include "common-hal/audiobusio/I2SOut.h"
#endif
#if CIRCUITPY_AUDIOIO
#include "common-hal/audioio/AudioOut.h" #include "common-hal/audioio/AudioOut.h"
#endif
#if CIRCUITPY_BUSIO_SPI
#include "common-hal/busio/SPI.h" #include "common-hal/busio/SPI.h"
#endif
#include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/Pin.h"
#if CIRCUITPY_PULSEIO
#include "common-hal/pulseio/PulseIn.h" #include "common-hal/pulseio/PulseIn.h"
#include "common-hal/pulseio/PulseOut.h" #include "common-hal/pulseio/PulseOut.h"
#endif
#if CIRCUITPY_PWMIO
#include "common-hal/pwmio/PWMOut.h" #include "common-hal/pwmio/PWMOut.h"
#endif
#if CIRCUITPY_PS2IO
#include "common-hal/ps2io/Ps2.h" #include "common-hal/ps2io/Ps2.h"
#endif
#if CIRCUITPY_RTC
#include "common-hal/rtc/RTC.h" #include "common-hal/rtc/RTC.h"
#endif
#if CIRCUITPY_TOUCHIO_USE_NATIVE #if CIRCUITPY_TOUCHIO_USE_NATIVE
#include "common-hal/touchio/TouchIn.h" #include "common-hal/touchio/TouchIn.h"
@ -516,8 +540,13 @@ void evsyshandler_common(void) {
supervisor_tick(); supervisor_tick();
} }
#endif #endif
#if CIRCUITPY_AUDIOIO || CIRCUITPY_AUDIOBUSIO #if CIRCUITPY_AUDIOIO || CIRCUITPY_AUDIOBUSIO
audio_evsys_handler(); audio_dma_evsys_handler();
#endif
#if CIRCUITPY_AUDIOBUSIO
pdmin_evsys_handler();
#endif #endif
} }