Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AudioPlaySdWav: buffer alignment and irq priority #294

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@
#define AudioNoInterrupts() (NVIC_DISABLE_IRQ(IRQ_SOFTWARE))
#define AudioInterrupts() (NVIC_ENABLE_IRQ(IRQ_SOFTWARE))

/**
\brief Get Base Priority
\details Returns the current value of the Base Priority register
\return Base Priority register value
*/
static inline uint32_t __get_BASEPRI() __attribute__((always_inline));

static inline uint32_t __get_BASEPRI() {
uint32_t result;
__asm volatile(
"mrs %0, basepri"
: "=r" (result)
);
return result;
}


/**
\brief Set Base Priority
\details Assigns the given value to the Base Priority register
\param[in] basePri Base Priority value to set
*/
static inline void __set_BASEPRI(const uint32_t value) __attribute__((always_inline));

static inline void __set_BASEPRI(const uint32_t value) {
__asm volatile(
"msr basepri, %0"
:: "r" (value) : "memory"
);
}

// include all the library headers, so a sketch can use a single
// #include <Audio.h> to get the whole library
//
Expand Down
8 changes: 6 additions & 2 deletions play_sd_raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <Arduino.h>
#include "Audio.h"
#include "play_sd_raw.h"
#include "spi_interrupt.h"

Expand All @@ -45,9 +46,12 @@ bool AudioPlaySdRaw::play(const char *filename)
#else
AudioStartUsingSPI();
#endif
__disable_irq();
/* disable interrupts with lower priority than SDHC DMA */
const uint32_t last_pri { __get_BASEPRI() };
const uint8_t sdhc_pri { NVIC_GET_PRIORITY(IRQ_SDHC) };
__set_BASEPRI((sdhc_pri / 16U + 1U) * 16U);
rawfile = SD.open(filename);
__enable_irq();
__set_BASEPRI(last_pri);
if (!rawfile) {
//Serial.println("unable to open file");
#if defined(HAS_KINETIS_SDHC)
Expand Down
8 changes: 6 additions & 2 deletions play_sd_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <Arduino.h>
#include "Audio.h"
#include "play_sd_wav.h"
#include "spi_interrupt.h"

Expand Down Expand Up @@ -67,9 +68,12 @@ bool AudioPlaySdWav::play(const char *filename)
#else
AudioStartUsingSPI();
#endif
__disable_irq();
/* disable interrupts with lower priority than SDHC DMA */
const uint32_t last_pri { __get_BASEPRI() };
const uint8_t sdhc_pri { NVIC_GET_PRIORITY(IRQ_SDHC) };
__set_BASEPRI((sdhc_pri / 16U + 1U) * 16U);
wavfile = SD.open(filename);
__enable_irq();
__set_BASEPRI(last_pri);
if (!wavfile) {
#if defined(HAS_KINETIS_SDHC)
if (!(SIM_SCGC3 & SIM_SCGC3_SDHC)) AudioStopUsingSPI();
Expand Down
2 changes: 1 addition & 1 deletion play_sd_wav.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AudioPlaySdWav : public AudioStream
audio_block_t *block_left;
audio_block_t *block_right;
uint16_t block_offset; // how much data is in block_left & block_right
uint8_t buffer[512]; // buffer one block of data
alignas(4) uint8_t buffer[512]; // buffer one block of data
uint16_t buffer_offset; // where we're at consuming "buffer"
uint16_t buffer_length; // how much data is in "buffer" (512 until last read)
uint8_t header_offset; // number of bytes in header[]
Expand Down