@@ -203,9 +203,9 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t* self,
203
203
204
204
i2s_set_enable (true);
205
205
206
- // Run the serializer all the time. This eliminates startup delay for the microphone.
206
+ // Run the clock all the time. This eliminates startup delay for the microphone,
207
+ // which can be 10-100ms. Turn serializer on as needed.
207
208
i2s_set_clock_unit_enable (self -> clock_unit , true);
208
- i2s_set_serializer_enable (self -> serializer , true);
209
209
210
210
claim_pin (clock_pin );
211
211
claim_pin (data_pin );
@@ -379,6 +379,8 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
379
379
380
380
dma_configure (dma_channel , trigger_source , true);
381
381
init_event_channel_interrupt (event_channel , CORE_GCLK , EVSYS_ID_GEN_DMAC_CH_0 + dma_channel );
382
+ // Turn on serializer now to get it in sync with DMA.
383
+ i2s_set_serializer_enable (self -> serializer , true);
382
384
dma_enable_channel (dma_channel );
383
385
384
386
// Record
@@ -399,11 +401,9 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
399
401
#ifdef SAMD51
400
402
#define MAX_WAIT_COUNTS 6000
401
403
#endif
402
- while (!event_interrupt_active (event_channel )) {
403
- if (wait_counts ++ > MAX_WAIT_COUNTS ) {
404
- // Buffer has stopped filling; DMA may have missed an I2S trigger event.
405
- break ;
406
- }
404
+ // If wait_counts exceeds the max count, buffer has probably stopped filling;
405
+ // DMA may have missed an I2S trigger event.
406
+ while (!event_interrupt_active (event_channel ) && ++ wait_counts < MAX_WAIT_COUNTS ) {
407
407
#ifdef MICROPY_VM_HOOK_LOOP
408
408
MICROPY_VM_HOOK_LOOP
409
409
#endif
@@ -463,6 +463,8 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
463
463
464
464
disable_event_channel (event_channel );
465
465
dma_disable_channel (dma_channel );
466
+ // Turn off serializer, but leave clock on, to avoid mic startup delay.
467
+ i2s_set_serializer_enable (self -> serializer , false);
466
468
467
469
return values_output ;
468
470
}
0 commit comments