Skip to main content
April 13, 2023
Question

BSP_AUDIO_OUT_Play never finishes and blocks execution

  • April 13, 2023
  • 3 replies
  • 4627 views

Hi all,

I'm having trouble implementing audio output into a TouchGFX application for the STM32F746G-DISCO board.

The TouchGFX part is done, and I also managed to include the necessary files for audio output into the project and call a function to start playing audio.

However, once that function is called, it blocks everything else and the entire board becomes unresponsive. Audio is played in an endless loop and no other interaction is possible.

The nearly exact same question was asked a couple of years ago already, but got no answer because of missing details:

https://community.st.com/s/question/0D50X00009XkdwfSAB/bspaudiooutplay-function-stops-program-execution

So, here are some more details. I've reduced the code as much as possible.

Call hierarchy:

  • TouchGFX button press
    • Interaction "Call virtual function" --> in the corresponding View.cpp class
      • View calls Presenter, Presenter Calls Model
        • Model has an include of "myaudio.c" and calls the PlaySound() function
          • Class "myaudio.c" then calls the necessary BSP functions

The relevant parts of the class "myaudio.c" are:

#include "main.h"
#include "myaudio.h"
#include "math.h"
 
static uint16_t audioBuf[4096];
 
static int PlayerInit()
{
 if(BSP_AUDIO_OUT_INIT(OUTPUT_DEVICE_HEADPHONE, 20, 48000) != 0)
 {
 return -1;
 }
 else
 {
 BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
 return 0;
 }
}
 
static int PlaySound()
{
 for(int i=0; i<=4094; i+=2)
 {
 audioBuf[2*i] = 32767.0f + roundf(32767.0f * sinf((2.0f * 3.14159f + 440.0f * (float)i) / 48000));
 audioBuf[2*i+1] = 32767.0f + roundf(32767.0f * sinf((2.0f * 3.14159f + 220.0f * (float)i) / 48000));
 }
 
 if(BSP_AUDIO_OUT_Play( (uint16_t *) audioBuf, 4096 ) != 0)
 {
 return -1;
 }
 else {
 return 0;
 }
}

The return values are propagated back up to the View, where they are displayed in the UI.

However, none of that happens because the code gets stuck at the BSP_AUDIO_OUT_Play step. Nothing after that gets executed. If I comment it out, everything works fine (but then there is no audio output, obviously).

Where is my mistake, or is this a bug in the audio driver?

Is it intended behaviour of the BSP Play function to play the given audio buffer array in an endless loop? If so, how can it be changed to play a sound only as a one-shot event and then stop?

    This topic has been closed for replies.

    3 replies

    April 20, 2023

    Does anyone have an idea? Is there any special configuration needed in CubeMX that I don't have? I've configured SAI and DMA based on some tutorial, and since I can produce audio output, I think that at least something must be correct there. But why is it then freezing, i.e. why does the BSP_AUDIO_OUT_play function never finish?

    Super User
    April 20, 2023

    only guessed : the "right" priorities are critical ! so try ...in nvic and dma settings.

    i made an audio player, can play from SDcard, USB stick or Web radio (data from ESP8266 -> SPI ), wav, flac and MP3 stream/data . so i know... :)

    +

    i see no info - how you wanna do it?

    • read file/data - from ?
    • play data from buffer + circular dma ?
    • buffer reload in callbacks ? (i do it this way )
    • your basic source is from : ... ?

     https://github.com/STMicroelectronics/STM32CubeF7/tree/master/Projects/STM32746G-Discovery/Applications/Audio/Audio_playback_and_record/Src

    April 21, 2023

    Many thanks for the answer! Unfortunately I don't really know what you mean by that. I attached two Screenshots from CubeMX for the "DMA" and "NVIC" sections.


    _legacyfs_online_stmicro_images_0693W00000biamqQAA.png
    _legacyfs_online_stmicro_images_0693W00000biamgQAA.pngI'm not even at the point where I want to play a real sound file yet. That is all complexity that I can add once the basics work. Right now, I generate the samples directly in code using a sine function. And that works, since I can already hear it.

    My basic starting point is TouchGFX Designer. That's where I created a new project and that's also what generated my board config.

    I know and already have tested the sample project you linked to, but it is without TouchGFX and istn't compatible with it.

    As far as I understood and have experienced so far, CubeIDE and TouchGFX are two pretty separate environments, and projects generated in one will most likely not run directly on the other without some adjustments (which are documented nowhere).

    Super User
    April 21, 2023

    now more info - so i cant help you much, because i never used TouchGFX (have my own driver for simple text and graphic on small TFT) , and same for freertos .

    your generated sound works, try to debug , jump/step in BSP_AUDIO_OUT_Play()and find out, whats freezing then.

    maybe something with priorities, see: if you start something (maybe : play) and this function use INT or callback , but here some other function (maybe, read data from USB) is called and this waits for an INT on same or lower priority level, it can never get this INT and "freezes" in this waiting state. but if priority higher, INT will come and finish (whatever it should do) and your function works and goes on.

    April 24, 2023

    Alright, thanks for the additional info!

    Right now I can't really debug the whole thing, because it only runs in TouchGFX Designer and won't start in CubeIDE... pretty sad that those two tools are so incompatible.

    But I will have a look at those return values and priorities, thanks!

    Super User
    April 24, 2023

    oh, i didnt know:

    > it only runs in TouchGFX Designer and won't start in CubeIDE... pretty sad that those two tools are so incompatible

    but you can select it in IDE -> software packs


    _legacyfs_online_stmicro_images_0693W00000bifxAQAQ.pngand from https://www.st.com/en/development-tools/touchgfxdesigner.html ->

    • Tool-generated code entirely separated from user code
    • Support of several integrated development environments such as IAR Embedded Workbench, Arm Keil, and GCC-based IDEs

    should work in IDE... read:

    https://electronics.stackexchange.com/questions/570667/stm32cubeide-how-to-import-complete-working-program-from-touchgfx-designer

    + howto...

    https://community.element14.com/technologies/embedded/b/blog/posts/stm32h7b3i---create-a-touchgfx-project-with-support-for-the-designer-cubeide-and-debugger---pt-1-screen-works