Skip to content

Commit 851765e

Browse files
committed
Improve dma initialization.
1 parent 1aaf4f1 commit 851765e

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

ports/raspberrypi/common-hal/audiobusio/I2S.c

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,14 @@ void common_hal_audiobusio_i2s_construct(audiobusio_i2s_obj_t *self,
106106
audio_dma_init(&self->dma);
107107
}
108108

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) {
111110
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;
115113
}
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);
120117
}
121118

122119
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,
173170
}
174171

175172
// 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);
177174

178175
size_t output_count = 0;
179176
int16_t *buffer;
@@ -182,11 +179,7 @@ uint32_t common_hal_audiobusio_i2s_record_to_buffer(audiobusio_i2s_obj_t *self,
182179
while (output_count < output_buffer_length) {
183180
// Do other things while we wait for the buffer to fill.
184181
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;
190183
}
191184
self->last_record_index = self->dma.input_index;
192185

@@ -229,7 +222,7 @@ void common_hal_audiobusio_i2s_play(audiobusio_i2s_obj_t *self,
229222
}
230223
}
231224

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);
233226
self->playing = true;
234227
}
235228

@@ -311,7 +304,7 @@ void audiobusio_i2s_reset_buffer(audiobusio_i2s_obj_t *self,
311304
mp_raise_NotImplementedError(MP_ERROR_TEXT("Single channel output not supported."));
312305
}
313306

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);
315308
self->last_record_index = -1;
316309
self->last_sample_index = -1;
317310
}
@@ -331,11 +324,7 @@ audioio_get_buffer_result_t audiobusio_i2s_get_buffer(audiobusio_i2s_obj_t *self
331324

332325
// Do other things while we wait for the buffer to fill.
333326
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;
339328
}
340329
self->last_sample_index = self->dma.input_index;
341330

ports/raspberrypi/common-hal/audiobusio/I2S.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ void audiobusio_i2s_get_buffer_structure(audiobusio_i2s_obj_t *self, bool single
4444
uint32_t *max_buffer_length, uint8_t *spacing);
4545

4646
void i2s_configure_audio_dma(audiobusio_i2s_obj_t *self, mp_obj_t sample, bool loop,
47-
uint32_t sample_rate, uint8_t bits_per_sample);
47+
uint32_t sample_rate, uint8_t bits_per_sample, bool force);

0 commit comments

Comments
 (0)