samd: audio_dma: Track channel allocation

Previously, we depended on allocated channels to always be
"dma_channel_enabled".  However, (A) sometimes, many operations
would take place between find_free_audio_dma_channel and
audio_dma_enable_channel, and (B) some debugging I did led me to believe
that "dma_channel_enabled" would become false when the hardware ended
a scheduled DMA transaction, but while a CP object would still think it
owned the DMA channel.

((B) is not documented in the datasheet and I am not 100% convinced that
my debugging session was not simply missing where we were disabling the
channel, but in either case, it shows a need to directly track allocated
separately from enabled)

Therefore,
 * Add audio_dma_{allocate,free}_channel.
   * audio_dma_free_channel implies audio_dma_disable_channel
   * track via a new array audio_dma_allocated[]
 * clear all allocated flags on soft-reboot
 * Convert find_free_audio_dma_channel to audio_dma_allocate_channel
   * use audio_dma_allocated[] instead of dma_channel_enabled() to check
     availability
 * remove find_free_audio_dma_channel
 * For each one, find a matching audio_dma_disable_channel to convert
   to audio_dma_free_channel

Closes: #2058
crypto-aes
jepler 3 years ago
parent 0b00787b4d
commit 30a9346373
  1. 20
      ports/atmel-samd/audio_dma.c
  2. 3
      ports/atmel-samd/audio_dma.h
  3. 4
      ports/atmel-samd/common-hal/audiobusio/PDMIn.c

@ -42,14 +42,24 @@ static audio_dma_t* audio_dma_state[AUDIO_DMA_CHANNEL_COUNT];
// This cannot be in audio_dma_state because it's volatile.
static volatile bool audio_dma_pending[AUDIO_DMA_CHANNEL_COUNT];
uint8_t find_free_audio_dma_channel(void) {
static bool audio_dma_allocated[AUDIO_DMA_CHANNEL_COUNT];
uint8_t audio_dma_allocate_channel(void) {
uint8_t channel;
for (channel = 0; channel < AUDIO_DMA_CHANNEL_COUNT; channel++) {
if (!dma_channel_enabled(channel)) {
if (!audio_dma_allocated[channel]) {
audio_dma_allocated[channel] = true;
return channel;
}
}
return channel;
return channel; // i.e., return failure
}
void audio_dma_free_channel(uint8_t channel) {
assert(channel < AUDIO_DMA_CHANNEL_COUNT);
assert(audio_dma_allocated[channel]);
audio_dma_disable_channel(channel);
audio_dma_allocated[channel] = false;
}
void audio_dma_disable_channel(uint8_t channel) {
@ -167,7 +177,7 @@ audio_dma_result audio_dma_setup_playback(audio_dma_t* dma,
bool output_signed,
uint32_t output_register_address,
uint8_t dma_trigger_source) {
uint8_t dma_channel = find_free_audio_dma_channel();
uint8_t dma_channel = audio_dma_allocate_channel();
if (dma_channel >= AUDIO_DMA_CHANNEL_COUNT) {
return AUDIO_DMA_DMA_BUSY;
}
@ -278,6 +288,7 @@ void audio_dma_stop(audio_dma_t* dma) {
disable_event_channel(dma->event_channel);
MP_STATE_PORT(playing_audio)[channel] = NULL;
audio_dma_state[channel] = NULL;
audio_dma_free_channel(dma->dma_channel);
}
dma->dma_channel = AUDIO_DMA_CHANNEL_COUNT;
}
@ -307,6 +318,7 @@ void audio_dma_reset(void) {
for (uint8_t i = 0; i < AUDIO_DMA_CHANNEL_COUNT; i++) {
audio_dma_state[i] = NULL;
audio_dma_pending[i] = false;
audio_dma_allocated[i] = false;
audio_dma_disable_channel(i);
dma_descriptor(i)->BTCTRL.bit.VALID = false;
MP_STATE_PORT(playing_audio)[i] = NULL;

@ -64,7 +64,8 @@ uint8_t audiosample_channel_count(mp_obj_t sample_obj);
void audio_dma_init(audio_dma_t* dma);
void audio_dma_reset(void);
uint8_t find_free_audio_dma_channel(void);
uint8_t audio_dma_allocate_channel(void);
void audio_dma_free_channel(uint8_t channel);
// This sets everything up but doesn't start the timer.
// Sample is the python object for the sample to play.

@ -355,7 +355,7 @@ static uint16_t filter_sample(uint32_t pdm_samples[4]) {
// output_buffer_length is the number of slots, not the number of bytes.
uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self,
uint16_t* output_buffer, uint32_t output_buffer_length) {
uint8_t dma_channel = find_free_audio_dma_channel();
uint8_t dma_channel = audio_dma_allocate_channel();
uint8_t event_channel = find_sync_event_channel();
if (event_channel >= EVSYS_SYNCH_NUM) {
mp_raise_RuntimeError(translate("All sync event channels in use"));
@ -464,7 +464,7 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
}
disable_event_channel(event_channel);
audio_dma_disable_channel(dma_channel);
audio_dma_free_channel(dma_channel);
// Turn off serializer, but leave clock on, to avoid mic startup delay.
i2s_set_serializer_enable(self->serializer, false);

Loading…
Cancel
Save