@@ -106,17 +106,14 @@ void common_hal_audiobusio_i2s_construct(audiobusio_i2s_obj_t *self,
106
106
audio_dma_init (& self -> dma );
107
107
}
108
108
109
- void i2s_configure_audio_dma (audiobusio_i2s_obj_t * self , mp_obj_t sample , bool loop , uint32_t sample_rate , uint8_t bits_per_sample ) {
110
-
109
+ void i2s_configure_audio_dma (audiobusio_i2s_obj_t * self , mp_obj_t sample , bool loop , uint32_t sample_rate , uint8_t bits_per_sample , bool force ) {
111
110
if (self -> dma .output_channel [0 ] != NUM_DMA_CHANNELS || self -> dma .input_channel [0 ] != NUM_DMA_CHANNELS ) {
112
- return ;
113
- if (self -> state_machine .out ) {
114
- audio_dma_stop_output (& self -> dma );
111
+ if (!force ) {
112
+ return ;
115
113
}
116
- if (self -> state_machine .in ) {
117
- audio_dma_stop_input (& self -> dma );
118
- }
119
- audio_dma_deinit (& self -> dma );
114
+
115
+ audio_dma_stop (& self -> dma );
116
+ common_hal_rp2pio_statemachine_stop (& self -> state_machine );
120
117
}
121
118
122
119
common_hal_rp2pio_statemachine_set_frequency (& self -> state_machine , sample_rate * bits_per_sample * 16 );
@@ -173,7 +170,7 @@ uint32_t common_hal_audiobusio_i2s_record_to_buffer(audiobusio_i2s_obj_t *self,
173
170
}
174
171
175
172
// Make sure that dma is running.
176
- i2s_configure_audio_dma (self , self , true, self -> sample_rate , self -> bits_per_sample );
173
+ i2s_configure_audio_dma (self , self , true, self -> sample_rate , self -> bits_per_sample , true );
177
174
178
175
size_t output_count = 0 ;
179
176
int16_t * buffer ;
@@ -182,11 +179,7 @@ uint32_t common_hal_audiobusio_i2s_record_to_buffer(audiobusio_i2s_obj_t *self,
182
179
while (output_count < output_buffer_length ) {
183
180
// Do other things while we wait for the buffer to fill.
184
181
while (self -> last_record_index == self -> dma .input_index ) {
185
- if (self -> state_machine .out ) {
186
- common_hal_mcu_delay_us (1000000 / self -> sample_rate );
187
- } else {
188
- RUN_BACKGROUND_TASKS ;
189
- }
182
+ RUN_BACKGROUND_TASKS ;
190
183
}
191
184
self -> last_record_index = self -> dma .input_index ;
192
185
@@ -229,7 +222,7 @@ void common_hal_audiobusio_i2s_play(audiobusio_i2s_obj_t *self,
229
222
}
230
223
}
231
224
232
- i2s_configure_audio_dma (self , sample , loop , sample_rate , bits_per_sample );
225
+ i2s_configure_audio_dma (self , sample , loop , sample_rate , bits_per_sample , true );
233
226
self -> playing = true;
234
227
}
235
228
@@ -311,7 +304,7 @@ void audiobusio_i2s_reset_buffer(audiobusio_i2s_obj_t *self,
311
304
mp_raise_NotImplementedError (MP_ERROR_TEXT ("Single channel output not supported." ));
312
305
}
313
306
314
- i2s_configure_audio_dma (self , self , true, self -> sample_rate , self -> bits_per_sample );
307
+ i2s_configure_audio_dma (self , self , true, self -> sample_rate , self -> bits_per_sample , false );
315
308
self -> last_record_index = -1 ;
316
309
self -> last_sample_index = -1 ;
317
310
}
@@ -331,11 +324,7 @@ audioio_get_buffer_result_t audiobusio_i2s_get_buffer(audiobusio_i2s_obj_t *self
331
324
332
325
// Do other things while we wait for the buffer to fill.
333
326
while (self -> last_sample_index == self -> dma .input_index ) {
334
- if (self -> state_machine .out ) {
335
- common_hal_mcu_delay_us (1000000 / self -> sample_rate );
336
- } else {
337
- RUN_BACKGROUND_TASKS ;
338
- }
327
+ RUN_BACKGROUND_TASKS ;
339
328
}
340
329
self -> last_sample_index = self -> dma .input_index ;
341
330
0 commit comments