Skip to content

Commit 9eaed80

Browse files
mjchen0danieldegrasse
authored andcommitted
drivers: audio: dmic_mcux: init active_buf_idx when setting up dma
In case the dmic had previous been run and stopped, make sure active_buf_idx is initialized to 0 when setting up dma on start, otherwise the dma callback can return the wrong buffer. Also purge the rx_queue before freeing the slab buffers, to minimize risk of any async read request getting back a slab buffer that is freed. Signed-off-by: Mike J. Chen <mjchen@google.com>
1 parent ae3fb6e commit 9eaed80

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

drivers/audio/dmic_mcux.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ static int dmic_mcux_enable_dma(struct mcux_dmic_drv_data *drv_data, bool enable
125125
}
126126
} else {
127127
if (dma_stop(pdm_channel->dma, pdm_channel->dma_chan)) {
128+
LOG_ERR("Error stopping DMA for HW channel %d",
129+
hw_chan);
128130
ret = -EIO;
129131
}
130132
}
@@ -181,14 +183,17 @@ static int dmic_mcux_stop(struct mcux_dmic_drv_data *drv_data)
181183
/* Disable DMA */
182184
dmic_mcux_enable_dma(drv_data, false);
183185

186+
/* Purge the RX queue first, to minimize possibility of
187+
* an async read request returning a ptr to a buffer we're
188+
* about to free.
189+
*/
190+
k_msgq_purge(drv_data->rx_queue);
191+
184192
/* Free all memory slabs */
185193
for (uint32_t i = 0; i < CONFIG_DMIC_MCUX_DMA_BUFFERS; i++) {
186194
k_mem_slab_free(drv_data->mem_slab, drv_data->dma_bufs[i]);
187195
}
188196

189-
/* Purge the RX queue as well. */
190-
k_msgq_purge(drv_data->rx_queue);
191-
192197
drv_data->dmic_state = DMIC_STATE_CONFIGURED;
193198

194199
return 0;
@@ -283,6 +288,7 @@ static int dmic_mcux_setup_dma(const struct device *dev)
283288
uint8_t hw_chan;
284289
int ret = 0;
285290

291+
drv_data->active_buf_idx = 0;
286292

287293
/* Setup DMA configuration common between all channels */
288294
dma_cfg.user_data = drv_data;

0 commit comments

Comments
 (0)