Skip to content

Commit 79ee78e

Browse files
committed
fix SAMD21 PDMIn DMA event use
1 parent ec335c1 commit 79ee78e

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

ports/atmel-samd/audio_dma.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ STATIC void dma_callback_fun(void *arg) {
396396
}
397397
}
398398

399-
void audio_evsys_handler(void) {
399+
void audio_dma_evsys_handler(void) {
400400
for (uint8_t i = 0; i < AUDIO_DMA_CHANNEL_COUNT; i++) {
401401
audio_dma_t *dma = audio_dma_state[i];
402402
if (dma == NULL) {

ports/atmel-samd/audio_dma.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ void audio_dma_background(void);
101101

102102
uint8_t find_sync_event_channel_raise(void);
103103

104-
void audio_evsys_handler(void);
104+
void audio_dma_evsys_handler(void);
105105

106106
#endif // MICROPY_INCLUDED_ATMEL_SAMD_AUDIO_DMA_H

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "common-hal/audiobusio/PDMIn.h"
3636
#include "shared-bindings/analogio/AnalogOut.h"
3737
#include "shared-bindings/audiobusio/PDMIn.h"
38+
#include "shared-bindings/microcontroller/__init__.h"
3839
#include "shared-bindings/microcontroller/Pin.h"
3940
#include "supervisor/shared/translate.h"
4041

@@ -64,7 +65,20 @@
6465
#define SERCTRL(name) I2S_RXCTRL_ ## name
6566
#endif
6667

68+
// Set by interrupt handler when DMA block has finished transferring.
69+
static bool pdmin_dma_block_done;
70+
// Event channel used to trigger interrupt. Set to invalid value EVSYS_SYNCH_NUM when not in use.
71+
static uint8_t pdmin_event_channel;
72+
73+
void pdmin_evsys_handler(void) {
74+
if (pdmin_event_channel < EVSYS_SYNCH_NUM && event_interrupt_active(pdmin_event_channel)) {
75+
pdmin_dma_block_done = true;
76+
}
77+
}
78+
6779
void pdmin_reset(void) {
80+
pdmin_event_channel = EVSYS_SYNCH_NUM;
81+
6882
while (I2S->SYNCBUSY.reg & I2S_SYNCBUSY_ENABLE) {}
6983
I2S->INTENCLR.reg = I2S_INTENCLR_MASK;
7084
I2S->INTFLAG.reg = I2S_INTFLAG_MASK;
@@ -368,7 +382,8 @@ static uint16_t filter_sample(uint32_t pdm_samples[4]) {
368382
uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self,
369383
uint16_t* output_buffer, uint32_t output_buffer_length) {
370384
uint8_t dma_channel = dma_allocate_channel();
371-
uint8_t event_channel = find_sync_event_channel_raise();
385+
pdmin_event_channel = find_sync_event_channel_raise();
386+
pdmin_dma_block_done = false;
372387

373388
// We allocate two buffers on the stack to use for double buffering.
374389
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
391406
#endif
392407

393408
dma_configure(dma_channel, trigger_source, true);
394-
init_event_channel_interrupt(event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel);
409+
init_event_channel_interrupt(pdmin_event_channel, CORE_GCLK, EVSYS_ID_GEN_DMAC_CH_0 + dma_channel);
395410
// Turn on serializer now to get it in sync with DMA.
396411
i2s_set_serializer_enable(self->serializer, true);
397412
audio_dma_enable_channel(dma_channel);
@@ -402,23 +417,12 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
402417

403418
uint32_t remaining_samples_needed = output_buffer_length;
404419
while (values_output < output_buffer_length) {
405-
if (event_interrupt_overflow(event_channel)) {
406-
// Looks like we aren't keeping up. We shouldn't skip a buffer so stop early.
407-
break;
408-
}
409-
// Wait for the next buffer to fill
410-
uint32_t wait_counts = 0;
411-
#ifdef SAMD21
412-
#define MAX_WAIT_COUNTS 1000
413-
#endif
414-
#ifdef SAM_D5X_E5X
415-
#define MAX_WAIT_COUNTS 6000
416-
#endif
417-
// If wait_counts exceeds the max count, buffer has probably stopped filling;
418-
// DMA may have missed an I2S trigger event.
419-
while (!event_interrupt_active(event_channel) && ++wait_counts < MAX_WAIT_COUNTS) {
420+
while (!pdmin_dma_block_done) {
420421
RUN_BACKGROUND_TASKS;
421422
}
423+
common_hal_mcu_disable_interrupts();
424+
pdmin_dma_block_done = false;
425+
common_hal_mcu_enable_interrupts();
422426

423427
// The mic is running all the time, so we don't need to wait the usual 10msec or 100msec
424428
// for it to start up.
@@ -430,6 +434,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
430434
buffer = second_buffer;
431435
descriptor = &second_descriptor;
432436
}
437+
433438
// Decimate and filter the buffer that was just filled.
434439
uint32_t samples_gathered = descriptor->BTCNT.reg / words_per_sample;
435440
// 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
472477
}
473478
}
474479

475-
disable_event_channel(event_channel);
480+
disable_event_channel(pdmin_event_channel);
481+
pdmin_event_channel = EVSYS_SYNCH_NUM; // Invalid event_channel.
476482
dma_free_channel(dma_channel);
477483
// Turn off serializer, but leave clock on, to avoid mic startup delay.
478484
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
481487
}
482488

483489
void common_hal_audiobusio_pdmin_record_to_file(audiobusio_pdmin_obj_t* self, uint8_t* buffer, uint32_t length) {
484-
485490
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ typedef struct {
4646

4747
void pdmin_reset(void);
4848

49+
void pdmin_evsys_handler(void);
50+
4951
void pdmin_background(void);
5052

5153
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_AUDIOBUSIO_AUDIOOUT_H

ports/atmel-samd/supervisor/port.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,42 @@
5353
#error Unknown chip family
5454
#endif
5555

56+
#if CIRCUITPY_ANALOGIO
5657
#include "common-hal/analogio/AnalogIn.h"
5758
#include "common-hal/analogio/AnalogOut.h"
59+
#endif
60+
61+
#if CIRCUITPY_AUDIOBUSIO
5862
#include "common-hal/audiobusio/PDMIn.h"
5963
#include "common-hal/audiobusio/I2SOut.h"
64+
#endif
65+
66+
#if CIRCUITPY_AUDIOIO
6067
#include "common-hal/audioio/AudioOut.h"
68+
#endif
69+
70+
#if CIRCUITPY_BUSIO_SPI
6171
#include "common-hal/busio/SPI.h"
72+
#endif
73+
6274
#include "common-hal/microcontroller/Pin.h"
75+
76+
#if CIRCUITPY_PULSEIO
6377
#include "common-hal/pulseio/PulseIn.h"
6478
#include "common-hal/pulseio/PulseOut.h"
79+
#endif
80+
81+
#if CIRCUITPY_PWMIO
6582
#include "common-hal/pwmio/PWMOut.h"
83+
#endif
84+
85+
#if CIRCUITPY_PS2IO
6686
#include "common-hal/ps2io/Ps2.h"
87+
#endif
88+
89+
#if CIRCUITPY_RTC
6790
#include "common-hal/rtc/RTC.h"
91+
#endif
6892

6993
#if CIRCUITPY_TOUCHIO_USE_NATIVE
7094
#include "common-hal/touchio/TouchIn.h"
@@ -516,8 +540,13 @@ void evsyshandler_common(void) {
516540
supervisor_tick();
517541
}
518542
#endif
543+
519544
#if CIRCUITPY_AUDIOIO || CIRCUITPY_AUDIOBUSIO
520-
audio_evsys_handler();
545+
audio_dma_evsys_handler();
546+
#endif
547+
548+
#if CIRCUITPY_AUDIOBUSIO
549+
pdmin_evsys_handler();
521550
#endif
522551
}
523552

0 commit comments

Comments
 (0)