Skip to main content
Visitor
Oct 29, 2026 5:22 PM UTC
Question

Threadx USBX audio thread and peripheral thread

I’m trying to get two threads running, one that changes audio volume based on a rotary encoder input and one that actually pipes the music to the speaker. I am able to get both working independently with good audio on the audio thread, but I cannot get BOTH threads to work at the same time. I’ve been tweaking the thread priority, preemption priority, time slice/no time slice, and thread sleep parameters (the sleep not being a parameter but just a tx_thread_sleep() call and the end of the thread while loop). My last iteration had the following params and still didnt work:
Volume thread:

  • Priority: 16
  • Preempt: 0
  • Time slice: 1
  • sleep: 100

USB Audio thread:

  • Priority 15
  • Preempt: 15
  • Time slice: 200
  • sleep: 1
     

Would love to get direction in getting BOTH working with clear audio.

 

7 replies

mƎALLEm
ST Technical Moderator
Oct 30, 2026 11:06 AM UTC

Hello ​@Rglasgow and welcome to the ST community,

I intervene here as admin.

Better to share your code otherwise no one can help you over here.

How to write your question to maximize your chances to find a solution

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
ST Technical Moderator
Oct 30, 2026 11:39 AM UTC

Hi ​@Rglasgow 

 

What happens when both threads run: does the audio glitch or stop, does the volume stop responding, or do both threads seem to get stuck? Does it happen right away or only when you turn the encoder?

How the audio gets to the speaker ? Is it through SAI/I2S with DMA? and how you change the volume, such as updating codec registers or adjusting the audio samples in software.

As suggested ​@mƎALLEm,  minimal project to reproduce would be a good starting point.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
RglasgowAuthor
Visitor
Oct 30, 2026 1:28 PM UTC

The situation is broad enough I wasnt sure what even to share. This is work for a private company so the amount I can share is up in the air. What code would you want to see that would help? When playing around with the parameters, what I will see is that one of the threads will run but the other will NEVER run. Audio is through I2S, I didn’t write that part of the code though. The behavior happens right away, one of the threads will run but never allow the other to run. Both threads have a sleep call at the end of the while(1) loop that should relinquish the cpu. As previously noted, I tweaked all sorts of things related to the threads themselves. 

RglasgowAuthor
Visitor
Oct 30, 2026 1:31 PM UTC

I guess a decent starting question would be, if you were running an audio thread and a volume control thread, how would you choose to set the various thread parameters? Audio needs high runtime to not get crappy audio

Senior III
Oct 30, 2026 3:50 PM UTC

What do you mean with “it doesn’t work”? Do you have noisy sounds?

I’ve a similar setup with a thread that manage RX/processing/TX audio via USBX and one that manages TouchGFX and have no issue with the audio nor slow fps and/or lag on the touchgfx thread.

I’m not using tx_thread_sleep on the audio thread tho

If your audio is real time, 1ms of sleep is audible (pop/crackle/glitch)

RglasgowAuthor
Visitor
Oct 30, 2026 4:58 PM UTC

See my other reply where i say “the thread doesnt run”. Like, it does not run. Ill put a break point at the top of the while loop and it never gets hit. I cannot get BOTH threads to run for some reason

Senior III
Oct 31, 2026 9:12 AM UTC

without code is really difficult. Have you checked that the sequence of creating and starting the thread and the kernel is correct?

Maybe you have not enough stack to allocate for both so, one of the two thread fails initialization but the other start working? Have you tried to add a breakpoint after tx_thread_create to catch the return of both? Or after the memory allocation?