@@ -203,9 +203,9 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t* self,
203203
204204 i2s_set_enable (true);
205205
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.
207208 i2s_set_clock_unit_enable (self -> clock_unit , true);
208- i2s_set_serializer_enable (self -> serializer , true);
209209
210210 claim_pin (clock_pin );
211211 claim_pin (data_pin );
@@ -379,6 +379,8 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
379379
380380 dma_configure (dma_channel , trigger_source , true);
381381 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);
382384 dma_enable_channel (dma_channel );
383385
384386 // Record
@@ -399,11 +401,9 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
399401 #ifdef SAMD51
400402 #define MAX_WAIT_COUNTS 6000
401403 #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 ) {
407407 #ifdef MICROPY_VM_HOOK_LOOP
408408 MICROPY_VM_HOOK_LOOP
409409 #endif
@@ -463,6 +463,8 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
463463
464464 disable_event_channel (event_channel );
465465 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);
466468
467469 return values_output ;
468470}
0 commit comments