Skip to content

Commit

Permalink
Improve interrupt handling for simultaneous WAV file playing
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Nov 3, 2020
1 parent 6ab951b commit 6369a6a
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions play_sd_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "play_sd_wav.h"
#include "spi_interrupt.h"


#define STATE_DIRECT_8BIT_MONO 0 // playing mono at native sample rate
#define STATE_DIRECT_8BIT_STEREO 1 // playing stereo at native sample rate
#define STATE_DIRECT_16BIT_MONO 2 // playing mono at native sample rate
Expand Down Expand Up @@ -62,20 +63,24 @@ void AudioPlaySdWav::begin(void)
bool AudioPlaySdWav::play(const char *filename)
{
stop();
#if defined(HAS_KINETIS_SDHC)
bool irq = false;
if (NVIC_IS_ENABLED(IRQ_SOFTWARE)) {
NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
irq = true;
}
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStartUsingSPI();
#else
#else
AudioStartUsingSPI();
#endif
__disable_irq();
wavfile = SD.open(filename);
__enable_irq();
if (!wavfile) {
#if defined(HAS_KINETIS_SDHC)
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
#else
#else
AudioStopUsingSPI();
#endif
#endif
if (irq) NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
return false;
}
buffer_length = 0;
Expand All @@ -84,30 +89,33 @@ bool AudioPlaySdWav::play(const char *filename)
data_length = 20;
header_offset = 0;
state = STATE_PARSE1;
if (irq) NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
return true;
}

void AudioPlaySdWav::stop(void)
{
__disable_irq();
bool irq = false;
if (NVIC_IS_ENABLED(IRQ_SOFTWARE)) {
NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
irq = true;
}
if (state != STATE_STOP) {
audio_block_t *b1 = block_left;
block_left = NULL;
audio_block_t *b2 = block_right;
block_right = NULL;
state = STATE_STOP;
__enable_irq();
if (b1) release(b1);
if (b2) release(b2);
wavfile.close();
#if defined(HAS_KINETIS_SDHC)
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
#else
#else
AudioStopUsingSPI();
#endif
} else {
__enable_irq();
#endif
}
if (irq) NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
}


Expand Down Expand Up @@ -165,11 +173,11 @@ void AudioPlaySdWav::update(void)
}
end: // end of file reached or other reason to stop
wavfile.close();
#if defined(HAS_KINETIS_SDHC)
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
#else
#else
AudioStopUsingSPI();
#endif
#endif
state_play = STATE_STOP;
state = STATE_STOP;
cleanup:
Expand Down

0 comments on commit 6369a6a

Please sign in to comment.