8000 Merge pull request #10278 from relic-se/rp2040_dma_buffer_realloc · rianadon/circuitpython@443f829 · GitHub
[go: up one dir, main page]

Skip to content

Commit 443f829

Browse files
authored
Merge pull request adafruit#10278 from relic-se/rp2040_dma_buffer_realloc
Fix audio DMA buffer allocation on RP2040
2 parents dc325a3 + ba9e809 commit 443f829

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

ports/raspberrypi/audio_dma.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,31 @@ audio_dma_result audio_dma_setup_playback(
227227
max_buffer_length /= dma->sample_spacing;
228228
}
229229

230+
#ifdef PICO_RP2350
230231
dma->buffer[0] = (uint8_t *)port_realloc(dma->buffer[0], max_buffer_length, true);
232+
#else
233+
dma->buffer[0] = (uint8_t *)m_realloc(dma->buffer[0],
234+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
235+
dma->buffer_length[0], // Old size
236+
#endif
237+
max_buffer_length);
238+
#endif
231239
dma->buffer_length[0] = max_buffer_length;
232240

233241
if (dma->buffer[0] == NULL) {
234242
return AUDIO_DMA_MEMORY_ERROR;
235243
}
236244

237245
if (!single_buffer) {
246+
#ifdef PICO_RP2350
247+
dma->buffer[1] = (uint8_t *)port_realloc(dma->buffer[0], max_buffer_length, true);
248+
#else
249+
dma->buffer[1] = (uint8_t *)m_realloc(dma->buffer[0],
250+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
251+
dma->buffer_length[1], // Old size
252+
#endif
253+
max_buffer_length);
254+
#endif
238255
dma->buffer[1] = (uint8_t *)port_realloc(dma->buffer[1], max_buffer_length, true);
239256
dma->buffer_length[1] = max_buffer_length;
240257

@@ -429,11 +446,27 @@ void audio_dma_init(audio_dma_t *dma) {
429446
}
430447

431448
void audio_dma_deinit(audio_dma_t *dma) {
449+
#ifdef PICO_RP2350
432450
port_free(dma->buffer[0]);
451+
#else
452+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
453+
m_free(dma->buffer[0], dma->buffer_length[0]);
454+
#else
455+
m_free(dma->buffer[0]);
456+
#endif
457+
#endif
433458
dma->buffer[0] = NULL;
434459
dma->buffer_length[0] = 0;
435460

461+
#ifdef PICO_RP2350
436462
port_free(dma->buffer[1]);
463+
#else
464+
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
465+
m_free(dma->buffer[1], dma->buffer_length[1]);
466+
#else
467+
m_free(dma->buffer[1]);
468+
#endif
469+
#endif
437470
dma->buffer[1] = NULL;
438471
dma->buffer_length[1] = 0;
439472
}

ports/raspberrypi/audio_dma.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef enum {
2020

2121
typedef struct {
2222
mp_obj_t sample;
23-
uint8_t *buffer[2]; // Allocated through port_malloc so they are dma-able
23+
uint8_t *buffer[2]; // Allocated through port_malloc on RP2350 so they are dma-able
2424
size_t buffer_length[2];
2525
uint32_t channels_to_load_mask;
2626
uint32_t output_register_address;

0 commit comments

Comments
 (0)
0